<?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.

-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
           xmlns:jaas="http://karaf.apache.org/xmlns/jaas/v1.0.0">

    <!-- Allow the use of system properties -->
    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]">
        <ext:default-properties>
            <ext:property name="karaf.admin.role" value="admin" />
        </ext:default-properties>
    </ext:property-placeholder>

    <!-- Property place holder -->
    <cm:property-placeholder persistent-id="org.apache.karaf.management" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="rmiRegistryHost" value="0.0.0.0"/>
            <cm:property name="rmiRegistryPort" value="1099"/>
            <cm:property name="rmiServerPort" value="44444"/>
            <cm:property name="jmxRealm" value="karaf"/>
            <cm:property name="jmxRole" value="$[karaf.admin.role]"/>
            <cm:property name="serviceUrl" value="service:jmx:rmi://localhost:44444/jndi/rmi://localhost:1099/karaf-$[karaf.name]"/>
            <cm:property name="daemon" value="true"/>
            <cm:property name="threaded" value="true"/>
            <cm:property name="objectName" value="connector:name=rmi"/>
            <!-- SSL support -->
            <cm:property name="keyStoreAvailabilityTimeout" value="5000"/>
            <cm:property name="authenticatorType" value="password"/>
            <cm:property name="secured" value="false"/>
            <cm:property name="secureAlgorithm" value="default"/>
            <cm:property name="secureProtocol" value="TLS"/>
            <cm:property name="keyStore" value="karaf.ks"/>
            <cm:property name="keyAlias" value="karaf"/>
            <cm:property name="trustStore" value="karaf.ts"/>
            <cm:property name="clientAuth" value="false"/>
        </cm:default-properties>
    </cm:property-placeholder>

    <!-- MBeanServer bean -->
    <bean id="mbeanServerFactory" class="org.apache.karaf.management.MBeanServerFactory" init-method="init"
          destroy-method="destroy" depends-on="rmiRegistryFactory">
        <property name="locateExistingServerIfPossible" value="true"/>
    </bean>
    <bean id="mbeanServer" factory-ref="mbeanServerFactory" factory-method="getServer"/>

    <!-- Export the MBeanServer as an OSGi service -->
    <service ref="mbeanServer" interface="javax.management.MBeanServer"/>

    <!-- Create a RMI registry -->
    <bean id="rmiRegistryFactory" class="org.apache.karaf.management.RmiRegistryFactory" init-method="init"
          destroy-method="destroy">
        <property name="create" value="true"/>
        <property name="locate" value="true"/>
        <property name="host" value="${rmiRegistryHost}"/>
        <property name="port" value="${rmiRegistryPort}"/>
    </bean>

    <!-- Create a JMX connector ServiceFactory -->
    <reference id="keystoreManager" interface="org.apache.karaf.jaas.config.KeystoreManager"/>

    <bean id="connectorFactory" class="org.apache.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">
            <map>
                <entry key="jmx.remote.authenticator" value-ref="jaasAuthenticator"/>
            </map>
        </property>
        <property name="keyStoreAvailabilityTimeout" value="${keyStoreAvailabilityTimeout}"/>
        <property name="authenticatorType" value="${authenticatorType}"/>
        <property name="secured" value="${secured}"/>
        <property name="algorithm" value="${secureAlgorithm}"/>
        <property name="secureProtocol" value="${secureProtocol}"/>
        <property name="keyStore" value="${keyStore}"/>
        <property name="keyAlias" value="${keyAlias}"/>
        <property name="trustStore" value="${trustStore}"/>
        <property name="keystoreManager" ref="keystoreManager"/>
    </bean>

    <!-- JAAS authenticator -->
    <bean id="jaasAuthenticator" class="org.apache.karaf.management.JaasAuthenticator">
        <property name="realm" value="${jmxRealm}"/>
        <property name="role" value="${jmxRole}"/>
    </bean>

</blueprint>

