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/02 15:12:00 UTC

svn commit: r730727 - in /activemq/camel/branches/camel-1.x: camel-core/src/main/java/org/apache/camel/impl/converter/ components/camel-osgi/ components/camel-osgi/src/main/java/org/apache/camel/osgi/

Author: ningjiang
Date: Fri Jan  2 06:11:59 2009
New Revision: 730727

URL: http://svn.apache.org/viewvc?rev=730727&view=rev
Log:
CAMEL-1043 Using OSGI bundle activator to search the component and type converters

Added:
    activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java   (with props)
    activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java   (with props)
    activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java   (with props)
Modified:
    activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
    activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
    activemq/camel/branches/camel-1.x/components/camel-osgi/pom.xml
    activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java
    activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiComponentResolver.java

Modified: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java?rev=730727&r1=730726&r2=730727&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java (original)
+++ activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java Fri Jan  2 06:11:59 2009
@@ -50,16 +50,22 @@
 public class AnnotationTypeConverterLoader implements TypeConverterLoader {
     public static final String META_INF_SERVICES = "META-INF/services/org/apache/camel/TypeConverter";
     private static final transient Log LOG = LogFactory.getLog(AnnotationTypeConverterLoader.class);
-    private ResolverUtil resolver = new ResolverUtil();
+    private ResolverUtil resolver;
     private Set<Class> visitedClasses = new HashSet<Class>();
-
+    
     public AnnotationTypeConverterLoader() {
         // use WebSphere specific resolver if running on WebSphere
         if (WebSphereResolverUtil.isWebSphereClassLoader(this.getClass().getClassLoader())) {
             LOG.info("Using WebSphere specific ResolverUtil");
             resolver = new WebSphereResolverUtil(META_INF_SERVICES);
+        } else {
+            resolver = new ResolverUtil();
         }
     }
+    
+    public AnnotationTypeConverterLoader(ResolverUtil resolverUtil) {
+        this.resolver = resolverUtil;
+    }
 
     public void load(TypeConverterRegistry registry) throws Exception {
         String[] packageNames = findPackageNames();

Modified: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?rev=730727&r1=730726&r2=730727&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java (original)
+++ activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java Fri Jan  2 06:11:59 2009
@@ -60,7 +60,11 @@
         addFallbackConverter(new ArrayTypeConverter());
         addFallbackConverter(new EnumTypeConverter());
     }
-
+    
+    public List<TypeConverterLoader> getTypeConverterLoaders() {
+        return typeConverterLoaders;
+    }    
+    
     public <T> T convertTo(Class<T> type, Object value) {
         return convertTo(type, null, value);
     }

Modified: activemq/camel/branches/camel-1.x/components/camel-osgi/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-osgi/pom.xml?rev=730727&r1=730726&r2=730727&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-osgi/pom.xml (original)
+++ activemq/camel/branches/camel-1.x/components/camel-osgi/pom.xml Fri Jan  2 06:11:59 2009
@@ -127,6 +127,21 @@
       </resource>
     </resources>
     <plugins>
+	<plugin>
+		<groupId>org.apache.felix</groupId>
+		<artifactId>maven-bundle-plugin</artifactId>
+		<configuration>
+			<instructions>
+				<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>				
+				<Export-Package>${camel.osgi.export.pkg}</Export-Package>
+				<Import-Package>*</Import-Package>				
+				<Bundle-Activator>org.apache.camel.osgi.Activator</Bundle-Activator>
+				<Implementation-Title>Apache Camel</Implementation-Title>
+				<Implementation-Version>${project.version}</Implementation-Version>
+			</instructions>
+			<unpackBundle>true</unpackBundle>
+		</configuration>
+	</plugin>
 
       <plugin>
         <groupId>org.codehaus.mojo</groupId>

Added: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java?rev=730727&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java (added)
+++ activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java Fri Jan  2 06:11:59 2009
@@ -0,0 +1,263 @@
+/**
+ * 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.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.SynchronousBundleListener;
+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_COMPONENT = "/META-INF/services/org/apache/camel/component/";
+    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 Bundle bundle;
+    
+    private class ComponentEntry {
+        Bundle bundle;
+        String path;
+        String name;
+        Class type;
+    }
+    
+    private class TypeConverterEntry {
+        Bundle bundle;
+        URL resource;
+        Set<String> converterPackages;
+    }
+    
+    public void bundleChanged(BundleEvent event) {
+        try {
+            Bundle bundle = event.getBundle();
+            if (event.getType() == BundleEvent.RESOLVED) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Bundle resolved: " + bundle.getSymbolicName());
+                }
+                mayBeAddComponentFor(bundle);
+                mayBeAddTypeConverterFor(bundle);
+            } else if (event.getType() == BundleEvent.UNRESOLVED) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Bundle unresolved: " + bundle.getSymbolicName());
+                }
+                mayBeRemoveComponentFor(bundle);
+                mayBeRemoveTypeConverterFor(bundle);
+            }
+        } catch (Throwable e) {
+            LOG.fatal("Exception handing bundle changed event", e);
+        }
+        
+    }
+    
+    protected synchronized void mayBeAddComponentFor(Bundle bundle) {
+        Enumeration e = bundle.getEntryPaths(META_INF_COMPONENT);
+        if (e != null) {
+            while (e.hasMoreElements()) {
+                String path = (String)e.nextElement();
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Found entry: " + path + " in bundle " + bundle.getSymbolicName());
+                }
+                ComponentEntry entry = new ComponentEntry();
+                entry.bundle = bundle;
+                entry.path = path;
+                entry.name = path.substring(path.lastIndexOf("/") + 1);
+                COMPONENTS.put(entry.name, entry);
+            }
+        }
+    }
+    
+    protected synchronized void mayBeAddTypeConverterFor(Bundle bundle) {
+        try {
+            Enumeration e = bundle.getResources(META_INF_TYPE_CONVERTER);
+            if (e != null) {
+                while (e.hasMoreElements()) {
+                    URL resource = (URL)e.nextElement();
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Found entry: " + resource + " in bundle " + bundle.getSymbolicName());
+                    }
+                    TypeConverterEntry entry = new TypeConverterEntry();
+                    entry.bundle = bundle;                   
+                    entry.resource = resource;
+                    entry.converterPackages = getConverterPackages(resource);
+                    TYPE_CONVERTERS.put(bundle, entry);
+                }
+            }
+        } catch (IOException ignore) {
+            // can't find the resource
+        }
+    }
+
+    protected synchronized void mayBeRemoveComponentFor(Bundle bundle) {
+        ComponentEntry[] entriesArray = COMPONENTS.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);
+            }
+        }
+    }
+    
+    protected synchronized void mayBeRemoveTypeConverterFor(Bundle bundle) {
+        TypeConverterEntry[] entriesArray = TYPE_CONVERTERS.values().toArray(new TypeConverterEntry[0]);
+        for (TypeConverterEntry entry : entriesArray) {
+            if (entry.bundle == bundle) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Removing entry: " + entry.resource + " in bundle " + bundle.getSymbolicName());
+                }
+                COMPONENTS.remove(bundle);
+            }
+        }
+    }
+
+    public void start(BundleContext context) throws Exception {
+        bundle = context.getBundle();       
+        context.addBundleListener(this);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("checking existing bundles");
+        }
+        for (Bundle bundle : context.getBundles()) {
+            if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING
+                || bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STOPPING) {
+                mayBeAddComponentFor(bundle);
+                mayBeAddTypeConverterFor(bundle);
+            }
+        }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("actived");
+        }
+        
+    }    
+
+    public void stop(BundleContext context) throws Exception {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("removing the components of existing bundles");
+        }
+        for (Bundle bundle : context.getBundles()) {
+            if (bundle.getState() == Bundle.RESOLVED || bundle.getState() == Bundle.STARTING 
+                || bundle.getState() == Bundle.ACTIVE || bundle.getState() == Bundle.STOPPING) {
+                mayBeRemoveComponentFor(bundle);
+                mayBeRemoveTypeConverterFor(bundle);
+            }
+        }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("stopped");
+        }
+    }
+    
+    protected Set<String> getConverterPackages(URL resource) {
+        Set<String> packages = new HashSet<String>();
+        if (resource != null) {
+            BufferedReader reader = null;
+            try {
+                reader = new BufferedReader(new InputStreamReader(resource.openStream()));
+                while (true) {
+                    String line = reader.readLine();
+                    if (line == null) {
+                        break;
+                    }
+                    line = line.trim();
+                    if (line.startsWith("#") || line.length() == 0) {
+                        continue;
+                    }
+                    tokenize(packages, line);
+                }
+            } catch (Exception ignore) {
+                // Do nothing here
+            } finally {
+                if (reader != null) {
+                    ObjectHelper.close(reader, null, LOG);
+                }
+            }
+        }
+        return packages;
+    }
+    
+    protected void tokenize(Set<String> packages, String line) {
+        StringTokenizer iter = new StringTokenizer(line, ",");
+        while (iter.hasMoreTokens()) {
+            String name = iter.nextToken().trim();
+            if (name.length() > 0) {
+                packages.add(name);
+            }
+        }
+    }
+    
+    protected static Bundle getBundle() {
+        return bundle;
+    }
+    
+    protected static synchronized String[] findTypeConverterPackageNames() {
+        Set<String> packages = new HashSet<String>();
+        for (TypeConverterEntry entry : TYPE_CONVERTERS.values()) {
+            for (String packageName : entry.converterPackages) {
+                packages.add(packageName);
+            }
+        }
+        return packages.toArray(new String[packages.size()]);
+    }
+        
+    protected static synchronized Class getComponent(String name) throws Exception {
+        ComponentEntry entry = COMPONENTS.get(name);
+        if (entry == null) {
+            return null;
+        }
+        if (entry.type == null) {
+            URL url = entry.bundle.getEntry(entry.path);
+            // lets load the file
+            Properties properties = new Properties();
+            BufferedInputStream reader = null;
+            try {
+                reader = new BufferedInputStream(url.openStream());
+                properties.load(reader);
+            } finally {
+                try {
+                    reader.close();
+                } catch (Exception ignore) {
+                }
+            }
+            String classname = (String)properties.get("class");
+            ClassLoader loader = BundleDelegatingClassLoader.createBundleClassLoaderFor(entry.bundle);
+            entry.type = loader.loadClass(classname);
+        }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Found component: " + name + " via type: " + entry.type.getName());
+        }
+        return entry.type;
+    }
+
+}

Propchange: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/Activator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java?rev=730727&r1=730726&r2=730727&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java Fri Jan  2 06:11:59 2009
@@ -16,11 +16,16 @@
  */
 package org.apache.camel.osgi;
 
+import java.util.List;
+
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
+import org.apache.camel.impl.converter.AnnotationTypeConverterLoader;
+import org.apache.camel.impl.converter.DefaultTypeConverter;
+import org.apache.camel.impl.converter.TypeConverterLoader;
 import org.apache.camel.spring.SpringCamelContext;
 import org.osgi.framework.BundleContext;
 import org.springframework.osgi.context.BundleContextAware;
@@ -44,9 +49,28 @@
     protected SpringCamelContext createContext() {
         SpringCamelContext context = super.createContext();
         if (bundleContext != null) {
-            context.setComponentResolver(new OsgiComponentResolver(bundleContext));
+            context.setComponentResolver(new OsgiComponentResolver());
+            addOsgiAnnotationTypeConverterLoader(context, bundleContext);
         }
+        
         return context;
     }
+    
+    protected void addOsgiAnnotationTypeConverterLoader(SpringCamelContext context, BundleContext bundleContext) {
+        DefaultTypeConverter typeConverter = (DefaultTypeConverter) context.getTypeConverter();
+        List<TypeConverterLoader> typeConverterLoaders = typeConverter.getTypeConverterLoaders();
+        // Remove the AnnotationTypeConverterLoader
+        TypeConverterLoader atLoader = null; 
+        for (TypeConverterLoader loader : typeConverterLoaders) {
+            if (loader instanceof AnnotationTypeConverterLoader) {
+                atLoader = loader;
+                break;
+            }
+        }
+        if (atLoader != null) {
+            typeConverterLoaders.remove(atLoader);
+        }
+        typeConverterLoaders.add(new OsgiAnnotationTypeConverterLoader(bundleContext));
+    }
 
 }

