Author: gnodet
Date: Wed Feb 18 09:19:44 2009
New Revision: 745449

URL: http://svn.apache.org/viewvc?rev=745449&view=rev
Log:
SMX4NMR-94: JNDI integration

Added:
    servicemix/smx4/nmr/trunk/naming/   (with props)
    servicemix/smx4/nmr/trunk/naming/pom.xml
    servicemix/smx4/nmr/trunk/naming/src/
    servicemix/smx4/nmr/trunk/naming/src/main/
    servicemix/smx4/nmr/trunk/naming/src/main/java/
    servicemix/smx4/nmr/trunk/naming/src/main/java/org/
    servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/
    servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/
    servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/
    
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/GlobalInitialContextFactoryBuilder.java
    
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/InitialContextFactoryWrapper.java
    
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiContext.java
    
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiInitialContextFactoryBuilder.java
    servicemix/smx4/nmr/trunk/naming/src/main/resources/
    servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/
    servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/spring/
    
servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/spring/servicemix-naming.xml
Modified:
    servicemix/smx4/nmr/trunk/assembly/pom.xml
    servicemix/smx4/nmr/trunk/assembly/src/main/filtered-resources/features.xml
    servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/InstallationContextImpl.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
    
servicemix/smx4/nmr/trunk/jbi/osgi/src/main/resources/META-INF/spring/servicemix-jbi.xml
    
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/EnvironmentImpl.java
    servicemix/smx4/nmr/trunk/pom.xml

Modified: servicemix/smx4/nmr/trunk/assembly/pom.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/assembly/pom.xml?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/assembly/pom.xml (original)
+++ servicemix/smx4/nmr/trunk/assembly/pom.xml Wed Feb 18 09:19:44 2009
@@ -48,6 +48,10 @@
             <artifactId>org.apache.servicemix.document</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.servicemix.naming</groupId>
+            <artifactId>org.apache.servicemix.naming</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.apache.servicemix.nmr</groupId>
             <artifactId>org.apache.servicemix.nmr.api</artifactId>
         </dependency>

Modified: 
servicemix/smx4/nmr/trunk/assembly/src/main/filtered-resources/features.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/assembly/src/main/filtered-resources/features.xml?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/assembly/src/main/filtered-resources/features.xml 
(original)
+++ servicemix/smx4/nmr/trunk/assembly/src/main/filtered-resources/features.xml 
Wed Feb 18 09:19:44 2009
@@ -32,10 +32,12 @@
         
<bundle>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.javamail-api-1.4/${servicemix.specs.version}</bundle>
         
<bundle>mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/${geronimo.jta.version}</bundle>
         
