You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gg...@apache.org on 2015/08/25 09:53:49 UTC

[2/2] camel git commit: [CAMEL-9099] Correctly read META-INF/services/javax.script.ScriptEngineFactory

[CAMEL-9099] Correctly read META-INF/services/javax.script.ScriptEngineFactory

(cherry picked from commit bc103e141cae698091a3ea2c36ec44471d926486)


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/644e013c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/644e013c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/644e013c

Branch: refs/heads/camel-2.15.x
Commit: 644e013cfda43d67e411847832528c1c23fa5929
Parents: 194bdaf
Author: Grzegorz Grzybek <gr...@gmail.com>
Authored: Tue Aug 25 09:38:40 2015 +0200
Committer: Grzegorz Grzybek <gr...@gmail.com>
Committed: Tue Aug 25 09:39:36 2015 +0200

----------------------------------------------------------------------
 components/camel-script/pom.xml                 |  5 ++
 .../org/apache/camel/script/osgi/Activator.java | 13 +++-
 .../apache/camel/script/osgi/ActivatorTest.java | 74 ++++++++++++++++++++
 3 files changed, 91 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/644e013c/components/camel-script/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-script/pom.xml b/components/camel-script/pom.xml
index 0cfafe9..5ae4707 100644
--- a/components/camel-script/pom.xml
+++ b/components/camel-script/pom.xml
@@ -115,6 +115,11 @@
             <artifactId>slf4j-log4j12</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
 
     </dependencies>
 

http://git-wip-us.apache.org/repos/asf/camel/blob/644e013c/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java b/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
index 0db41a4..33365b2 100644
--- a/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
+++ b/components/camel-script/src/main/java/org/apache/camel/script/osgi/Activator.java
@@ -224,7 +224,18 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
         private ScriptEngineFactory getFactory() {
             try {
                 BufferedReader in = IOHelper.buffered(new InputStreamReader(configFile.openStream()));
-                String className = in.readLine();
+                String className = null;
+                while ((className = in.readLine()) != null) {
+                    if ("".equals(className.trim()) || className.trim().startsWith("#")) {
+                        continue;
+                    } else if (className.contains("#")) {
+                        className = className.substring(0, className.indexOf('#')).trim();
+                        break;
+                    } else {
+                        className = className.trim();
+                        break;
+                    }
+                }
                 in.close();
                 Class<?> cls = bundle.loadClass(className);
                 if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {

http://git-wip-us.apache.org/repos/asf/camel/blob/644e013c/components/camel-script/src/test/java/org/apache/camel/script/osgi/ActivatorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-script/src/test/java/org/apache/camel/script/osgi/ActivatorTest.java b/components/camel-script/src/test/java/org/apache/camel/script/osgi/ActivatorTest.java
new file mode 100644
index 0000000..b2719df
--- /dev/null
+++ b/components/camel-script/src/test/java/org/apache/camel/script/osgi/ActivatorTest.java
@@ -0,0 +1,74 @@
+/**
+ * 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.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import javax.script.ScriptEngineFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.ReturnValues;
+import org.mockito.internal.stubbing.answers.AnswerReturnValuesAdapter;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.osgi.framework.Bundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.when;
+
+public class ActivatorTest {
+
+    public static Logger LOG = LoggerFactory.getLogger(ActivatorTest.class);
+
+    private Bundle mockBundle;
+
+    @Before
+    public void mockBundle() throws ClassNotFoundException {
+        mockBundle = Mockito.mock(Bundle.class);
+        when(mockBundle.loadClass(anyString())).thenAnswer(new Answer() {
+            @Override
+            public Object answer(InvocationOnMock invocation) throws Throwable {
+                return ActivatorTest.class.getClassLoader().loadClass(invocation.getArguments()[0].toString());
+            }
+        });
+    }
+
+    @Test
+    public void findScriptEngines() throws IOException {
+        Enumeration<URL> urls = getClass().getClassLoader().getResources(Activator.META_INF_SERVICES_DIR + "/" + Activator.SCRIPT_ENGINE_SERVICE_FILE);
+        assertThat(urls.hasMoreElements(), is(true));
+        while (urls.hasMoreElements()) {
+            URL url = urls.nextElement();
+            LOG.info("Found {}", url);
+            System.out.println("Found: " + url);
+            Activator.BundleScriptEngineResolver resolver = new Activator.BundleScriptEngineResolver(mockBundle, url);
+            ScriptEngineFactory factory = ReflectionTestUtils.invokeMethod(resolver, "getFactory");
+            System.out.println("Factory: " + factory);
+            assertNotNull(factory);
+        }
+    }
+
+}