FELIX-1098: Remove some spring dependencies

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@771206 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/management/pom.xml b/karaf/management/pom.xml
index dcdfbf4..dfc5997 100644
--- a/karaf/management/pom.xml
+++ b/karaf/management/pom.xml
@@ -35,14 +35,6 @@
 
     <dependencies>
         <dependency>
-            <groupId>org.springframework.osgi</groupId>
-            <artifactId>spring-osgi-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-context</artifactId>
-        </dependency>
-        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.osgi.core</artifactId>
             <scope>provided</scope>
@@ -74,10 +66,9 @@
                         <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                         <Import-Package>
                             javax.management,
-                            org.springframework.jmx.support,
                             *
                         </Import-Package>
-                        <Export-Package>org.apache.servicemix.management*</Export-Package>
+                        <Export-Package>org.apache.felix.karaf.management*</Export-Package>
                         <Spring-Context>*;publish-context:=false</Spring-Context>
                     </instructions>
                 </configuration>
diff --git a/karaf/management/src/main/java/org/apache/felix/karaf/management/ConnectorServerFactory.java b/karaf/management/src/main/java/org/apache/felix/karaf/management/ConnectorServerFactory.java
new file mode 100644
index 0000000..4ab9f39
--- /dev/null
+++ b/karaf/management/src/main/java/org/apache/felix/karaf/management/ConnectorServerFactory.java
@@ -0,0 +1,138 @@
+/*
+ * 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.felix.karaf.management;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnectorServer;
+import javax.management.remote.JMXConnectorServerFactory;
+import javax.management.remote.JMXServiceURL;
+
+public class ConnectorServerFactory {
+
+    private MBeanServer server;
+    private String serviceUrl;
+    private Map environment;
+    private ObjectName objectName;
+    private boolean threaded = false;
+    private boolean daemon = false;
+    private JMXConnectorServer connectorServer;
+
+    public MBeanServer getServer() {
+        return server;
+    }
+
+    public void setServer(MBeanServer server) {
+        this.server = server;
+    }
+
+    public String getServiceUrl() {
+        return serviceUrl;
+    }
+
+    public void setServiceUrl(String serviceUrl) {
+        this.serviceUrl = serviceUrl;
+    }
+
+    public Map getEnvironment() {
+        return environment;
+    }
+
+    public void setEnvironment(Map environment) {
+        this.environment = environment;
+    }
+
+    public ObjectName getObjectName() {
+        return objectName;
+    }
+
+    public void setObjectName(ObjectName objectName) {
+        this.objectName = objectName;
+    }
+
+    public boolean isThreaded() {
+        return threaded;
+    }
+
+    public void setThreaded(boolean threaded) {
+        this.threaded = threaded;
+    }
+
+    public boolean isDaemon() {
+        return daemon;
+    }
+
+    public void setDaemon(boolean daemon) {
+        this.daemon = daemon;
+    }
+
+    public void init() throws Exception {
+        if (this.server == null) {
+            throw new IllegalArgumentException("server must be set");
+        }
+        JMXServiceURL url = new JMXServiceURL(this.serviceUrl);
+        this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, this.server);
+        if (this.objectName != null) {
+            this.server.registerMBean(this.connectorServer, this.objectName);
+        }
+        try {
+            if (this.threaded) {
+                Thread connectorThread = new Thread() {
+                    public void run() {
+                        try {
+                            connectorServer.start();
+                        } catch (IOException ex) {
+                            throw new RuntimeException("Could not start JMX connector server", ex);
+                        }
+                    }
+                };
+                connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]");
+                connectorThread.setDaemon(this.daemon);
+                connectorThread.start();
+            }
+            else {
+                this.connectorServer.start();
+            }
+        } catch (Exception ex) {
+            doUnregister(this.objectName);
+            throw ex;
+        }
+    }
+
+    public void destroy() throws Exception {
+        try {
+            this.connectorServer.stop();
+        } finally {
+            doUnregister(this.objectName);
+        }
+    }
+
+    protected void doUnregister(ObjectName objectName) {
+        try {
+            if (this.objectName != null && this.server.isRegistered(objectName)) {
+                this.server.unregisterMBean(objectName);
+            }
+        }
+        catch (JMException ex) {
+            // Ignore
+        }
+    }
+}
diff --git a/karaf/management/src/main/java/org/apache/servicemix/management/JaasAuthenticator.java b/karaf/management/src/main/java/org/apache/felix/karaf/management/JaasAuthenticator.java
similarity index 98%
rename from karaf/management/src/main/java/org/apache/servicemix/management/JaasAuthenticator.java
rename to karaf/management/src/main/java/org/apache/felix/karaf/management/JaasAuthenticator.java
index 1353d3f..a3897ed 100644
--- a/karaf/management/src/main/java/org/apache/servicemix/management/JaasAuthenticator.java
+++ b/karaf/management/src/main/java/org/apache/felix/karaf/management/JaasAuthenticator.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicemix.management;
+package org.apache.felix.karaf.management;
 
 import java.io.IOException;
 
diff --git a/karaf/management/src/main/java/org/apache/felix/karaf/management/MBeanServerFactory.java b/karaf/management/src/main/java/org/apache/felix/karaf/management/MBeanServerFactory.java
new file mode 100644
index 0000000..6bbc780
--- /dev/null
+++ b/karaf/management/src/main/java/org/apache/felix/karaf/management/MBeanServerFactory.java
@@ -0,0 +1,116 @@
+/*
+ * 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.felix.karaf.management;
+
+import java.lang.management.ManagementFactory;
+import java.util.List;
+
+import javax.management.MBeanServer;
+
+public class MBeanServerFactory {
+
+    private boolean locateExistingServerIfPossible = false;
+    private String agentId;
+    private String defaultDomain;
+    private boolean registerWithFactory = true;
+    private MBeanServer server;
+    private boolean newlyRegistered = false;
+
+    public boolean isLocateExistingServerIfPossible() {
+        return locateExistingServerIfPossible;
+    }
+
+    public void setLocateExistingServerIfPossible(boolean locateExistingServerIfPossible) {
+        this.locateExistingServerIfPossible = locateExistingServerIfPossible;
+    }
+
+    public String getAgentId() {
+        return agentId;
+    }
+
+    public void setAgentId(String agentId) {
+        this.agentId = agentId;
+    }
+
+    public String getDefaultDomain() {
+        return defaultDomain;
+    }
+
+    public void setDefaultDomain(String defaultDomain) {
+        this.defaultDomain = defaultDomain;
+    }
+
+    public boolean isRegisterWithFactory() {
+        return registerWithFactory;
+    }
+
+    public void setRegisterWithFactory(boolean registerWithFactory) {
+        this.registerWithFactory = registerWithFactory;
+    }
+
+    public boolean isNewlyRegistered() {
+        return newlyRegistered;
+    }
+
+    public void setNewlyRegistered(boolean newlyRegistered) {
+        this.newlyRegistered = newlyRegistered;
+    }
+
+    public MBeanServer getServer() throws Exception {
+        if (this.server == null) {
+            init();
+        }
+        return server;
+    }
+
+    public void init() throws Exception {
+        if (this.locateExistingServerIfPossible || this.agentId != null) {
+            try {
+                List servers = javax.management.MBeanServerFactory.findMBeanServer(agentId);
+                MBeanServer server = null;
+                if (servers != null && servers.size() > 0) {
+                    this.server = (MBeanServer) servers.get(0);
+                }
+                if (server == null && agentId == null) {
+                    this.server = ManagementFactory.getPlatformMBeanServer();
+                }
+                if (this.server == null) {
+                    throw new Exception("Unable to locate MBeanServer");
+                }
+            } catch (Exception ex) {
+                if (this.agentId != null) {
+                    throw ex;
+                }
+            }
+        }
+        if (this.server == null) {
+            if (this.registerWithFactory) {
+                this.server = javax.management.MBeanServerFactory.createMBeanServer(this.defaultDomain);
+            } else {
+                this.server = javax.management.MBeanServerFactory.newMBeanServer(this.defaultDomain);
+            }
+            this.newlyRegistered = this.registerWithFactory;
+        }
+    }
+
+    public void destroy() throws Exception {
+        if (this.newlyRegistered) {
+            javax.management.MBeanServerFactory.releaseMBeanServer(this.server);
+        }
+    }
+
+}
diff --git a/karaf/management/src/main/java/org/apache/servicemix/management/RmiRegistryFactoryBean.java b/karaf/management/src/main/java/org/apache/felix/karaf/management/RmiRegistryFactory.java
similarity index 83%
rename from karaf/management/src/main/java/org/apache/servicemix/management/RmiRegistryFactoryBean.java
rename to karaf/management/src/main/java/org/apache/felix/karaf/management/RmiRegistryFactory.java
index 1a5a995..640789a 100644
--- a/karaf/management/src/main/java/org/apache/servicemix/management/RmiRegistryFactoryBean.java
+++ b/karaf/management/src/main/java/org/apache/felix/karaf/management/RmiRegistryFactory.java
@@ -14,22 +14,18 @@
  * See the License for the specific language governing permissions and

  * limitations under the License.

  */

