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 2009/01/07 04:09:24 UTC

svn commit: r732207 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/util/ compone...

Author: ningjiang
Date: Tue Jan  6 19:09:24 2009
New Revision: 732207

URL: http://svn.apache.org/viewvc?rev=732207&view=rev
Log:
CAMEL-1223 FactoryFinder should support OSGI as well

Added:
    activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiFactoryFinder.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
    activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
    activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java
    activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=732207&r1=732206&r2=732207&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Tue Jan  6 19:09:24 2009
@@ -31,6 +31,7 @@
 import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.TypeConverterRegistry;
+import org.apache.camel.util.FactoryFinder;
 
 /**
  * Interface used to represent the context used to configure routes and the
@@ -302,4 +303,17 @@
      * @return the data formats available
      */
     Map<String, DataFormatType> getDataFormats();
+    
+    /**
+     * Create a FactoryFinder which will be used for the loading the factory class from META-INF
+     * @return the factory finder
+     */
+    FactoryFinder createFactoryFinder();
+    
+    /**
+     * Create a FactoryFinder which will be used for the loading the factory class from META-INF
+     * @param path the META-INF path
+     * @return the factory finder
+     */
+    FactoryFinder createFactoryFinder(String path);
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?rev=732207&r1=732206&r2=732207&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java Tue Jan  6 19:09:24 2009
@@ -477,7 +477,7 @@
     protected FileProcessStrategy createFileStrategy() {
         Class<?> factory = null;
         try {
-            FactoryFinder finder = new FactoryFinder("META-INF/services/org/apache/camel/component/");
+            FactoryFinder finder = getCamelContext().createFactoryFinder("META-INF/services/org/apache/camel/component/");
             factory = finder.findClass("file", "strategy.factory.");
         } catch (ClassNotFoundException e) {
             LOG.debug("'strategy.factory.class' not found", e);

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=732207&r1=732206&r2=732207&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Tue Jan  6 19:09:24 2009
@@ -17,6 +17,7 @@
 package org.apache.camel.impl;
 
 import java.io.IOException;
+import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -68,7 +69,6 @@
 import static org.apache.camel.util.ServiceHelper.startServices;
 import static org.apache.camel.util.ServiceHelper.stopServices;
 
-
 /**
  * Represents the context used to configure routes and the policies to use.
  *
@@ -99,6 +99,7 @@
     private Long delay;
     private ErrorHandlerBuilder errorHandlerBuilder;
     private Map<String, DataFormatType> dataFormats = new HashMap<String, DataFormatType>();
+    private Class<? extends FactoryFinder> factoryFinderClass = FactoryFinder.class;
 
     public DefaultCamelContext() {
         name = NAME_PREFIX + ++nameSuffix;
@@ -716,7 +717,7 @@
      * Lazily create a default implementation
      */
     protected Injector createInjector() {
-        FactoryFinder finder = new FactoryFinder();
+        FactoryFinder finder = createFactoryFinder();
         try {
             return (Injector) finder.newInstance("Injector");
         } catch (NoFactoryAvailableException e) {
@@ -794,4 +795,27 @@
     public Map<String, DataFormatType> getDataFormats() {
         return dataFormats;
     }
+    
+    public void setFactoryFinderClass(Class<? extends FactoryFinder> finderClass) {
+        factoryFinderClass = finderClass;
+    }
+
+    public FactoryFinder createFactoryFinder() {
+        try {
+            return factoryFinderClass.newInstance();
+        } catch (Exception e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
+
+    public FactoryFinder createFactoryFinder(String path) {
+        try {
+            Constructor<? extends FactoryFinder> constructor;
+            constructor = factoryFinderClass.getConstructor(String.class);
+            return constructor.newInstance(path);
+        } catch (Exception e) {
+            throw new RuntimeCamelException(e);
+        }
+        
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java?rev=732207&r1=732206&r2=732207&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FactoryFinder.java Tue Jan  6 19:09:24 2009
@@ -32,7 +32,7 @@
  */
 public class FactoryFinder {
     private final String path;
-    private final ConcurrentHashMap classMap = new ConcurrentHashMap();
+    protected final ConcurrentHashMap<String, Class> classMap = new ConcurrentHashMap<String, Class>();
 
     public FactoryFinder() {
         this("META-INF/services/org/apache/camel/");
@@ -66,7 +66,7 @@
 
     public Object newInstance(String key, Injector injector, String propertyPrefix) throws IOException,
         ClassNotFoundException {
-        Class type = findClass(key, propertyPrefix);
+        Class<?> type = findClass(key, propertyPrefix);
         return injector.newInstance(type);
     }
 
@@ -77,7 +77,7 @@
 
     public <T> T newInstance(String key, Injector injector, String propertyPrefix, Class<T> expectedType)
         throws IOException, ClassNotFoundException {
-        Class type = findClass(key, propertyPrefix);
+        Class<?> type = findClass(key, propertyPrefix);
         Object value = injector.newInstance(type);
         if (expectedType.isInstance(value)) {
             return expectedType.cast(value);

Modified: activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java?rev=732207&r1=732206&r2=732207&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java (original)
+++ activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java Tue Jan  6 19:09:24 2009
@@ -389,7 +389,7 @@
     protected RemoteFileProcessStrategy createRemoteFileStrategy() {
         Class<?> factory = null;
         try {
-            FactoryFinder finder = new FactoryFinder("META-INF/services/org/apache/camel/component/");
+            FactoryFinder finder = getCamelContext().createFactoryFinder("META-INF/services/org/apache/camel/component/");
             factory = finder.findClass("ftp", "strategy.factory.");
         } catch (ClassNotFoundException e) {
             LOG.debug("'strategy.factory.class' not found", e);

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=732207&r1=732206&r2=732207&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 Tue Jan  6 19:09:24 2009
@@ -27,6 +27,7 @@
 import org.apache.camel.impl.converter.DefaultTypeConverter;
 import org.apache.camel.impl.converter.TypeConverterLoader;
 import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.util.FactoryFinder;
 import org.osgi.framework.BundleContext;
 import org.springframework.osgi.context.BundleContextAware;
 
@@ -44,14 +45,14 @@
     public void setBundleContext(BundleContext bundleContext) {
         this.bundleContext = bundleContext;
     }
-
-
+    
     protected SpringCamelContext createContext() {
         SpringCamelContext context = super.createContext();
         if (bundleContext != null) {
             context.setComponentResolver(new OsgiComponentResolver());
             context.setLanguageResolver(new OsgiLanguageResolver());
-            addOsgiAnnotationTypeConverterLoader(context, bundleContext);            
+            addOsgiAnnotationTypeConverterLoader(context, bundleContext);
+            context.setFactoryFinderClass(OsgiFactoryFinder.class);
         }
         
         return context;
@@ -72,6 +73,6 @@
             typeConverterLoaders.remove(atLoader);
         }
         typeConverterLoaders.add(new OsgiAnnotationTypeConverterLoader(bundleContext));
-    }
-
+    }    
+    
 }

Added: activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiFactoryFinder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiFactoryFinder.java?rev=732207&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiFactoryFinder.java (added)
+++ activemq/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiFactoryFinder.java Tue Jan  6 19:09:24 2009
@@ -0,0 +1,99 @@
+/**
+ * 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 java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Properties;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+import org.apache.camel.util.FactoryFinder;
+import org.apache.camel.util.NoFactoryAvailableException;
+import org.apache.camel.util.ObjectHelper;
+
+public class OsgiFactoryFinder extends FactoryFinder {
+    
+    private class BundleEntry {
+        URL url;
+        Bundle bundle;
+    }
+    
+    public OsgiFactoryFinder() {
+        super();
+    }
+    
+    public OsgiFactoryFinder(String path) {
+        super(path);
+    }
+    
+    public Class findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException {
+        if (propertyPrefix == null) {
+            propertyPrefix = "";
+        }
+
+        Class clazz = (Class)classMap.get(propertyPrefix + key);
+        Properties properties = null;
+        if (clazz == null) {
+            BundleEntry entry = getResource(key);
+            if (entry != null) {
+                URL url = entry.url;
+                InputStream in = url.openStream();
+                // lets load the file
+                BufferedInputStream reader = null;
+                try {
+                    reader = new BufferedInputStream(in);
+                    properties = new Properties();
+                    properties.load(reader);
+                    String className = properties.getProperty(propertyPrefix + "class");
+                    if (className == null) {
+                        throw new IOException("Expected property is missing: " + propertyPrefix + "class");
+                    }
+                    clazz = entry.bundle.loadClass(className);
+                    classMap.put(propertyPrefix + key, clazz);
+                } finally {
+                    ObjectHelper.close(reader, key, null);
+                    ObjectHelper.close(in, key, null);
+                }
+            } else {
+                throw new NoFactoryAvailableException(propertyPrefix + key);
+            }           
+        }
+        return clazz;
+    }
+    
+       
+    public BundleEntry getResource(String path) {
+        URL url = null;
+        BundleEntry entry = null;
+        BundleContext bundleContext =  Activator.getBundle().getBundleContext();
+        for (Bundle bundle : bundleContext.getBundles()) {
+            url = bundle.getEntry(path);
+            if (url != null) {
+                entry = new BundleEntry();
+                entry.url = url;
+                entry.bundle = bundle;
+                break;
+            }
+        }
+        return entry;
+    }
+
+}

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

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

Modified: activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java?rev=732207&r1=732206&r2=732207&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java Tue Jan  6 19:09:24 2009
@@ -18,7 +18,6 @@
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.NoTypeConversionAvailableException;
-import org.apache.camel.Processor;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.component.event.EventComponent;
 import org.apache.camel.component.event.EventEndpoint;
@@ -213,5 +212,6 @@
 
     public boolean getShouldStartContext() {
         return shouldStartContext;
-    }
+    }    
+    
 }