<bundle>mvn:org.apache.xbean/xbean-classloader/${xbean.version}</bundle>
+        <bundle>mvn:org.apache.xbean/xbean-naming/${xbean.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.woodstox/${woodstox.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.wsdl4j/${wsdl4j.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.ant/${ant.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.jbi-api-1.0/${servicemix.specs.version}</bundle>
+        
<bundle>mvn:org.apache.servicemix.naming/org.apache.servicemix.naming/${pom.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.jbi/org.apache.servicemix.jbi.runtime/${pom.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.jbi/org.apache.servicemix.jbi.osgi/${pom.version}</bundle>
         
<bundle>mvn:org.apache.servicemix.jbi/org.apache.servicemix.jbi.deployer/${pom.version}</bundle>

Modified: servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml (original)
+++ servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml Wed Feb 18 09:19:44 2009
@@ -77,6 +77,7 @@
                 <configuration>
                     <instructions>
                         
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+                        <Import-Package>javax.transaction,*</Import-Package>
                         
<Export-Package>org.apache.servicemix.jbi.deployer*,org.apache.servicemix.jbi.management*</Export-Package>
                         
<Spring-Context>*;publish-context:=false;create-asynchronously:=false</Spring-Context>
                     </instructions>

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/InstallationContextImpl.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/InstallationContextImpl.java?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/InstallationContextImpl.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/InstallationContextImpl.java
 Wed Feb 18 09:19:44 2009
@@ -66,6 +66,7 @@
                                    NamingStrategy namingStrategy,
                                    ManagementAgent managementAgent) {
         this.descriptor = descriptor;
+        this.environment = environment;
         this.namingStrategy = namingStrategy;
         this.managementAgent = managementAgent;
         if (descriptor.getComponentClassPath() != null

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
 Wed Feb 18 09:19:44 2009
@@ -99,7 +99,7 @@
     <bean id="environment" 
class="org.apache.servicemix.jbi.runtime.impl.EnvironmentImpl">
         <property name="transactionManagers" ref="transactionManagers" />
         <property name="mbeanServer" ref="mbeanServer" />
-        <property name="namingContexts" ref="namingContexts" />
+        <property name="namingContext" ref="namingContext" />
     </bean>
 
     <!-- Reference to the Transaction Manager -->
@@ -107,10 +107,10 @@
                interface="javax.transaction.TransactionManager"
                cardinality="0..N" />
 
-    <!-- Reference to the InitialContext -->
-    <osgi:list id="namingContexts"
-               interface="javax.naming.InitialContext"
-               cardinality="0..N" />
+    <!-- The InitialContext -->
+    <bean id="namingContext" class="javax.naming.InitialContext">
+        <constructor-arg><null/></constructor-arg>
+    </bean>
 
     <!-- MBeanServer -->
     <osgi:reference id="mbeanServer"

Modified: 
servicemix/smx4/nmr/trunk/jbi/osgi/src/main/resources/META-INF/spring/servicemix-jbi.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/osgi/src/main/resources/META-INF/spring/servicemix-jbi.xml?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/osgi/src/main/resources/META-INF/spring/servicemix-jbi.xml
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/osgi/src/main/resources/META-INF/spring/servicemix-jbi.xml
 Wed Feb 18 09:19:44 2009
@@ -47,7 +47,7 @@
     <bean id="environment" 
class="org.apache.servicemix.jbi.runtime.impl.EnvironmentImpl">
         <property name="transactionManagers" ref="transactionManagers" />
         <property name="mbeanServers" ref="mbeanServers" />
-        <property name="namingContexts" ref="namingContexts" />
+        <property name="namingContext" ref="namingContext" />
     </bean>
 
     <bean id="nmrWrapper" class="org.apache.servicemix.jbi.osgi.NMRWrapper">
@@ -79,10 +79,10 @@
                interface="javax.management.MBeanServer"
                cardinality="0..N" />
 
-    <!-- Reference to the InitialContext -->
-    <osgi:list id="namingContexts"
-               interface="javax.naming.InitialContext"
-               cardinality="0..N" />
+    <!-- The InitialContext -->
+    <bean id="namingContext" class="javax.naming.InitialContext">
+        <constructor-arg><null/></constructor-arg>
+    </bean>
 
     <!-- Expose the JBI components registry in OSGi -->
     <osgi:service ref="componentRegistry">

Modified: 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/EnvironmentImpl.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/EnvironmentImpl.java?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/EnvironmentImpl.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/EnvironmentImpl.java
 Wed Feb 18 09:19:44 2009
@@ -44,7 +44,7 @@
         return null;
     }
 
-    public void setMBeanServer(MBeanServer mbeanServer) {
+    public void setMbeanServer(MBeanServer mbeanServer) {
         this.mbeanServer = mbeanServer;
     }
 

Propchange: servicemix/smx4/nmr/trunk/naming/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Feb 18 09:19:44 2009
@@ -0,0 +1,10 @@
+bin
+eclipse-classes
+target
+*.iml
+*.ipr
+*.iws
+.classpath
+.project
+.settings
+data

Added: servicemix/smx4/nmr/trunk/naming/pom.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/naming/pom.xml?rev=745449&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/naming/pom.xml (added)
+++ servicemix/smx4/nmr/trunk/naming/pom.xml Wed Feb 18 09:19:44 2009
@@ -0,0 +1,101 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+
+    <!--
+
+        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.
+    -->
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.servicemix.nmr</groupId>
+        <artifactId>nmr-parent</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.servicemix.naming</groupId>
+    <artifactId>org.apache.servicemix.naming</artifactId>
+    <packaging>bundle</packaging>
+    <name>Apache ServiceMix Naming</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>commons-logging</groupId>
+            <artifactId>commons-logging</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-beans</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.osgi</groupId>
+            <artifactId>spring-osgi-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.xbean</groupId>
+            <artifactId>xbean-naming</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.servicemix.kernel.testing</groupId>
+            
<artifactId>org.apache.servicemix.kernel.testing.support</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+                        <Export-Package>${pom.artifactId}*</Export-Package>
+                        <DynamicImport-Package>*</DynamicImport-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+            <!-- generate dependencies versions -->
+            <plugin>
+                <groupId>org.apache.servicemix.tooling</groupId>
+                <artifactId>depends-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>generate-depends-file</id>
+                        <goals>
+                            <goal>generate-depends-file</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
\ No newline at end of file

Added: 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/GlobalInitialContextFactoryBuilder.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/GlobalInitialContextFactoryBuilder.java?rev=745449&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/GlobalInitialContextFactoryBuilder.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/GlobalInitialContextFactoryBuilder.java
 Wed Feb 18 09:19:44 2009
@@ -0,0 +1,34 @@
+package org.apache.servicemix.naming;
+
+import java.util.Hashtable;
+
+import javax.naming.spi.InitialContextFactoryBuilder;
+import javax.naming.spi.InitialContextFactory;
+import javax.naming.NamingException;
+import javax.naming.Context;
+
+import org.springframework.beans.factory.InitializingBean;
+import org.apache.xbean.naming.global.GlobalContextManager;
+
+/**
+ */
+public class GlobalInitialContextFactoryBuilder implements 
InitialContextFactoryBuilder, InitializingBean {
+
+    private Context globalContext;
+
+    public Context getGlobalContext() {
+        return globalContext;
+    }
+
+    public void setGlobalContext(Context globalContext) {
+        this.globalContext = globalContext;
+    }
+
+    public void afterPropertiesSet() throws Exception {
+        GlobalContextManager.setGlobalContext(globalContext);
+    }
+
+    public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> 
environment) throws NamingException {
+        return new GlobalContextManager();
+    }
+}

Added: 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/InitialContextFactoryWrapper.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/InitialContextFactoryWrapper.java?rev=745449&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/InitialContextFactoryWrapper.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/InitialContextFactoryWrapper.java
 Wed Feb 18 09:19:44 2009
@@ -0,0 +1,66 @@
+/*
+ * 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.servicemix.naming;
+
+import java.util.Hashtable;
+
+import javax.naming.spi.InitialContextFactory;
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import org.apache.xbean.naming.context.ContextFlyweight;
+
+/**
+ */
+public class InitialContextFactoryWrapper implements InitialContextFactory {
+
+    private final InitialContextFactory delegate;
+    private final Context osgiContext;
+
+    public InitialContextFactoryWrapper(InitialContextFactory delegate, 
Context osgiContext) {
+        this.delegate = delegate;
+        this.osgiContext = osgiContext;
+    }
+
+    public Context getInitialContext(Hashtable<?, ?> environment) throws 
NamingException {
+        return new ContextWrapper(delegate.getInitialContext(environment));
+    }
+
+    public class ContextWrapper extends ContextFlyweight {
+
+        private final Context delegate;
+
+        public ContextWrapper(Context delegate) {
+            this.delegate = delegate;
+        }
+
+        protected Context getContext() throws NamingException {
+            return delegate;
+        }
+
+        public Object lookup(String name) throws NamingException {
+            if (name.length() == 0) {
+                return this;
+            }
+            if (name.startsWith("osgi:")) {
+                return osgiContext.lookup(name);
+            }
+            return delegate.lookup(name);
+        }
+
+    }
+}

Added: 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiContext.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiContext.java?rev=745449&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiContext.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiContext.java
 Wed Feb 18 09:19:44 2009
@@ -0,0 +1,104 @@
+/*
+ * 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.servicemix.naming;
+
+import java.util.Collections;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.naming.NameNotFoundException;
+
+import org.apache.xbean.naming.context.ImmutableContext;
+import 
org.springframework.osgi.service.importer.support.OsgiServiceProxyFactoryBean;
+import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.util.ClassUtils;
+import org.osgi.framework.BundleContext;
+
+/**
+ *
+ */
+public class OSGiContext extends ImmutableContext implements 
BundleContextAware {
+
+    private final Context services = new OSGiServicesContext();
+    private BundleContext bundleContext;
+
+    public OSGiContext() throws NamingException {
+        super(Collections.EMPTY_MAP);
+    }
+
+    public BundleContext getBundleContext() {
+        return bundleContext;
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+    @Override
+    public Object lookup(String name) throws NamingException {
+        if (name.startsWith("osgi:")) {
+            name = name.substring(5);
+            if (name.length() == 0) {
+                return this;
+            }
+            if ("services".equals(name)) {
+                return services;
+            } else if (name.startsWith("services/")) {
+                return services.lookup(name.substring(9));
+            } else if ("/services".equals(name)) {
+                return services;
+            } else if (name.startsWith("/services/")) {
+                return services.lookup(name.substring(10));
+            } else {
+                 throw new NameNotFoundException("Unrecognized name, does not 
start with expected 'services': " + name);
+             }
+        }
+        return super.lookup(name);
+    }
+
+    public class OSGiServicesContext extends ImmutableContext {
+
+        public OSGiServicesContext() throws NamingException {
+            super(Collections.EMPTY_MAP);
+        }
+
+        @Override
+        public Object lookup(String name) throws NamingException {
+            String[] parts = name.split("/");
+            if (parts.length == 0 || parts.length > 2) {
+                throw new NameNotFoundException("Unrecognized name, should be 
osgi:services:<interface>[/<filter>]");
+            }
+            String className = parts[0];
+            String filter = parts.length == 2 ? parts[1] : null;
+            try {
+                ClassLoader classLoader = getClass().getClassLoader();
+                OsgiServiceProxyFactoryBean factory = new 
OsgiServiceProxyFactoryBean();
+                factory.setBundleContext(getBundleContext());
+                factory.setBeanClassLoader(classLoader);
+                factory.setFilter(filter);
+                factory.setInterfaces(new Class[] { 
ClassUtils.resolveClassName(className, classLoader) });
+                factory.afterPropertiesSet();
+                return factory.getObject();
+            } catch 
(org.springframework.osgi.service.ServiceUnavailableException e) {
+                NameNotFoundException ex = new 
NameNotFoundException(e.getMessage());
+                ex.initCause(e);
+                throw ex;
+            }
+        }
+
+    }
+}

Added: 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiInitialContextFactoryBuilder.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiInitialContextFactoryBuilder.java?rev=745449&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiInitialContextFactoryBuilder.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/naming/src/main/java/org/apache/servicemix/naming/OSGiInitialContextFactoryBuilder.java
 Wed Feb 18 09:19:44 2009
@@ -0,0 +1,124 @@
+/*
+ * 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.servicemix.naming;
+
+import java.util.Hashtable;
+import java.lang.reflect.Field;
+
+import javax.naming.spi.InitialContextFactoryBuilder;
+import javax.naming.spi.InitialContextFactory;
+import javax.naming.spi.NamingManager;
+import javax.naming.NamingException;
+import javax.naming.Context;
+import javax.naming.NoInitialContextException;
+
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.framework.BundleContext;
+import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.util.Assert;
+
+/**
+ */
+public class OSGiInitialContextFactoryBuilder implements 
InitialContextFactoryBuilder, BundleContextAware,
+                                                         InitializingBean, 
DisposableBean {
+
+    private BundleContext bundleContext;
+    private Context osgiContext;
+    private ServiceTracker tracker;
+
+    public OSGiInitialContextFactoryBuilder() {
+    }
+
+    public BundleContext getBundleContext() {
+        return bundleContext;
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+    public Context getOsgiContext() {
+        return osgiContext;
+    }
+
+    public void setOsgiContext(Context osgiContext) {
+        this.osgiContext = osgiContext;
+    }
+
+    public void afterPropertiesSet() throws NamingException {
+        Assert.notNull(bundleContext, "Required 'bundleContext' property was 
not set.");
+        Assert.notNull(osgiContext, "Required 'osgiContext' property was not 
set.");
+        tracker = new ServiceTracker(bundleContext, 
InitialContextFactoryBuilder.class.getName(), null);
+        tracker.open();
+
+        NamingManager.setInitialContextFactoryBuilder(this);
+    }
+
+    public void destroy() {
+        // Close the tracker
+        tracker.close();
+        tracker = null;
+
+        // Try to reset the InitialContextFactoryBuilder on the NamingManager
+        // As there is no public API to do so, we try using reflection.
+        // The following code will try to nullify all static fields of type
+        // InitialContextFactoryBuilder on the NamingManager class.
+        try {
+            for (Field field : NamingManager.class.getDeclaredFields()) {
+                if 
(InitialContextFactoryBuilder.class.equals(field.getType())) {
+                    field.setAccessible(true);
+                    field.set(null, null);
+                }
+            }
+        } catch (Throwable t) {
+            // Ignore
+        }
+    }
+
+    public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> 
env) throws NamingException {
+        if (tracker == null) {
+            throw new IllegalStateException("OSGiInitialContextFactoryBuilder 
is not initialized");
+        }
+        InitialContextFactory factory = null;
+        InitialContextFactoryBuilder factoryBuilder = 
(InitialContextFactoryBuilder) tracker.getService();
+        if (factoryBuilder != null) {
+            factory = factoryBuilder.createInitialContextFactory(env);
+        }
+        if (factory == null && env != null) {
+            String className = env != null ? (String) 
env.get(Context.INITIAL_CONTEXT_FACTORY) : null;
+            try {
+                factory = (InitialContextFactory) 
Class.forName(className).newInstance();
+            } catch (Exception e) {
+                NoInitialContextException ne = new 
NoInitialContextException("Cannot instantiate class: " + className);
+                ne.setRootCause(e);
+                throw ne;
+            }
+        }
+        if (factory == null) {
+            NoInitialContextException ne = new NoInitialContextException(
+                "Need to specify class name in environment or system " +
+                "property, or as an applet parameter, or in an " +
+                "application resource file:  " +
+                Context.INITIAL_CONTEXT_FACTORY);
+            throw ne;
+        }
+        return new InitialContextFactoryWrapper(factory, osgiContext);
+    }
+
+}

Added: 
servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/spring/servicemix-naming.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/spring/servicemix-naming.xml?rev=745449&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/spring/servicemix-naming.xml
 (added)
+++ 
servicemix/smx4/nmr/trunk/naming/src/main/resources/META-INF/spring/servicemix-naming.xml
 Wed Feb 18 09:19:44 2009
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns:osgi="http://www.springframework.org/schema/osgi";
+       xmlns:util="http://www.springframework.org/schema/util";
+       xsi:schemaLocation="
+  http://www.springframework.org/schema/beans
+  http://www.springframework.org/schema/beans/spring-beans.xsd
+  http://www.springframework.org/schema/util
+  http://www.springframework.org/schema/util/spring-util.xsd
+  http://www.springframework.org/schema/osgi
+  http://www.springframework.org/schema/osgi/spring-osgi.xsd";>
+
+    <bean id="osgiContext" class="org.apache.servicemix.naming.OSGiContext" />
+
+    <bean id="osgiInitialContextFactoryBuilder" 
class="org.apache.servicemix.naming.OSGiInitialContextFactoryBuilder">
+        <property name="osgiContext" ref="osgiContext" />
+    </bean>
+
+    <bean id="icfb" 
class="org.apache.servicemix.naming.GlobalInitialContextFactoryBuilder">
+        <property name="globalContext" ref="globalContext" />
+    </bean>
+
+    <bean id="globalContext" 
class="org.apache.xbean.naming.context.WritableContext" />
+
+    <osgi:service ref="icfb" 
interface="javax.naming.spi.InitialContextFactoryBuilder" ranking="-1" />
+
+</beans>

Modified: servicemix/smx4/nmr/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/pom.xml?rev=745449&r1=745448&r2=745449&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/pom.xml (original)
+++ servicemix/smx4/nmr/trunk/pom.xml Wed Feb 18 09:19:44 2009
@@ -36,6 +36,7 @@
       <module>branding</module>
       <module>nmr</module>
       <module>document</module>
+      <module>naming</module>
       <module>jbi</module>
       <module>bundle</module>
       <module>assembly</module>
@@ -269,6 +270,11 @@
           <artifactId>org.apache.servicemix.document</artifactId>
           <version>${pom.version}</version>
       </dependency>
+        <dependency>
+            <groupId>org.apache.servicemix.naming</groupId>
+            <artifactId>org.apache.servicemix.naming</artifactId>
+            <version>${pom.version}</version>
+        </dependency>
       <dependency>
           <groupId>org.apache.servicemix.nmr</groupId>
           <artifactId>org.apache.servicemix.nmr.api</artifactId>
@@ -390,6 +396,11 @@
           <artifactId>org.apache.felix.framework</artifactId>
           <version>${felix.framework.version}</version>
       </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <version>${felix.compendium.version}</version>
+        </dependency>
       <dependency>
           <groupId>org.apache.servicemix.specs</groupId>
           
<artifactId>org.apache.servicemix.specs.activation-api-1.1</artifactId>


Reply via email to