incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n..@apache.org
Subject svn commit: r1041780 - in /incubator/aries/trunk/jndi: jndi-api/ jndi-api/src/main/java/org/apache/aries/jndi/api/ jndi-url/src/main/java/org/apache/aries/jndi/services/ jndi-url/src/test/java/org/apache/aries/jndi/url/
Date Fri, 03 Dec 2010 12:03:22 GMT
Author: not
Date: Fri Dec  3 12:03:22 2010
New Revision: 1041780

URL: http://svn.apache.org/viewvc?rev=1041780&view=rev
Log:
ARIES-508 Allow the JNDI client to set a property to cause the damped service proxy to wait
for a replacement up to a timeout.

Added:
    incubator/aries/trunk/jndi/jndi-api/src/main/java/org/apache/aries/jndi/api/
    incubator/aries/trunk/jndi/jndi-api/src/main/java/org/apache/aries/jndi/api/JNDIConstants.java
Modified:
    incubator/aries/trunk/jndi/jndi-api/pom.xml
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java

Modified: incubator/aries/trunk/jndi/jndi-api/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-api/pom.xml?rev=1041780&r1=1041779&r2=1041780&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-api/pom.xml (original)
+++ incubator/aries/trunk/jndi/jndi-api/pom.xml Fri Dec  3 12:03:22 2010
@@ -33,6 +33,7 @@
         <aries.osgi.export>
             org.osgi.service.jndi;version="1.0.0",
             org.apache.aries.jndi.urls;version=${version},
+            org.apache.aries.jndi.api;version=${version},
             org.apache.aries.jndi.spi;version=${version}
         </aries.osgi.export>
         <aries.osgi.import>

