incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n..@apache.org
Subject svn commit: r922460 - in /incubator/aries/trunk/jndi/jndi-url/src: main/java/org/apache/aries/jndi/services/ main/java/org/apache/aries/jndi/url/ test/java/org/apache/aries/jndi/url/
Date Sat, 13 Mar 2010 00:26:41 GMT
Author: not
Date: Sat Mar 13 00:26:40 2010
New Revision: 922460

URL: http://svn.apache.org/viewvc?rev=922460&view=rev
Log:
ARIES-250 Fix the service name support bugs.

Modified:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/OsgiName.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryListContext.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/OsgiNameParserTest.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java

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=922460&r1=922459&r2=922460&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
Sat Mar 13 00:26:40 2010
@@ -34,6 +34,7 @@ import java.util.Set;
 
 import javax.naming.NamingException;
 
+import org.apache.aries.jndi.url.OsgiName;
 import org.apache.aries.util.BundleToClassLoaderAdapter;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -191,10 +192,14 @@ public final class ServiceHelper
     });
   }
 
-  public static Object getService(String interface1, String filter, String serviceName, String
id, boolean dynamicRebind, Map<String, Object> env) throws NamingException
+  public static Object getService(OsgiName lookupName, String id, boolean dynamicRebind,
Map<String, Object> env) throws NamingException
   {
     Object result = null;
     
+    String interfaceName = lookupName.getInterface();
+    String filter = lookupName.getFilter();
+    String serviceName = lookupName.getServiceName();
+    
     BundleContext ctx = getBundleContext(env);
     
     if (id != null && filter == null) {
@@ -203,20 +208,22 @@ public final class ServiceHelper
       filter = "(&(" + Constants.SERVICE_ID + '=' + id + ')' + filter + ')'; 
     }
     
-    ServicePair pair = findService(ctx, interface1, filter);
+    ServicePair pair = null;
+    
+    if (!!!lookupName.isServiceNameBased()) pair = findService(ctx, interfaceName, filter);
     
     if (pair == null) {
-      interface1 = null;
+      interfaceName = null;
       if (id == null) {
         filter = "(osgi.jndi.service.name=" + serviceName + ')';
       } else {
         filter = "(&(" + Constants.SERVICE_ID + '=' + id + ")(osgi.jndi.service.name="
+ serviceName + "))";
       }
-      pair = findService(ctx, interface1, filter);
+      pair = findService(ctx, interfaceName, filter);
     }
     
     if (pair != null) {
-      result = proxy(interface1, filter, dynamicRebind, ctx, pair);
+      result = proxy(interfaceName, filter, dynamicRebind, ctx, pair);
     }
     
     return result;
@@ -308,7 +315,8 @@ public final class ServiceHelper
       }
       
     } catch (InvalidSyntaxException e) {
-      throw (NamingException) new NamingException(e.getMessage()).initCause(e);
+      // If we get an invalid syntax exception we just ignore it. Null will be returned which
+      // is valid and that may result in a NameNotFoundException if that is the right thing
to do
     }
     
     return p;

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/OsgiName.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/OsgiName.java?rev=922460&r1=922459&r2=922460&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/OsgiName.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/OsgiName.java
Sat Mar 13 00:26:40 2010
@@ -18,7 +18,10 @@
  */
 package org.apache.aries.jndi.url;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.List;
 
 import javax.naming.CompositeName;
 import javax.naming.InvalidNameException;
@@ -45,7 +48,7 @@ public final class OsgiName extends Comp
   
   public OsgiName(String name) throws InvalidNameException
   {
-    super(name);
+    super(split(name));
   }
 
   public OsgiName(Name name) throws InvalidNameException
@@ -53,6 +56,33 @@ public final class OsgiName extends Comp
     this(name.toString());
   }
 
