You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/08/10 18:48:54 UTC

svn commit: r1156260 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/ tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/

Author: davsclaus
Date: Wed Aug 10 16:48:54 2011
New Revision: 1156260

URL: http://svn.apache.org/viewvc?rev=1156260&view=rev
Log:
CAMEL-4326: Fixed using doCatch loading exception classes, now uses ClassResolver to ensure it can be loaded in OSGi etc.

Added:
    camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint5Test.java
      - copied, changed from r1156108, camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint4Test.java
    camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/MyException.java
    camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-23.xml
      - copied, changed from r1156108, camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-21.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java?rev=1156260&r1=1156259&r2=1156260&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java Wed Aug 10 16:48:54 2011
@@ -26,6 +26,7 @@ import javax.xml.bind.annotation.XmlElem
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
@@ -33,7 +34,6 @@ import org.apache.camel.builder.Expressi
 import org.apache.camel.processor.CatchProcessor;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.util.CastUtils;
-import org.apache.camel.util.ObjectHelper;
 import static org.apache.camel.builder.PredicateBuilder.toPredicate;
 
 /**
@@ -86,8 +86,13 @@ public class CatchDefinition extends Pro
 
     @Override
     public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
+        // create and load exceptions if not done
+        if (exceptionClasses == null) {
+            exceptionClasses = createExceptionClasses(routeContext.getCamelContext());
+        }
+
         // must have at least one exception
-        if (getExceptionClasses().isEmpty()) {
+        if (exceptionClasses.isEmpty()) {
             throw new IllegalArgumentException("At least one Exception must be configured to catch");
         }
 
@@ -104,7 +109,7 @@ public class CatchDefinition extends Pro
             handle = handled.createPredicate(routeContext);
         }
 
-        return new CatchProcessor(getExceptionClasses(), childProcessor, when, handle);
+        return new CatchProcessor(exceptionClasses, childProcessor, when, handle);
     }
 
     public List<ProcessorDefinition> getOutputs() {
@@ -120,9 +125,6 @@ public class CatchDefinition extends Pro
     }
 
     public List<Class> getExceptionClasses() {
-        if (exceptionClasses == null) {
-            exceptionClasses = createExceptionClasses();
-        }
         return exceptionClasses;
     }
 
@@ -243,11 +245,13 @@ public class CatchDefinition extends Pro
         this.handled = handled;
     }
 
-    protected List<Class> createExceptionClasses() {
+    protected List<Class> createExceptionClasses(CamelContext context) throws ClassNotFoundException {
+        // must use the class resolver from CamelContext to load classes to ensure it can
+        // be loaded in all kind of environments such as JEE servers and OSGi etc.
         List<String> list = getExceptions();
         List<Class> answer = new ArrayList<Class>(list.size());
         for (String name : list) {
-            Class<Exception> type = CastUtils.cast(ObjectHelper.loadClass(name, getClass().getClassLoader()));
+            Class<Exception> type = CastUtils.cast(context.getClassResolver().resolveMandatoryClass(name));
             answer.add(type);
         }
         return answer;

Copied: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint5Test.java (from r1156108, camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint4Test.java)
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint5Test.java?p2=camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint5Test.java&p1=camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint4Test.java&r1=1156108&r2=1156260&rev=1156260&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint4Test.java (original)
+++ camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/CamelBlueprint5Test.java Wed Aug 10 16:48:54 2011
@@ -19,7 +19,6 @@ package org.apache.camel.itest.osgi.blue
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.karaf.testing.Helper;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Option;
@@ -31,79 +30,35 @@ import org.osgi.service.blueprint.contai
 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;
 
 /**
  * @version 
  */
 @RunWith(JUnit4TestRunner.class)
