incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mnutt...@apache.org
Subject svn commit: r1043401 - in /incubator/aries/trunk: jndi/jndi-url/ jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ testsupport/testsupport-unit/src/main/java/org/apache/aries/mocks/
Date Wed, 08 Dec 2010 13:10:06 GMT
Author: mnuttall
Date: Wed Dec  8 13:10:06 2010
New Revision: 1043401

URL: http://svn.apache.org/viewvc?rev=1043401&view=rev
Log:
ARIES-512: Initial code drop

Added:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/AbstractName.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintName.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintNameParser.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContext.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextFactory.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextServiceFactory.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/BlueprintURLContextTest.java
Modified:
    incubator/aries/trunk/jndi/jndi-url/pom.xml
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/OsgiName.java
    incubator/aries/trunk/testsupport/testsupport-unit/src/main/java/org/apache/aries/mocks/BundleMock.java

Modified: incubator/aries/trunk/jndi/jndi-url/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/pom.xml?rev=1043401&r1=1043400&r2=1043401&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/pom.xml (original)
+++ incubator/aries/trunk/jndi/jndi-url/pom.xml Wed Dec  8 13:10:06 2010
@@ -41,6 +41,10 @@
         <aries.osgi.activator>
             org.apache.aries.jndi.url.Activator
         </aries.osgi.activator>
+        <aries.osgi.import.pkg>
+          org.osgi.service.blueprint.container;resolution:=optional,
+          *
+        </aries.osgi.import.pkg>
     </properties>
 
     <dependencies>
@@ -63,6 +67,11 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.aries.jndi</groupId>
             <artifactId>org.apache.aries.jndi.core</artifactId>
             <scope>test</scope>
@@ -72,11 +81,7 @@
             <artifactId>org.apache.aries.testsupport.unit</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.compendium</artifactId>