+  private static Enumeration<String> split(String name)
+  {
+    List<String> elements = new ArrayList<String>();
+
+    StringBuilder builder = new StringBuilder();
+    
+    int len = name.length();
+    int count = 0;
+    
+    for (int i = 0; i < len; i++) {
+      char c = name.charAt(i);
+      
+      if (c == '/' && count == 0) {
+        elements.add(builder.toString());
+        builder = new StringBuilder();
+        continue;
+      } else if (c == '(') count++;
+      else if (c == ')') count++;
+      
+      builder.append(c);
+    }
+    
+    elements.add(builder.toString());
+    
+    return Collections.enumeration(elements);
+  }
+
   public boolean hasFilter()
   {
     return size() == 3;

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java?rev=922460&r1=922459&r2=922460&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java
Sat Mar 13 00:26:40 2010
@@ -75,14 +75,13 @@ public class ServiceRegistryContext exte
     OsgiName validName = convert(name);
     
     String pathFragment = validName.getSchemePath();
-    String serviceName = validName.getServiceName();
     String schemeName = validName.getScheme();
     
     if (OsgiName.FRAMEWORK_PATH.equals(pathFragment) && "bundleContext".equals(validName.getServiceName()))
{
       result = ServiceHelper.getBundleContext(env);
     } else if ((OsgiName.SERVICE_PATH.equals(pathFragment) && OsgiName.OSGI_SCHEME.equals(schemeName))
||
                (OsgiName.SERVICES_PATH.equals(pathFragment) && OsgiName.ARIES_SCHEME.equals(schemeName)))
{
-      result = ServiceHelper.getService(validName.getInterface(), validName.getFilter(),
serviceName, null, true, env);
+      result = ServiceHelper.getService(validName, null, true, env);
     } else if (OsgiName.SERVICE_LIST_PATH.equals(pathFragment)) {
       result = new ServiceRegistryListContext(env, validName);
     } else {

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryListContext.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryListContext.java?rev=922460&r1=922459&r2=922460&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryListContext.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryListContext.java
Sat Mar 13 00:26:40 2010
@@ -171,7 +171,7 @@ public class ServiceRegistryListContext 
   {
     Object result = null;
     
-    result = ServiceHelper.getService(parentName.getInterface(), parentName.getFilter(),
parentName.getServiceName(), name, false, env);
+    result = ServiceHelper.getService(parentName, name, false, env);
     
     if (result == null) {
       throw new NameNotFoundException(name.toString());

Modified: incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/OsgiNameParserTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/OsgiNameParserTest.java?rev=922460&r1=922459&r2=922460&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/OsgiNameParserTest.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/OsgiNameParserTest.java
Sat Mar 13 00:26:40 2010
@@ -51,6 +51,8 @@ public class OsgiNameParserTest
     checkName("osgi","servicelist","java.lang.Runnable", "(a=b)");
     checkName("osgi","servicelist","jdbc", "grok", "DataSource");
     checkName("osgi", "framework", "bundleContext");
+    checkName("osgi","service","javax.sql.DataSource", "(osgi.jndi.servicee.name=jdbc/myDataSource)");
+    checkName("osgi","service","javax.sql.DataSource", "(&(a=/b)(c=/d))");
   }
   
   /**
@@ -126,19 +128,16 @@ public class OsgiNameParserTest
     
     if (elements.length > 1) {
       assertEquals(elements[0], n.getInterface());
-    }
-    
-    if (elements.length == 1) {
-      assertFalse(n.hasFilter());
-    }
-    
-    if (elements.length > 2) {
       if (elements.length == 2) {
-        assertTrue(n.hasFilter());
+        assertTrue("There is no filter in the name", n.hasFilter());
         assertEquals(elements[1], n.getFilter());
       } else assertFalse(n.hasFilter());
     }
     
+    if (elements.length == 1) {
+      assertFalse("There is a filter in the name", n.hasFilter());
+    }
+    
     assertEquals(serviceName.toString(), n.getServiceName());
   }
 }
\ No newline at end of file

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=922460&r1=922459&r2=922460&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
Sat Mar 13 00:26:40 2010
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTru
 import static org.junit.Assert.fail;
 
 import java.lang.reflect.Field;
+import java.sql.SQLException;
 import java.util.Hashtable;
 import java.util.Properties;
 
@@ -36,6 +37,7 @@ import javax.naming.NameNotFoundExceptio
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.spi.ObjectFactory;
+import javax.sql.DataSource;
 
 import org.apache.aries.jndi.ContextHelper;
 import org.apache.aries.jndi.OSGiObjectFactoryBuilder;
@@ -139,6 +141,41 @@ public class ServiceRegistryContextTest
     assertNotNull("The aries url context object factory was null", factory);
   }
   
+  @Test
+  public void jndiLookupServiceNameTest() throws NamingException, SQLException
+  {
+    InitialContext ctx = new InitialContext(new Hashtable<Object, Object>());
+    
+    BundleMock mock = new BundleMock("scooby.doo", new Properties());
+    
+    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
+    
+    DataSource first = Skeleton.newMock(DataSource.class);
+    DataSource second = Skeleton.newMock(DataSource.class);
+    
+    Hashtable<String, String> properties = new Hashtable<String, String>();
+    properties.put("osgi.jndi.service.name", "jdbc/myDataSource");
+    
+    bc.registerService(DataSource.class.getName(), first, properties);
+
+    properties = new Hashtable<String, String>();
+    properties.put("osgi.jndi.service.name", "jdbc/myDataSource2");
+    
+    bc.registerService(DataSource.class.getName(), second, properties);
+    
+    DataSource s = (DataSource) ctx.lookup("osgi:service/jdbc/myDataSource");
+    
+    assertNotNull(s);
+    
+    s = (DataSource) ctx.lookup("osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/myDataSource2)");
+    
+    assertNotNull(s);
+    
+    s.isWrapperFor(DataSource.class); // don't care about the method, just need to call something.
+    
+    Skeleton.getSkeleton(second).assertCalled(new MethodCall(DataSource.class, "isWrapperFor",
Class.class));
+  }
+  
   /**
    * This test does a simple JNDI lookup to prove that works.
    * @throws NamingException



Mime
View raw message