Author: ningjiang
Date: Thu Apr 14 13:44:33 2011
New Revision: 1092236
URL: http://svn.apache.org/viewvc?rev=1092236&view=rev
Log:
CAMEL-3481 camel-script supports better with OSGi
Added:
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java
(with props)
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
(with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java
camel/trunk/components/camel-script/pom.xml
camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java?rev=1092236&r1=1092235&r2=1092236&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java
Thu Apr 14 13:44:33 2011
@@ -36,6 +36,8 @@ import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.Converter;
@@ -53,6 +55,7 @@ import org.apache.camel.spi.Injector;
import org.apache.camel.spi.Language;
import org.apache.camel.spi.LanguageResolver;
import org.apache.camel.spi.PackageScanFilter;
+import org.apache.camel.spi.ScriptEngineResolver;
import org.apache.camel.spi.TypeConverterLoader;
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.camel.util.IOHelper;
@@ -74,6 +77,8 @@ public class Activator implements Bundle
public static final String META_INF_DATAFORMAT =
"META-INF/services/org/apache/camel/dataformat/";
public static final String META_INF_TYPE_CONVERTER =
"META-INF/services/org/apache/camel/TypeConverter";
public static final String META_INF_FALLBACK_TYPE_CONVERTER =
"META-INF/services/org/apache/camel/FallbackTypeConverter";
+ public static final String META_INF_SERVICES_DIR = "META-INF/services";
+ public static final String SCRIPT_ENGINE_SERVICE_FILE =
"javax.script.ScriptEngineFactory";
private static final transient Logger LOG =
LoggerFactory.getLogger(Activator.class);
@@ -97,6 +102,7 @@ public class Activator implements Bundle
LOG.debug("Bundle started: {}", bundle.getSymbolicName());
List<BaseService> r = new ArrayList<BaseService>();
registerComponents(bundle, r);
+ registerScriptEngines(bundle, r);
registerLanguages(bundle, r);
registerDataFormats(bundle, r);
registerTypeConverterLoader(bundle, r);
@@ -135,6 +141,17 @@ public class Activator implements Bundle
}
}
+ protected void registerScriptEngines(Bundle bundle, List<BaseService>
resolvers) {
+ URL configURL = null;
+ for (Enumeration e = bundle.findEntries(META_INF_SERVICES_DIR,
SCRIPT_ENGINE_SERVICE_FILE, false); e != null && e.hasMoreElements();) {
+ configURL = (URL) e.nextElement();
+ }
+ if (configURL != null) {
+ LOG.info("Found ScriptEngineFactory in " +
bundle.getSymbolicName());
+ resolvers.add(new BundleScriptEngineResolver(bundle, configURL));
+ }
+ }
+
protected void registerLanguages(Bundle bundle, List<BaseService>
resolvers) {
if (checkCompat(bundle, Language.class)) {
Map<String, String> languages = new HashMap<String, String>();
@@ -260,6 +277,54 @@ public class Activator implements Bundle
}
}
+ protected static class BundleScriptEngineResolver extends
BaseResolver<ScriptEngineFactory> implements ScriptEngineResolver {
+ private final URL configFile;
+
+ public BundleScriptEngineResolver(Bundle bundle, URL configFile) {
+ super(bundle, ScriptEngineFactory.class);
+ this.configFile = configFile;
+ }
+
+ public ScriptEngine resolveScriptEngine(String name) {
+ try {
+ BufferedReader in = new BufferedReader(new
InputStreamReader(configFile.openStream()));
+ String className = in.readLine();
+ in.close();
+ Class cls = bundle.loadClass(className);
+ if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
+ throw new IllegalStateException("Invalid script engine
factory " + cls.getName());
+ }
+ ScriptEngineFactory factory = (ScriptEngineFactory)
cls.newInstance();
+ List<String> names = factory.getNames();
+ for (String test : names) {
+ if (test.equals(name)) {
+ ClassLoader old =
Thread.currentThread().getContextClassLoader();
+ // JRuby seems to require the correct TCCL to call
getScriptEngine
+
Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
+ ScriptEngine engine = factory.getScriptEngine();
+ Thread.currentThread().setContextClassLoader(old);
+ return engine;
+ }
+ }
+ LOG.debug("Script Engine Factory for " +
factory.getEngineName() + " does not match " + name);
+ return null;
+ } catch (Exception e) {
+ LOG.warn("Unable to create script engine factory: " +
e.getClass().getName(), e);
+ return null;
+ }
+ }
+
+ @Override
+ public void register() {
+ doRegister(ScriptEngineResolver.class);
+ }
+
+ @Override
+ public String toString() {
+ return "OSGi script engine resolver for " +
bundle.getSymbolicName();
+ }
+ }
+
protected static class BundleTypeConverterLoader extends
BaseResolver<TypeConverter> implements TypeConverterLoader {
private final AnnotationTypeConverterLoader loader = new Loader();
Added:
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java?rev=1092236&view=auto
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java
(added)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java
Thu Apr 14 13:44:33 2011
@@ -0,0 +1,33 @@
+/**
+ * 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 org.apache.camel.spi;
+
+import javax.script.ScriptEngine;
+
+/**
+ * Used to load scripting engines when the default service registration
+ * mechanism does not work (e.g. OSGi)
+ */
+public interface ScriptEngineResolver {
+ /**
+ * Resolves the given script engine given its name.
+ *
+ * @param name the name of the script engine you're looking for
+ * @return the script engine factory or <tt>null</tt> if not possible to
resolve
+ */
+ ScriptEngine resolveScriptEngine(String name);
+}
Propchange:
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ScriptEngineResolver.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/components/camel-script/pom.xml
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-script/pom.xml?rev=1092236&r1=1092235&r2=1092236&view=diff
==============================================================================
--- camel/trunk/components/camel-script/pom.xml (original)
+++ camel/trunk/components/camel-script/pom.xml Thu Apr 14 13:44:33 2011
@@ -32,7 +32,17 @@
<description>Camel Script support</description>
<properties>
-
<camel.osgi.export.pkg>org.apache.camel.builder.script.*</camel.osgi.export.pkg>
+ <camel.osgi.export.pkg>
+ org.apache.camel.builder.script.*;${camel.osgi.version},
+ org.apache.camel.script.osgi.*
+ </camel.osgi.export.pkg>
+ <camel.osgi.import.pkg>
+ !org.apache.camel.builder.script.*,
+ !org.apache.camel.script.osgi.*,
+ ${camel.osgi.import.defaults},
+ *
+ </camel.osgi.import.pkg>
+
<camel.osgi.activator>org.apache.camel.script.osgi.Activator</camel.osgi.activator>
</properties>
<repositories>
@@ -55,21 +65,15 @@
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
-
- <!-- ServiceMix Repo Script Engines -->
+
<dependency>
- <groupId>com.sun.script</groupId>
- <artifactId>groovy-engine</artifactId>
- <version>20080611</version>
- <scope>test</scope>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ <optional>true</optional>
</dependency>
- <dependency>
- <groupId>com.sun.script</groupId>
- <artifactId>js-engine</artifactId>
- <version>20080611</version>
- <scope>test</scope>
- </dependency>
+ <!-- ServiceMix Repo Script Engines -->
<dependency>
<groupId>com.sun.script</groupId>
@@ -78,20 +82,6 @@
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>com.sun.script</groupId>
- <artifactId>jruby-engine</artifactId>
- <version>20080611</version>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>com.sun.script</groupId>
- <artifactId>bsh-engine</artifactId>
- <version>20080611</version>
- <scope>test</scope>
- </dependency>
-
<!-- testing -->
<!-- TODO: use by language test, which we should refactor into
camel-test JAR -->
<dependency>
Modified:
camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java?rev=1092236&r1=1092235&r2=1092236&view=diff
==============================================================================
---
camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
(original)
+++
camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
Thu Apr 14 13:44:33 2011
@@ -19,6 +19,7 @@ package org.apache.camel.builder.script;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.lang.reflect.Method;
import java.net.URL;
import javax.script.Compilable;
@@ -33,6 +34,7 @@ import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.converter.ObjectConverter;
+import org.apache.camel.spi.ScriptEngineResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
@@ -471,6 +473,9 @@ public class ScriptBuilder implements Ex
+ ", please ensure correct JARs is provided on
classpath.");
}
if (engine == null) {
+ engine = checkForOSGiEngine();
+ }
+ if (engine == null) {
throw new IllegalArgumentException("No script engine could be
created for: " + getScriptEngineName());
}
if (isPython()) {
@@ -480,6 +485,24 @@ public class ScriptBuilder implements Ex
return engine;
}
+ private ScriptEngine checkForOSGiEngine() {
+ LOG.debug("No script engine found for " + scriptEngineName + " using
standard javax.script auto-registration. Checking OSGi registry...");
+ try {
+ // Test the OSGi environment with the Activator
+ Class<?> c =
Class.forName("org.apache.camel.script.osgi.Activator");
+ Method mth = c.getDeclaredMethod("getBundleContext");
+ Object ctx = mth.invoke(null);
+ LOG.debug("Found OSGi BundleContext " + ctx);
+ if (ctx != null) {
+ Method resolveScriptEngine =
c.getDeclaredMethod("resolveScriptEngine", String.class);
+ return (ScriptEngine)resolveScriptEngine.invoke(null,
scriptEngineName);
+ }
+ } catch (Throwable t) {
+ LOG.debug("Unable to load OSGi, script engine cannot be found", t);
+ }
+ return null;
+ }
+
protected void compileScript(Compilable compilable) {
try {
if (scriptText != null) {
Added:
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java?rev=1092236&view=auto
==============================================================================
---
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
(added)
+++
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
Thu Apr 14 13:44:33 2011
@@ -0,0 +1,76 @@
+/**
+ * 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 org.apache.camel.script.osgi;
+
+import java.lang.reflect.Method;
+
+import javax.script.ScriptEngine;
+
+import org.apache.camel.spi.ScriptEngineResolver;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Activator implements BundleActivator {
+ private static final transient Logger LOG =
LoggerFactory.getLogger(Activator.class);
+ private static BundleContext context;
+ private static Bundle bundle;
+
+
+ public static Bundle getBundle() {
+ return bundle;
+ }
+
+ public static BundleContext getBundleContext() {
+ return context;
+ }
+
+ public static ScriptEngine resolveScriptEngine(String scriptEngineName)
throws InvalidSyntaxException {
+ ServiceReference[] refs =
context.getServiceReferences(ScriptEngineResolver.class.getName(), null);
+ if (refs == null) {
+ LOG.info("No OSGi script engine resolvers available!");
+ return null;
+ }
+
+ LOG.debug("Found " + refs.length + " OSGi ScriptEngineResolver
services");
+
+ for (ServiceReference ref : refs) {
+ ScriptEngineResolver resolver = (ScriptEngineResolver)
context.getService(ref);
+ ScriptEngine engine =
resolver.resolveScriptEngine(scriptEngineName);
+ context.ungetService(ref);
+ LOG.debug("OSGi resolver " + resolver + " produced " +
scriptEngineName + " engine " + engine);
+ if (engine != null) {
+ return engine;
+ }
+ }
+ return null;
+ }
+
+ public void start(BundleContext context) throws Exception {
+ Activator.context = context;
+ Activator.bundle = context.getBundle();
+ }
+
+ public void stop(BundleContext context) throws Exception {
+ Activator.context = null;
+ Activator.bundle = null;
+ }
+}
Propchange:
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
------------------------------------------------------------------------------
svn:keywords = Rev Date