-public class CamelBlueprint4Test extends OSGiBlueprintTestSupport {
+public class CamelBlueprint5Test extends OSGiBlueprintTestSupport {
 
     @Test
-    public void testRouteWithXSLT() throws Exception {
-        getInstalledBundle("CamelBlueprintTestBundle19").start();
-        BlueprintContainer ctn = getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=CamelBlueprintTestBundle19)", 10000);
-        CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.symbolicname=CamelBlueprintTestBundle19)", 10000);
-
-        ProducerTemplate template = ctx.createProducerTemplate();
-
-        MockEndpoint mock = ctx.getEndpoint("mock:result", MockEndpoint.class);
-        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
-        mock.message(0).body().isInstanceOf(String.class);
-
-        template.sendBody("direct:start", "<hello>world!</hello>");
-
-        mock.assertIsSatisfied();
-        template.stop();
-    }
-
-    @Test
-    public void testRouteWithVelocity() throws Exception {
-        getInstalledBundle("CamelBlueprintTestBundle20").start();
-        BlueprintContainer ctn = getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=CamelBlueprintTestBundle20)", 10000);
-        CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.symbolicname=CamelBlueprintTestBundle20)", 10000);
-
-        ProducerTemplate template = ctx.createProducerTemplate();
-        Object out = template.requestBody("direct:a", "world");
-        assertEquals("<hello>world</hello>", out);
-        template.stop();
-    }
-
-    @Test
-    public void testSetHeaderXPathResultType() throws Exception {
-        getInstalledBundle("CamelBlueprintTestBundle21").start();
-        BlueprintContainer ctn = getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=CamelBlueprintTestBundle21)", 10000);
-        CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.symbolicname=CamelBlueprintTestBundle21)", 10000);
+    public void testTryCatch() throws Exception {
+        getInstalledBundle("CamelBlueprintTestBundle23").start();
+        BlueprintContainer ctn = getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=CamelBlueprintTestBundle23)", 10000);
+        CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.symbolicname=CamelBlueprintTestBundle23)", 10000);
 
         ProducerTemplate template = ctx.createProducerTemplate();
 
         MockEndpoint mock = ctx.getEndpoint("mock:result", MockEndpoint.class);
         mock.expectedMessageCount(1);
-        mock.message(0).header("foo").isInstanceOf(Boolean.class);
-        mock.message(0).header("foo").isEqualTo(true);
-        mock.message(0).header("bar").isInstanceOf(Boolean.class);
-        mock.message(0).header("bar").isEqualTo(false);
 
-        template.sendBody("direct:start", "<hello>World</hello>");
+        MockEndpoint error = ctx.getEndpoint("mock:error", MockEndpoint.class);
+        error.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "Kaboom");
 
         mock.assertIsSatisfied();
+        error.assertIsSatisfied();
         template.stop();
     }
 
