incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r943955 - in /incubator/aries/trunk/jndi: jndi-core/src/main/java/org/apache/aries/jndi/ jndi-core/src/main/java/org/apache/aries/jndi/startup/ jndi-core/src/test/java/org/apache/aries/jndi/ jndi-url/src/test/java/org/apache/aries/jndi/url/
Date Thu, 13 May 2010 17:50:55 GMT
Author: gawor
Date: Thu May 13 17:50:55 2010
New Revision: 943955

URL: http://svn.apache.org/viewvc?rev=943955&view=rev
Log:
ARIES-311: Add support for JNDIProviderAdmin service

Added:
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
  (with props)
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
  (with props)
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
  (with props)
Modified:
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextHelper.java
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextManagerService.java
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/OSGiObjectFactoryBuilder.java
    incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
    incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java

Modified: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextHelper.java?rev=943955&r1=943954&r2=943955&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextHelper.java
(original)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextHelper.java
Thu May 13 17:50:55 2010
@@ -21,6 +21,7 @@ package org.apache.aries.jndi;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.Hashtable;
+import java.util.Map;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
@@ -38,8 +39,11 @@ import org.osgi.service.jndi.JNDIConstan
  * Provides helper methods for the DelegateContext. This provides the methods so
  * there can be many DelegateContexts, but few service trackers.
  */
