Author: ningjiang
Date: Thu Oct 27 04:01:23 2011
New Revision: 1189600

URL: http://svn.apache.org/viewvc?rev=1189600&view=rev
Log:
CAMEL-4587 Camel Velocity component should not require you to import 
CamelVelocityClasspathResourceLoader

Added:
    
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java
   (with props)
    
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java
   (with props)
    
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
   (with props)
Modified:
    
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java

Added: 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java?rev=1189600&view=auto
==============================================================================
--- 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java
 (added)
+++ 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java
 Thu Oct 27 04:01:23 2011
@@ -0,0 +1,40 @@
+/**
+ * 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.component.velocity;
+
+
+/**
+ * This class loader is used to help us load the 
CamelVelocityClasspathResourceLoader in OSGi
+ */
+public class CamelVelocityDelegateClassLoader extends ClassLoader {
+    private static final String CAMLE_VELOCITY_CLASSPATH_RESOURCE_LOADER 
+        = CamelVelocityClasspathResourceLoader.class.getName();
+      
+    CamelVelocityDelegateClassLoader(ClassLoader parent) {
+        super(parent);
+    }
+   
+    protected Class< ? > findClass(String name) throws ClassNotFoundException {
+        if (CAMLE_VELOCITY_CLASSPATH_RESOURCE_LOADER.equals(name)) {
+            return CamelVelocityClasspathResourceLoader.class;
+        } 
+        return super.findClass(name);
+        
+    }
+    
+
+}

Propchange: 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/CamelVelocityDelegateClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java?rev=1189600&r1=1189599&r2=1189600&view=diff
==============================================================================
--- 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
 (original)
+++ 
camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
 Thu Oct 27 04:01:23 2011
@@ -98,7 +98,15 @@ public class VelocityEndpoint extends Re
             
properties.setProperty(CommonsLogLogChute.LOGCHUTE_COMMONS_LOG_NAME, 
VelocityEndpoint.class.getName());
 
             log.debug("Initializing VelocityEngine with properties {}", 
properties);
-            velocityEngine.init(properties);
+            // help the velocityEngine to load the 
CamelVelocityClasspathResourceLoader 
+            ClassLoader old = Thread.currentThread().getContextClassLoader();
+            try {
+                ClassLoader delegate = new 
CamelVelocityDelegateClassLoader(old);
+                Thread.currentThread().setContextClassLoader(delegate);
+                velocityEngine.init(properties);
+            } finally {
+                Thread.currentThread().setContextClassLoader(old);
+            }
         }
         return velocityEngine;
     }

Added: 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java?rev=1189600&view=auto
==============================================================================
--- 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java
 (added)
+++ 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java
 Thu Oct 27 04:01:23 2011
@@ -0,0 +1,86 @@
+/**
+ * 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.itest.osgi.velocity;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.itest.osgi.blueprint.OSGiBlueprintTestSupport;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.Constants;
+
+import static org.ops4j.pax.exam.OptionUtils.combine;
+import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.scanFeatures;
+import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.newBundle;
+import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.withBnd;
+
+@RunWith(JUnit4TestRunner.class)
+public class VelocityBlueprintTest extends OSGiBlueprintTestSupport {
+    private CamelContext camelContext;
+    private ProducerTemplate mytemplate;
+    
+    @Test
+    public void testReceivesResponse() throws Exception {        
+        assertRespondsWith("foo", "<header>foo</header><hello>foo</hello>");
+        assertRespondsWith("bar", "<header>bar</header><hello>bar</hello>");
+    }
+
+    protected void assertRespondsWith(final String value, String expectedBody) 
throws InvalidPayloadException {
+        Exchange response = mytemplate.request("direct:start", new Processor() 
{
+            public void process(Exchange exchange) throws Exception {
+                Message in = exchange.getIn();
+                in.setBody("answer");
+                in.setHeader("cheese", value);
+            }
+        });
+        assertOutMessageBodyEquals(response, expectedBody);
+    }
+    
+    protected void doPostSetup() throws Exception {
+        getInstalledBundle("VelocityBlueprintRouterTest").start();
+        camelContext = getOsgiService(CamelContext.class, 
"(camel.context.symbolicname=VelocityBlueprintRouterTest)", 10000);
+        mytemplate = camelContext.createProducerTemplate();
+        mytemplate.start();
+    }
+
+    @Configuration
+    public static Option[] configure() throws Exception {
+        Option[] options = combine(
+                getDefaultCamelKarafOptions(),
+                // using the features to install the camel components
+                scanFeatures(getCamelKarafFeatureUrl(),
+                        "camel-blueprint", "camel-velocity"),
+
+                bundle(newBundle()
+                        .add("OSGI-INF/blueprint/test.xml", 
VelocityBlueprintTest.class.getResource("VelocityBlueprintRouter.xml"))
+                        .add("example.vm", 
VelocityBlueprintTest.class.getResource("example.vm"))
+                        .set(Constants.BUNDLE_SYMBOLICNAME, 
"VelocityBlueprintRouterTest")
+                        .build(withBnd())).noStart()
+
+        );
+
+        return options;
+    }
+
+}

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/velocity/VelocityBlueprintTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml?rev=1189600&view=auto
==============================================================================
--- 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
 (added)
+++ 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
 Thu Oct 27 04:01:23 2011
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";>
+
+    <camelContext xmlns="http://camel.apache.org/schema/blueprint";>
+        <route>
+            <from uri="direct:start"/>
+            <to uri="velocity:example.vm"/>
+        </route>
+    </camelContext>
+
+</blueprint>

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/velocity/VelocityBlueprintRouter.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml


Reply via email to