Move ServiceMix Kernel trunk into Felix

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@768912 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/management/src/main/java/org/apache/servicemix/management/JaasAuthenticator.java b/karaf/management/src/main/java/org/apache/servicemix/management/JaasAuthenticator.java
new file mode 100644
index 0000000..1353d3f
--- /dev/null
+++ b/karaf/management/src/main/java/org/apache/servicemix/management/JaasAuthenticator.java
@@ -0,0 +1,72 @@
+/*
+ * 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.management;
+
+import java.io.IOException;
+
+import javax.management.remote.JMXAuthenticator;
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+
+public class JaasAuthenticator implements JMXAuthenticator {
+
+    private String realm;
+
+    public String getRealm() {
+        return realm;
+    }
+
+    public void setRealm(String realm) {
+        this.realm = realm;
+    }
+
+    public Subject authenticate(Object credentials) throws SecurityException {
+        if (!(credentials instanceof String[])) {
+            throw new IllegalArgumentException("Expected String[2], got "
+                            + (credentials != null ? credentials.getClass().getName() : null));
+        }
+        final String[] params = (String[]) credentials;
+        if (params.length != 2) {
+            throw new IllegalArgumentException("Expected String[2] but length was " + params.length);
+        }
+        try {
+            LoginContext loginContext = new LoginContext(realm, new CallbackHandler() {
+                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+                    for (int i = 0; i < callbacks.length; i++) {
+                        if (callbacks[i] instanceof NameCallback) {
+                            ((NameCallback) callbacks[i]).setName(params[0]);
+                        } else if (callbacks[i] instanceof PasswordCallback) {
+                            ((PasswordCallback) callbacks[i]).setPassword((params[1].toCharArray()));
+                        } else {
+                            throw new UnsupportedCallbackException(callbacks[i]);
+                        }
+                    }
+                }
+            });
+            loginContext.login();
+            return loginContext.getSubject();
+        } catch (LoginException e) {
+            throw new SecurityException("Authentication failed", e);
+        }
+    }
+}
diff --git a/karaf/management/src/main/java/org/apache/servicemix/management/RmiRegistryFactoryBean.java b/karaf/management/src/main/java/org/apache/servicemix/management/RmiRegistryFactoryBean.java
new file mode 100644
index 0000000..1a5a995
--- /dev/null
+++ b/karaf/management/src/main/java/org/apache/servicemix/management/RmiRegistryFactoryBean.java
@@ -0,0 +1,118 @@
+/*

+ * 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.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 {

+

+    private int port = Registry.REGISTRY_PORT;

+    private Registry registry;

+    private boolean locate;

+    private boolean create = true;

+    private boolean locallyCreated;

+    

+    /**

+     * @return the create

+     */

+    public boolean isCreate() {

+        return create;

+    }

+

+    /**

+     * @param create the create to set

+     */

+    public void setCreate(boolean create) {

+        this.create = create;

+    }

+

+    /**

+     * @return the locate

+     */

+    public boolean isLocate() {

+        return locate;

+    }

+

+    /**

+     * @param locate the locate to set

+     */

+    public void setLocate(boolean locate) {

+        this.locate = locate;

+    }

+

+    /**

+     * @return the port

+     */

+    public int getPort() {

+        return port;

+    }

+

+    /**

+     * @param port the port to set

+     */

+    public void setPort(int port) {

+        this.port = port;

+    }

+

+    public Object getObject() throws Exception {

+        return registry;

+    }

+

+    public Class getObjectType() {

+        return Registry.class;

+    }

+

+    public boolean isSingleton() {

+        return true;

+    }

+

+    public void afterPropertiesSet() throws RemoteException {

+        if (registry == null && locate) {

+            try {

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

+                reg.list();

+                registry = reg;

+            } catch (RemoteException e) {

+                // ignore

+            }

+        }

+        if (registry == null && create) {

+            registry = LocateRegistry.createRegistry(getPort());

+            locallyCreated = true;

+        }

+    }

+

+    public void destroy() throws RemoteException {

+        if (registry != null && locallyCreated) {

+            Registry reg = registry;

+            registry = null;

+            UnicastRemoteObject.unexportObject(reg, true);

+        }

+    }

+

+}

diff --git a/karaf/management/src/main/resources/META-INF/spring/servicemix-management.xml b/karaf/management/src/main/resources/META-INF/spring/servicemix-management.xml
new file mode 100644
index 0000000..6a9bcc3
--- /dev/null
+++ b/karaf/management/src/main/resources/META-INF/spring/servicemix-management.xml
@@ -0,0 +1,90 @@
+<?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:ctx="http://www.springframework.org/schema/context"

+       xmlns:osgi="http://www.springframework.org/schema/osgi"

+       xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"

+       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/context

+  http://www.springframework.org/schema/context/spring-context.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

+  http://www.springframework.org/schema/osgi-compendium

+  http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">

+

+    <!-- MBeanServer bean -->

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

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

+    </bean>

+

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

+    <osgi:service ref="mbeanServer">

+        <osgi:interfaces>

+            <value>javax.management.MBeanServer</value>

+        </osgi:interfaces>

+    </osgi:service>

+

+    <!-- Create a RMI registry -->

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

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

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

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

+    </bean>

+

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

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

+        <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" />

+    </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">

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

+    </bean>

+

+    <!-- Property place holder -->

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

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

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

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

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

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

+        <prop key="objectName">connector:name=rmi</prop>

+    </osgix:cm-properties>

+

+    <ctx:property-placeholder properties-ref="cmProps" />

+

+</beans>

+

diff --git a/karaf/management/src/test/configs/factories/management.properties b/karaf/management/src/test/configs/factories/management.properties
new file mode 100644
index 0000000..10c22db
--- /dev/null
+++ b/karaf/management/src/test/configs/factories/management.properties
@@ -0,0 +1,26 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+service.pid=org.apache.servicemix.management.JmxConnectorServiceFactory
+instances=1
+keys=objectName,serviceUrl,threaded,daemon,
+objectName.1=connector:name=rmi
+serviceUrl.1=service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
+threaded.1=true
+daemon.1=true
diff --git a/karaf/management/src/test/configs/services/org.ops4j.pax.logging.properties b/karaf/management/src/test/configs/services/org.ops4j.pax.logging.properties
new file mode 100644
index 0000000..23075bf
--- /dev/null
+++ b/karaf/management/src/test/configs/services/org.ops4j.pax.logging.properties
@@ -0,0 +1,34 @@
+# 
+# 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.
+#
+#
+log4j.rootLogger=DEBUG, out
+
+log4j.logger.org.springframework=DEBUG
+
+# CONSOLE appender not used by default
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n
+
+# File appender
+log4j.appender.out=org.apache.log4j.FileAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n
+log4j.appender.out.file=target/gshell.log
+log4j.appender.out.append=true