-public final class ContextHelper
-{
+public final class ContextHelper {
+
+    public static final Comparator<ServiceReference> SERVICE_REFERENCE_COMPARATOR =

+        new ServiceReferenceComparator();
+    
 	/** The bundle context we use for accessing the SR */
   private static BundleContext context;
   
@@ -187,7 +191,7 @@ public final class ContextHelper
                 }
                 if (references != null) {
                     Context initialContext = null;
-                    Arrays.sort(references, new ServiceReferenceComparator());
+                    Arrays.sort(references, SERVICE_REFERENCE_COMPARATOR);
                     for (ServiceReference reference : references) {
                         InitialContextFactory factory = (InitialContextFactory) context.getService(reference);
                         try {
@@ -216,7 +220,7 @@ public final class ContextHelper
 
             if (references != null && references.length > 0) {
                 Context initialContext = null;
-                Arrays.sort(references, new ServiceReferenceComparator());
+                Arrays.sort(references, SERVICE_REFERENCE_COMPARATOR);
                 ServiceReference reference = references[0];
                 InitialContextFactory factory = (InitialContextFactory) context.getService(reference);
                 try {
@@ -246,7 +250,7 @@ public final class ContextHelper
             ServiceReference[] refs = context.getAllServiceReferences(InitialContextFactoryBuilder.class.getName(),
null);
             if (refs != null) {
                 InitialContextFactory factory = null;
-                Arrays.sort(refs, new ServiceReferenceComparator());
+                Arrays.sort(refs, SERVICE_REFERENCE_COMPARATOR);
                 for (ServiceReference ref : refs) {                    
                     InitialContextFactoryBuilder builder = (InitialContextFactoryBuilder)
context.getService(ref);
                     try {
@@ -284,9 +288,22 @@ public final class ContextHelper
         }
     }
     
-    public static class ServiceReferenceComparator implements Comparator<ServiceReference>
{
+    private static class ServiceReferenceComparator implements Comparator<ServiceReference>
{        
         public int compare(ServiceReference o1, ServiceReference o2) {        
           return o2.compareTo(o1);
         }
     }
+    
+    public static Hashtable toHashtable(Map map) {
+        Hashtable env;
+        if (map instanceof Hashtable) {
+            env = (Hashtable) map;
+        } else {
+            env = new Hashtable();
+            if (map != null) {
+                env.putAll(map);
+            }
+        }
+        return env;
+    }
 }

Modified: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextManagerService.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextManagerService.java?rev=943955&r1=943954&r2=943955&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextManagerService.java
(original)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ContextManagerService.java
Thu May 13 17:50:55 2010
@@ -70,18 +70,9 @@ public class ContextManagerService imple
         return DirContext.class.cast( getInitialContext(environment) );
     }
     
-    private Context getInitialContext(Map environment) throws NamingException {
-        Hashtable env;
-        if (environment instanceof Hashtable) {
-            env = (Hashtable) environment;
-        } else {
-            env = new Hashtable();
-            if (environment != null) {
-                env.putAll(environment);        
-            }
-        }
-        
+    private Context getInitialContext(Map environment) throws NamingException {        
         BundleContext bundleContext = bundle.getBundleContext();
+        Hashtable env = ContextHelper.toHashtable(environment);
         Context context = ContextHelper.getInitialContext(bundleContext, env);
         contexts.add(context);
         return context;

Added: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java?rev=943955&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
(added)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
Thu May 13 17:50:55 2010
@@ -0,0 +1,252 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.RefAddr;
+import javax.naming.Reference;
+import javax.naming.Referenceable;
+import javax.naming.StringRefAddr;
+import javax.naming.directory.Attributes;
+import javax.naming.spi.DirObjectFactory;
+import javax.naming.spi.ObjectFactory;
+import javax.naming.spi.ObjectFactoryBuilder;
+
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.jndi.JNDIConstants;
+
+public class DirObjectFactoryHelper extends ObjectFactoryHelper implements DirObjectFactory
{
+
+    public Object getObjectInstance(Object obj,
+                                    Name name,
+                                    Context nameCtx,
+                                    Hashtable<?, ?> environment,
+                                    Attributes attrs) throws Exception {
+
+        // Step 1
+        if (obj instanceof Referenceable) {
+            obj = ((Referenceable) obj).getReference();
+        }
+
+        Object result = obj;
+
+        // Step 2
+        if (obj instanceof Reference) {
+            Reference ref = (Reference) obj;
+            String className = ref.getFactoryClassName();
+
+            if (className != null) {
+                // Step 3
+                result = getObjectInstanceUsingClassName(obj, className, obj, name, nameCtx,
environment, attrs);
+            } else {
+                // Step 4
+                result = getObjectInstanceUsingRefAddress(ref.getAll(), obj, name, nameCtx,
environment, attrs);
+            }
+        }
+
+        // Step 5
+        if (result == null || result == obj) {
+            result = getObjectInstanceUsingObjectFactoryBuilders(obj, name, nameCtx, environment,
attrs);
+        }
+        
+        // Step 6
+        if (result == null || result == obj) {                
+            if ((obj instanceof Reference && ((Reference) obj).getFactoryClassName()
== null) ||
+                !(obj instanceof Reference)) {
+                result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment,
attrs);
+            }
+        }
+
+        return (result == null) ? obj : result;
+    }
+
+    private Object getObjectInstanceUsingObjectFactories(Object obj,
+                                                         Name name,
+                                                         Context nameCtx,
+                                                         Hashtable<?, ?> environment,
+                                                         Attributes attrs) 
+        throws Exception {
+        Object result = null;
+        try {
+            ServiceReference[] refs = context.getAllServiceReferences(DirObjectFactory.class.getName(),
null);
+            if (refs != null) {
+                Arrays.sort(refs, ContextHelper.SERVICE_REFERENCE_COMPARATOR);
+                for (ServiceReference ref : refs) {
+                    DirObjectFactory factory = (DirObjectFactory) context.getService(ref);
+
+                    try {
+                        result = factory.getObjectInstance(obj, name, nameCtx, environment,
attrs);
+                    } finally {
+                        context.ungetService(ref);
+                    }
+
+                    // if the result comes back and is not null and not the reference
+                    // object then we should return the result, so break out of the
+                    // loop we are in.
+                    if (result != null && result != obj) {
+                        break;
+                    }
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            // should not happen
+            throw new RuntimeException("Invalid filter", e);
+        }
+
+        if (result == null) {
+            result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment);
+        }
+        
+        return (result == null) ? obj : result;
+    }
+
+    private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses,
+                                                    Object obj,
+                                                    Name name,
+                                                    Context nameCtx,
+                                                    Hashtable<?, ?> environment,
+                                                    Attributes attrs) 
+        throws Exception {
+        Object result = null;
+        while (addresses.hasMoreElements()) {
+            RefAddr address = addresses.nextElement();
+            if (address instanceof StringRefAddr && "URL".equals(address.getType()))
{
+                String urlScheme = getUrlScheme( (String) address.getContent() );
+                DirObjectFactory factory = null;
+                ServiceReference ref = null;
+                try {
+                    ServiceReference[] services = context.getServiceReferences(DirObjectFactory.class.getName(),

+                            "(&(" + JNDIConstants.JNDI_URLSCHEME + "=" + urlScheme +
"))");
+
+                    if (services != null && services.length > 0) {
+                        ref = services[0];
+                    }
+                } catch (InvalidSyntaxException e) {
+                    // should not happen
+                    throw new RuntimeException("Invalid filter", e);
+                }
+
+                if (ref != null) {
+                    factory = (DirObjectFactory) context.getService(ref);
+                    
+                    String value = (String) address.getContent();
+                    try {
+                        result = factory.getObjectInstance(value, name, nameCtx, environment,
attrs);
+                    } finally {
+                        context.ungetService(ref);
+                    }
+
+                    // if the result comes back and is not null and not the reference
+                    // object then we should return the result, so break out of the
+                    // loop we are in.
+                    if (result != null && result != obj) {
+                        break;
+                    }
+                }
+            }
+        }
+
+        return (result == null) ? obj : result;
+    }
+
+    private Object getObjectInstanceUsingClassName(Object reference,
+                                                   String className,
+                                                   Object obj,
+                                                   Name name,
+                                                   Context nameCtx,
+                                                   Hashtable<?, ?> environment,
+                                                   Attributes attrs)
+        throws Exception {
+        ServiceReference serviceReference = null;
+
+        try {
+            ServiceReference[] refs = context.getAllServiceReferences(className, null);
+            if (refs != null && refs.length > 0) {
+                serviceReference = refs[0];
+            }
+        } catch (InvalidSyntaxException e) {
+            // should not happen
+            throw new RuntimeException("Invalid filter", e);
+        }
+
+        Object result = null;
+        
+        if (serviceReference != null) {
+            DirObjectFactory factory = (DirObjectFactory) context.getService(serviceReference);
+            try {
+                result = factory.getObjectInstance(reference, name, nameCtx, environment,
attrs);
+            } finally {
+                context.ungetService(serviceReference);
+            }
+        }
+
+        return (result == null) ? obj : result;
+    }
+  
+    private Object getObjectInstanceUsingObjectFactoryBuilders(Object obj,
+                                                               Name name,
+                                                               Context nameCtx,
+                                                               Hashtable<?, ?> environment,
+                                                               Attributes attrs) 
+        throws Exception {
+        ObjectFactory factory = null;
+        try {
+            ServiceReference[] refs = context.getServiceReferences(ObjectFactoryBuilder.class.getName(),
null);
+            if (refs != null) {
+                Arrays.sort(refs, ContextHelper.SERVICE_REFERENCE_COMPARATOR);
+                for (ServiceReference ref : refs) {
+                    ObjectFactoryBuilder builder = (ObjectFactoryBuilder) context.getService(ref);
+                    try {
+                        factory = builder.createObjectFactory(obj, environment);
+                    } catch (NamingException e) {
+                        // TODO: log it
+                    } finally {
+                        context.ungetService(ref);
+                    }
+                    if (factory != null) {
+                        break;
+                    }
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            // should not happen
+            throw new RuntimeException("Invalid filter", e);
+        }
+
+        Object result = null;
+        
+        if (factory != null) {
+            if (factory instanceof DirObjectFactory) {       
+                result = ((DirObjectFactory) factory).getObjectInstance(obj, name, nameCtx,
environment, attrs);
+            } else {
+                result = factory.getObjectInstance(obj, name, nameCtx, environment);
+            }
+        }
+        
+        return (result == null) ? obj : result;
+    }
+
+}

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/DirObjectFactoryHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/OSGiObjectFactoryBuilder.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/OSGiObjectFactoryBuilder.java?rev=943955&r1=943954&r2=943955&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/OSGiObjectFactoryBuilder.java
(original)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/OSGiObjectFactoryBuilder.java
Thu May 13 17:50:55 2010
@@ -18,229 +18,44 @@
  */
 package org.apache.aries.jndi;
 
-import java.util.Enumeration;
 import java.util.Hashtable;
 
 import javax.naming.Context;
 import javax.naming.Name;
 import javax.naming.NamingException;
-import javax.naming.RefAddr;
-import javax.naming.Reference;
-import javax.naming.Referenceable;
-import javax.naming.StringRefAddr;
 import javax.naming.directory.Attributes;
 import javax.naming.spi.DirObjectFactory;
 import javax.naming.spi.ObjectFactory;
 import javax.naming.spi.ObjectFactoryBuilder;
 
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
 
-public class OSGiObjectFactoryBuilder implements ObjectFactoryBuilder, ObjectFactory, DirObjectFactory
-{
-  /** The bundle context we use for accessing the SR */
-  private static BundleContext context;
-  
-  public static void setBundleContext(BundleContext ctx)
-  {
-    context = ctx;
-  }
-
-  public ObjectFactory createObjectFactory(Object obj, Hashtable<?, ?> environment)
-      throws NamingException
-  {
-    return this;
-  }
-
-  public Object getObjectInstance(Object obj, Name name, Context nameCtx,
-      Hashtable<?, ?> environment) throws Exception
-  {
-  	
-    Reference ref = null;
-
-    if (obj instanceof Reference) {
-      ref = (Reference) obj;
-    } else if (obj instanceof Referenceable) {
-      ref = ((Referenceable)obj).getReference();
-    }
-
-    Object result = ref;
-
-    if (ref != null) {
-      String className = ref.getFactoryClassName();
-
-      if (className != null) {
-        result = getObjectInstanceUsingClassName(obj, className, obj, name, nameCtx, environment);
-      } else {
-        result = getObjectInstanceUsingRefAddress(ref.getAll(), obj, name, nameCtx, environment);
-
-        if (result == null || result == obj) {
-          result = getObjectInstanceUsingAllObjectFactories(obj, name, nameCtx, environment);
-        }
-      }
-    }
+public class OSGiObjectFactoryBuilder implements ObjectFactoryBuilder, ObjectFactory, DirObjectFactory
{
 
-    if (result == null) result = ref;
+    private static DirObjectFactoryHelper helper = new DirObjectFactoryHelper();
     
-    return result;
-  }
-
-  private Object getObjectInstanceUsingAllObjectFactories(Object obj, Name name, Context
nameCtx,
-      Hashtable<?, ?> environment)
-  {
-    Object result = obj;
-    try {
-      ServiceReference[] refs = context.getAllServiceReferences(ObjectFactory.class.getName(),
null);
-
-      if (refs != null) {
-        for (ServiceReference ref : refs) {
-          ObjectFactory factory = (ObjectFactory) context.getService(ref);
-  
-          if (factory != null) {
-            try {
-              result = factory.getObjectInstance(obj, name, nameCtx, environment);
-            } catch (Exception e) {
-              // TODO Auto-generated catch block
-              e.printStackTrace();
-            }
-  
-            context.ungetService(ref);
-  
-            // if the result comes back and is not null and not the reference
-            // object then we should return the result, so break out of the
-            // loop we are in.
-            if (result != null && result != obj) break;
-          }
-        }
-      }
-    } catch (InvalidSyntaxException e) {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
+    public static void setBundleContext(BundleContext ctx) {
+        helper.setBundleContext(ctx);
     }
 
-    if (result == null) result = obj;
-  	
-    return result;
-  }
-
-  private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses, Object
obj, Name name,
-      Context nameCtx, Hashtable<?, ?> environment)
-  {
-    Object result = obj;
-
-    while (addresses.hasMoreElements()) {
-      RefAddr address = addresses.nextElement();
-      if (address instanceof StringRefAddr && "URL".equals(address.getType())) {
-        String urlScheme = (String)address.getContent();
-        ObjectFactory factory = null;
-        ServiceReference ref = null;
-        try {
-          ServiceReference[] services = context.getServiceReferences(ObjectFactory.class.getName(),
"(|(osgi.jndi.urlScheme=" + urlScheme + ")(urlScheme=" + urlScheme + "))");
-
-          if (services != null) {
-            ref = services[0];
-            factory = (ObjectFactory) context.getService(ref);
-          }
-        } catch (InvalidSyntaxException e) {
-          e.printStackTrace();
-        }
-
-        if (factory != null) {
-          try {
-            result = factory.getObjectInstance(obj, name, nameCtx, environment);
-          } catch (Exception e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-          }
-        }
-
-        if (ref != null) context.ungetService(ref);
-        
-        // if the result is not null and references a different object from
-        // the obj then we have resolved the object, so we stop searching.
-        if (result != null && result != obj) break;
-      }
+    public ObjectFactory createObjectFactory(Object obj, Hashtable<?, ?> environment)
+        throws NamingException {
+        return this;
     }
 
-    return result;
-  }
-
-  private Object getObjectInstanceUsingClassName(Object reference, String className, Object
obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
-  {
-    Object result = obj;
-
-    ObjectFactory factory = null;
-    ServiceReference ref = null;
-
-    if (className != null) {
-      try {
-        ServiceReference[] refs = context.getAllServiceReferences(className, null);
-
-        if (refs != null) {
-          for (ServiceReference ref1 : refs) {
-            factory = (ObjectFactory) context.getService(ref1);
-
-            if (factory != null) {
-              ref = ref1;
-              break;
-            }
-          }
-        }
-      } catch (InvalidSyntaxException e) {
-        // TODO Auto-generated catch block
-        e.printStackTrace();
-      }
+    public Object getObjectInstance(Object obj,
+                                    Name name,
+                                    Context nameCtx,
+                                    Hashtable<?, ?> environment) throws Exception {
+        return helper.getObjectInstance(obj, name, nameCtx, environment);
     }
 
-    if (factory == null) {
-      try {
-        ServiceReference[] refs = context.getServiceReferences(ObjectFactoryBuilder.class.getName(),
null);
-
-        if (refs != null) {
-          for (ServiceReference ofRef : refs) {
-            ObjectFactoryBuilder builder = (ObjectFactoryBuilder) context.getService(ofRef);
-            try {
-              factory = builder.createObjectFactory(reference, environment);
-            } catch (NamingException e) {
-              // TODO Auto-generated catch block
-              e.printStackTrace();
-            }
-
-            context.ungetService(ofRef);
-            if (factory != null) {
-              break;
-            }
-          }
-        }
-      } catch (InvalidSyntaxException e) {
-        // TODO Auto-generated catch block
-        e.printStackTrace();
-      }
+    public Object getObjectInstance(Object obj,
+                                    Name name,
+                                    Context nameCtx,
+                                    Hashtable<?, ?> environment,
+                                    Attributes attrs) throws Exception {
+        return helper.getObjectInstance(obj, name, nameCtx, environment, attrs);
     }
 
-    if (factory != null) {
-      try {
-        result = factory.getObjectInstance(obj, name, nameCtx, environment);
-      } catch (Exception e) {
-        // TODO Auto-generated catch block
-        e.printStackTrace();
-      } finally {
-        if (ref != null) context.ungetService(ref);
-      }
-    }
-
-    return result;
-  }
-  
-  /**
-   * when we get called by DirectoryManager#getObjectInstance if we can't find the object

-   * instance, we just need to return the passed in refInfo  
-   */
-  public Object getObjectInstance(Object refInfo, Name name, Context nameCtx, 
-                                  Hashtable<?, ?> environment, Attributes attrs) throws
Exception {
-      Object result = getObjectInstance(refInfo, name, nameCtx, environment);
-      return result == null ? refInfo : result;
-  }
-
-}
\ No newline at end of file
+}

Added: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java?rev=943955&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
(added)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
Thu May 13 17:50:55 2010
@@ -0,0 +1,253 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.RefAddr;
+import javax.naming.Reference;
+import javax.naming.Referenceable;
+import javax.naming.StringRefAddr;
+import javax.naming.spi.ObjectFactory;
+import javax.naming.spi.ObjectFactoryBuilder;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.jndi.JNDIConstants;
+
+public class ObjectFactoryHelper implements ObjectFactory {
+
+    protected BundleContext context;
+
+    public void setBundleContext(BundleContext ctx) {
+        context = ctx;
+    }
+
+    public Object getObjectInstance(Object obj,
+                                    Name name,
+                                    Context nameCtx,
+                                    Hashtable<?, ?> environment) throws Exception {
+
+        // Step 1
+        if (obj instanceof Referenceable) {
+            obj = ((Referenceable) obj).getReference();
+        }
+
+        Object result = obj;
+
+        // Step 2
+        if (obj instanceof Reference) {
+            Reference ref = (Reference) obj;
+            String className = ref.getFactoryClassName();
+
+            if (className != null) {
+                // Step 3
+                result = getObjectInstanceUsingClassName(obj, className, obj, name, nameCtx,
environment);
+            } else {
+                // Step 4
+                result = getObjectInstanceUsingRefAddress(ref.getAll(), obj, name, nameCtx,
environment);
+            }
+        }
+
+        // Step 5
+        if (result == null || result == obj) {
+            result = getObjectInstanceUsingObjectFactoryBuilders(obj, name, nameCtx, environment);
+        }
+
+        // Step 6
+        if (result == null || result == obj) {                
+            if ((obj instanceof Reference && ((Reference) obj).getFactoryClassName()
== null) ||
+                !(obj instanceof Reference)) {
+                result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment);
+            }
+        }
+
+        return (result == null) ? obj : result;
+    }
+
+    protected Object getObjectInstanceUsingObjectFactories(Object obj,
+                                                           Name name,
+                                                           Context nameCtx,
+                                                           Hashtable<?, ?> environment)

+        throws Exception {
+        Object result = null;
+        try {
+            ServiceReference[] refs = context.getAllServiceReferences(ObjectFactory.class.getName(),
null);
+            if (refs != null) {
+                Arrays.sort(refs, ContextHelper.SERVICE_REFERENCE_COMPARATOR);
+                for (ServiceReference ref : refs) {
+                    ObjectFactory factory = (ObjectFactory) context.getService(ref);
+
+                    try {
+                        result = factory.getObjectInstance(obj, name, nameCtx, environment);
+                    } finally {
+                        context.ungetService(ref);
+                    }
+
+                    // if the result comes back and is not null and not the reference
+                    // object then we should return the result, so break out of the
+                    // loop we are in.
+                    if (result != null && result != obj) {
+                        break;
+                    }
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            // should not happen
+            throw new RuntimeException("Invalid filter", e);
+        }
+
+        return (result == null) ? obj : result;
+    }
+
+    protected static String getUrlScheme(String name) {
+        String scheme = name;   
+        int index = name.indexOf(':');
+        if (index != -1) {
+            scheme = name.substring(0, index);
+        }
+        return scheme;
+    }
+        
+    private Object getObjectInstanceUsingRefAddress(Enumeration<RefAddr> addresses,
+                                                    Object obj,
+                                                    Name name,
+                                                    Context nameCtx,
+                                                    Hashtable<?, ?> environment)
+        throws Exception {       
+        Object result = null;
+        while (addresses.hasMoreElements()) {
+            RefAddr address = addresses.nextElement();
+            if (address instanceof StringRefAddr && "URL".equals(address.getType()))
{
+                String urlScheme = getUrlScheme( (String) address.getContent() );
+                ObjectFactory factory = null;
+                ServiceReference ref = null;
+                try {
+                    ServiceReference[] services = context.getServiceReferences(ObjectFactory.class.getName(),

+                            "(&(" + JNDIConstants.JNDI_URLSCHEME + "=" + urlScheme +
"))");
+
+                    if (services != null && services.length > 0) {
+                        ref = services[0];
+                    }
+                } catch (InvalidSyntaxException e) {
+                    // should not happen
+                    throw new RuntimeException("Invalid filter", e);
+                }
+
+                if (ref != null) {
+                    factory = (ObjectFactory) context.getService(ref);
+                    
+                    String value = (String) address.getContent();
+                    try {
+                        result = factory.getObjectInstance(value, name, nameCtx, environment);
+                    } finally {
+                        context.ungetService(ref);
+                    }
+                    
+                    // if the result comes back and is not null and not the reference
+                    // object then we should return the result, so break out of the
+                    // loop we are in.
+                    if (result != null && result != obj) {
+                        break;
+                    }
+                }
+            }
+        }
+
+        return (result == null) ? obj : result;
+    }
+
+    private Object getObjectInstanceUsingClassName(Object reference,
+                                                   String className,
+                                                   Object obj,
+                                                   Name name,
+                                                   Context nameCtx,
+                                                   Hashtable<?, ?> environment) 
+        throws Exception {
+        ServiceReference serviceReference = null;
+
+        try {
+            ServiceReference[] refs = context.getAllServiceReferences(className, null);
+            if (refs != null && refs.length > 0) {
+                serviceReference = refs[0];
+            }
+        } catch (InvalidSyntaxException e) {
+            // should not happen
+            throw new RuntimeException("Invalid filter", e);
+        }
+
+        Object result = null;
+        
+        if (serviceReference != null) {
+            ObjectFactory factory = (ObjectFactory) context.getService(serviceReference);
+            try {
+                result = factory.getObjectInstance(reference, name, nameCtx, environment);
+            } finally {
+                context.ungetService(serviceReference);
+            }
+        }
+
+        return (result == null) ? obj : result;
+    }
+  
+    private Object getObjectInstanceUsingObjectFactoryBuilders(Object obj,
+                                                               Name name,
+                                                               Context nameCtx,
+                                                               Hashtable<?, ?> environment)

+        throws Exception {
+        ObjectFactory factory = null;
+        try {
+            ServiceReference[] refs = context.getServiceReferences(ObjectFactoryBuilder.class.getName(),
null);
+            if (refs != null) {
+                Arrays.sort(refs, ContextHelper.SERVICE_REFERENCE_COMPARATOR);
+                for (ServiceReference ref : refs) {
+                    ObjectFactoryBuilder builder = (ObjectFactoryBuilder) context.getService(ref);
+                    try {
+                        factory = builder.createObjectFactory(obj, environment);
+                    } catch (NamingException e) {
+                        // TODO: log it
+                    } finally {
+                        context.ungetService(ref);
+                    }
+                    if (factory != null) {
+                        break;
+                    }
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            // should not happen
+            throw new RuntimeException("Invalid filter", e);
+        }
+
+        Object result = null;
+        
+        if (factory != null) {
+            result = factory.getObjectInstance(obj, name, nameCtx, environment);
+        }
+        
+        return (result == null) ? obj : result;
+    }
+
+}
\ No newline at end of file

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ObjectFactoryHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java?rev=943955&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
(added)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
Thu May 13 17:50:55 2010
@@ -0,0 +1,59 @@
+/*
+ * 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;
+
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.directory.Attributes;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.service.jndi.JNDIProviderAdmin;
+
+public class ProviderAdminService implements JNDIProviderAdmin {
+
+    private DirObjectFactoryHelper helper;
+    
+    public ProviderAdminService(BundleContext context) {
+        helper = new DirObjectFactoryHelper();
+        helper.setBundleContext(context);
+    }
+    
+    public Object getObjectInstance(Object obj, 
+                                    Name name, 
+                                    Context context, 
+                                    Map environment)
+        throws Exception {
+        Hashtable env = ContextHelper.toHashtable(environment);
+        return helper.getObjectInstance(obj, name, context, env);
+    }
+
+    public Object getObjectInstance(Object obj,
+                                    Name name,
+                                    Context context,
+                                    Map environment,
+                                    Attributes attributes) 
+        throws Exception {
+        Hashtable env = ContextHelper.toHashtable(environment);
+        return helper.getObjectInstance(obj, name, context, env, attributes);
+    }
+
+}

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/ProviderAdminService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java?rev=943955&r1=943954&r2=943955&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
(original)
+++ incubator/aries/trunk/jndi/jndi-core/src/main/java/org/apache/aries/jndi/startup/Activator.java
Thu May 13 17:50:55 2010
@@ -29,12 +29,14 @@ import org.osgi.framework.BundleActivato
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.jndi.JNDIContextManager;
+import org.osgi.service.jndi.JNDIProviderAdmin;
 
 import org.apache.aries.jndi.ContextHelper;
 import org.apache.aries.jndi.ContextManagerServiceFactory;
 import org.apache.aries.jndi.JREInitialContextFactoryBuilder;
 import org.apache.aries.jndi.OSGiInitialContextFactoryBuilder;
 import org.apache.aries.jndi.OSGiObjectFactoryBuilder;
+import org.apache.aries.jndi.ProviderAdminService;
 
 /**
  * The activator for this bundle makes sure the static classes in it are
@@ -46,6 +48,10 @@ public class Activator implements Bundle
     
     public void start(BundleContext context) {
   
+        registrations.add(context.registerService(JNDIProviderAdmin.class.getName(), 
+                                                  new ProviderAdminService(context), 
+                                                  null));
+        
         registrations.add(context.registerService(InitialContextFactoryBuilder.class.getName(),

                                                  new JREInitialContextFactoryBuilder(), 
                                                  null));

Modified: incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java?rev=943955&r1=943954&r2=943955&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
(original)
+++ incubator/aries/trunk/jndi/jndi-core/src/test/java/org/apache/aries/jndi/ObjectFactoryTest.java
Thu May 13 17:50:55 2010
@@ -64,10 +64,7 @@ public class ObjectFactoryTest
     Field f = ContextHelper.class.getDeclaredField("context");
     f.setAccessible(true);
     f.set(null, bc);
-    f = OSGiObjectFactoryBuilder.class.getDeclaredField("context");
-    f.setAccessible(true);
-    f.set(null, bc);
-
+    OSGiObjectFactoryBuilder.setBundleContext(bc);
   }
 
   /**

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=943955&r1=943954&r2=943955&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
Thu May 13 17:50:55 2010
@@ -86,10 +86,7 @@ public class ServiceRegistryContextTest
     Field f = ContextHelper.class.getDeclaredField("context");
     f.setAccessible(true);
     f.set(null, bc);
-    f = OSGiObjectFactoryBuilder.class.getDeclaredField("context");
-    f.setAccessible(true);
-    f.set(null, bc);
-
+    OSGiObjectFactoryBuilder.setBundleContext(bc);
 
     service = Skeleton.newMock(Runnable.class);
     



Mime
View raw message