-            <scope>test</scope>
-        </dependency>
+
     </dependencies>
 
 </project>

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/AbstractName.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/AbstractName.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/AbstractName.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/AbstractName.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,94 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.naming.CompositeName;
+
+@SuppressWarnings("serial")
+public abstract class AbstractName extends CompositeName 
+{
+  public AbstractName (Enumeration<String> e) { 
+    super(e);
+  }
+
+  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 String getScheme()
+  {
+    String part0 = get(0);
+    int index = part0.indexOf(':');
+    
+    String result;
+    
+    if (index > 0) {
+      result = part0.substring(0, index);
+    } else {
+      result = null;
+    }
+    
+    return result;
+  }
+  
+  public String getSchemePath()
+  {
+    String part0 = get(0);
+    int index = part0.indexOf(':');
+    
+    String result;
+    
+    if (index > 0) {
+      result = part0.substring(index + 1);
+    } else {
+      result = null;
+    }
+    
+    return result;
+  }
+}

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java?rev=1043401&r1=1043400&r2=1043401&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
Wed Dec  8 13:10:06 2010
@@ -33,9 +33,11 @@ import org.osgi.service.jndi.JNDIConstan
 public class Activator implements BundleActivator, SingleServiceListener 
 {
     private BundleContext ctx;
-    private volatile ServiceRegistration reg;
+    private volatile ServiceRegistration osgiUrlReg = null;
+    private volatile ServiceRegistration blueprintUrlReg = null;
     private static SingleServiceTracker<ProxyManager> proxyManager;
 
+    @Override
     public void start(BundleContext context) 
     {
         ctx = context;
@@ -43,24 +45,44 @@ public class Activator implements Bundle
         proxyManager.open();
     }
 
-    public void stop(BundleContext context) 
-    {
-        proxyManager.close();
-        if (reg != null) reg.unregister();
+    @Override
+    public void stop(BundleContext context) {
+      proxyManager.close();
+      if (osgiUrlReg != null) osgiUrlReg.unregister();
+      if (blueprintUrlReg != null) blueprintUrlReg.unregister();
     }
+  
 
+  @Override
     public void serviceFound() 
     {
-      Hashtable<Object, Object> props = new Hashtable<Object, Object>();
-      props.put(JNDIConstants.JNDI_URLSCHEME, new String[] { "osgi", "aries" });
-      reg = ctx.registerService(ObjectFactory.class.getName(), new OsgiURLContextServiceFactory(),
props);
-    }
+    Hashtable<Object, Object> osgiUrlprops = new Hashtable<Object, Object>();
+    osgiUrlprops.put(JNDIConstants.JNDI_URLSCHEME, new String[] { "osgi", "aries" });
+    osgiUrlReg = ctx.registerService(ObjectFactory.class.getName(),
+        new OsgiURLContextServiceFactory(), osgiUrlprops);
 
-    public void serviceLost() 
-    {
-      if (reg != null) reg.unregister();
-      reg = null;
+    // Blueprint URL scheme requires access to the BlueprintContainer service.
+    // We have an optional import
+    // on org.osgi.service.blueprint.container: only register the blueprint:comp/URL
+    // scheme if it's present
+    try {
+      ctx.getBundle().loadClass("org.osgi.service.blueprint.container.BlueprintContainer");
+      Hashtable<Object, Object> blueprintURlSchemeProps = new Hashtable<Object,
Object>();
+      blueprintURlSchemeProps.put(JNDIConstants.JNDI_URLSCHEME, new String[] { "blueprint"
});
+      blueprintUrlReg = ctx.registerService(ObjectFactory.class.getName(),
+          new BlueprintURLContextServiceFactory(), blueprintURlSchemeProps);
+    } catch (ClassNotFoundException cnfe) {
+      // The blueprint packages aren't available, so do nothing. That's fine.
     }
+  }
+
+  public void serviceLost() 
+  {
+    if (osgiUrlReg != null) osgiUrlReg.unregister();
+    osgiUrlReg = null;
+    if (blueprintUrlReg != null) blueprintUrlReg.unregister();
+    blueprintUrlReg = null;
+  }
 
     public void serviceReplaced() 
     {

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintName.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintName.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintName.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintName.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,50 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import javax.naming.InvalidNameException;
+import javax.naming.Name;
+
+public class BlueprintName extends AbstractName 
+{
+  /**
+   * 
+   */
+  private static final long serialVersionUID = 7460901600614300179L;
+
+  public BlueprintName(String name) throws InvalidNameException
+  {
+    super(split(name));
+  }
+
+  public BlueprintName(Name name) throws InvalidNameException
+  {
+    this(name.toString());
+  }
+
+  public String getComponentId()
+  {
+    return get(1);
+  }
+  
+  public boolean hasComponent() { 
+    return size() > 1;
+  }
+}

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintNameParser.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintNameParser.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintNameParser.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintNameParser.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,58 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import javax.naming.InvalidNameException;
+import javax.naming.Name;
+import javax.naming.NameParser;
+import javax.naming.NamingException;
+
+/**
+ * A parser for the aries namespace
+ */
+public final class BlueprintNameParser implements NameParser
+{
+  private static final String BLUEPRINT_SCHEME = "blueprint";
+  private static final String COMP_PATH = "comp";
+  
+  @Override
+  public Name parse(String name) throws NamingException
+  {
+    BlueprintName result = new BlueprintName(name);
+    String urlScheme = result.getScheme();
+    String schemePath = result.getSchemePath();
+    
+    if (!BLUEPRINT_SCHEME.equals(urlScheme) || !COMP_PATH.equals(schemePath)) {
+      throw new InvalidNameException(name);
+    }
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object other)
+  {
+    return other instanceof OsgiNameParser;
+  }
+  
+  @Override
+  public int hashCode()
+  {
+    return 100004;
+  }
+}
\ No newline at end of file

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContext.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContext.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContext.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContext.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,399 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameParser;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.OperationNotSupportedException;
+import javax.naming.ServiceUnavailableException;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.container.NoSuchComponentException;
+
+public class BlueprintURLContext implements Context {
+  private static final String BLUEPRINT_NAMESPACE = "blueprint:comp/";
+  private Bundle _callersBundle;
+  private Map<String, Object> _env;
+  private NameParser _parser = new BlueprintNameParser();
+  private BlueprintContainer _blueprintContainer;
+  private BlueprintName _parentName;
+  
+  // listBindings wants a NamingEnumeration<Binding>
+  // list wants a NamingEnumeration<NameClassPair>
+  // Both are very similar. As per ServiceRegistryListContext we delegate to a closure to
do the final processing
+  
+  private interface ComponentProcessor<T> { 
+    T get (Binding b);
+  }
+
+  private static class BlueprintComponentNamingEnumeration<T> implements NamingEnumeration<T>
+  {
+    private Binding[] blueprintIdToComponentBindings;
+    private int position = 0;
+    private ComponentProcessor<T> processor;
+    
+    public BlueprintComponentNamingEnumeration (BlueprintContainer bpc, ComponentProcessor<T>
p) 
+    { 
+      @SuppressWarnings("unchecked")
+      Set<String> componentIds = bpc.getComponentIds();
+      blueprintIdToComponentBindings = new Binding[componentIds.size()];
+      Iterator<String> idIterator= componentIds.iterator();
+      for (int i=0; i < blueprintIdToComponentBindings.length; i++) { 
+        String id = idIterator.next();
+        Object o = bpc.getComponentInstance(id);
+        blueprintIdToComponentBindings[i] = new Binding (id, o);
+      }
+      processor = p;
+    }
+    
+
+    @Override
+    public boolean hasMoreElements()
+    {
+      return position < blueprintIdToComponentBindings.length;
+    }
+
+    @Override
+    public T nextElement()
+    {
+      if (!hasMoreElements()) throw new NoSuchElementException();
+      Binding bindingToProcess = blueprintIdToComponentBindings[position];
+      position++;
+      T result = processor.get(bindingToProcess);
+      return result;
+    }
+
+    @Override
+    public T next() throws NamingException
+    {
+      return nextElement();
+    }
+
+    @Override
+    public boolean hasMore() throws NamingException
+    {
+      return hasMoreElements();
+    }
+
+    @Override
+    public void close() throws NamingException
+    {
+      // Nothing to do
+    }
+    
+  }
+  
+  @SuppressWarnings("unchecked")
+  public BlueprintURLContext(Bundle callersBundle, Hashtable<?, ?> env) throws ServiceUnavailableException

+  {
+    _callersBundle = callersBundle;
+    _env = new HashMap<String, Object>();
+    _env.putAll((Map<? extends String, ? extends Object>) env);
+    _parentName = null;
+    ServiceReference bpContainerRef = getBlueprintContainerRef(_callersBundle);
+    if (bpContainerRef != null) { 
+      _blueprintContainer = (BlueprintContainer) _callersBundle.getBundleContext().getService(bpContainerRef);
+    } else { 
+      throw new ServiceUnavailableException ();
+    }
+  }
+  
+  public BlueprintURLContext (Bundle callersBundle, BlueprintName parentName, Map<String,
Object> env, 
+      BlueprintContainer bpc) { 
+    _callersBundle = callersBundle;
+    _parentName = parentName;
+    _env = env;
+    _blueprintContainer = bpc;
+    
+  }
+
+  @Override
+  public Object addToEnvironment(String propName, Object propVal)
+      throws NamingException
+  {
+    return _env.put(propName, propVal);
+  }
+
+  @Override
+  public void bind(Name n, Object o) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void bind(String s, Object o) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void close() throws NamingException
+  {
+    _env = null;
+  }
+
+  @Override
+  public Name composeName(Name name, Name prefix) throws NamingException
+  {
+    String result = prefix + "/" + name;
+    String ns = BLUEPRINT_NAMESPACE;
+    if (result.startsWith(ns)) {
+      ns = "";
+    }
+    return _parser.parse(ns + result);
+  }
+
+  @Override
+  public String composeName(String name, String prefix) throws NamingException
+  {
+    String result = prefix + "/" + name;
+    String ns = BLUEPRINT_NAMESPACE;
+    if (result.startsWith(ns)) {
+      ns = "";
+    }
+    _parser.parse(ns + result);
+    return result;
+  }
+
+  @Override
+  public Context createSubcontext(Name n) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public Context createSubcontext(String s) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void destroySubcontext(Name n) throws NamingException
+  {
+    // No-op we don't support sub-contexts in our context
+  }
+
+  @Override
+  public void destroySubcontext(String s) throws NamingException
+  {
+    // No-op we don't support sub-contexts in our context
+  }
+
+  @Override
+  public Hashtable<?, ?> getEnvironment() throws NamingException
+  {
+    Hashtable<Object, Object> environment = new Hashtable<Object, Object>();
+    environment.putAll(_env);
+    return environment;
+  }
+
+  @Override
+  public String getNameInNamespace() throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public NameParser getNameParser(Name n) throws NamingException
+  {
+    return _parser;
+  }
+
+  @Override
+  public NameParser getNameParser(String s) throws NamingException
+  {
+    return _parser;
+  }
+
+  @Override
+  public NamingEnumeration<NameClassPair> list(Name name) throws NamingException
+  {
+    return list(name.toString());
+  }
+
+  @Override
+  public NamingEnumeration<NameClassPair> list(String s) throws NamingException
+  {
+    NamingEnumeration<NameClassPair> result = new BlueprintComponentNamingEnumeration<NameClassPair>(_blueprintContainer,
new ComponentProcessor<NameClassPair>() {
+      @Override
+      public NameClassPair get(Binding b)
+      {
+        NameClassPair result = new NameClassPair (b.getName(), b.getClassName());
+        return result;
+      } 
+    });
+    return result;
+  }
+
+  @Override
+  public NamingEnumeration<Binding> listBindings(Name name) throws NamingException
+  {
+    return listBindings(name.toString());
+  }
+
+  @Override
+  public NamingEnumeration<Binding> listBindings(String name)
+      throws NamingException
+  {
+    NamingEnumeration<Binding> result = new BlueprintComponentNamingEnumeration<Binding>(_blueprintContainer,
new ComponentProcessor<Binding>() {
+      @Override
+      public Binding get(Binding b)
+      {
+        return b;
+      } 
+    });
+    return result;
+  }
+
+  @Override
+  public Object lookup(Name name) throws NamingException
+  {
+    BlueprintName bpName;
+    if (name instanceof BlueprintName) { 
+      bpName = (BlueprintName) name; 
+    } else if (_parentName != null) { 
+      bpName = new BlueprintName (_parentName.toString() + "/" + name.toString());
+    } else { 
+      bpName = (BlueprintName) _parser.parse(name.toString());
+    }
+
+    Object result;
+    if (bpName.hasComponent()) { 
+      String componentId = bpName.getComponentId();
+      ServiceReference bpContainerRef = getBlueprintContainerRef(_callersBundle);
+      BlueprintContainer bpc; 
+      if (bpContainerRef != null) { 
+        bpc = (BlueprintContainer) _callersBundle.getBundleContext().getService(bpContainerRef);
+      } else { 
+        throw new NamingException();
+      }
+      
+      try { 
+        result = bpc.getComponentInstance(componentId);
+      } catch (NoSuchComponentException nsce) { 
+        throw new NamingException (nsce.getMessage());
+      } finally {
+        _callersBundle.getBundleContext().ungetService(bpContainerRef);
+     } 
+   } else { 
+     result = new BlueprintURLContext (_callersBundle, bpName, _env, _blueprintContainer);
+   }
+   return result;
+  }
+
+  @Override
+  public Object lookup(String name) throws NamingException
+  {
+    if (_parentName != null) {
+      name = _parentName.toString() + "/" + name;
+    }
+    Object result = lookup (_parser.parse(name));
+    return result;
+  }
+
+  @Override
+  public Object lookupLink(Name n) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public Object lookupLink(String s) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void rebind(Name n, Object o) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void rebind(String s, Object o) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public Object removeFromEnvironment(String propName) throws NamingException
+  {
+    return _env.remove(propName);
+  }
+
+  @Override
+  public void rename(Name nOld, Name nNew) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void rename(String sOld, String sNew) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void unbind(Name n) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+
+  @Override
+  public void unbind(String s) throws NamingException
+  {
+    throw new OperationNotSupportedException();
+  }
+  
+  private ServiceReference getBlueprintContainerRef(Bundle b)
+  {
+    ServiceReference[] refs = b.getRegisteredServices();
+    ServiceReference result = null;
+    outer: for (ServiceReference r : refs) { 
+      String[] objectClasses = (String[]) r.getProperty(Constants.OBJECTCLASS);
+      for (String objectClass : objectClasses) { 
+        if (objectClass.equals(BlueprintContainer.class.getName())) { 
+          // Arguably we could put an r.isAssignableTo(jndi-url-bundle, BlueprintContainer.class.getName())

+          // check here. But if you've got multiple, class-space inconsistent instances of
blueprint in 
+          // your environment, you've almost certainly got other problems. 
+          result = r;
+          break outer;
+        }
+      }
+    }
+    return result;
+  }
+
+}

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextFactory.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextFactory.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextFactory.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,57 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.spi.ObjectFactory;
+
+import org.osgi.framework.Bundle;
+
+public class BlueprintURLContextFactory implements ObjectFactory {
+
+  final private Bundle _callersBundle;
+
+  public BlueprintURLContextFactory(Bundle callersBundle) {
+    _callersBundle = callersBundle;
+  }
+
+  @Override
+  public Object getObjectInstance(Object obj, Name name, Context callersCtx, Hashtable<?,
?> envmt) throws Exception {
+    Object result = null;
+    if (obj == null) {
+      result = new BlueprintURLContext(_callersBundle, envmt);
+    } else if (obj instanceof String) {
+      Context ctx = null;
+      try {
+        ctx = new BlueprintURLContext(_callersBundle, envmt);
+        result = ctx.lookup((String) obj);
+      } finally {
+        if (ctx != null) {
+          ctx.close();
+        }
+      }
+    } 
+    return result;
+  }
+
+}

Added: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextServiceFactory.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextServiceFactory.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextServiceFactory.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/BlueprintURLContextServiceFactory.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,42 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+
+
+/**
+ * A factory for the aries blueprint context
+ */
+public class BlueprintURLContextServiceFactory implements ServiceFactory 
+{
+
+  @Override
+  public Object getService(Bundle bundle, ServiceRegistration registration) {
+    return new BlueprintURLContextFactory(bundle);
+  }
+
+  @Override
+  public void ungetService(Bundle bundle, ServiceRegistration registration, Object service)

+  {
+  }
+}

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=1043401&r1=1043400&r2=1043401&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
Wed Dec  8 13:10:06 2010
@@ -18,12 +18,8 @@
  */
 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;
 import javax.naming.Name;
 
@@ -35,7 +31,7 @@ import javax.naming.Name;
  * component 1: interface
  * component 2: filter
  */
-public final class OsgiName extends CompositeName
+public final class OsgiName extends AbstractName
 {
   /** The serial version UID */
   private static final long serialVersionUID = 6617580228852444656L;
@@ -56,33 +52,6 @@ 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;
@@ -93,38 +62,6 @@ public final class OsgiName extends Comp
     return size() > 3;
   }
   
-  public String getScheme()
-  {
-    String part0 = get(0);
-    int index = part0.indexOf(':');
-    
-    String result;
-    
-    if (index > 0) {
-      result = part0.substring(0, index);
-    } else {
-      result = null;
-    }
-    
-    return result;
-  }
-  
-  public String getSchemePath()
-  {
-    String part0 = get(0);
-    int index = part0.indexOf(':');
-    
-    String result;
-    
-    if (index > 0) {
-      result = part0.substring(index + 1);
-    } else {
-      result = null;
-    }
-    
-    return result;
-  }
-  
   public String getInterface()
   {
     return get(1);

Added: incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/BlueprintURLContextTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/BlueprintURLContextTest.java?rev=1043401&view=auto
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/BlueprintURLContextTest.java
(added)
+++ incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/BlueprintURLContextTest.java
Wed Dec  8 13:10:06 2010
@@ -0,0 +1,92 @@
+/*
+ * 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 WARRANTIESOR 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.url;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Hashtable;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import org.apache.aries.mocks.BundleMock;
+import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+
+public class BlueprintURLContextTest {
+
+  private static Bundle bundle;
+  
+  static class SimpleComponent { 
+    String id;
+    public SimpleComponent (String i) { 
+      id = i;
+    }
+    public String getIdMessage () { 
+      return id + "_message";
+    }
+  }
+  
+  
+  @BeforeClass
+  public static void setup() { 
+    bundle = Skeleton.newMock(new BundleMock("aBundle", new Hashtable<String, String>()),
Bundle.class);
+    BundleContext bc = bundle.getBundleContext();
+    new org.apache.aries.jndi.startup.Activator().start(bc);
+    new Activator().start(bc);
+    
+    // Register a BlueprintContainer mock that will answer getComponentInstance(String id)
calls
+    SimpleComponent comp1 = new SimpleComponent ("comp1");
+    BlueprintContainer bpc = Skeleton.newMock(BlueprintContainer.class);
+    Skeleton.getSkeleton(bpc).setReturnValue(new MethodCall(BlueprintContainer.class, "getComponentInstance",
String.class), comp1);
+    bc.registerService("org.osgi.service.blueprint.container.BlueprintContainer", bpc, new
Hashtable<String, String>());
+    
+  }
+  
+  @Test
+  public void simpleComponentLookupTest () throws Exception { 
+    BlueprintURLContext bpURLc = new BlueprintURLContext (bundle, new Hashtable<String,
String>());
+    SimpleComponent sc = (SimpleComponent) bpURLc.lookup("blueprint:comp/comp1");
+    assertNotNull (sc);
+    String msg = sc.getIdMessage();
+    assertEquals ("comp1 message wrong", "comp1_message", msg);
+  }
+  
+  @Test
+  public void twoLevelComponentLookupTest() throws Exception { 
+    BundleMock mock = new BundleMock("bundle.for.new.initial.context", new Properties());
+    Thread.currentThread().setContextClassLoader(mock.getClassLoader());
+    
+    InitialContext ctx = new InitialContext();
+    Context ctx2 = (Context) ctx.lookup("blueprint:comp");
+    SimpleComponent sc = (SimpleComponent) ctx2.lookup("comp1"); 
+    assertNotNull (sc);
+    String msg = sc.getIdMessage();
+    assertEquals ("comp1 message wrong", "comp1_message", msg);
+  }
+  
+}

Modified: incubator/aries/trunk/testsupport/testsupport-unit/src/main/java/org/apache/aries/mocks/BundleMock.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/testsupport/testsupport-unit/src/main/java/org/apache/aries/mocks/BundleMock.java?rev=1043401&r1=1043400&r2=1043401&view=diff
==============================================================================
--- incubator/aries/trunk/testsupport/testsupport-unit/src/main/java/org/apache/aries/mocks/BundleMock.java
(original)
+++ incubator/aries/trunk/testsupport/testsupport-unit/src/main/java/org/apache/aries/mocks/BundleMock.java
Wed Dec  8 13:10:06 2010
@@ -36,17 +36,18 @@ import java.util.regex.Pattern;
 
 import junit.framework.AssertionFailedError;
 
+import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.MethodCallHandler;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.apache.aries.unittest.mocks.annotations.Singleton;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleReference;
 import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
 
-import org.apache.aries.unittest.mocks.MethodCall;
-import org.apache.aries.unittest.mocks.MethodCallHandler;
-import org.apache.aries.unittest.mocks.Skeleton;
-import org.apache.aries.unittest.mocks.annotations.Singleton;
-
 @Singleton
 public class BundleMock
 {
@@ -388,4 +389,16 @@ public class BundleMock
   {
     return cl;
   }
+  
+  // This is good enough for Mocks' needs in unit test, but isn't how it works in the real
world!
+  public ServiceReference[] getRegisteredServices() 
+  {
+    ServiceReference[] result = null;
+    try { 
+      result = bc.getServiceReferences(null, null);
+    } catch (InvalidSyntaxException isx) { 
+      // no-op: Swallow exception
+    }
+    return result;
+  }
 }
\ No newline at end of file



Mime
View raw message