camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [07/11] camel git commit: CAMEL-8546 Polish the code and fixed a NPE error when starting the camel-script bundle
Date Thu, 26 Mar 2015 08:44:51 GMT
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 <willem.jiang@gmail.com>
Authored: Thu Mar 26 14:40:19 2015 +0800
Committer: Willem Jiang <willem.jiang@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();


Mime
View raw message