git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@422698 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.mosgi.jmx.registry/pom.xml b/org.apache.felix.mosgi.jmx.registry/pom.xml
new file mode 100644
index 0000000..c0363b6
--- /dev/null
+++ b/org.apache.felix.mosgi.jmx.registry/pom.xml
@@ -0,0 +1,58 @@
+<project>
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>0.8.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>osgi-bundle</packaging>
+  <name>MOSGi JMX registry</name>
+  <artifactId>org.apache.felix.mosgi.jmx.registry</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.framework</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix.plugins</groupId>
+        <artifactId>maven-osgi-plugin</artifactId>
+        <version>${pom.version}</version>
+        <extensions>true</extensions>
+        <configuration>
+          <osgiManifest>
+            <bundleName>MOSGi JMX rmiregistry</bundleName>
+            <bundleDescription>MOSGi JMX rmiregistry</bundleDescription>
+            <bundleActivator>auto-detect</bundleActivator>
+            <bundleDocUrl>http://oscar-osgi.sf.net/obr2/${pom.artifactId}/</bundleDocUrl>
+            <bundleUrl>http://oscar-osgi.sf.net/obr2/${pom.artifactId}/${pom.artifactId}-${pom.version}.jar</bundleUrl>
+            <bundleSource>http://oscar-osgi.sf.net/obr2/${pom.artifactId}/${pom.artifactId}-${pom.version}-src.jar</bundleSource>
+            <bundleSymbolicName>${pom.artifactId}</bundleSymbolicName>
+            <exportPackage>
+      ${pom.artifactId}.mx4j.tools.naming;specification-version="1.0.0"
+            </exportPackage>
+	    <dynamicImportPackage>
+	      *
+	    </dynamicImportPackage>
+          </osgiManifest>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingService.java b/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingService.java
new file mode 100644
index 0000000..d3e985a
--- /dev/null
+++ b/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingService.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) MX4J.
+ * All rights reserved.
+ *
+ * This software is distributed under the terms of the MX4J License version 1.0.
+ * See the terms of the MX4J License in the documentation provided with this software.
+ */
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.felix.mosgi.jmx.registry.mx4j.tools.naming;
+
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.rmi.server.UnicastRemoteObject;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+import javax.management.ObjectName;
+
+import org.osgi.service.log.LogService;
+
+import org.apache.felix.framework.cache.BundleCache;
+
+public class NamingService implements BundleActivator,NamingServiceIfc {
+  private String version=null;
+  private ObjectName namingServiceName=null;
+
+  private ServiceRegistration sReg=null;
+  private Registry m_registry=null;
+  private BundleContext bc=null;
+
+  public void start(BundleContext bc) throws Exception {
+    this.version=(String)bc.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
+    this.bc=bc;
+    String profile=bc.getProperty(BundleCache.CACHE_PROFILE_PROP);
+    if (profile==null){
+      profile=System.getProperty(BundleCache.CACHE_PROFILE_PROP);
+    }
+    String rmiPortS=bc.getProperty("insa.jmxconsole.rmiport."+profile);
+    int rmiPort=1099;
+    if (rmiPortS!=null){
+      rmiPort=Integer.parseInt(rmiPortS);
+    }
+    try {
+      this.log(LogService.LOG_INFO, "Running rmiregistry on "+rmiPort,null);
+      Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); //!! Absolutely nececary for RMIClassLoading to work
+      m_registry=LocateRegistry.createRegistry(rmiPort);
+      //java.rmi.server.RemoteServer.setLog(System.out);
+    } catch (Exception e) {
+      this.bc=null;
+      throw new BundleException("Impossible to start rmiregistry");
+    }
+    sReg=bc.registerService(NamingServiceIfc.class.getName(), this, null);
+    this.log(LogService.LOG_INFO, "RMI Registry started "+version,null);
+  }
+
+  public void stop(BundleContext bc) throws Exception {
+    this.log(LogService.LOG_INFO, "Stopping RMI Registry "+version,null);
+    UnicastRemoteObject.unexportObject(m_registry, true);
+    this. m_registry = null;
+    sReg.unregister();    
+    this.sReg=null;
+    this.log(LogService.LOG_INFO, "RMI Stopped "+version,null);
+    this.bc=null;
+  }
+
+  private void log(int prio, String message, Throwable t){
+    if (this.bc!=null){
+      ServiceReference logSR=this.bc.getServiceReference(LogService.class.getName());
+      if (logSR!=null){
+        ((LogService)this.bc.getService(logSR)).log(prio, message, t);
+      }else{
+        System.out.println("No Log Service");
+      }
+    }else{
+      System.out.println(NamingService.class.getName()+": No bundleContext");
+    }
+  }
+}
diff --git a/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingServiceIfc.java b/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingServiceIfc.java
new file mode 100644
index 0000000..0edac27
--- /dev/null
+++ b/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingServiceIfc.java
@@ -0,0 +1,19 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.felix.mosgi.jmx.registry.mx4j.tools.naming;
+
+public interface NamingServiceIfc{}
diff --git a/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingServiceMBean.java b/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingServiceMBean.java
new file mode 100644
index 0000000..1603caa
--- /dev/null
+++ b/org.apache.felix.mosgi.jmx.registry/src/main/java/org/apache/felix/mosgi/jmx/registry/mx4j/tools/naming/NamingServiceMBean.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) MX4J.
+ * All rights reserved.
+ *
+ * This software is distributed under the terms of the MX4J License version 1.0.
+ * See the terms of the MX4J License in the documentation provided with this software.*
+ *
+ *
+ */
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.felix.mosgi.jmx.registry.mx4j.tools.naming;
+
+/**
+ * Management interface for the NamingService MBean.
+ * @author <a href="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
+ * @version $Revision: 1.3 $
+ */
+public interface NamingServiceMBean
+{
+   /**
+    * Sets the port on which rmiregistry listens for incoming connections.
+    * @see #getPort
+    */
+   public void setPort(int port);
+
+   /**
+    * Returns the port on which rmiregistry listens for incoming connections
+    * @see #setPort
+    */
+   public int getPort();
+
+   /**
+    * Returns whether this MBean has been started and not yet stopped.
+    * @see #start
+    */
+   public boolean isRunning();
+
+}