Author: gnodet
Date: Wed Sep 17 00:26:57 2008
New Revision: 696184

URL: http://svn.apache.org/viewvc?rev=696184&view=rev
Log:
SMX4-95: camel example doesn't work

Added:
    
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/osgi/
    
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/osgi/CamelComponentFactoryBean.java
Modified:
    servicemix/components/engines/servicemix-camel/trunk/pom.xml
    
servicemix/components/engines/servicemix-camel/trunk/src/main/resources/META-INF/spring/servicemix-camel.xml

Modified: servicemix/components/engines/servicemix-camel/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/pom.xml?rev=696184&r1=696183&r2=696184&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-camel/trunk/pom.xml (original)
+++ servicemix/components/engines/servicemix-camel/trunk/pom.xml Wed Sep 17 
00:26:57 2008
@@ -163,6 +163,20 @@
       <artifactId>xercesImpl</artifactId>
       <scope>provided</scope>
     </dependency>
+      <dependency>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>org.osgi.core</artifactId>
+        <version>1.0.0</version>
+        <scope>provided</scope>
+        <optional>true</optional>
+      </dependency>
+      <dependency>
+        <groupId>org.springframework.osgi</groupId>
+        <artifactId>spring-osgi-core</artifactId>
+        <version>1.1.0</version>
+        <scope>provided</scope>
+        <optional>true</optional>
+      </dependency>
   </dependencies>
   
   <build>

Added: 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/osgi/CamelComponentFactoryBean.java
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/osgi/CamelComponentFactoryBean.java?rev=696184&view=auto
==============================================================================
--- 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/osgi/CamelComponentFactoryBean.java
 (added)
+++ 
servicemix/components/engines/servicemix-camel/trunk/src/main/java/org/apache/servicemix/camel/osgi/CamelComponentFactoryBean.java
 Wed Sep 17 00:26:57 2008
@@ -0,0 +1,128 @@
+/*
+ * 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.servicemix.camel.osgi;
+
+import java.util.List;
+import java.util.Collections;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.osgi.util.BundleDelegatingClassLoader;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Bundle;
+import org.apache.servicemix.camel.CamelJbiComponent;
+import org.apache.servicemix.camel.CamelSpringDeployer;
+import org.apache.servicemix.common.Deployer;
+import org.apache.servicemix.common.BaseServiceUnitManager;
+import org.apache.servicemix.common.ServiceMixComponent;
+import org.apache.servicemix.common.xbean.ClassLoaderXmlPreprocessor;
+import org.apache.xbean.spring.context.SpringApplicationContext;
+import org.apache.xbean.classloader.JarFileClassLoader;
+
+/**
+ * A bean factory for the camel component in OSGi.
+ *
+ * When deploying a JBI packaged SU to camel component, camel-spring can not 
be found
+ * by Spring/XBean, thus leading to an exception about the spring namespace 
not being
+ * found.  We need to hack the clasloader for SUs to force a reference to 
camel-spring
+ * in the SU classloader parents.
+ *
+ * This does not completely solve the problem, as converters can not be found 
because
+ * camel-core ResolverUtil does not work well
+ */
+public class CamelComponentFactoryBean implements FactoryBean, 
BundleContextAware, InitializingBean {
+
+    private BundleContext bundleContext;
+    private CamelJbiComponent component;
+
+    public Object getObject() throws Exception {
+        return component;
+    }
+
+    public Class getObjectType() {
+        return CamelJbiComponent.class;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+        try {
+            Enumeration e = 
this.bundleContext.getBundle().getResources("META-INF/spring.handlers");
+            while (e.hasMoreElements()) {
+                System.err.println(e.nextElement());
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        component = new OsgiCamelJbiComponent();
+    }
+
+    public class OsgiCamelJbiComponent extends CamelJbiComponent {
+        public BaseServiceUnitManager createServiceUnitManager() {
+            CamelSpringDeployer deployer = new OsgiCamelSpringDeployer(this);
+            return new BaseServiceUnitManager(this, new Deployer[] {deployer});
+        }
+    }
+
+    public class OsgiCamelSpringDeployer extends CamelSpringDeployer {
+        public OsgiCamelSpringDeployer(CamelJbiComponent component) {
+            super(component);
+        }
+        protected List getXmlPreProcessors(String serviceUnitRootPath) {
+            ClassLoaderXmlPreprocessor classLoaderXmlPreprocessor =
+                    new OsgiClassLoaderXmlPreprocessor(new 
File(serviceUnitRootPath),
+                                                       component);
+            return Collections.singletonList(classLoaderXmlPreprocessor);
+        }
+    }
+
+    public class OsgiClassLoaderXmlPreprocessor extends 
ClassLoaderXmlPreprocessor {
+        public OsgiClassLoaderXmlPreprocessor(File root, ServiceMixComponent 
component) {
+            super(root, component);
+        }
+
+        protected ClassLoader getParentClassLoader(SpringApplicationContext 
applicationContext) {
+            List<ClassLoader> parents = new ArrayList<ClassLoader>();
+            parents.add(super.getParentClassLoader(applicationContext));
+            for (Bundle bundle : bundleContext.getBundles()) {
+                try {
+                    if (bundle.getSymbolicName().contains("camel-spring")) {
+                        
parents.add(BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle));
+                    }
+                } catch (Throwable e) {
+                    // Do nothing
+                }
+            }
+            return new JarFileClassLoader("SU parent class loader",
+                                          new URL[0],
+                                          parents.toArray(new 
ClassLoader[parents.size()]));
+        }
+    }
+
+}

Modified: 
servicemix/components/engines/servicemix-camel/trunk/src/main/resources/META-INF/spring/servicemix-camel.xml
URL: 
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-camel/trunk/src/main/resources/META-INF/spring/servicemix-camel.xml?rev=696184&r1=696183&r2=696184&view=diff
==============================================================================
--- 
servicemix/components/engines/servicemix-camel/trunk/src/main/resources/META-INF/spring/servicemix-camel.xml
 (original)
+++ 
servicemix/components/engines/servicemix-camel/trunk/src/main/resources/META-INF/spring/servicemix-camel.xml
 Wed Sep 17 00:26:57 2008
@@ -29,7 +29,7 @@
   http://www.springframework.org/schema/osgi
   http://www.springframework.org/schema/osgi/spring-osgi.xsd";>
 
-    <bean id="servicemix-camel" 
class="org.apache.servicemix.camel.CamelJbiComponent">
+    <bean id="servicemix-camel" 
class="org.apache.servicemix.camel.osgi.CamelComponentFactoryBean">
     </bean>
 
     <osgi:service ref="servicemix-camel">


Reply via email to