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:
042: package org.netbeans.modules.j2ee.sun.bridge;
043:
044: import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
045: import com.sun.enterprise.admin.jmx.remote.SunOneHttpJmxConnectorFactory;
046: import com.sun.enterprise.config.serverbeans.Config;
047: import com.sun.enterprise.config.serverbeans.Domain;
048: import com.sun.enterprise.config.serverbeans.HttpListener;
049: import com.sun.enterprise.deployapi.SunDeploymentManager;
050: import com.sun.enterprise.deployapi.SunTargetModuleID;
051: import com.sun.enterprise.deployment.client.ServerConnectionEnvironment;
052: import java.io.BufferedInputStream;
053: import java.io.File;
054: import java.io.FileInputStream;
055: import java.io.FileNotFoundException;
056: import java.io.IOException;
057: import java.io.InputStream;
058: import java.util.Locale;
059: import java.util.logging.Level;
060: import java.util.logging.Logger;
061: import javax.enterprise.deploy.shared.ModuleType;
062: import javax.enterprise.deploy.spi.DeploymentManager;
063: import javax.enterprise.deploy.spi.TargetModuleID;
064: import javax.management.MBeanServerConnection;
065: import javax.management.ObjectName;
066: import javax.management.remote.JMXConnector;
067: import javax.management.remote.JMXServiceURL;
068:
069: /*
070: * wrap some App Server internal APIS calls
071: * for the netbeans plugin
072: * to compile this class, you need some App Server jar files like appsrv-rt.jar and appsrv-admin.jar
073: * @author: Ludovic Champenois
074: *
075: **/
076:
077: public class AppServerBridge {
078:
079: public static java.io.File getDirLocation(TargetModuleID tmid) {
080: java.io.File dirLocation = null;
081: String modulePart = null;
082: try {
083: SunTargetModuleID ddd = (SunTargetModuleID) tmid;
084:
085: String earPart = null;
086:
087: String mid = ddd.getModuleID();
088: int dex = mid.indexOf('#');
089: if (dex > -1) {
090: earPart = mid.substring(0, dex);
091: modulePart = mid.substring(dex + 1);
092: mid = earPart;
093: }
094:
095: if (ddd.getModuleType().equals(ModuleType.WAR)) {
096: if (null == earPart) {
097: ObjectName aaaa = new ObjectName(
098: "com.sun.appserv:type=web-module,name="
099: + mid + ",category=config");
100: dirLocation = new java.io.File(""
101: + ddd.getMBeanServerConnection()
102: .getAttribute(aaaa, "location"));
103: } else {
104: // watch out for a context root of "/" in a web app deployed
105: // as part of an ear.
106: if (!modulePart.startsWith("/"))
107: modulePart = "/" + modulePart;
108: ObjectName aaaa = new ObjectName(
109: "com.sun.appserv:j2eeType=WebModule,name=//server"
110: + modulePart + ",J2EEApplication="
111: + earPart + ",J2EEServer=server");
112: dirLocation = new java.io.File(""
113: + ddd.getMBeanServerConnection()
114: .getAttribute(aaaa, "docBase"));
115: modulePart = null;
116: }
117: } else if (mid == earPart) { // yes I want to use == for this compare
118: ObjectName aaaa = new ObjectName(
119: "com.sun.appserv:type=j2ee-application,name="
120: + mid + ",category=config");
121: dirLocation = new java.io.File(""
122: + ddd.getMBeanServerConnection().getAttribute(
123: aaaa, "location"));
124: } else if (ddd.getModuleType().equals(ModuleType.EJB)) {
125: ObjectName aaaa = new ObjectName(
126: "com.sun.appserv:type=ejb-module,name=" + mid
127: + ",category=config");
128: dirLocation = new java.io.File(""
129: + ddd.getMBeanServerConnection().getAttribute(
130: aaaa, "location"));
131: } else if (ddd.getModuleType().equals(ModuleType.EAR)) {
132: ObjectName aaaa = new ObjectName(
133: "com.sun.appserv:type=j2ee-application,name="
134: + mid + ",category=config");
135: dirLocation = new java.io.File(""
136: + ddd.getMBeanServerConnection().getAttribute(
137: aaaa, "location"));
138: } else if (ddd.getModuleType().equals(ModuleType.CAR)) {
139: ObjectName aaaa = new ObjectName(
140: "com.sun.appserv:type=appclient-module,name="
141: + mid + ",category=config");
142: dirLocation = new java.io.File(""
143: + ddd.getMBeanServerConnection().getAttribute(
144: aaaa, "location"));
145: } else {
146: System.out
147: .println("Still Some Work to do in ModuleRestartActikon is AS 8.1 plugin code");
148: }
149:
150: } catch (Exception e) {
151: e.printStackTrace();
152: IllegalStateException ise = new IllegalStateException(e
153: .getMessage());
154: ise.initCause(e);
155: throw ise;
156: }
157: if (null != modulePart) {
158: dirLocation = new File(dirLocation.getAbsolutePath(),
159: DirectoryDeployment.transform(modulePart));
160: }
161: return dirLocation;
162: }
163:
164: public static boolean isApp(TargetModuleID tmid) {
165: ModuleType mt = ((SunTargetModuleID) tmid).getModuleType();
166: return mt.equals(ModuleType.EAR);
167: }
168:
169: public static boolean isWar(TargetModuleID tmid) {
170: ModuleType mt = ((SunTargetModuleID) tmid).getModuleType();
171: return mt.equals(ModuleType.WAR);
172: }
173:
174: public static Boolean isCar(TargetModuleID tmid) {
175: ModuleType mt = ((SunTargetModuleID) tmid).getModuleType();
176: return Boolean.valueOf(mt.equals(ModuleType.CAR));
177: }
178:
179: /**
180: * Get the URI pointing to location of child module inside a application archive.
181: * For a root module, service provider does not need to override this method.
182: *
183: * @param module TargetModuleID of the child module
184: * @return its relative path within application archive, returns null by
185: * default (for standalone module)
186: */
187: public static String getModuleUrl(TargetModuleID module) {
188: ModuleType mt = ((SunTargetModuleID) module).getModuleType();
189: String suffix = ".jar";
190: String moduleID = module.getModuleID();
191: int i = moduleID.indexOf('#');
192: if (i > -1) {
193: moduleID = moduleID.substring(i + 1);
194: }
195: if (mt.equals(ModuleType.EAR)) {
196: suffix = ".ear";
197: }
198: if (mt.equals(ModuleType.WAR)) {
199: suffix = ".war";
200: java.io.File dirLocation = null;
201: String modulePart = null;
202: if (!moduleID.toLowerCase(Locale.ENGLISH).endsWith(suffix))
203: try {
204: SunTargetModuleID ddd = (SunTargetModuleID) module;
205:
206: String earPart = null;
207:
208: String mid = ddd.getModuleID();
209: int dex = mid.indexOf('#');
210: if (dex > -1) {
211: earPart = mid.substring(0, dex);
212: modulePart = mid.substring(dex + 1);
213: mid = earPart;
214: }
215: if (!modulePart.startsWith("/"))
216: modulePart = "/" + modulePart;
217:
218: if (ddd.getModuleType().equals(ModuleType.WAR)) {
219: if (null == earPart) {
220: ObjectName aaaa = new ObjectName(
221: "com.sun.appserv:type=web-module,name="
222: + mid + ",category=config");
223: dirLocation = new java.io.File(""
224: + ddd.getMBeanServerConnection()
225: .getAttribute(aaaa,
226: "location"));
227: } else {
228: ObjectName aaaa = new ObjectName(
229: "com.sun.appserv:j2eeType=WebModule,name=//server"
230: + modulePart
231: + ",J2EEApplication="
232: + earPart
233: + ",J2EEServer=server");
234: dirLocation = new java.io.File(""
235: + ddd.getMBeanServerConnection()
236: .getAttribute(aaaa,
237: "docBase"));
238: String t = dirLocation.getName();
239: moduleID = t.substring(0, t.length() - 4);
240: modulePart = null;
241: }
242: }
243: } catch (Exception e) {
244: e.printStackTrace();
245: IllegalStateException ise = new IllegalStateException(
246: e.getMessage());
247: ise.initCause(e);
248: throw ise;
249: }
250: }
251: if (mt.equals(ModuleType.RAR)) {
252: suffix = ".rar";
253: }
254:
255: if (moduleID.endsWith(suffix)
256: || moduleID
257: .endsWith(suffix.toUpperCase(Locale.ENGLISH))) {
258: return moduleID;
259: }
260: return moduleID + suffix;
261: }
262:
263: public static String getHostPort(File domainXml, File platformDir) {
264: String hostPort = null;
265: InputStream inFile = null;
266: try {
267: inFile = new BufferedInputStream(new FileInputStream(
268: domainXml));
269: Domain domain = Domain.createGraph(inFile);
270: String domainSysID = domain.graphManager().getXmlDocument()
271: .getDoctype().getSystemId();
272:
273: // make sure the platform will support this domain..
274:
275: // unknown domain.xml content.. we don't support that
276: if (null == domainSysID) {
277: return null;
278: }
279:
280: // the sys ID doesn't contain the content we expect... we don't support that
281: int dtdsDex = domainSysID.indexOf("dtds/"); // NOI18N
282: if (-1 == dtdsDex) {
283: return null;
284: }
285:
286: File domainDtd = new File(platformDir, "lib/" + // NOI18N
287: domainSysID.substring(dtdsDex));
288:
289: // the installation doesn't have the dtd where we expect it... we don't support that
290: if (!domainDtd.exists()) {
291: return null;
292: }
293:
294: Config conf = domain.getConfigs().getConfigByName(
295: "server-config"); //NOI18N
296: HttpListener list = conf.getHttpService()
297: .getHttpListenerById("admin-listener"); //NOI18N
298: hostPort = "localhost:" + list.getPort(); //NOI18N
299: } catch (FileNotFoundException ex) {
300: Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge")
301: .log(Level.FINER, "", ex); // NOI18N
302: } catch (IOException ex) {
303: Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge")
304: .log(Level.FINER, "", ex); // NOI18N
305: } catch (RuntimeException re) {
306: Logger.getLogger("org.netbeans.modules.j2ee.sun.bridge")
307: .log(Level.FINER, "", re); // NOI18N
308: } finally {
309: if (null != inFile)
310: try {
311: inFile.close();
312: } catch (IOException ioe) {
313: // what about this???
314: Logger.getLogger(
315: "org.netbeans.modules.j2ee.sun.bridge")
316: .log(Level.FINEST, "", ioe); // NOI18N
317: }
318: }
319: return hostPort;
320: }
321:
322: /* return the port number used bu the server instance (usually, it is the 8080...
323: * This is not the admin port number which is usally 4848
324: **/
325: public String getNonAdminPortNumber(File domainXml) {
326: String port = null;
327: try {
328: InputStream inFile = new FileInputStream(domainXml);
329: Domain domain = Domain.createGraph(inFile);
330: Config conf = domain.getConfigs().getConfigByName(
331: "server-config"); //NOI18N
332: HttpListener list = conf.getHttpService()
333: .getHttpListenerById("http-listener-1"); //NOI18N
334: port = list.getPort(); //NOI18N
335: inFile.close();
336: } catch (Exception ex) {
337: return null;
338: //Suppressing exception while trying to get admin port.
339: //Null port value is handled in AddServerChoiceVisualPanel
340: }
341: return port;
342: }
343:
344: public static MBeanServerConnection getJMXConnector(String host,
345: int port, String username, String password, boolean secure)
346: throws java.net.MalformedURLException, java.io.IOException {
347: MBeanServerConnection serverConn = null;
348: String mode = null;
349: if (secure)
350: mode = "s1ashttps";
351: else
352: mode = "s1ashttp";
353: JMXServiceURL serverUrl = new JMXServiceURL(mode, host, port); //NOI18N
354: final JMXConnector connector = SunOneHttpJmxConnectorFactory
355: .connect(serverUrl, username, password);
356: serverConn = connector.getMBeanServerConnection();
357:
358: return serverConn;
359: }
360:
361: /**
362: * This method initializes a newly DeploymentManager by creating and
363: * setting a ServerConnectionEnvironement with Deploytool's
364: * X509TrustManager. This method will case the given DeploymentManager
365: * to a SunDeploymentManager in order to make
366: * invoke the appropriate setter for the ServerConnectionEnvironment.
367: */
368: public static void setServerConnectionEnvironment(
369: DeploymentManager dm) {
370: ServerConnectionEnvironment env = new ServerConnectionEnvironment();
371: env.put(DefaultConfiguration.TRUST_MANAGER_PROPERTY_NAME,
372: new X509TrustManager());
373: if (dm instanceof SunDeploymentManager) {
374: ((SunDeploymentManager) dm)
375: .setServerConnectionEnvironment(env);
376: }// else {
377: // Print.dprintStackTrace(null, new IllegalArgumentException(
378: // "Unsupported DeploymentManager type: '"+
379: // dm.getClass().getName()+"'."));
380: // }
381: }
382:
383: }
|