001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.farm.config;
021    
022    import java.io.IOException;
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import javax.management.MBeanServerConnection;
027    import javax.management.remote.JMXConnector;
028    import javax.management.remote.JMXConnectorFactory;
029    import javax.management.remote.JMXServiceURL;
030    
031    import org.apache.geronimo.gbean.GBeanInfo;
032    import org.apache.geronimo.gbean.GBeanInfoBuilder;
033    import org.apache.geronimo.kernel.Kernel;
034    import org.apache.geronimo.system.jmx.KernelDelegate;
035    
036    /**
037     *
038     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    public class BasicNodeInfo implements NodeInfo {
041        private final String name;
042        private final ExtendedJMXConnectorInfo connectorInfo;
043        private final Kernel kernel;
044    
045        public BasicNodeInfo(Kernel kernel, String name, ExtendedJMXConnectorInfo connectorInfo) {
046            if (null == kernel) {
047                throw new IllegalArgumentException("kernel is required");
048            } else if (null == name) {
049                throw new IllegalArgumentException("name is required");
050            } else if (null == connectorInfo) {
051                throw new IllegalArgumentException("connectorInfo is required");
052            }
053            this.kernel = kernel;
054            this.name = name;
055            this.connectorInfo = connectorInfo;
056        }
057        
058        public String getName() {
059            return name;
060        }
061    
062        public ExtendedJMXConnectorInfo getConnectorInfo() {
063            return connectorInfo;
064        }
065    
066        public Kernel newKernel() throws IOException {
067            if (connectorInfo.isLocal()) {
068                return kernel;
069            }
070            
071            String url = "service:jmx:rmi://" + connectorInfo.getHost() + "/jndi/"
072                            + connectorInfo.getProtocol() + "://" + connectorInfo.getHost() + ":"
073                            + connectorInfo.getPort() + "/" + connectorInfo.getUrlPath();
074    
075            Map environment = new HashMap();
076            environment.put("jmx.remote.credentials",
077                new String[] {connectorInfo.getUsername(), connectorInfo.getPassword()});
078    
079            return newKernel(url, environment);
080        }
081    
082        protected Kernel newKernel(String url, Map environment) throws IOException {
083            JMXConnector jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(url), environment);
084            MBeanServerConnection mbServerConnection = jmxConnector.getMBeanServerConnection();
085            return new KernelDelegate(mbServerConnection);
086        }
087        
088        public static final GBeanInfo GBEAN_INFO;
089    
090        public static final String GBEAN_J2EE_TYPE = "NodeInfo";
091        public static final String GBEAN_ATTR_KERNEL = "kernel";
092        public static final String GBEAN_ATTR_NODE_NAME = "name";
093        public static final String GBEAN_ATTR_EXT_JMX_CONN_INFO = "extendedJMXConnectorInfo";
094        static {
095            GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(BasicNodeInfo.class, GBEAN_J2EE_TYPE);
096    
097            builder.addAttribute(GBEAN_ATTR_KERNEL, Kernel.class, false);
098            builder.addAttribute(GBEAN_ATTR_NODE_NAME, String.class, true);
099            builder.addAttribute(GBEAN_ATTR_EXT_JMX_CONN_INFO, ExtendedJMXConnectorInfo.class, true);
100            
101            builder.addInterface(NodeInfo.class);
102            
103            builder.setConstructor(new String[]{GBEAN_ATTR_KERNEL,
104                GBEAN_ATTR_NODE_NAME,
105                GBEAN_ATTR_EXT_JMX_CONN_INFO});
106    
107            GBEAN_INFO = builder.getBeanInfo();
108        }
109    
110        public static GBeanInfo getGBeanInfo() {
111            return GBEAN_INFO;
112        }
113    
114    }