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:$ $Date:$
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,
046: ExtendedJMXConnectorInfo connectorInfo) {
047: if (null == kernel) {
048: throw new IllegalArgumentException("kernel is required");
049: } else if (null == name) {
050: throw new IllegalArgumentException("name is required");
051: } else if (null == connectorInfo) {
052: throw new IllegalArgumentException(
053: "connectorInfo is required");
054: }
055: this .kernel = kernel;
056: this .name = name;
057: this .connectorInfo = connectorInfo;
058: }
059:
060: public String getName() {
061: return name;
062: }
063:
064: public ExtendedJMXConnectorInfo getConnectorInfo() {
065: return connectorInfo;
066: }
067:
068: public Kernel newKernel() throws IOException {
069: if (connectorInfo.isLocal()) {
070: return kernel;
071: }
072:
073: String url = "service:jmx:rmi://" + connectorInfo.getHost()
074: + "/jndi/" + connectorInfo.getProtocol() + "://"
075: + connectorInfo.getHost() + ":"
076: + connectorInfo.getPort() + "/"
077: + connectorInfo.getUrlPath();
078:
079: Map environment = new HashMap();
080: environment.put("jmx.remote.credentials", new String[] {
081: connectorInfo.getUsername(),
082: connectorInfo.getPassword() });
083:
084: return newKernel(url, environment);
085: }
086:
087: protected Kernel newKernel(String url, Map environment)
088: throws IOException {
089: JMXConnector jmxConnector = JMXConnectorFactory.connect(
090: new JMXServiceURL(url), environment);
091: MBeanServerConnection mbServerConnection = jmxConnector
092: .getMBeanServerConnection();
093: return new KernelDelegate(mbServerConnection);
094: }
095:
096: public static final GBeanInfo GBEAN_INFO;
097:
098: public static final String GBEAN_J2EE_TYPE = "NodeInfo";
099: public static final String GBEAN_ATTR_KERNEL = "kernel";
100: public static final String GBEAN_ATTR_NODE_NAME = "name";
101: public static final String GBEAN_ATTR_EXT_JMX_CONN_INFO = "extendedJMXConnectorInfo";
102: static {
103: GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(
104: BasicNodeInfo.class, GBEAN_J2EE_TYPE);
105:
106: builder.addAttribute(GBEAN_ATTR_KERNEL, Kernel.class, false);
107: builder.addAttribute(GBEAN_ATTR_NODE_NAME, String.class, true);
108: builder.addAttribute(GBEAN_ATTR_EXT_JMX_CONN_INFO,
109: ExtendedJMXConnectorInfo.class, true);
110:
111: builder.addInterface(NodeInfo.class);
112:
113: builder.setConstructor(new String[] { GBEAN_ATTR_KERNEL,
114: GBEAN_ATTR_NODE_NAME, GBEAN_ATTR_EXT_JMX_CONN_INFO });
115:
116: GBEAN_INFO = builder.getBeanInfo();
117: }
118:
119: public static GBeanInfo getGBeanInfo() {
120: return GBEAN_INFO;
121: }
122:
123: }
|