-package org.apache.servicemix.management;

+package org.apache.felix.karaf.management;

 

 import java.rmi.RemoteException;

 import java.rmi.registry.LocateRegistry;

 import java.rmi.registry.Registry;

 import java.rmi.server.UnicastRemoteObject;

 

-import org.springframework.beans.factory.DisposableBean;

-import org.springframework.beans.factory.FactoryBean;

-import org.springframework.beans.factory.InitializingBean;

-

 /**

  * 

  * @author gnodet

  */

-public class RmiRegistryFactoryBean implements FactoryBean, InitializingBean, DisposableBean {

+public class RmiRegistryFactory {

 

     private int port = Registry.REGISTRY_PORT;

     private Registry registry;

@@ -83,15 +79,7 @@
         return registry;

     }

 

-    public Class getObjectType() {

-        return Registry.class;

-    }

-

-    public boolean isSingleton() {

-        return true;

-    }

-

-    public void afterPropertiesSet() throws RemoteException {

+    public void init() throws RemoteException {

         if (registry == null && locate) {

             try {

                 Registry reg = LocateRegistry.getRegistry(getPort());

diff --git a/karaf/management/src/main/resources/META-INF/spring/servicemix-management.xml b/karaf/management/src/main/resources/META-INF/spring/karaf-management.xml
similarity index 73%
rename from karaf/management/src/main/resources/META-INF/spring/servicemix-management.xml
rename to karaf/management/src/main/resources/META-INF/spring/karaf-management.xml
index 6a9bcc3..5ccd002 100644
--- a/karaf/management/src/main/resources/META-INF/spring/servicemix-management.xml
+++ b/karaf/management/src/main/resources/META-INF/spring/karaf-management.xml
@@ -36,9 +36,11 @@
   http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">

 

     <!-- MBeanServer bean -->

-    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">

+    <bean id="mbeanServerFactory" class="org.apache.felix.karaf.management.MBeanServerFactory" init-method="init" destroy-method="destroy">

         <property name="locateExistingServerIfPossible" value="true"/>

     </bean>

+    <bean id="mbeanServer" factory-bean="mbeanServerFactory" factory-method="getServer">

+    </bean>

 

     <!-- Export the MBeanServer as an OSGi service -->

     <osgi:service ref="mbeanServer">

@@ -48,36 +50,40 @@
     </osgi:service>

 

     <!-- Create a RMI registry -->

-    <bean id="rmiRegistry" class="org.apache.servicemix.management.RmiRegistryFactoryBean">

+    <bean id="rmiRegistryFactory" class="org.apache.felix.karaf.management.RmiRegistryFactory" init-method="init" destroy-method="destroy">

         <property name="create" value="true" />

         <property name="locate" value="true" />

         <property name="port" value="${rmiRegistryPort}" />

     </bean>

+    <bean id="rmiRegistry" factory-bean="rmiRegistryFactory" factory-method="getObject"/>

 

     <!-- Create a JMX connector ServiceFactory -->

-    <bean id="jmxConnectorService" class="org.springframework.jmx.support.ConnectorServerFactoryBean">

+    <bean id="jmxConnectorServiceFactory" class="org.apache.felix.karaf.management.ConnectorServerFactory" init-method="init" destroy-method="destroy">

         <property name="server" ref="mbeanServer" />

         <property name="serviceUrl" value="${serviceUrl}" />

         <property name="daemon" value="${daemon}" />

         <property name="threaded" value="${threaded}" />

-        <property name="objectName" value="${objectName}" />

-        <property name="environment" ref="jmxConnectorEnvironment" />

+        <property name="objectName">

+            <bean class="javax.management.ObjectName" factory-method="getInstance">

+                <constructor-arg value="${objectName}"/>

+            </bean>

+        </property>

+        <property name="environment">

+            <map>

+                <entry key="jmx.remote.authenticator" value-ref="jaasAuthenticator" />

+            </map>

+        </property>

     </bean>

 

-    <!-- Environment map for connectors -->

-    <util:map id="jmxConnectorEnvironment">

-        <entry key="jmx.remote.authenticator" value-ref="jaasAuthenticator" />

-    </util:map>

-

     <!-- JAAS authenticator -->

-    <bean id="jaasAuthenticator" class="org.apache.servicemix.management.JaasAuthenticator">

+    <bean id="jaasAuthenticator" class="org.apache.felix.karaf.management.JaasAuthenticator">

         <property name="realm" value="${jmxRealm}" />

     </bean>

 

     <!-- Property place holder -->

-    <osgix:cm-properties id="cmProps" persistent-id="org.apache.servicemix.management">

+    <osgix:cm-properties id="cmProps" persistent-id="org.apache.felix.karaf.management">

         <prop key="rmiRegistryPort">1099</prop>

-        <prop key="jmxRealm">servicemix</prop>

+        <prop key="jmxRealm">karaf</prop>

         <prop key="serviceUrl">service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi</prop>

         <prop key="daemon">true</prop>

         <prop key="threaded">true</prop>

diff --git a/karaf/management/src/test/configs/factories/management.properties b/karaf/management/src/test/configs/factories/management.properties
index 10c22db..3887ba8 100644
--- a/karaf/management/src/test/configs/factories/management.properties
+++ b/karaf/management/src/test/configs/factories/management.properties
@@ -17,7 +17,7 @@
 #
 ################################################################################
 
-service.pid=org.apache.servicemix.management.JmxConnectorServiceFactory
+service.pid=org.apache.felix.karaf.management.JmxConnectorServiceFactory
 instances=1
 keys=objectName,serviceUrl,threaded,daemon,
 objectName.1=connector:name=rmi