-    @Test
-    public void testGetApplicationContextClassloader() throws Exception {
-        getInstalledBundle("CamelBlueprintTestBundle22").start();
-        BlueprintContainer ctn = getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=CamelBlueprintTestBundle22)", 10000);
-        CamelContext ctx = getOsgiService(CamelContext.class, "(camel.context.symbolicname=CamelBlueprintTestBundle22)", 10000);
-
-        // test the application context classloader
-        assertNotNull("The application context classloader should not be null", ctx.getApplicationContextClassLoader());
-        ClassLoader cl = ctx.getApplicationContextClassLoader();
-        assertNotNull("It should load the TestRouteBuilder class", cl.getResource("OSGI-INF/blueprint/test.xml"));
-        assertNotNull("It should load the TestRouteBuilder class", cl.loadClass("org.apache.camel.itest.osgi.blueprint.TestRouteBuilder"));
-
-    }
-
     @Configuration
     public static Option[] configure() throws Exception {
 
@@ -111,31 +66,14 @@ public class CamelBlueprint4Test extends
                 getDefaultCamelKarafOptions(),
 
                 bundle(newBundle()
-                        .add("OSGI-INF/blueprint/test.xml", OSGiBlueprintTestSupport.class.getResource("blueprint-19.xml"))
-                        .add("org/apache/camel/itest/osgi/blueprint/example.xsl", OSGiBlueprintTestSupport.class.getResource("example.xsl"))
-                        .set(Constants.BUNDLE_SYMBOLICNAME, "CamelBlueprintTestBundle19")
+                        .add("OSGI-INF/blueprint/test.xml", OSGiBlueprintTestSupport.class.getResource("blueprint-23.xml"))
+                        .set(Constants.BUNDLE_SYMBOLICNAME, "CamelBlueprintTestBundle23")
+                        .add(MyException.class)
                         .build()).noStart(),
 
-                bundle(newBundle()
-                        .add("OSGI-INF/blueprint/test.xml", OSGiBlueprintTestSupport.class.getResource("blueprint-20.xml"))
-                        .add("org/apache/camel/itest/osgi/blueprint/example.vm", OSGiBlueprintTestSupport.class.getResource("example.vm"))
-                        .set(Constants.BUNDLE_SYMBOLICNAME, "CamelBlueprintTestBundle20")
-                        .build()).noStart(),
-                
-                bundle(newBundle()
-                        .add("OSGI-INF/blueprint/test.xml", OSGiBlueprintTestSupport.class.getResource("blueprint-21.xml"))
-                        .set(Constants.BUNDLE_SYMBOLICNAME, "CamelBlueprintTestBundle21")
-                        .build()).noStart(),
-
-                bundle(newBundle()
-                        .add("OSGI-INF/blueprint/test.xml", OSGiBlueprintTestSupport.class.getResource("blueprint-13.xml"))
-                        .set(Constants.BUNDLE_SYMBOLICNAME, "CamelBlueprintTestBundle22")
-                        .add(TestRouteBuilder.class)
-                        .build(withBnd())).noStart(),
-
                 // using the features to install the camel components
                 scanFeatures(getCamelKarafFeatureUrl(),
-                        "camel-blueprint", "camel-velocity"));
+                        "camel-blueprint"));
 
         return options;
     }

Added: camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/MyException.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/MyException.java?rev=1156260&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/MyException.java (added)
+++ camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/blueprint/MyException.java Wed Aug 10 16:48:54 2011
@@ -0,0 +1,30 @@
+/**
+ * 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.blueprint;
+
+/**
+ *
+ */
+public class MyException extends Exception {
+
+    public MyException() {
+    }
+
+    public MyException(String s) {
+        super(s);
+    }
+}

Copied: camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-23.xml (from r1156108, camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-21.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-23.xml?p2=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-23.xml&p1=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-21.xml&r1=1156108&r2=1156260&rev=1156260&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-21.xml (original)
+++ camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-23.xml Wed Aug 10 16:48:54 2011
@@ -17,19 +17,26 @@
 -->
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
 
+  <bean id="damn" class="org.apache.camel.itest.osgi.blueprint.MyException"/>
+
   <camelContext xmlns="http://camel.apache.org/schema/blueprint">
     <route>
       <from uri="direct:start"/>
-      <!-- is there a hello tag in the body -->
-      <setHeader headerName="foo">
-        <xpath resultType="java.lang.Boolean">/hello</xpath>
-      </setHeader>
-      <!-- is there a bye tag in the body -->
-      <setHeader headerName="bar">
-        <!-- use Boolean shorthand to test we use Camel class resolver, and not JAXB -->
-        <xpath resultType="Boolean">/bye</xpath>
-      </setHeader>
-      <to uri="mock:result"/>
+      <doTry>
+        <choice>
+          <when>
+            <simple>${body} == 'Kaboom'</simple>
+            <throwException ref="damn"/>
+          </when>
+          <otherwise>
+            <to uri="mock:result"/>
+          </otherwise>
+        </choice>
+        <doCatch>
+          <exception>org.apache.camel.itest.osgi.blueprint.MyException</exception>
+          <to uri="mock:error"/>
+        </doCatch>
+      </doTry>
     </route>
   </camelContext>