Added: incubator/aries/trunk/jndi/jndi-api/src/main/java/org/apache/aries/jndi/api/JNDIConstants.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-api/src/main/java/org/apache/aries/jndi/api/JNDIConstants.java?rev=1041780&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-api/src/main/java/org/apache/aries/jndi/api/JNDIConstants.java
(added)
+++ incubator/aries/trunk/jndi/jndi-api/src/main/java/org/apache/aries/jndi/api/JNDIConstants.java
Fri Dec  3 12:03:22 2010
@@ -0,0 +1,27 @@
+/*
+ * 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.aries.jndi.api;
+
+
+public final class JNDIConstants
+{
+  public static final String REBIND_TIMEOUT = "org.apache.aries.jndi.rebind.timeout";
+  
+  private JNDIConstants() {}
+}

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java?rev=1041780&r1=1041779&r2=1041780&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
Fri Dec  3 12:03:22 2010
@@ -160,21 +160,33 @@ public final class ServiceHelper
     private String interfaceName;
     private String filter;
     private boolean dynamic;
+    private int rebindTimeout;
 
     public JNDIServiceDamper(BundleContext context, String i, String f, ServicePair service,
-        boolean d)
+        boolean d, int timeout)
     {
       ctx = context;
       pair = service;
       interfaceName = i;
       filter = f;
       dynamic = d;
+      rebindTimeout = timeout;
     }
 
     public Object call() throws NamingException {
       if (pair == null || pair.ref.getBundle() == null) {
         if (dynamic) {
           pair = findService(ctx, interfaceName, filter);
+          if (pair == null && rebindTimeout > 0) {
+            long startTime = System.currentTimeMillis();
+            try {
+              while (pair == null && System.currentTimeMillis() - startTime <
rebindTimeout) {
+                Thread.sleep(100);
+                pair = findService(ctx, interfaceName, filter);
+              }
+            } catch (InterruptedException e) {
+            }
+          }
         } else {
           pair = null;
         }
@@ -233,7 +245,15 @@ public final class ServiceHelper
     
     if (pair != null) {
       if (requireProxy) {
-        result = proxy(interfaceName, filter, dynamicRebind, ctx, pair);
+        Object obj = env.get(org.apache.aries.jndi.api.JNDIConstants.REBIND_TIMEOUT);
+        int timeout = 0;
+        if (obj instanceof String) {
+          timeout = Integer.parseInt((String)obj);
+        } else if (obj instanceof Integer) {
+          timeout = (Integer)obj;
+        }
+        
+        result = proxy(interfaceName, filter, dynamicRebind, ctx, pair, timeout);
       } else {
         result = pair.service;
       }
@@ -243,7 +263,7 @@ public final class ServiceHelper
   }
 
   private static Object proxy(final String interface1, final String filter, final boolean
rebind,
-                              final BundleContext ctx, final ServicePair pair)
+                              final BundleContext ctx, final ServicePair pair, final int
timeout)
   {
     Object result = null;
     Bundle owningBundle = ctx.getBundle();
@@ -262,7 +282,7 @@ public final class ServiceHelper
       result = AccessController.doPrivileged(new PrivilegedAction<Object>() {
         public Object run()
         {
-          return proxyPriviledged(interface1, filter, rebind, ctx, pair);
+          return proxyPriviledged(interface1, filter, rebind, ctx, pair, timeout);
         }
       });
 
@@ -276,7 +296,7 @@ public final class ServiceHelper
     return result;
   }
 
-  private static Object proxyPriviledged(String interface1, String filter, boolean dynamicRebind,
BundleContext ctx, ServicePair pair)
+  private static Object proxyPriviledged(String interface1, String filter, boolean dynamicRebind,
BundleContext ctx, ServicePair pair, int timeout)
   {
     String[] interfaces = null;
     if (interface1 != null) {
@@ -328,7 +348,7 @@ public final class ServiceHelper
       throw new IllegalArgumentException(Arrays.asList(interfaces).toString());
     }
 
-    Callable<Object> ih = new JNDIServiceDamper(ctx, interface1, filter, pair, dynamicRebind);
+    Callable<Object> ih = new JNDIServiceDamper(ctx, interface1, filter, pair, dynamicRebind,
timeout);
 
     // The ClassLoader needs to be able to load the service interface
     // classes so it needs to be
@@ -423,7 +443,7 @@ public final class ServiceHelper
       pair.ref = ref;
       pair.service = service;
 
-      result = proxy(null, null, false, ctx, pair);
+      result = proxy(null, null, false, ctx, pair, 0);
     }
 
     return result;

Modified: incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java?rev=1041780&r1=1041779&r2=1041780&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
Fri Dec  3 12:03:22 2010
@@ -44,6 +44,7 @@ import javax.naming.NamingException;
 import javax.naming.spi.ObjectFactory;
 import javax.sql.DataSource;
 
+import org.apache.aries.jndi.api.JNDIConstants;
 import org.apache.aries.mocks.BundleContextMock;
 import org.apache.aries.mocks.BundleMock;
 import org.apache.aries.proxy.ProxyManager;
@@ -180,6 +181,37 @@ public class ServiceRegistryContextTest
      assertTrue("expected non-proxied service class", r2 == service);
   }
   
+  @Test
+  public void testLookupWithPause() throws NamingException
+  {
+     BundleMock mock = new BundleMock("scooby.doo", new Properties());
+        
+     Thread.currentThread().setContextClassLoader(mock.getClassLoader());
+
+     Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+     env.put(JNDIConstants.REBIND_TIMEOUT, 1000);
+     
+     InitialContext ctx = new InitialContext(env);
+     
+     Context ctx2 = (Context) ctx.lookup("osgi:service");
+     
+     Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable");   
+     
+     reg.unregister();
+     
+     long startTime = System.currentTimeMillis();
+     
+     try {
+       r1.run();
+       fail("Should have received an exception");
+     } catch (ServiceException e) {
+       long endTime = System.currentTimeMillis();
+       long diff = endTime - startTime;
+       
+       assertTrue("The run method did not fail in the expected time (1s): " + diff, diff
>= 1000);
+     }
+  }
+  
   /**
    * This test checks that we correctly register and deregister the url context
    * object factory in the service registry.



Mime
View raw message