You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2015/03/26 09:44:45 UTC

[01/11] camel git commit: [CAMEL-8547] Usage of camel-xmlbeans depends on TCCL

Repository: camel
Updated Branches:
  refs/heads/camel-2.14.x 56fa1bc54 -> d51c60071
  refs/heads/camel-2.15.x 7ed9589af -> a121d067e
  refs/heads/master 2fa60111a -> 0d7cfb621


[CAMEL-8547] Usage of camel-xmlbeans depends on TCCL


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

Branch: refs/heads/camel-2.15.x
Commit: dec46867f6d02fa26e14892776b68c33428ec708
Parents: 7ed9589
Author: James Netherton <jn...@redhat.com>
Authored: Wed Mar 25 16:19:16 2015 +0000
Committer: James Netherton <jn...@redhat.com>
Committed: Wed Mar 25 16:30:14 2015 +0000

----------------------------------------------------------------------
 .../converter/xmlbeans/XmlBeansConverter.java   | 104 ++++++++++++++++---
 .../converter/xmlbeans/XmlBeansDataFormat.java  |  28 ++++-
 .../xmlbeans/XmlBeansConverterTest.java         |  18 ++--
 3 files changed, 125 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/dec46867/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
index 8068e86..5b1b011 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
@@ -45,48 +45,120 @@ public final class XmlBeansConverter {
     }
 
     @Converter
