001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.hivemind.management.impl;
016
017import java.util.HashMap;
018import java.util.Map;
019
020import javax.management.MBeanServer;
021import javax.management.remote.JMXConnectorServer;
022import javax.management.remote.JMXServiceURL;
023
024import org.apache.hivemind.ApplicationRuntimeException;
025import org.apache.hivemind.ServiceImplementationFactory;
026import org.apache.hivemind.ServiceImplementationFactoryParameters;
027import org.apache.hivemind.management.ManagementMessages;
028
029/**
030 * An implementation of {@link org.apache.hivemind.ServiceImplementationFactory} that creates
031 * JMXConnectorServer instances using {@link javax.management.remote.JMXConnectorServerFactory}
032 * 
033 * @author Achim Huegen
034 * @since 1.1
035 */
036public class JMXConnectorServerFactory implements ServiceImplementationFactory
037{
038    public JMXConnectorServerFactory(MBeanServer beanServer)
039    {
040    }
041
042    public Object createCoreServiceImplementation(
043            ServiceImplementationFactoryParameters factoryParameters)
044    {
045        // Read the parameters of the factory call
046        JMXConnectorServerParameter parameter = (JMXConnectorServerParameter) factoryParameters
047                .getFirstParameter();
048
049        Map env = new HashMap();
050
051        try
052        {
053            // Convert the serviceUrl string to instance of JMXServiceURL
054            JMXServiceURL address = new JMXServiceURL(parameter.getJmxServiceURL());
055
056            JMXConnectorServer server = javax.management.remote.JMXConnectorServerFactory
057                    .newJMXConnectorServer(address, env, null);
058
059            return server;
060        }
061        catch (Exception e)
062        {
063            throw new ApplicationRuntimeException(ManagementMessages
064                    .errorInstantiatingConnectorServer(parameter.getJmxServiceURL(), env, e), e);
065        }
066    }
067
068}