camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1438881 - in /camel/branches/camel-2.10.x: ./ components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
Date Sat, 26 Jan 2013 11:46:42 GMT
Author: davsclaus
Date: Sat Jan 26 11:46:42 2013
New Revision: 1438881

URL: http://svn.apache.org/viewvc?rev=1438881&view=rev
Log:
CAMEL-5999: Improved restlet component to better deregister restlet routers when routes is
removed in Camel.

Modified:
    camel/branches/camel-2.10.x/   (props changed)
    camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
    camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1438880

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java?rev=1438881&r1=1438880&r2=1438881&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
(original)
+++ camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/MethodBasedRouter.java
Sat Jan 26 11:46:42 2013
@@ -65,6 +65,10 @@ class MethodBasedRouter extends Restlet 
     void removeRoute(Method method) {
         routes.remove(method);
     }
+
+    boolean hasRoutes() {
+        return !routes.isEmpty();
+    }
     
     /**
      * This method does "test-and-set" on the underlying flag that indicates

Modified: camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java?rev=1438881&r1=1438880&r2=1438881&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
(original)
+++ camel/branches/camel-2.10.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
Sat Jan 26 11:46:42 2013
@@ -117,6 +117,8 @@ public class RestletComponent extends He
     @Override
     protected void doStop() throws Exception {
         component.stop();
+        // component stop will stop servers so we should clear our list as well
+        servers.clear();
         // routers map entries are removed as consumer stops and servers map
         // is not touch so to keep in sync with component's servers
         super.doStop();
@@ -147,20 +149,20 @@ public class RestletComponent extends He
     public void disconnect(RestletConsumer consumer) throws Exception {
         RestletEndpoint endpoint = consumer.getEndpoint();
 
-        List<MethodBasedRouter> routers = new ArrayList<MethodBasedRouter>();
+        List<MethodBasedRouter> routesToRemove = new ArrayList<MethodBasedRouter>();
 
         String pattern = decodePattern(endpoint.getUriPattern());
         if (pattern != null && !pattern.isEmpty()) {
-            routers.add(getMethodRouter(pattern));
+            routesToRemove.add(getMethodRouter(pattern, false));
         }
 
         if (endpoint.getRestletUriPatterns() != null) {
             for (String uriPattern : endpoint.getRestletUriPatterns()) {
-                routers.add(getMethodRouter(uriPattern));
+                routesToRemove.add(getMethodRouter(uriPattern, false));
             }
         }
 
-        for (MethodBasedRouter router : routers) {
+        for (MethodBasedRouter router : routesToRemove) {
             if (endpoint.getRestletMethods() != null) {
                 Method[] methods = endpoint.getRestletMethods();
                 for (Method method : methods) {
@@ -174,13 +176,22 @@ public class RestletComponent extends He
                 LOG.debug("Detached restlet uriPattern: {} method: {}", router.getUriPattern(),
                           endpoint.getRestletMethod());
             }
+
+            // remove router if its no longer in use
+            if (!router.hasRoutes()) {
+                deattachUriPatternFrimRestlet(router.getUriPattern(), endpoint, router);
+                if (!router.isStopped()) {
+                    router.stop();
+                }
+                routers.remove(router.getUriPattern());
+            }
         }
     }
 
-    private MethodBasedRouter getMethodRouter(String uriPattern) {
+    private MethodBasedRouter getMethodRouter(String uriPattern, boolean addIfEmpty) {
         synchronized (routers) {
             MethodBasedRouter result = routers.get(uriPattern);
-            if (result == null) {
+            if (result == null && addIfEmpty) {
                 result = new MethodBasedRouter(uriPattern);
                 LOG.debug("Added method based router: {}", result);
                 routers.put(uriPattern, result);
@@ -255,9 +266,9 @@ public class RestletComponent extends He
         return endpoint.getHost() + ":" + endpoint.getPort();
     }
 
-    private void attachUriPatternToRestlet(String uriPattern, RestletEndpoint endpoint, Restlet
target) {
+    private void attachUriPatternToRestlet(String uriPattern, RestletEndpoint endpoint, Restlet
target) throws Exception {
         uriPattern = decodePattern(uriPattern);
-        MethodBasedRouter router = getMethodRouter(uriPattern);
+        MethodBasedRouter router = getMethodRouter(uriPattern, true);
 
         Map<String, String> realm = endpoint.getRestletRealm();
         if (realm != null && realm.size() > 0) {
@@ -289,6 +300,16 @@ public class RestletComponent extends He
             component.getDefaultHost().attach(uriPattern, router);
             LOG.debug("Attached methodRouter uriPattern: {}", uriPattern);
         }
+
+        if (!router.isStarted()) {
+            router.start();
+            LOG.debug("Started methodRouter uriPattern: {}", uriPattern);
+        }
+    }
+
+    private void deattachUriPatternFrimRestlet(String uriPattern, RestletEndpoint endpoint,
Restlet target) throws Exception {
+        component.getDefaultHost().detach(target);
+        LOG.debug("Deattached methodRouter uriPattern: {}", uriPattern);
     }
 
     @Deprecated



Mime
View raw message