-    public static XmlObject toXmlObject(File value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(File value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
-    public static XmlObject toXmlObject(Reader value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(Reader value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
-    public static XmlObject toXmlObject(Node value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(Node value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
-    public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(InputStream value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
     public static XmlObject toXmlObject(String value, Exchange exchange) throws IOException, XmlException {
-        return toXmlObject(IOConverter.toInputStream(value, exchange));
+        return toXmlObject(IOConverter.toInputStream(value, exchange), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException {
-        return toXmlObject(IOConverter.toInputStream(value));
+    public static XmlObject toXmlObject(byte[] value, Exchange exchange) throws IOException, XmlException {
+        return toXmlObject(IOConverter.toInputStream(value), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException {
-        return toXmlObject(NIOConverter.toInputStream(value));
+    public static XmlObject toXmlObject(ByteBuffer value, Exchange exchange) throws IOException, XmlException {
+        return toXmlObject(NIOConverter.toInputStream(value), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(XMLStreamReader value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
     public static XmlObject toXmlObject(Source value, Exchange exchange) throws IOException, XmlException, NoTypeConversionAvailableException {
         Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value);
-        return XmlObject.Factory.parse(reader);
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(reader);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/dec46867/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
index 6559813..28043b5 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
@@ -31,11 +31,35 @@ import org.apache.xmlbeans.XmlObject;
 public class XmlBeansDataFormat implements DataFormat {
 
     public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
         XmlObject object = ExchangeHelper.convertToMandatoryType(exchange, XmlObject.class, body);
-        object.save(stream);
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            object.save(stream);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
-        return XmlObject.Factory.parse(stream);
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(stream);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/dec46867/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
index 86aac74..d73a277 100644
--- a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
+++ b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
@@ -65,13 +65,15 @@ public class XmlBeansConverterTest extends CamelTestSupport {
 
     @Test
     public void toXmlObjectFromFile() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"));
+        XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromReader() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"));
+        XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
@@ -84,13 +86,14 @@ public class XmlBeansConverterTest extends CamelTestSupport {
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.parse(new InputSource(new StringReader(PAYLOAD)));
         
-        XmlObject result = XmlBeansConverter.toXmlObject(document);
+        XmlObject result = XmlBeansConverter.toXmlObject(document, new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromInputStream() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"));
+        XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
@@ -102,19 +105,20 @@ public class XmlBeansConverterTest extends CamelTestSupport {
 
     @Test
     public void toXmlObjectFromByteArray() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes());
+        XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes(), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromByteBuffer() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()));
+        XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromXMLStreamReader() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false));
+        XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 


[07/11] camel git commit: CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle

Posted by ni...@apache.org.
CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle


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

Branch: refs/heads/camel-2.15.x
Commit: a121d067e946149117f5e3e49e8ebe325806bdd8
Parents: 7058dfb
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Mar 26 14:40:19 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:42:35 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/script/osgi/Activator.java | 56 ++++++++++----------
 1 file changed, 29 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a121d067/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 76f02a5..4ffb37d 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
@@ -19,7 +19,13 @@ package org.apache.camel.script.osgi;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.ScriptEngine;
@@ -29,9 +35,15 @@ import org.apache.camel.impl.osgi.tracker.BundleTracker;
 import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer;
 import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.IOHelper;
-
-import org.osgi.framework.*;
-
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -96,7 +108,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
         }
     }
 
-    private String[] getAvailableScriptNames(){
+    private String[] getAvailableScriptNames() {
         List<String> names = new ArrayList<String>();
         for (List<BundleScriptEngineResolver> list : resolvers.values()) {
             for (BundleScriptEngineResolver r : list) {
@@ -107,25 +119,28 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
     }
 
     private void updateAvailableScriptLanguages() {
-        if (registration != null) {
-            registration.unregister();
-            registration = null;
-        }
         ServiceReference<LanguageResolver> ref = null;
         try {
             Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
             if (references.size() == 1) {
+                // Unregistry the old language resolver first
+                if (registration != null) {
+                    registration.unregister();
+                    registration = null;
+                }
                 ref = references.iterator().next();
                 LanguageResolver resolver = context.getService(ref);
-
                 Dictionary props = new Hashtable();
+                // Just publish the language resolve with the language we found
                 props.put("language", getAvailableScriptNames());
                 registration = context.registerService(LanguageResolver.class, resolver, props);
             }
         } catch (InvalidSyntaxException e) {
             LOG.error("Invalid syntax for LanguageResolver service reference filter.");
         } finally {
-            context.ungetService(ref);
+            if (ref != null) {
+                context.ungetService(ref);
+            }
         }
     }
 
@@ -172,7 +187,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
 
     protected static class BundleScriptEngineResolver implements ScriptEngineResolver {
         protected final Bundle bundle;
-        private ServiceRegistration reg;
+        private ServiceRegistration<?> reg;
         private final URL configFile;
 
         public BundleScriptEngineResolver(Bundle bundle, URL configFile) {
@@ -192,7 +207,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
             return getScriptNames(getFactory());
         }
 
-        private List<String> getScriptNames(ScriptEngineFactory factory){
+        private List<String> getScriptNames(ScriptEngineFactory factory) {
             List<String> names = factory.getNames();
             return names;
         }
@@ -207,7 +222,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
                     throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
                 }
                 return (ScriptEngineFactory) cls.newInstance();
-            }catch (Exception e){
+            } catch (Exception e) {
                 //do something
                 return null;
             }
@@ -240,19 +255,6 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
             }
         }
 
-        private boolean matchEngine(String name, String engineName) {
-            if (name.equals(engineName)) {
-                return true;
-            }
-
-            // javascript have many aliases
-            if (name.equals("js") || name.equals("javaScript") || name.equals("ECMAScript")) {
-                return engineName.equals("js") || engineName.equals("javaScript") || engineName.equals("ECMAScript");
-            }
-
-            return false;
-        }
-
         @Override
         public String toString() {
             return "OSGi script engine resolver for " + bundle.getSymbolicName();


[05/11] camel git commit: CAMEL-8547 Usage of camel-xmlbeans depends on TCCL

Posted by ni...@apache.org.
CAMEL-8547 Usage of camel-xmlbeans depends on TCCL


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

Branch: refs/heads/master
Commit: 0d7cfb6212c184c52987b18c51b97d7afd58f02e
Parents: 07587b9
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Mar 26 16:23:51 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:41:44 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/util/ObjectHelper.java     | 38 ++++++++++
 .../converter/xmlbeans/XmlBeansConverter.java   | 73 ++++++++++++++------
 .../converter/xmlbeans/XmlBeansDataFormat.java  | 24 +++++--
 .../xmlbeans/XmlBeansConverterTest.java         | 42 +++++------
 4 files changed, 128 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index 4c1d22b..3aba156 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -43,6 +43,7 @@ import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Properties;
 import java.util.Scanner;
+import java.util.concurrent.Callable;
 
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -1815,4 +1816,41 @@ public final class ObjectHelper {
         // value must be a number
         return value.equals(Float.NaN) || value.equals(Double.NaN);
     }
+    
+    /**
+     * Calling the Callable with the setting of TCCL with the camel context application classloader;
+     * 
+     * @param call the Callable instance
+     * @param exchange the exchange 
+     * @return the result of Callable return  
+     */
+    public static Object callWithTCCL(Callable<?> call, Exchange exchange) throws Exception {
+        ClassLoader apcl = null;
+        if (exchange != null && exchange.getContext() != null) {
+            apcl = exchange.getContext().getApplicationContextClassLoader();
+        }
+        return callWithTCCL(call, apcl);
+    }
+    
+    /**
+     * Calling the Callable with the setting of TCCL with a given classloader;
+     * 
+     * @param call the Callable instance
+     * @param  the exchange 
+     * @return the result of Callable return  
+     */
+    public static Object callWithTCCL(Callable<?> call, ClassLoader classloader) throws Exception {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+        try {
+            if (classloader != null) {
+                Thread.currentThread().setContextClassLoader(classloader);
+            }
+            return call.call();
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
index 8068e86..3b034b8 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
@@ -17,23 +17,25 @@
 package org.apache.camel.converter.xmlbeans;
 
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.nio.ByteBuffer;
+import java.util.concurrent.Callable;
+
 import javax.xml.transform.Source;
 
 import org.w3c.dom.Node;
 
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
-import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.converter.IOConverter;
 import org.apache.camel.converter.NIOConverter;
-import org.apache.xmlbeans.XmlException;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader;
 
+
+
 /**
  * A <a href="http://camel.apache.org/type-coverter.html">Type Converter</a>
  * of XMLBeans objects
@@ -43,50 +45,75 @@ public final class XmlBeansConverter {
 
     private XmlBeansConverter() {
     }
-
+    
     @Converter
-    public static XmlObject toXmlObject(File value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(final File value, Exchange exchange) throws Exception {
+        return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() {
+            public XmlObject call() throws Exception {
+                return XmlObject.Factory.parse(value);
+            }
+        }, exchange); 
+        
     }
 
     @Converter
-    public static XmlObject toXmlObject(Reader value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(final Reader value, Exchange exchange) throws Exception {
+        return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() {
+            public XmlObject call() throws Exception {
+                return XmlObject.Factory.parse(value);
+            }
+        }, exchange);    
     }
 
     @Converter
-    public static XmlObject toXmlObject(Node value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(final Node value, Exchange exchange) throws Exception {
+        return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() {
+            public XmlObject call() throws Exception {
+                return XmlObject.Factory.parse(value);
+            }
+        }, exchange);    
     }
 
     @Converter
-    public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(final InputStream value, Exchange exchange) throws Exception {
+        return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() {
+            public XmlObject call() throws Exception {
+                return XmlObject.Factory.parse(value);
+            }
+        }, exchange);    
     }
 
     @Converter
-    public static XmlObject toXmlObject(String value, Exchange exchange) throws IOException, XmlException {
-        return toXmlObject(IOConverter.toInputStream(value, exchange));
+    public static XmlObject toXmlObject(String value, Exchange exchange) throws Exception {
+        return toXmlObject(IOConverter.toInputStream(value, exchange), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException {
-        return toXmlObject(IOConverter.toInputStream(value));
+    public static XmlObject toXmlObject(byte[] value, Exchange exchange) throws Exception {
+        return toXmlObject(IOConverter.toInputStream(value), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException {
-        return toXmlObject(NIOConverter.toInputStream(value));
+    public static XmlObject toXmlObject(ByteBuffer value, Exchange exchange) throws Exception {
+        return toXmlObject(NIOConverter.toInputStream(value), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(final XMLStreamReader value, Exchange exchange) throws Exception {
+        return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() {
+            public XmlObject call() throws Exception {
+                return XmlObject.Factory.parse(value);
+            }
+        }, exchange);    
     }
 
     @Converter
-    public static XmlObject toXmlObject(Source value, Exchange exchange) throws IOException, XmlException, NoTypeConversionAvailableException {
-        Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value);
-        return XmlObject.Factory.parse(reader);
+    public static XmlObject toXmlObject(Source value, Exchange exchange) throws Exception {
+        final Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value);
+        return (XmlObject) ObjectHelper.callWithTCCL(new Callable<XmlObject>() {
+            public XmlObject call() throws Exception {
+                return XmlObject.Factory.parse(reader);
+            }
+        }, exchange);   
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
index 6559813..41dd0ff 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
@@ -18,10 +18,12 @@ package org.apache.camel.converter.xmlbeans;
 
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.concurrent.Callable;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.xmlbeans.XmlObject;
 
 /**
@@ -30,12 +32,24 @@ import org.apache.xmlbeans.XmlObject;
  */
 public class XmlBeansDataFormat implements DataFormat {
 
-    public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
-        XmlObject object = ExchangeHelper.convertToMandatoryType(exchange, XmlObject.class, body);
-        object.save(stream);
+    public void marshal(final Exchange exchange, final Object body, final OutputStream stream) throws Exception {
+        ObjectHelper.callWithTCCL(new Callable<Void>() {
+            @Override
+            public Void call() throws Exception {
+                XmlObject object = ExchangeHelper.convertToMandatoryType(exchange, XmlObject.class, body);
+                object.save(stream);
+                return null;
+            }
+        }, exchange);
+        
     }
 
-    public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
-        return XmlObject.Factory.parse(stream);
+    public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception {
+        return ObjectHelper.callWithTCCL(new Callable<Object>() {
+            @Override
+            public Object call() throws Exception {
+                return XmlObject.Factory.parse(stream);
+            }
+        }, exchange);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0d7cfb62/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
index 86aac74..3638268 100644
--- a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
+++ b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
@@ -20,26 +20,21 @@ import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
-import java.io.IOException;
 import java.io.StringReader;
 import java.nio.ByteBuffer;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 
 import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
 
 import org.apache.camel.BytesSource;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
-import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.test.junit4.CamelTestSupport;
-import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.impl.piccolo.xml.XMLStreamReader;
 import org.junit.Test;
@@ -64,19 +59,21 @@ public class XmlBeansConverterTest extends CamelTestSupport {
     }
 
     @Test
-    public void toXmlObjectFromFile() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"));
+    public void toXmlObjectFromFile() throws Exception {
+        XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"),
+                                                         new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromReader() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"));
+    public void toXmlObjectFromReader() throws Exception {
+        XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"), 
+                                                         new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromNode() throws IOException, XmlException, ParserConfigurationException, SAXException {
+    public void toXmlObjectFromNode() throws Exception {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         factory.setNamespaceAware(true);
         factory.setIgnoringElementContentWhitespace(true);
@@ -84,42 +81,45 @@ public class XmlBeansConverterTest extends CamelTestSupport {
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.parse(new InputSource(new StringReader(PAYLOAD)));
         
-        XmlObject result = XmlBeansConverter.toXmlObject(document);
+        XmlObject result = XmlBeansConverter.toXmlObject(document, 
+                                                         new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromInputStream() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"));
+    public void toXmlObjectFromInputStream() throws Exception {
+        XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"),
+                                                         new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromString() throws IOException, XmlException {
+    public void toXmlObjectFromString() throws Exception {
         XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD, new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromByteArray() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes());
+    public void toXmlObjectFromByteArray() throws Exception {
+        XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes(), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromByteBuffer() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()));
+    public void toXmlObjectFromByteBuffer() throws Exception {
+        XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromXMLStreamReader() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false));
+    public void toXmlObjectFromXMLStreamReader() throws Exception {
+        XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false), 
+                                                         new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
-    public void toXmlObjectFromSource() throws IOException, XmlException, NoTypeConversionAvailableException {
+    public void toXmlObjectFromSource() throws Exception {
         XmlObject result = XmlBeansConverter.toXmlObject(new BytesSource(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }


[04/11] camel git commit: CAMEL-8546: fix script language resolvers

Posted by ni...@apache.org.
CAMEL-8546: fix script language resolvers


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

Branch: refs/heads/master
Commit: 929ec2cdd128f2fed7fb913baead8f728b918444
Parents: 2fa6011
Author: bart <ba...@anova.be>
Authored: Wed Mar 25 16:59:57 2015 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:41:44 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/script/osgi/Activator.java | 90 ++++++++++++++++----
 1 file changed, 73 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/929ec2cd/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 3dd1c73..76f02a5 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
@@ -19,10 +19,7 @@ package org.apache.camel.script.osgi;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.ScriptEngine;
@@ -30,27 +27,23 @@ import javax.script.ScriptEngineFactory;
 
 import org.apache.camel.impl.osgi.tracker.BundleTracker;
 import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer;
+import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.IOHelper;
 
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.*;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class Activator implements BundleActivator, BundleTrackerCustomizer {
+public class Activator implements BundleActivator, BundleTrackerCustomizer, ServiceListener {
     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 Logger LOG = LoggerFactory.getLogger(Activator.class);
     private static BundleContext context;
     private BundleTracker tracker;
-    
+    private ServiceRegistration<LanguageResolver> registration;
+
     private Map<Long, List<BundleScriptEngineResolver>> resolvers 
         = new ConcurrentHashMap<Long, List<BundleScriptEngineResolver>>();
 
@@ -63,12 +56,17 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
         LOG.info("Camel-Script activator starting");
         tracker = new BundleTracker(context, Bundle.ACTIVE, this);
         tracker.open();
+        context.addServiceListener(this, "(&(resolver=default)(objectClass=org.apache.camel.spi.LanguageResolver))");
         LOG.info("Camel-Script activator started");
     }
 
     public void stop(BundleContext context) throws Exception {
         LOG.info("Camel-Script activator stopping");
         tracker.close();
+        context.removeServiceListener(this);
+        if (registration != null) {
+            registration.unregister();
+        }
         LOG.info("Camel-Script activator stopped");
         Activator.context = null;
     }
@@ -80,6 +78,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             service.register();
         }
         resolvers.put(bundle.getBundleId(), r);
+        updateAvailableScriptLanguages();
         return bundle;
     }
 
@@ -90,12 +89,46 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
         LOG.debug("Bundle stopped: {}", bundle.getSymbolicName());
         List<BundleScriptEngineResolver> r = resolvers.remove(bundle.getBundleId());
         if (r != null) {
+            updateAvailableScriptLanguages();
             for (BundleScriptEngineResolver service : r) {
                 service.unregister();
             }
         }
     }
 
+    private String[] getAvailableScriptNames(){
+        List<String> names = new ArrayList<String>();
+        for (List<BundleScriptEngineResolver> list : resolvers.values()) {
+            for (BundleScriptEngineResolver r : list) {
+                names.addAll(r.getScriptNames());
+            }
+        }
+        return names.toArray(new String[]{});
+    }
+
+    private void updateAvailableScriptLanguages() {
+        if (registration != null) {
+            registration.unregister();
+            registration = null;
+        }
+        ServiceReference<LanguageResolver> ref = null;
+        try {
+            Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
+            if (references.size() == 1) {
+                ref = references.iterator().next();
+                LanguageResolver resolver = context.getService(ref);
+
+                Dictionary props = new Hashtable();
+                props.put("language", getAvailableScriptNames());
+                registration = context.registerService(LanguageResolver.class, resolver, props);
+            }
+        } catch (InvalidSyntaxException e) {
+            LOG.error("Invalid syntax for LanguageResolver service reference filter.");
+        } finally {
+            context.ungetService(ref);
+        }
+    }
+
     public static ScriptEngine resolveScriptEngine(String scriptEngineName) throws InvalidSyntaxException {
         ServiceReference<?>[] refs = context.getServiceReferences(ScriptEngineResolver.class.getName(), null);
         if (refs == null) {
@@ -126,7 +159,12 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             LOG.info("Found ScriptEngineFactory in " + bundle.getSymbolicName());
             resolvers.add(new BundleScriptEngineResolver(bundle, configURL));
         }
-    } 
+    }
+
+    @Override
+    public void serviceChanged(ServiceEvent event) {
+        updateAvailableScriptLanguages();
+    }
 
     public interface ScriptEngineResolver {
         ScriptEngine resolveScriptEngine(String name);
@@ -150,7 +188,16 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             reg.unregister();
         }
 
-        public ScriptEngine resolveScriptEngine(String name) {
+        private List<String> getScriptNames() {
+            return getScriptNames(getFactory());
+        }
+
+        private List<String> getScriptNames(ScriptEngineFactory factory){
+            List<String> names = factory.getNames();
+            return names;
+        }
+
+        private ScriptEngineFactory getFactory() {
             try {
                 BufferedReader in = IOHelper.buffered(new InputStreamReader(configFile.openStream()));
                 String className = in.readLine();
@@ -159,8 +206,17 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
                 if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
                     throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
                 }
-                ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
-                List<String> names = factory.getNames();
+                return (ScriptEngineFactory) cls.newInstance();
+            }catch (Exception e){
+                //do something
+                return null;
+            }
+        }
+
+        public ScriptEngine resolveScriptEngine(String name) {
+            try {
+                ScriptEngineFactory factory = getFactory();
+                List<String> names = getScriptNames(factory);
                 for (String test : names) {
                     if (test.equals(name)) {
                         ClassLoader old = Thread.currentThread().getContextClassLoader();


[10/11] camel git commit: CAMEL-8546: fix script language resolvers

Posted by ni...@apache.org.
CAMEL-8546: fix script language resolvers


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

Branch: refs/heads/camel-2.14.x
Commit: 9c473db957f59f08bbed159572a417e1c99dddd0
Parents: 35a87e6
Author: bart <ba...@anova.be>
Authored: Wed Mar 25 16:59:57 2015 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:44:04 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/script/osgi/Activator.java | 90 ++++++++++++++++----
 1 file changed, 73 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9c473db9/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 3dd1c73..76f02a5 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
@@ -19,10 +19,7 @@ package org.apache.camel.script.osgi;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.ScriptEngine;
@@ -30,27 +27,23 @@ import javax.script.ScriptEngineFactory;
 
 import org.apache.camel.impl.osgi.tracker.BundleTracker;
 import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer;
+import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.IOHelper;
 
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.*;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class Activator implements BundleActivator, BundleTrackerCustomizer {
+public class Activator implements BundleActivator, BundleTrackerCustomizer, ServiceListener {
     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 Logger LOG = LoggerFactory.getLogger(Activator.class);
     private static BundleContext context;
     private BundleTracker tracker;
-    
+    private ServiceRegistration<LanguageResolver> registration;
+
     private Map<Long, List<BundleScriptEngineResolver>> resolvers 
         = new ConcurrentHashMap<Long, List<BundleScriptEngineResolver>>();
 
@@ -63,12 +56,17 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
         LOG.info("Camel-Script activator starting");
         tracker = new BundleTracker(context, Bundle.ACTIVE, this);
         tracker.open();
+        context.addServiceListener(this, "(&(resolver=default)(objectClass=org.apache.camel.spi.LanguageResolver))");
         LOG.info("Camel-Script activator started");
     }
 
     public void stop(BundleContext context) throws Exception {
         LOG.info("Camel-Script activator stopping");
         tracker.close();
+        context.removeServiceListener(this);
+        if (registration != null) {
+            registration.unregister();
+        }
         LOG.info("Camel-Script activator stopped");
         Activator.context = null;
     }
@@ -80,6 +78,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             service.register();
         }
         resolvers.put(bundle.getBundleId(), r);
+        updateAvailableScriptLanguages();
         return bundle;
     }
 
@@ -90,12 +89,46 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
         LOG.debug("Bundle stopped: {}", bundle.getSymbolicName());
         List<BundleScriptEngineResolver> r = resolvers.remove(bundle.getBundleId());
         if (r != null) {
+            updateAvailableScriptLanguages();
             for (BundleScriptEngineResolver service : r) {
                 service.unregister();
             }
         }
     }
 
+    private String[] getAvailableScriptNames(){
+        List<String> names = new ArrayList<String>();
+        for (List<BundleScriptEngineResolver> list : resolvers.values()) {
+            for (BundleScriptEngineResolver r : list) {
+                names.addAll(r.getScriptNames());
+            }
+        }
+        return names.toArray(new String[]{});
+    }
+
+    private void updateAvailableScriptLanguages() {
+        if (registration != null) {
+            registration.unregister();
+            registration = null;
+        }
+        ServiceReference<LanguageResolver> ref = null;
+        try {
+            Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
+            if (references.size() == 1) {
+                ref = references.iterator().next();
+                LanguageResolver resolver = context.getService(ref);
+
+                Dictionary props = new Hashtable();
+                props.put("language", getAvailableScriptNames());
+                registration = context.registerService(LanguageResolver.class, resolver, props);
+            }
+        } catch (InvalidSyntaxException e) {
+            LOG.error("Invalid syntax for LanguageResolver service reference filter.");
+        } finally {
+            context.ungetService(ref);
+        }
+    }
+
     public static ScriptEngine resolveScriptEngine(String scriptEngineName) throws InvalidSyntaxException {
         ServiceReference<?>[] refs = context.getServiceReferences(ScriptEngineResolver.class.getName(), null);
         if (refs == null) {
@@ -126,7 +159,12 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             LOG.info("Found ScriptEngineFactory in " + bundle.getSymbolicName());
             resolvers.add(new BundleScriptEngineResolver(bundle, configURL));
         }
-    } 
+    }
+
+    @Override
+    public void serviceChanged(ServiceEvent event) {
+        updateAvailableScriptLanguages();
+    }
 
     public interface ScriptEngineResolver {
         ScriptEngine resolveScriptEngine(String name);
@@ -150,7 +188,16 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             reg.unregister();
         }
 
-        public ScriptEngine resolveScriptEngine(String name) {
+        private List<String> getScriptNames() {
+            return getScriptNames(getFactory());
+        }
+
+        private List<String> getScriptNames(ScriptEngineFactory factory){
+            List<String> names = factory.getNames();
+            return names;
+        }
+
+        private ScriptEngineFactory getFactory() {
             try {
                 BufferedReader in = IOHelper.buffered(new InputStreamReader(configFile.openStream()));
                 String className = in.readLine();
@@ -159,8 +206,17 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
                 if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
                     throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
                 }
-                ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
-                List<String> names = factory.getNames();
+                return (ScriptEngineFactory) cls.newInstance();
+            }catch (Exception e){
+                //do something
+                return null;
+            }
+        }
+
+        public ScriptEngine resolveScriptEngine(String name) {
+            try {
+                ScriptEngineFactory factory = getFactory();
+                List<String> names = getScriptNames(factory);
                 for (String test : names) {
                     if (test.equals(name)) {
                         ClassLoader old = Thread.currentThread().getContextClassLoader();


[02/11] camel git commit: CAMEL-8547 Polish the code to avoid breaking the API

Posted by ni...@apache.org.
CAMEL-8547 Polish the code to avoid breaking the API


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

Branch: refs/heads/camel-2.15.x
Commit: ff7a85328b4141c92a0a0d883e1f2cc03167a755
Parents: dec4686
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Mar 26 16:41:06 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:41:06 2015 +0800

----------------------------------------------------------------------
 .../converter/xmlbeans/XmlBeansConverter.java   | 84 +++++++++++++++++---
 1 file changed, 72 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ff7a8532/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
index 5b1b011..4239c09 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
@@ -44,12 +44,62 @@ public final class XmlBeansConverter {
     private XmlBeansConverter() {
     }
 
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(File value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(Reader value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(Node value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(String value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+
     @Converter
     public static XmlObject toXmlObject(File value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -64,9 +114,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(Reader value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -81,9 +133,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(Node value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -98,9 +152,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(InputStream value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -130,9 +186,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(XMLStreamReader value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -148,9 +206,11 @@ public final class XmlBeansConverter {
     public static XmlObject toXmlObject(Source value, Exchange exchange) throws IOException, XmlException, NoTypeConversionAvailableException {
         Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value);
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }


[06/11] camel git commit: CAMEL-8546: fix script language resolvers

Posted by ni...@apache.org.
CAMEL-8546: fix script language resolvers


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

Branch: refs/heads/camel-2.15.x
Commit: 7058dfb0c03234a3127b4b3a73201c08c4e6a0c5
Parents: ff7a853
Author: bart <ba...@anova.be>
Authored: Wed Mar 25 16:59:57 2015 +0100
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:42:22 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/script/osgi/Activator.java | 90 ++++++++++++++++----
 1 file changed, 73 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7058dfb0/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 3dd1c73..76f02a5 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
@@ -19,10 +19,7 @@ package org.apache.camel.script.osgi;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.ScriptEngine;
@@ -30,27 +27,23 @@ import javax.script.ScriptEngineFactory;
 
 import org.apache.camel.impl.osgi.tracker.BundleTracker;
 import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer;
+import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.IOHelper;
 
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.*;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class Activator implements BundleActivator, BundleTrackerCustomizer {
+public class Activator implements BundleActivator, BundleTrackerCustomizer, ServiceListener {
     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 Logger LOG = LoggerFactory.getLogger(Activator.class);
     private static BundleContext context;
     private BundleTracker tracker;
-    
+    private ServiceRegistration<LanguageResolver> registration;
+
     private Map<Long, List<BundleScriptEngineResolver>> resolvers 
         = new ConcurrentHashMap<Long, List<BundleScriptEngineResolver>>();
 
@@ -63,12 +56,17 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
         LOG.info("Camel-Script activator starting");
         tracker = new BundleTracker(context, Bundle.ACTIVE, this);
         tracker.open();
+        context.addServiceListener(this, "(&(resolver=default)(objectClass=org.apache.camel.spi.LanguageResolver))");
         LOG.info("Camel-Script activator started");
     }
 
     public void stop(BundleContext context) throws Exception {
         LOG.info("Camel-Script activator stopping");
         tracker.close();
+        context.removeServiceListener(this);
+        if (registration != null) {
+            registration.unregister();
+        }
         LOG.info("Camel-Script activator stopped");
         Activator.context = null;
     }
@@ -80,6 +78,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             service.register();
         }
         resolvers.put(bundle.getBundleId(), r);
+        updateAvailableScriptLanguages();
         return bundle;
     }
 
@@ -90,12 +89,46 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
         LOG.debug("Bundle stopped: {}", bundle.getSymbolicName());
         List<BundleScriptEngineResolver> r = resolvers.remove(bundle.getBundleId());
         if (r != null) {
+            updateAvailableScriptLanguages();
             for (BundleScriptEngineResolver service : r) {
                 service.unregister();
             }
         }
     }
 
+    private String[] getAvailableScriptNames(){
+        List<String> names = new ArrayList<String>();
+        for (List<BundleScriptEngineResolver> list : resolvers.values()) {
+            for (BundleScriptEngineResolver r : list) {
+                names.addAll(r.getScriptNames());
+            }
+        }
+        return names.toArray(new String[]{});
+    }
+
+    private void updateAvailableScriptLanguages() {
+        if (registration != null) {
+            registration.unregister();
+            registration = null;
+        }
+        ServiceReference<LanguageResolver> ref = null;
+        try {
+            Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
+            if (references.size() == 1) {
+                ref = references.iterator().next();
+                LanguageResolver resolver = context.getService(ref);
+
+                Dictionary props = new Hashtable();
+                props.put("language", getAvailableScriptNames());
+                registration = context.registerService(LanguageResolver.class, resolver, props);
+            }
+        } catch (InvalidSyntaxException e) {
+            LOG.error("Invalid syntax for LanguageResolver service reference filter.");
+        } finally {
+            context.ungetService(ref);
+        }
+    }
+
     public static ScriptEngine resolveScriptEngine(String scriptEngineName) throws InvalidSyntaxException {
         ServiceReference<?>[] refs = context.getServiceReferences(ScriptEngineResolver.class.getName(), null);
         if (refs == null) {
@@ -126,7 +159,12 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             LOG.info("Found ScriptEngineFactory in " + bundle.getSymbolicName());
             resolvers.add(new BundleScriptEngineResolver(bundle, configURL));
         }
-    } 
+    }
+
+    @Override
+    public void serviceChanged(ServiceEvent event) {
+        updateAvailableScriptLanguages();
+    }
 
     public interface ScriptEngineResolver {
         ScriptEngine resolveScriptEngine(String name);
@@ -150,7 +188,16 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
             reg.unregister();
         }
 
-        public ScriptEngine resolveScriptEngine(String name) {
+        private List<String> getScriptNames() {
+            return getScriptNames(getFactory());
+        }
+
+        private List<String> getScriptNames(ScriptEngineFactory factory){
+            List<String> names = factory.getNames();
+            return names;
+        }
+
+        private ScriptEngineFactory getFactory() {
             try {
                 BufferedReader in = IOHelper.buffered(new InputStreamReader(configFile.openStream()));
                 String className = in.readLine();
@@ -159,8 +206,17 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer {
                 if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
                     throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
                 }
-                ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
-                List<String> names = factory.getNames();
+                return (ScriptEngineFactory) cls.newInstance();
+            }catch (Exception e){
+                //do something
+                return null;
+            }
+        }
+
+        public ScriptEngine resolveScriptEngine(String name) {
+            try {
+                ScriptEngineFactory factory = getFactory();
+                List<String> names = getScriptNames(factory);
                 for (String test : names) {
                     if (test.equals(name)) {
                         ClassLoader old = Thread.currentThread().getContextClassLoader();


[11/11] camel git commit: CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle

Posted by ni...@apache.org.
CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle


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

Branch: refs/heads/camel-2.14.x
Commit: d51c60071cdea97854d1f932a1a84d99381a0009
Parents: 9c473db
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Mar 26 14:40:19 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:44:21 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/script/osgi/Activator.java | 56 ++++++++++----------
 1 file changed, 29 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d51c6007/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 76f02a5..4ffb37d 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
@@ -19,7 +19,13 @@ package org.apache.camel.script.osgi;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.ScriptEngine;
@@ -29,9 +35,15 @@ import org.apache.camel.impl.osgi.tracker.BundleTracker;
 import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer;
 import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.IOHelper;
-
-import org.osgi.framework.*;
-
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -96,7 +108,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
         }
     }
 
-    private String[] getAvailableScriptNames(){
+    private String[] getAvailableScriptNames() {
         List<String> names = new ArrayList<String>();
         for (List<BundleScriptEngineResolver> list : resolvers.values()) {
             for (BundleScriptEngineResolver r : list) {
@@ -107,25 +119,28 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
     }
 
     private void updateAvailableScriptLanguages() {
-        if (registration != null) {
-            registration.unregister();
-            registration = null;
-        }
         ServiceReference<LanguageResolver> ref = null;
         try {
             Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
             if (references.size() == 1) {
+                // Unregistry the old language resolver first
+                if (registration != null) {
+                    registration.unregister();
+                    registration = null;
+                }
                 ref = references.iterator().next();
                 LanguageResolver resolver = context.getService(ref);
-
                 Dictionary props = new Hashtable();
+                // Just publish the language resolve with the language we found
                 props.put("language", getAvailableScriptNames());
                 registration = context.registerService(LanguageResolver.class, resolver, props);
             }
         } catch (InvalidSyntaxException e) {
             LOG.error("Invalid syntax for LanguageResolver service reference filter.");
         } finally {
-            context.ungetService(ref);
+            if (ref != null) {
+                context.ungetService(ref);
+            }
         }
     }
 
@@ -172,7 +187,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
 
     protected static class BundleScriptEngineResolver implements ScriptEngineResolver {
         protected final Bundle bundle;
-        private ServiceRegistration reg;
+        private ServiceRegistration<?> reg;
         private final URL configFile;
 
         public BundleScriptEngineResolver(Bundle bundle, URL configFile) {
@@ -192,7 +207,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
             return getScriptNames(getFactory());
         }
 
-        private List<String> getScriptNames(ScriptEngineFactory factory){
+        private List<String> getScriptNames(ScriptEngineFactory factory) {
             List<String> names = factory.getNames();
             return names;
         }
@@ -207,7 +222,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
                     throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
                 }
                 return (ScriptEngineFactory) cls.newInstance();
-            }catch (Exception e){
+            } catch (Exception e) {
                 //do something
                 return null;
             }
@@ -240,19 +255,6 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
             }
         }
 
-        private boolean matchEngine(String name, String engineName) {
-            if (name.equals(engineName)) {
-                return true;
-            }
-
-            // javascript have many aliases
-            if (name.equals("js") || name.equals("javaScript") || name.equals("ECMAScript")) {
-                return engineName.equals("js") || engineName.equals("javaScript") || engineName.equals("ECMAScript");
-            }
-
-            return false;
-        }
-
         @Override
         public String toString() {
             return "OSGi script engine resolver for " + bundle.getSymbolicName();


[09/11] camel git commit: CAMEL-8547 Polish the code to avoid breaking the API

Posted by ni...@apache.org.
CAMEL-8547 Polish the code to avoid breaking the API


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

Branch: refs/heads/camel-2.14.x
Commit: 35a87e6b31797019d242d10ac8a996e213e59219
Parents: 611781c
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Mar 26 16:41:06 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:43:52 2015 +0800

----------------------------------------------------------------------
 .../converter/xmlbeans/XmlBeansConverter.java   | 84 +++++++++++++++++---
 1 file changed, 72 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/35a87e6b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
index 5b1b011..4239c09 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
@@ -44,12 +44,62 @@ public final class XmlBeansConverter {
     private XmlBeansConverter() {
     }
 
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(File value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(Reader value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(Node value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(String value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+    
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+
+    @Deprecated
+    // It will be removed in Camel 2.16.0, please use the one with Exchange parameter
+    public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException {
+        return toXmlObject(value, null);
+    }
+
     @Converter
     public static XmlObject toXmlObject(File value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -64,9 +114,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(Reader value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -81,9 +133,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(Node value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -98,9 +152,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(InputStream value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -130,9 +186,11 @@ public final class XmlBeansConverter {
     @Converter
     public static XmlObject toXmlObject(XMLStreamReader value, Exchange exchange) throws IOException, XmlException {
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }
@@ -148,9 +206,11 @@ public final class XmlBeansConverter {
     public static XmlObject toXmlObject(Source value, Exchange exchange) throws IOException, XmlException, NoTypeConversionAvailableException {
         Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value);
         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-
         try {
-            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            ClassLoader apcl = null;
+            if (exchange != null && exchange.getContext() != null) {
+                apcl = exchange.getContext().getApplicationContextClassLoader();
+            }
             if (apcl != null) {
                 Thread.currentThread().setContextClassLoader(apcl);
             }


[08/11] camel git commit: [CAMEL-8547] Usage of camel-xmlbeans depends on TCCL

Posted by ni...@apache.org.
[CAMEL-8547] Usage of camel-xmlbeans depends on TCCL


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

Branch: refs/heads/camel-2.14.x
Commit: 611781c0a89ec2a9399d2cbd01b3b7c5d211d4a6
Parents: 56fa1bc
Author: James Netherton <jn...@redhat.com>
Authored: Wed Mar 25 16:19:16 2015 +0000
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:43:34 2015 +0800

----------------------------------------------------------------------
 .../converter/xmlbeans/XmlBeansConverter.java   | 104 ++++++++++++++++---
 .../converter/xmlbeans/XmlBeansDataFormat.java  |  28 ++++-
 .../xmlbeans/XmlBeansConverterTest.java         |  18 ++--
 3 files changed, 125 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/611781c0/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
index 8068e86..5b1b011 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansConverter.java
@@ -45,48 +45,120 @@ public final class XmlBeansConverter {
     }
 
     @Converter
-    public static XmlObject toXmlObject(File value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(File value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
-    public static XmlObject toXmlObject(Reader value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(Reader value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
-    public static XmlObject toXmlObject(Node value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(Node value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
-    public static XmlObject toXmlObject(InputStream value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(InputStream value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
     public static XmlObject toXmlObject(String value, Exchange exchange) throws IOException, XmlException {
-        return toXmlObject(IOConverter.toInputStream(value, exchange));
+        return toXmlObject(IOConverter.toInputStream(value, exchange), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(byte[] value) throws IOException, XmlException {
-        return toXmlObject(IOConverter.toInputStream(value));
+    public static XmlObject toXmlObject(byte[] value, Exchange exchange) throws IOException, XmlException {
+        return toXmlObject(IOConverter.toInputStream(value), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(ByteBuffer value) throws IOException, XmlException {
-        return toXmlObject(NIOConverter.toInputStream(value));
+    public static XmlObject toXmlObject(ByteBuffer value, Exchange exchange) throws IOException, XmlException {
+        return toXmlObject(NIOConverter.toInputStream(value), exchange);
     }
 
     @Converter
-    public static XmlObject toXmlObject(XMLStreamReader value) throws IOException, XmlException {
-        return XmlObject.Factory.parse(value);
+    public static XmlObject toXmlObject(XMLStreamReader value, Exchange exchange) throws IOException, XmlException {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(value);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     @Converter
     public static XmlObject toXmlObject(Source value, Exchange exchange) throws IOException, XmlException, NoTypeConversionAvailableException {
         Reader reader = exchange.getContext().getTypeConverter().mandatoryConvertTo(Reader.class, value);
-        return XmlObject.Factory.parse(reader);
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(reader);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/611781c0/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
index 6559813..28043b5 100644
--- a/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
+++ b/components/camel-xmlbeans/src/main/java/org/apache/camel/converter/xmlbeans/XmlBeansDataFormat.java
@@ -31,11 +31,35 @@ import org.apache.xmlbeans.XmlObject;
 public class XmlBeansDataFormat implements DataFormat {
 
     public void marshal(Exchange exchange, Object body, OutputStream stream) throws Exception {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
         XmlObject object = ExchangeHelper.convertToMandatoryType(exchange, XmlObject.class, body);
-        object.save(stream);
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            object.save(stream);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 
     public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
-        return XmlObject.Factory.parse(stream);
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+
+        try {
+            ClassLoader apcl = exchange.getContext().getApplicationContextClassLoader();
+            if (apcl != null) {
+                Thread.currentThread().setContextClassLoader(apcl);
+            }
+            return XmlObject.Factory.parse(stream);
+        } finally {
+            if (tccl != null) {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/611781c0/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
index 86aac74..d73a277 100644
--- a/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
+++ b/components/camel-xmlbeans/src/test/java/org/apache/camel/converter/xmlbeans/XmlBeansConverterTest.java
@@ -65,13 +65,15 @@ public class XmlBeansConverterTest extends CamelTestSupport {
 
     @Test
     public void toXmlObjectFromFile() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"));
+        XmlObject result = XmlBeansConverter.toXmlObject(new File("src/test/data/buyStocks.xml"),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromReader() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"));
+        XmlObject result = XmlBeansConverter.toXmlObject(new FileReader("src/test/data/buyStocks.xml"),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
@@ -84,13 +86,14 @@ public class XmlBeansConverterTest extends CamelTestSupport {
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.parse(new InputSource(new StringReader(PAYLOAD)));
         
-        XmlObject result = XmlBeansConverter.toXmlObject(document);
+        XmlObject result = XmlBeansConverter.toXmlObject(document, new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromInputStream() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"));
+        XmlObject result = XmlBeansConverter.toXmlObject(new FileInputStream("src/test/data/buyStocks.xml"),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
@@ -102,19 +105,20 @@ public class XmlBeansConverterTest extends CamelTestSupport {
 
     @Test
     public void toXmlObjectFromByteArray() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes());
+        XmlObject result = XmlBeansConverter.toXmlObject(PAYLOAD.getBytes(), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromByteBuffer() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()));
+        XmlObject result = XmlBeansConverter.toXmlObject(ByteBuffer.wrap(PAYLOAD.getBytes()), new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 
     @Test
     public void toXmlObjectFromXMLStreamReader() throws IOException, XmlException {
-        XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false));
+        XmlObject result = XmlBeansConverter.toXmlObject(new XMLStreamReader(new ByteArrayInputStream(PAYLOAD.getBytes()), false),
+                new DefaultExchange(new DefaultCamelContext()));
         assertBuyStocks(result);
     }
 


[03/11] camel git commit: CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle

Posted by ni...@apache.org.
CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle


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

Branch: refs/heads/master
Commit: 07587b91b64a6aeba19aea733fe50a0f280c2299
Parents: 929ec2c
Author: Willem Jiang <wi...@gmail.com>
Authored: Thu Mar 26 14:40:19 2015 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Thu Mar 26 16:41:44 2015 +0800

----------------------------------------------------------------------
 .../org/apache/camel/script/osgi/Activator.java | 56 ++++++++++----------
 1 file changed, 29 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/07587b91/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 76f02a5..4ffb37d 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
@@ -19,7 +19,13 @@ package org.apache.camel.script.osgi;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.ScriptEngine;
@@ -29,9 +35,15 @@ import org.apache.camel.impl.osgi.tracker.BundleTracker;
 import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer;
 import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.util.IOHelper;
-
-import org.osgi.framework.*;
-
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -96,7 +108,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
         }
     }
 
-    private String[] getAvailableScriptNames(){
+    private String[] getAvailableScriptNames() {
         List<String> names = new ArrayList<String>();
         for (List<BundleScriptEngineResolver> list : resolvers.values()) {
             for (BundleScriptEngineResolver r : list) {
@@ -107,25 +119,28 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
     }
 
     private void updateAvailableScriptLanguages() {
-        if (registration != null) {
-            registration.unregister();
-            registration = null;
-        }
         ServiceReference<LanguageResolver> ref = null;
         try {
             Collection<ServiceReference<LanguageResolver>> references = context.getServiceReferences(LanguageResolver.class, "(resolver=default)");
             if (references.size() == 1) {
+                // Unregistry the old language resolver first
+                if (registration != null) {
+                    registration.unregister();
+                    registration = null;
+                }
                 ref = references.iterator().next();
                 LanguageResolver resolver = context.getService(ref);
-
                 Dictionary props = new Hashtable();
+                // Just publish the language resolve with the language we found
                 props.put("language", getAvailableScriptNames());
                 registration = context.registerService(LanguageResolver.class, resolver, props);
             }
         } catch (InvalidSyntaxException e) {
             LOG.error("Invalid syntax for LanguageResolver service reference filter.");
         } finally {
-            context.ungetService(ref);
+            if (ref != null) {
+                context.ungetService(ref);
+            }
         }
     }
 
@@ -172,7 +187,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
 
     protected static class BundleScriptEngineResolver implements ScriptEngineResolver {
         protected final Bundle bundle;
-        private ServiceRegistration reg;
+        private ServiceRegistration<?> reg;
         private final URL configFile;
 
         public BundleScriptEngineResolver(Bundle bundle, URL configFile) {
@@ -192,7 +207,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
             return getScriptNames(getFactory());
         }
 
-        private List<String> getScriptNames(ScriptEngineFactory factory){
+        private List<String> getScriptNames(ScriptEngineFactory factory) {
             List<String> names = factory.getNames();
             return names;
         }
@@ -207,7 +222,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
                     throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
                 }
                 return (ScriptEngineFactory) cls.newInstance();
-            }catch (Exception e){
+            } catch (Exception e) {
                 //do something
                 return null;
             }
@@ -240,19 +255,6 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer, Serv
             }
         }
 
-        private boolean matchEngine(String name, String engineName) {
-            if (name.equals(engineName)) {
-                return true;
-            }
-
-            // javascript have many aliases
-            if (name.equals("js") || name.equals("javaScript") || name.equals("ECMAScript")) {
-                return engineName.equals("js") || engineName.equals("javaScript") || engineName.equals("ECMAScript");
-            }
-
-            return false;
-        }
-
         @Override
         public String toString() {
             return "OSGi script engine resolver for " + bundle.getSymbolicName();