camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r731169 - in /activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi: Activator.java CamelContextFactoryBean.java OsgiLanguageResolver.java
Date Sun, 04 Jan 2009 04:29:58 GMT
Author: ningjiang
Date: Sat Jan  3 20:29:58 2009
New Revision: 731169

URL: http://svn.apache.org/viewvc?rev=731169&view=rev
Log:
CAMEL-1221 Added OSGI language resolver as Michael suggested

Added:
    activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
  (with props)
Modified:
    activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
    activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java

Modified: activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java?rev=731169&r1=731168&r2=731169&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
(original)
+++ activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
Sat Jan  3 20:29:58 2009
@@ -40,11 +40,13 @@
 import org.springframework.osgi.util.BundleDelegatingClassLoader;
 
 public class Activator implements BundleActivator, SynchronousBundleListener {
-    public static final String META_INF_TYPE_CONVERTER = "META-INF/services/org/apache/camel/TypeConverter";
+    public static final String META_INF_TYPE_CONVERTER = "/META-INF/services/org/apache/camel/TypeConverter";
     public static final String META_INF_COMPONENT = "/META-INF/services/org/apache/camel/component/";
+    public static final String META_INF_LANGUAGE = "/META-INF/services/org/apache/camel/language/";
     private static final transient Log LOG = LogFactory.getLog(Activator.class);    
     private static final Map<String, ComponentEntry> COMPONENTS = new HashMap<String,
ComponentEntry>();
     private static final Map<Bundle, TypeConverterEntry> TYPE_CONVERTERS = new HashMap<Bundle,
TypeConverterEntry>();
+    private static final Map<String, ComponentEntry> LANGUAGES = new HashMap<String,
ComponentEntry>();
     private static Bundle bundle;
     
     private class ComponentEntry {
@@ -67,13 +69,13 @@
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Bundle resolved: " + bundle.getSymbolicName());
                 }
-                mayBeAddComponentFor(bundle);
+                mayBeAddComponentAndLanguageFor(bundle);                
                 mayBeAddTypeConverterFor(bundle);
             } else if (event.getType() == BundleEvent.UNRESOLVED) {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Bundle unresolved: " + bundle.getSymbolicName());
                 }
-                mayBeRemoveComponentFor(bundle);
+                mayBeRemoveComponentAndLanguageFor(bundle);                
                 mayBeRemoveTypeConverterFor(bundle);
             }
         } catch (Throwable e) {
@@ -81,9 +83,8 @@
         }
         
     }
-    
-    protected synchronized void mayBeAddComponentFor(Bundle bundle) {
-        Enumeration e = bundle.getEntryPaths(META_INF_COMPONENT);
+
+    protected void addComponentEntry(Enumeration e, Map<String, ComponentEntry> entries)
{
         if (e != null) {
             while (e.hasMoreElements()) {
                 String path = (String)e.nextElement();
@@ -94,9 +95,15 @@
                 entry.bundle = bundle;
                 entry.path = path;
                 entry.name = path.substring(path.lastIndexOf("/") + 1);
-                COMPONENTS.put(entry.name, entry);
+                entries.put(entry.name, entry);
             }
         }
+        
+    }
+
+    protected synchronized void mayBeAddComponentAndLanguageFor(Bundle bundle) {        
+        addComponentEntry(bundle.getEntryPaths(META_INF_COMPONENT), COMPONENTS);
+        addComponentEntry(bundle.getEntryPaths(META_INF_LANGUAGE), LANGUAGES);
     }
     
     protected synchronized void mayBeAddTypeConverterFor(Bundle bundle) {
@@ -120,16 +127,21 @@
         }
     }
 
-    protected synchronized void mayBeRemoveComponentFor(Bundle bundle) {
-        ComponentEntry[] entriesArray = COMPONENTS.values().toArray(new ComponentEntry[0]);
+    protected synchronized void mayBeRemoveComponentAndLanguageFor(Bundle bundle) {
+        removeComponentEntry(bundle, COMPONENTS);
+        removeComponentEntry(bundle, LANGUAGES);        
+    }
+    
+    protected void removeComponentEntry(Bundle bundle, Map<String, ComponentEntry>
entries) {
+        ComponentEntry[] entriesArray = entries.values().toArray(new ComponentEntry[0]);
         for (ComponentEntry entry : entriesArray) {        
             if (entry.bundle == bundle) {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Removing entry: " + entry.path + " in bundle " + bundle.getSymbolicName());
                 }
-                COMPONENTS.remove(entry.name);
+                entries.remove(entry.name);
             }
-        }
+        }        
     }
     
     protected synchronized void mayBeRemoveTypeConverterFor(Bundle bundle) {
@@ -153,7 +165,7 @@
         for (Bundle bundle : context.getBundles()) {
             if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING
                 || bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STOPPING)
{
-                mayBeAddComponentFor(bundle);
+                mayBeAddComponentAndLanguageFor(bundle);
                 mayBeAddTypeConverterFor(bundle);
             }
         }
