001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sun.manager.jbi.management;
042:
043: import com.sun.esb.management.api.administration.AdministrationService;
044: import com.sun.esb.management.api.configuration.ConfigurationService;
045: import com.sun.esb.management.api.deployment.DeploymentService;
046: import com.sun.esb.management.api.installation.InstallationService;
047: import com.sun.esb.management.client.ManagementClient;
048: import com.sun.esb.management.common.ManagementRemoteException;
049: import java.io.File;
050: import java.net.InetAddress;
051: import java.util.List;
052: import java.util.logging.Logger;
053: import javax.management.MBeanServerConnection;
054: import javax.management.ObjectName;
055: import org.netbeans.modules.sun.manager.jbi.management.connectors.HTTPServerConnector;
056: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.PerformanceMeasurementServiceWrapper;
057: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.RuntimeManagementServiceWrapper;
058: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.impl.PerformanceMeasurementServiceWrapperImpl;
059: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.impl.RuntimeManagementServiceWrapperImpl;
060: import org.netbeans.modules.sun.manager.jbi.util.ServerInstance;
061: import org.netbeans.modules.sun.manager.jbi.util.ServerInstanceReader;
062:
063: /**
064: *
065: * @author jqian
066: */
067: public class AppserverJBIMgmtController {
068:
069: // original MBeanServerConnection from NetBeans
070: private MBeanServerConnection mBeanServerConnection;
071:
072: // MBeanServerConnection with classpath problem fixed
073: private MBeanServerConnection myMBeanServerConnection;
074:
075: private ManagementClient managementClient;
076:
077: // cached services
078: private AdministrationService administrationService;
079: private DeploymentService deploymentService;
080: private InstallationService installationService;
081: private ConfigurationService configurationService;
082: private PerformanceMeasurementServiceWrapper performanceMeasurementServiceWrapper;
083: private RuntimeManagementServiceWrapper runtimeManagementServiceWrapper;
084:
085: public static final String SERVER_TARGET = "server";
086: private static final String HOST_MBEAN_NAME = "com.sun.appserv:name=server,type=virtual-server,category=monitor,server=server"; // NOI18N
087: private static final String HOST_ASADMIN_MBEAN_NAME = "com.sun.appserv:type=Host,host=__asadmin"; // NOI18N
088: private static final String HTTP_PORT_MBEAN_NAME = "com.sun.appserv:type=http-listener,id=http-listener-1,config=server-config,category=config"; // NOI18N
089: private static Logger logger = Logger
090: .getLogger("org.netbeans.modules.sun.manager.jbi.management.AppserverJBIMgmtController"); // NOI18N
091:
092: /**
093: * Creates a new instance of AppserverJBIMgmtController
094: */
095: public AppserverJBIMgmtController(
096: MBeanServerConnection mBeanServerConnection) {
097: this .mBeanServerConnection = mBeanServerConnection;
098: init();
099: managementClient = new ManagementClient(
100: myMBeanServerConnection, true);
101: }
102:
103: public boolean isJBIFrameworkEnabled() {
104: JBIFrameworkService service = new JBIFrameworkService(
105: mBeanServerConnection);
106: return service.isJbiFrameworkEnabled();
107: }
108:
109: public AdministrationService getAdministrationService()
110: throws ManagementRemoteException {
111: if (administrationService == null) {
112: administrationService = managementClient
113: .getAdministrationService();
114: }
115: return administrationService;
116: }
117:
118: public DeploymentService getDeploymentService()
119: throws ManagementRemoteException {
120: if (deploymentService == null) {
121: deploymentService = managementClient.getDeploymentService();
122: }
123: return deploymentService;
124: }
125:
126: public InstallationService getInstallationService()
127: throws ManagementRemoteException {
128: if (installationService == null) {
129: installationService = managementClient
130: .getInstallationService();
131: }
132: return installationService;
133: }
134:
135: public ConfigurationService getConfigurationService()
136: throws ManagementRemoteException {
137: if (configurationService == null) {
138: configurationService = managementClient
139: .getConfigurationService();
140: }
141: return configurationService;
142: }
143:
144: public PerformanceMeasurementServiceWrapper getPerformanceMeasurementServiceWrapper()
145: throws ManagementRemoteException {
146: if (performanceMeasurementServiceWrapper == null) {
147: performanceMeasurementServiceWrapper = new PerformanceMeasurementServiceWrapperImpl(
148: managementClient.getPerformanceMeasurementService());
149: }
150: return performanceMeasurementServiceWrapper;
151: }
152:
153: public RuntimeManagementServiceWrapper getRuntimeManagementServiceWrapper()
154: throws ManagementRemoteException {
155: if (runtimeManagementServiceWrapper == null) {
156: runtimeManagementServiceWrapper = new RuntimeManagementServiceWrapperImpl(
157: managementClient.getRuntimeManagementService());
158: }
159: return runtimeManagementServiceWrapper;
160: }
161:
162: private void init() {
163:
164: // The MBeanServerConnection we get from NetBeans
165: // (org.netbeans.modules.j2ee.sun.util.PluginRequestInterceptor)
166: // doesn't provide good error message when the RPC fails.
167: // The wrapped one (com.sun.enterprise.admin.jmx.remote.internal.RemoteMBeanServerConnection)
168: // doesn't do the job either.
169: // See ControllerUtil.java in org.netbeans.modules.j2ee.sun.ide.controllers
170: // in appserverplugin.
171:
172: String netBeansUserDir = System.getProperty("netbeans.user"); // NOI18N
173:
174: try {
175: if (netBeansUserDir != null) {
176: ServerInstance serverInstance = null;
177:
178: String settingsFileName = netBeansUserDir
179: + ServerInstanceReader.RELATIVE_FILE_PATH;
180: File settingsFile = new File(settingsFileName);
181:
182: if (!settingsFile.exists()) {
183: logger
184: .warning("The application server definition file "
185: + // NOI18N
186: settingsFileName + " is missing."); // NOI18N
187: } else {
188: ServerInstanceReader settings = new ServerInstanceReader(
189: settingsFileName);
190: List<ServerInstance> instances = settings
191: .getServerInstances();
192: for (ServerInstance instance : instances) {
193: if (isCurrentInstance(instance)) {
194: serverInstance = instance;
195: break;
196: }
197: }
198:
199: // If there is no match, and there is only one instance
200: // available, then use it.
201: if (serverInstance == null) {
202: if (instances.size() == 1) {
203: logger
204: .warning("Could not find the server instance. Use the only instance available in "
205: + settingsFileName + "."); // NOI18N
206: serverInstance = instances.get(0);
207: }
208: }
209:
210: // If there is no match, and there is only one remote
211: // instance available, use it.
212: if (serverInstance == null) {
213: int remoteInstances = 0;
214: for (ServerInstance instance : instances) {
215: if (!instance.getHostName().equals(
216: "localhost")) { // NOI18N
217: remoteInstances++;
218: }
219: }
220:
221: if (remoteInstances == 1) {
222: for (ServerInstance instance : instances) {
223: if (!instance.getHostName().equals(
224: "localhost")) { // NOI18N
225: logger
226: .warning("Could not find the server instance. Use the only remote instance defined in "
227: + settingsFileName
228: + "."); // NOI18N
229: serverInstance = instance;
230: break;
231: }
232: }
233: }
234: }
235:
236: if (serverInstance != null) {
237: JBIClassLoader jbiClassLoader = new JBIClassLoader(
238: serverInstance);
239:
240: String hostName = serverInstance.getHostName();
241: String port = serverInstance.getAdminPort();
242: String userName = serverInstance.getUserName();
243: String password = serverInstance.getPassword();
244:
245: HTTPServerConnector httpConnector = new HTTPServerConnector(
246: hostName, port, userName, password,
247: jbiClassLoader);
248:
249: myMBeanServerConnection = httpConnector
250: .getConnection();
251: }
252: }
253: }
254: } catch (Exception e) {
255: logger.warning(e.getMessage());
256: }
257:
258: if (myMBeanServerConnection == null) {
259: // Fall back on the mBeanServerConnection provided by NetBeans
260: try {
261: logger
262: .warning("Could not find the server instance. Falling back on the mBeanServerConnection provided by NetBeans."); // NOI18N
263: myMBeanServerConnection = mBeanServerConnection;
264: } catch (Exception e) {
265: logger.warning(e.getMessage());
266: }
267: }
268: }
269:
270: private boolean isCurrentInstance(ServerInstance instance) {
271:
272: boolean isLocalHost = false;
273: String instanceHost = instance.getHostName();
274: if (instanceHost.equals("localhost")) { // NOI18N
275: instanceHost = getHostName();
276: isLocalHost = true;
277: }
278:
279: try {
280: ObjectName objectName = new ObjectName(HOST_MBEAN_NAME);
281:
282: String host = (String) mBeanServerConnection.getAttribute(
283: objectName, "hosts-current"); // NOI18N
284:
285: // InetAddress's getCanonicalHostName() is a best-effort method
286: // and doesn't work if the name service is not available. (cordova)
287: // IP address is not reliable either.
288: String instanceHostCanonicalHostName = InetAddress
289: .getByName(instanceHost).getCanonicalHostName();
290: String hostCanonicalHostName = InetAddress.getByName(host)
291: .getCanonicalHostName();
292: String instanceHostAddress = InetAddress.getByName(
293: instanceHost).getHostAddress();
294: String hostAddress = InetAddress.getByName(host)
295: .getHostAddress();
296:
297: logger.fine("isCurrentInstance():");
298: logger.fine(" isLocalHost? " + isLocalHost);
299: logger.fine(" instanceHost=" + instanceHost
300: + " CanonicalHostName="
301: + instanceHostCanonicalHostName + " IP="
302: + instanceHostAddress);
303: logger.fine(" host=" + host
304: + " CanonicalHostName=" + hostCanonicalHostName
305: + " IP=" + hostAddress);
306: // for (InetAddress addr : InetAddress.getAllByName(host)) {
307: // logger.log(Level.FINE, " " + addr.getHostAddress());
308: // }
309:
310: if (instanceHostCanonicalHostName
311: .equals(hostCanonicalHostName)
312: || instanceHostAddress.equals(hostAddress)) {
313: objectName = new ObjectName(HOST_ASADMIN_MBEAN_NAME);
314: String appBase = (String) mBeanServerConnection
315: .getAttribute(objectName, "appBase"); // NOI18N
316:
317: // For local domains, use instance LOCATION instead of url location (#90749)
318: String localInstanceLocation = instance.getLocation();
319: assert localInstanceLocation != null;
320: localInstanceLocation = localInstanceLocation.replace(
321: '\\', '/'); // NOI18N
322:
323: if (isLocalHost) {
324: logger.fine(" localInstanceLocation="
325: + localInstanceLocation);
326: logger.fine(" appBase=" + appBase);
327: }
328:
329: if (!isLocalHost
330: || appBase.toLowerCase().startsWith(
331: localInstanceLocation.toLowerCase())) {
332: objectName = new ObjectName(HTTP_PORT_MBEAN_NAME);
333: String port = (String) mBeanServerConnection
334: .getAttribute(objectName, "port"); // NOI18N
335: String instanceHttpPort = instance
336: .getHttpPortNumber();
337:
338: logger.fine(" instanceHttpPort="
339: + instanceHttpPort);
340: logger.fine(" port=" + port);
341:
342: if (port.equals(instanceHttpPort)) {
343: return true;
344: }
345: }
346: }
347: } catch (Exception e) {
348: e.printStackTrace();
349: }
350:
351: return false;
352: }
353:
354: private static String getHostName() {
355: String hostName = null;
356: try {
357: InetAddress localMachine = InetAddress.getLocalHost();
358: hostName = localMachine.getHostName();
359: } catch (java.net.UnknownHostException e) {
360: e.printStackTrace();
361: }
362:
363: return hostName;
364: }
365: }
|