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/11/14 19:09:09 UTC

svn commit: r1201815 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/VetoCamelContextStartException.java main/java/org/apache/camel/impl/DefaultCamelContext.java test/java/org/apache/camel/impl/VetoCamelContextStartTest.java

Author: davsclaus
Date: Mon Nov 14 18:09:08 2011
New Revision: 1201815

URL: http://svn.apache.org/viewvc?rev=1201815&view=rev
Log:
CAMEL-4678: Veto starting CamelContext can now suppress rethrowing exception, to ensure valid state in OSGi for the bundle.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/VetoCamelContextStartException.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/VetoCamelContextStartException.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/VetoCamelContextStartException.java?rev=1201815&r1=1201814&r2=1201815&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/VetoCamelContextStartException.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/VetoCamelContextStartException.java Mon Nov 14 18:09:08 2011
@@ -18,24 +18,49 @@ package org.apache.camel;
 
 /**
  * An exception to veto starting {@link CamelContext}.
+ * <p/>
+ * The option rethrowException can be used to control whether to rethrow this exception
+ * when starting CamelContext or not.
  *
- * @version 
+ * @see org.apache.camel.spi.LifecycleStrategy
  */
 public class VetoCamelContextStartException extends Exception {
-    private static final long serialVersionUID = 8046489554418284256L;
+    private static final long serialVersionUID = 8046489554418284257L;
     private final CamelContext context;
+    private final boolean rethrowException;
 
     public VetoCamelContextStartException(String message, CamelContext context) {
+        this(message, context, true);
+    }
+
+    public VetoCamelContextStartException(String message, CamelContext context, boolean rethrowException) {
         super(message);
         this.context = context;
+        this.rethrowException = rethrowException;
     }
 
     public VetoCamelContextStartException(String message, Throwable cause, CamelContext context) {
+        this(message, cause, context, true);
+    }
+
+    public VetoCamelContextStartException(String message, Throwable cause, CamelContext context, boolean rethrowException) {
         super(message, cause);
         this.context = context;
+        this.rethrowException = rethrowException;
     }
 
     public CamelContext getContext() {
         return context;
     }
+
+    /**
+     * Whether to rethrow this exception when starting CamelContext, to cause an exception
+     * to be thrown from the start method.
+     * <p/>
+     * This option is default <tt>true</tt>.
+     */
+    public boolean isRethrowException() {
+        return rethrowException;
+    }
+
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1201815&r1=1201814&r2=1201815&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Mon Nov 14 18:09:08 2011
@@ -1356,7 +1356,18 @@ public class DefaultCamelContext extends
         firstStartDone = true;
 
         // super will invoke doStart which will prepare internal services and start routes etc.
-        super.start();
+        try {
+            super.start();
+        } catch (VetoCamelContextStartException e) {
+            if (e.isRethrowException()) {
+                throw e;
+            } else {
+                log.info("CamelContext ({}) vetoed to not start due {}", getName(), e.getMessage());
+                // swallow exception and change state of this camel context to stopped
+                stop();
+                return;
+            }
+        }
 
         stopWatch.stop();
         if (log.isInfoEnabled()) {
@@ -1444,10 +1455,10 @@ public class DefaultCamelContext extends
                 strategy.onContextStart(this);
             } catch (VetoCamelContextStartException e) {
                 // okay we should not start Camel since it was vetoed
-                log.warn("Lifecycle strategy vetoed starting CamelContext (" + getName() + ")", e);
+                log.warn("Lifecycle strategy vetoed starting CamelContext ({}) due {}", getName(), e.getMessage());
                 throw e;
             } catch (Exception e) {
-                log.warn("Lifecycle strategy " + strategy + " failed starting CamelContext (" + getName() + ")", e);
+                log.warn("Lifecycle strategy " + strategy + " failed starting CamelContext ({}) due {}", getName(), e.getMessage());
                 throw e;
             }
         }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java?rev=1201815&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/VetoCamelContextStartTest.java Mon Nov 14 18:09:08 2011
@@ -0,0 +1,122 @@
+/**
+ * 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.impl;
+
+import java.util.Collection;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.ErrorHandlerFactory;
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.Service;
+import org.apache.camel.VetoCamelContextStartException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.LifecycleStrategy;
+import org.apache.camel.spi.RouteContext;
+
+/**
+ *
+ */
+public class VetoCamelContextStartTest extends ContextTestSupport {
+
+    private LifecycleStrategy veto = new MyVeto();
+
+    public void testVetoCamelContextStart() throws Exception {
+        // context is veto'ed but appears as started
+        assertEquals(false, context.getStatus().isStarted());
+        assertEquals(true, context.getStatus().isStopped());
+        assertEquals(0, context.getRoutes().size());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("mock:result");
+            }
+        };
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.addLifecycleStrategy(veto);
+        return context;
+    }
+
+    private class MyVeto implements LifecycleStrategy {
+
+        @Override
+        public void onContextStart(CamelContext context) throws VetoCamelContextStartException {
+            // we just want camel context to not startup, but do not rethrow exception
+            throw new VetoCamelContextStartException("Forced", context, false);
+        }
+
+        @Override
+        public void onContextStop(CamelContext context) {
+        }
+
+        @Override
+        public void onComponentAdd(String name, Component component) {
+        }
+
+        @Override
+        public void onComponentRemove(String name, Component component) {
+        }
+
+        @Override
+        public void onEndpointAdd(Endpoint endpoint) {
+        }
+
+        @Override
+        public void onEndpointRemove(Endpoint endpoint) {
+        }
+
+        @Override
+        public void onServiceAdd(CamelContext context, Service service, Route route) {
+        }
+
+        @Override
+        public void onServiceRemove(CamelContext context, Service service, Route route) {
+        }
+
+        @Override
+        public void onRoutesAdd(Collection<Route> routes) {
+        }
+
+        @Override
+        public void onRoutesRemove(Collection<Route> routes) {
+        }
+
+        @Override
+        public void onRouteContextCreate(RouteContext routeContext) {
+        }
+
+        @Override
+        public void onErrorHandlerAdd(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) {
+        }
+
+        @Override
+        public void onThreadPoolAdd(CamelContext camelContext, ThreadPoolExecutor threadPool, String id, String sourceId, String routeId, String threadPoolProfileId) {
+        }
+    }
+}