@@ -170,7 +182,7 @@
         for (Bundle bundle : context.getBundles()) {
             if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING

                 || bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STOPPING)
{
-                mayBeRemoveComponentFor(bundle);
+                mayBeRemoveComponentAndLanguageFor(bundle);
                 mayBeRemoveTypeConverterFor(bundle);
             }
         }
@@ -231,8 +243,16 @@
         return packages.toArray(new String[packages.size()]);
     }
         
-    protected static synchronized Class getComponent(String name) throws Exception {
-        ComponentEntry entry = COMPONENTS.get(name);
+    public static synchronized Class getComponent(String name) throws Exception {
+        return getClassFromEntries(name, COMPONENTS);
+    }
+    
+    public static synchronized Class getLanguage(String name) throws Exception {
+        return getClassFromEntries(name, LANGUAGES);
+    }
+    
+    protected static Class getClassFromEntries(String name, Map<String, ComponentEntry>
entries) throws Exception {
+        ComponentEntry entry = entries.get(name);
         if (entry == null) {
             return null;
         }
@@ -246,7 +266,9 @@
                 properties.load(reader);
             } finally {
                 try {
-                    reader.close();
+                    if (reader != null) {
+                        reader.close();
+                    }
                 } catch (Exception ignore) {
                 }
             }
@@ -255,9 +277,10 @@
             entry.type = loader.loadClass(classname);
         }
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Found component: " + name + " via type: " + entry.type.getName());
+            LOG.debug("Found entry: " + name + " via type: " + entry.type.getName());
         }
         return entry.type;
     }
+    
 
 }

Modified: activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java?rev=731169&r1=731168&r2=731169&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java
(original)
+++ activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java
Sat Jan  3 20:29:58 2009
@@ -50,7 +50,8 @@
         SpringCamelContext context = super.createContext();
         if (bundleContext != null) {
             context.setComponentResolver(new OsgiComponentResolver());
-            addOsgiAnnotationTypeConverterLoader(context, bundleContext);
+            context.setLanguageResolver(new OsgiLanguageResolver());
+            addOsgiAnnotationTypeConverterLoader(context, bundleContext);            
         }
         
         return context;

Added: activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java?rev=731169&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
(added)
+++ activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
Sat Jan  3 20:29:58 2009
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.osgi;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.spi.Language;
+import org.apache.camel.spi.LanguageResolver;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class OsgiLanguageResolver implements LanguageResolver {
+    private static final transient Log LOG = LogFactory.getLog(OsgiLanguageResolver.class);
+
+    public Language resolveLanguage(String name, CamelContext context) {
+        Object bean = null;
+        try {
+            bean = context.getRegistry().lookup(name);
+            if (bean != null && LOG.isDebugEnabled()) {
+                LOG.debug("Found language: " + name + " in registry: " + bean);
+            }
+        } catch (Exception e) {
+            LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
+        }
+        if (bean != null) {
+            if (bean instanceof Language) {
+                return (Language)bean;
+            }
+            // we do not throw the exception here and try to auto create a Language from
OSGI bundle
+        }
+        // Check in OSGi bundles        
+        Class type = null;
+        try {
+            type = getLanaguage(name);
+        } catch (Throwable e) {
+            throw new IllegalArgumentException("Invalid URI, no Language registered for scheme
: " + name, e);
+        }
+        if (type == null) {
+            return null;
+        }
+        if (Language.class.isAssignableFrom(type)) {
+            return (Language)context.getInjector().newInstance(type);
+        } else {
+            throw new IllegalArgumentException("Type is not a Lanaguage implementation. Found:
"
+                                               + type.getName());
+        }
+    }
+
+    protected Class getLanaguage(String name) throws Exception {
+        return Activator.getLanguage(name);
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiLanguageResolver.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message