Author: gnodet
Date: Tue Jul 29 07:36:20 2008
New Revision: 680714
URL: http://svn.apache.org/viewvc?rev=680714&view=rev
Log:
SMX4-81: Add the javax.script API to the specs project
Added:
servicemix/smx4/specs/trunk/scripting-api-1.0/ (with props)
servicemix/smx4/specs/trunk/scripting-api-1.0/pom.xml
servicemix/smx4/specs/trunk/scripting-api-1.0/src/
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/script/
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/script/ScriptEngineManager.java
Modified:
servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
servicemix/smx4/specs/trunk/pom.xml
Modified:
servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java?rev=680714&r1=680713&r2=680714&view=diff
==============================================================================
---
servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
(original)
+++
servicemix/smx4/specs/trunk/locator/src/main/java/org/apache/servicemix/specs/locator/OsgiLocator.java
Tue Jul 29 07:36:20 2008
@@ -61,4 +61,20 @@
return null;
}
+ public static synchronized List<Class> locateAll(String factoryId) {
+ List<Class> classes = new ArrayList<Class>();
+ if (factories != null) {
+ List<Callable<Class>> l = factories.get(factoryId);
+ if (l != null) {
+ for (Callable<Class> c : l) {
+ try {
+ classes.add(c.call());
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+ return classes;
+ }
+
}
Modified: servicemix/smx4/specs/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/pom.xml?rev=680714&r1=680713&r2=680714&view=diff
==============================================================================
--- servicemix/smx4/specs/trunk/pom.xml (original)
+++ servicemix/smx4/specs/trunk/pom.xml Tue Jul 29 07:36:20 2008
@@ -42,6 +42,7 @@
<module>jaxb-api-2.1</module>
<module>jaxws-api-2.0</module>
<module>jaxws-api-2.1</module>
+ <module>scripting-api-1.0</module>
</modules>
<scm>
Propchange: servicemix/smx4/specs/trunk/scripting-api-1.0/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Jul 29 07:36:20 2008
@@ -0,0 +1,10 @@
+target
+*.iml
+*.ipr
+*.iws
+.classpath
+.project
+.settings
+bin
+
+
Added: servicemix/smx4/specs/trunk/scripting-api-1.0/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/scripting-api-1.0/pom.xml?rev=680714&view=auto
==============================================================================
--- servicemix/smx4/specs/trunk/scripting-api-1.0/pom.xml (added)
+++ servicemix/smx4/specs/trunk/scripting-api-1.0/pom.xml Tue Jul 29 07:36:20
2008
@@ -0,0 +1,167 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.servicemix.specs</groupId>
+ <artifactId>specs</artifactId>
+ <version>1.1-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.apache.servicemix.specs</groupId>
+ <artifactId>org.apache.servicemix.specs.scripting-api-1.0</artifactId>
+ <packaging>bundle</packaging>
+ <version>1.1-SNAPSHOT</version>
+ <name>Apache ServiceMix Specs :: Scripting API 1.0</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.livetribe</groupId>
+ <artifactId>livetribe-jsr223</artifactId>
+ <version>2.0.3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicemix.specs</groupId>
+ <artifactId>org.apache.servicemix.specs.locator</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <configuration>
+ <instructions>
+
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+
<Export-Package>javax.script*;version=1.0;-split-package:=merge-first</Export-Package>
+ <Import-Package>*</Import-Package>
+
<Private-Package>org.apache.servicemix.specs.locator;-split-package:=merge-first</Private-Package>
+
<Bundle-Activator>org.apache.servicemix.specs.locator.Activator</Bundle-Activator>
+ <Implementation-Title>Apache
ServiceMix</Implementation-Title>
+
<Implementation-Version>${project.version}</Implementation-Version>
+ </instructions>
+ <unpackBundle>true</unpackBundle>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <artifactSet>
+ <includes>
+
<include>org.livetribe:livetribe-jsr223</include>
+
<include>org.apache.servicemix.specs:org.apache.servicemix.specs.locator</include>
+ </includes>
+ </artifactSet>
+ <filters>
+ <filter>
+
<artifact>org.livetribe:livetribe-jsr223</artifact>
+ <excludes>
+ <exclude>javax/**</exclude>
+ </excludes>
+ </filter>
+ <filter>
+
<artifact>org.apache.servicemix.specs:org.apache.servicemix.specs.locator</artifact>
+ <excludes>
+ <exclude>org/**</exclude>
+ </excludes>
+ </filter>
+ </filters>
+
<createSourcesJar>${createSourcesJar}</createSourcesJar>
+
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
+
<createDependencyReducedPom>true</createDependencyReducedPom>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <profiles>
+ <profile>
+ <id>deploy</id>
+ <properties>
+ <createSourcesJar>true</createSourcesJar>
+ </properties>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>unpack-sources</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>unpack</goal>
+ </goals>
+ <configuration>
+ <artifactItems>
+ <artifactItem>
+ <groupId>org.livetribe</groupId>
+
<artifactId>livetribe-jsr223</artifactId>
+ <classifier>sources</classifier>
+ </artifactItem>
+ <artifactItem>
+
<groupId>org.apache.servicemix.specs</groupId>
+
<artifactId>org.apache.servicemix.specs.locator</artifactId>
+ <classifier>sources</classifier>
+ </artifactItem>
+ </artifactItems>
+
<outputDirectory>${project.build.directory}/sources</outputDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>package</id>
+ <phase>package</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <minmemory>128m</minmemory>
+ <maxmemory>512m</maxmemory>
+
<sourcepath>${project.build.directory}/sources</sourcepath>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+
+</project>
Added:
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/script/ScriptEngineManager.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/script/ScriptEngineManager.java?rev=680714&view=auto
==============================================================================
---
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/script/ScriptEngineManager.java
(added)
+++
servicemix/smx4/specs/trunk/scripting-api-1.0/src/main/java/javax/script/ScriptEngineManager.java
Tue Jul 29 07:36:20 2008
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.script;
+
+import java.io.*;
+import java.net.URL;
+import java.util.*;
+
+public class ScriptEngineManager {
+
+ private final Set engineSpis = new HashSet();
+ private final Map<String, ScriptEngineFactory> byName = new
HashMap<String, ScriptEngineFactory>();
+ private final Map<String, ScriptEngineFactory> registeredByName = new
HashMap();
+ private final Map<String, ScriptEngineFactory> byExtension = new
HashMap<String, ScriptEngineFactory>();
+ private final Map<String, ScriptEngineFactory> registeredByExtension = new
HashMap<String, ScriptEngineFactory>();
+ private final Map<String, ScriptEngineFactory> byMimeType = new
HashMap<String, ScriptEngineFactory>();
+ private final Map<String, ScriptEngineFactory> registeredByMimeType = new
HashMap<String, ScriptEngineFactory>();
+ private Bindings globalScope;
+
+ public ScriptEngineManager() {
+ this(Thread.currentThread().getContextClassLoader());
+ }
+
+ public ScriptEngineManager(ClassLoader classLoader) {
+ try
+ {
+ for (Enumeration factoryResources =
classLoader.getResources("META-INF/services/javax.script.ScriptEngineFactory");
+ factoryResources.hasMoreElements();) {
+ URL url = (URL) factoryResources.nextElement();
+ for (String className : getClassNames(url)) {
+ try {
+ Class factoryClass = classLoader.loadClass(className);
+ Object object = factoryClass.newInstance();
+ if (object instanceof ScriptEngineFactory) {
+ ScriptEngineFactory factory =
(ScriptEngineFactory) object;
+ for (String name : factory.getNames()) {
+
byName.put(name, factory);
+ }
+ for (String extension :
factory.getExtensions()) {
+
byExtension.put(extension, factory);
+ }
+ for (String mimeType :
factory.getMimeTypes()) {
+
byMimeType.put(mimeType, factory);
+ }
+ engineSpis.add(factory);
+ }
+ } catch(ClassNotFoundException doNothing) {
+ } catch(IllegalAccessException
doNothing) {
+ } catch(InstantiationException
doNothing) {
+ }
+ }
+ }
+ } catch(IOException doNothing) {
+ }
+ }
+
+ public Bindings getGlobalScope() {
+ return globalScope;
+ }
+
+ public void setGlobalScope(Bindings globalScope) {
+ this.globalScope = globalScope;
+ }
+
+ public void put(String key, Object value) {
+ if (globalScope != null) {
+ globalScope.put(key, value);
+ }
+ }
+
+ public Object get(String key) {
+ if (globalScope != null) {
+ return globalScope.get(key);
+ } else {
+ return null;
+ }
+ }
+
+ public ScriptEngine getEngineByName(String shortName) {
+ ScriptEngineFactory factory = null;
+ List<Class> factoryClasses =
org.apache.servicemix.specs.locator.OsgiLocator.locateAll("javax.script.ScriptEngineFactory");
+ for (Class factoryClass : factoryClasses) {
+ try {
+ ScriptEngineFactory f = (ScriptEngineFactory)
factoryClass.newInstance();
+ if (f.getNames().contains(shortName)) {
+ factory = f;
+ break;
+ }
+ } catch (Throwable doNothing) {
+ }
+ }
+ if (factory == null) {
+ factory = registeredByName.get(shortName);
+ }
+ if (factory == null) {
+ factory = byName.get(shortName);
+ }
+ if (factory == null) {
+ return null;
+ } else {
+ ScriptEngine engine = factory.getScriptEngine();
+ engine.setBindings(globalScope, 200);
+ return engine;
+ }
+ }
+
+ public ScriptEngine getEngineByExtension(String extension) {
+ ScriptEngineFactory factory = null;
+ List<Class> factoryClasses =
org.apache.servicemix.specs.locator.OsgiLocator.locateAll("javax.script.ScriptEngineFactory");
+ for (Class factoryClass : factoryClasses) {
+ try {
+ ScriptEngineFactory f = (ScriptEngineFactory)
factoryClass.newInstance();
+ if (f.getExtensions().contains(extension)) {
+ factory = f;
+ break;
+ }
+ } catch (Throwable doNothing) {
+ }
+ }
+ if (factory == null) {
+ factory = registeredByExtension.get(extension);
+ }
+ if (factory == null) {
+ factory = byExtension.get(extension);
+ }
+ if (factory == null) {
+ return null;
+ } else {
+ ScriptEngine engine = factory.getScriptEngine();
+ engine.setBindings(globalScope, 200);
+ return engine;
+ }
+ }
+
+ public ScriptEngine getEngineByMimeType(String mimeType) {
+ ScriptEngineFactory factory = null;
+ List<Class> factoryClasses =
org.apache.servicemix.specs.locator.OsgiLocator.locateAll("javax.script.ScriptEngineFactory");
+ for (Class factoryClass : factoryClasses) {
+ try {
+ ScriptEngineFactory f = (ScriptEngineFactory)
factoryClass.newInstance();
+ if (f.getMimeTypes().contains(mimeType)) {
+ factory = f;
+ break;
+ }
+ } catch (Throwable doNothing) {
+ }
+ }
+ if (factory == null) {
+ factory = registeredByMimeType.get(mimeType);
+ }
+ if(factory == null) {
+ factory = byMimeType.get(mimeType);
+ }
+ if (factory == null) {
+ return null;
+ } else {
+ ScriptEngine engine = factory.getScriptEngine();
+ engine.setBindings(globalScope, 200);
+ return engine;
+ }
+ }
+
+ public List<ScriptEngineFactory> getEngineFactories() {
+ List<ScriptEngineFactory> factories = new ArrayList();
+ List<Class> factoryClasses =
org.apache.servicemix.specs.locator.OsgiLocator.locateAll("javax.script.ScriptEngineFactory");
+ for (Class factoryClass : factoryClasses) {
+ try {
+ factories.add((ScriptEngineFactory)
factoryClass.newInstance());
+ } catch (Throwable doNothing) {
+ }
+ }
+ factories.addAll(engineSpis);
+ return factories;
+ }
+
+ public void registerEngineName(String name, ScriptEngineFactory factory) {
+ registeredByName.put(name, factory);
+ }
+
+ public void registerEngineMimeType(String type, ScriptEngineFactory
factory) {
+ registeredByMimeType.put(type, factory);
+ }
+
+ public void registerEngineExtension(String extension, ScriptEngineFactory
factory) {
+ registeredByExtension.put(extension, factory);
+ }
+
+ private Iterable<String> getClassNames(URL url) {
+ Stack<String> stack = new Stack<String>();
+ try {
+ BufferedReader r = new BufferedReader(new
InputStreamReader(url.openStream()));
+ String line;
+ while ((line = r.readLine()) != null) {
+ int comment = line.indexOf('#');
+ if (comment != -1) {
+ line = line.substring(0, comment);
+ }
+ stack.push(line.trim());
+ }
+ } catch(IOException doNothing) {
+ }
+ return stack;
+ }
+
+}
\ No newline at end of file