Added: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java?rev=730727&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java (added)
+++ activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java Fri Jan  2 06:11:59 2009
@@ -0,0 +1,37 @@
+/**
+ * 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.impl.converter.AnnotationTypeConverterLoader;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.framework.BundleContext;
+
+public class OsgiAnnotationTypeConverterLoader extends AnnotationTypeConverterLoader {
+    private static final transient Log LOG = LogFactory.getLog(OsgiAnnotationTypeConverterLoader.class);
+    
+    public OsgiAnnotationTypeConverterLoader(BundleContext context) {
+        super(new OsgiResolverUtil(context));
+    }
+    
+    protected String[] findPackageNames() {
+        return Activator.findTypeConverterPackageNames();
+    }
+    
+    
+
+}

Propchange: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiAnnotationTypeConverterLoader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiComponentResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiComponentResolver.java?rev=730727&r1=730726&r2=730727&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiComponentResolver.java (original)
+++ activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiComponentResolver.java Fri Jan  2 06:11:59 2009
@@ -35,121 +35,11 @@
 import org.springframework.osgi.util.BundleDelegatingClassLoader;
 
 public class OsgiComponentResolver implements ComponentResolver {
-
+    
     private static final transient Log LOG = LogFactory.getLog(OsgiComponentResolver.class);
 
-    private BundleContext bundleContext;
-    private Map<String, ComponentEntry> components;
-
-    private class BundleListener implements SynchronousBundleListener {
-        public void bundleChanged(BundleEvent event) {
-            try {
-                Bundle bundle = event.getBundle();
-                if (event.getType() == BundleEvent.RESOLVED) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Bundle resolved: " + bundle.getSymbolicName());
-                    }
-                    mayBeAddComponentFor(bundle);
-                } else if (event.getType() == BundleEvent.UNRESOLVED) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Bundle unresolved: " + bundle.getSymbolicName());
-                    }
-                    mayBeRemoveComponentFor(bundle);
-                }
-            } catch (Throwable e) {
-                LOG.fatal("Exception handing bundle changed event", e);
-            }
-        }
-    }
-
-    private class ComponentEntry {
-        Bundle bundle;
-        String path;
-        String name;
-        Class type;
-    }
-
-    public OsgiComponentResolver(BundleContext bundleContext) {
-        this.bundleContext = bundleContext;
-    }
-
-    protected void init() {
-        if (components != null) {
-            return;
-        }
-        LOG.debug("Initializing OsgiComponentResolver");
-        components = new HashMap<String, ComponentEntry>();
-        bundleContext.addBundleListener(new BundleListener());
-        Bundle[] previousBundles = bundleContext.getBundles();
-        for (int i = 0; i < previousBundles.length; i++) {
-            int state = previousBundles[i].getState();
-            if (state == Bundle.RESOLVED || state == Bundle.ACTIVE) {
-                try {
-                    mayBeAddComponentFor(previousBundles[i]);
-                } catch (Exception e) {
-                    LOG.error("Component " + previousBundles[i] + " not added due to " + e.toString(), e);
-                }
-            }
-        }
-    }
-
-    protected synchronized void mayBeAddComponentFor(Bundle bundle) {
-        Enumeration e = bundle.getEntryPaths("/META-INF/services/org/apache/camel/component/");
-        if (e != null) {
-            while (e.hasMoreElements()) {
-                String path = (String)e.nextElement();
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Found entry: " + path + " in bundle " + bundle.getSymbolicName());
-                }
-                ComponentEntry entry = new ComponentEntry();
-                entry.bundle = bundle;
-                entry.path = path;
-                entry.name = path.substring(path.lastIndexOf("/") + 1);
-                components.put(entry.name, entry);
-            }
-        }
-    }
-
-    protected synchronized void mayBeRemoveComponentFor(Bundle bundle) {
-        // To avoid the CurrentModificationException, do not use components.values directly 
-        ComponentEntry[] entriesArray = components.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);                
-            }
-        }
-    }
-
     protected synchronized Class getComponent(String name) throws Exception {
-        ComponentEntry entry = components.get(name);
-        if (entry == null) {
-            return null;
-        }
-        if (entry.type == null) {
-            URL url = entry.bundle.getEntry(entry.path);
-            // lets load the file
-            Properties properties = new Properties();
-            BufferedInputStream reader = null;
-            try {
-                reader = new BufferedInputStream(url.openStream());
-                properties.load(reader);
-            } finally {
-                try {
-                    reader.close();
-                } catch (Exception ignore) {
-                }
-            }
-            String classname = (String)properties.get("class");
-            ClassLoader loader = BundleDelegatingClassLoader.createBundleClassLoaderFor(entry.bundle);
-            entry.type = loader.loadClass(classname);
-        }
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Found component: " + name + " via type: " + entry.type.getName());
-        }
-        return entry.type;
+        return Activator.getComponent(name);       
     }
 
     public Component resolveComponent(String name, CamelContext context) throws Exception {
@@ -168,8 +58,7 @@
             }
             // we do not throw the exception here and try to auto create a component
         }
-        // Check in OSGi bundles
-        init();
+        // Check in OSGi bundles        
         Class type = null;
         try {
             type = getComponent(name);

Added: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java?rev=730727&view=auto
==============================================================================
--- activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java (added)
+++ activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java Fri Jan  2 06:11:59 2009
@@ -0,0 +1,47 @@
+/**
+ * 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.util.Set;
+
+import org.apache.camel.util.ResolverUtil;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.springframework.osgi.util.BundleDelegatingClassLoader;
+
+public class OsgiResolverUtil extends ResolverUtil {
+    private Bundle bundle;
+    
+    public OsgiResolverUtil(BundleContext context) {
+        bundle = context.getBundle();
+    }
+    
+    /**
+     * Returns the classloaders that will be used for scanning for classes. 
+     * Here we just add BundleDelegatingClassLoader here
+     *
+     * @return the ClassLoader instances that will be used to scan for classes
+     */
+    public Set<ClassLoader> getClassLoaders() {
+        Set<ClassLoader> classLoaders = super.getClassLoaders();
+        // Using the Activator's bundle to make up a class loader
+        ClassLoader osgiLoader = BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle);
+        classLoaders.add(osgiLoader);
+        return classLoaders;
+    }
+
+}

Propchange: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/branches/camel-1.x/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiResolverUtil.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date