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.compapp.jbiserver;
042:
043: import java.awt.Dialog;
044: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
045: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
046:
047: import javax.enterprise.deploy.spi.factories.DeploymentFactory;
048: import javax.enterprise.deploy.spi.DeploymentManager;
049: import java.io.File;
050: import java.util.HashMap;
051: import java.util.Map;
052: import java.util.Properties;
053: import java.util.ArrayList;
054:
055: import javax.swing.SwingUtilities;
056: import org.netbeans.modules.compapp.projects.jbi.JbiActionProvider;
057: import org.netbeans.modules.compapp.projects.jbi.JbiProject;
058: import org.netbeans.modules.compapp.projects.jbi.ui.NoSelectedServerWarning;
059: import org.netbeans.modules.compapp.projects.jbi.ui.customizer.JbiProjectProperties;
060: import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
061: import org.netbeans.modules.j2ee.deployment.impl.ServerString;
062: import org.netbeans.modules.j2ee.deployment.impl.ServerTarget;
063: import org.netbeans.modules.j2ee.deployment.plugins.spi.OptionalDeploymentManagerFactory;
064: import org.netbeans.modules.j2ee.deployment.plugins.spi.StartServer;
065: import org.netbeans.modules.sun.manager.jbi.management.JBIClassLoader;
066: import org.netbeans.spi.project.support.ant.AntProjectHelper;
067: import org.openide.DialogDescriptor;
068: import org.openide.DialogDisplayer;
069: import org.openide.NotifyDescriptor;
070: import org.openide.cookies.InstanceCookie;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.FileSystem;
073: import org.openide.filesystems.Repository;
074: import org.openide.loaders.DataObject;
075: import org.openide.util.Exceptions;
076: import org.openide.util.NbBundle;
077:
078: public class JbiManager {
079:
080: public static final String URL_ATTR = "url"; // NOI18N
081: public static final String HOSTNAME_ATTR = "hostname"; // NOI18N
082: public static final String PORT_ATTR = "port"; // NOI18N
083: public static final String USERNAME_ATTR = "username"; // NOI18N
084: public static final String PASSWORD_ATTR = "password"; // NOI18N
085: /** Mapping from ServerInstance to JbiClassLoader instances. */
086: private static Map<String, JBIClassLoader> loaderMap;
087:
088: public static JBIClassLoader getJBIClassLoader(String serverInstance) {
089: if (loaderMap == null) {
090: loaderMap = new HashMap<String, JBIClassLoader>();
091: }
092:
093: JBIClassLoader loader = loaderMap.get(serverInstance);
094: if (loader == null) {
095: try {
096: J2eePlatform platform = Deployment.getDefault()
097: .getJ2eePlatform(serverInstance);
098:
099: if (isAppServer(platform)) {
100:
101: loader = new JBIClassLoader(new Empty().getClass()
102: .getClassLoader());
103:
104: File[] roots = platform.getPlatformRoots();
105: for (int i = 0; i < roots.length; i++) {
106: File root = roots[i];
107: String rootPath = root.getAbsolutePath();
108:
109: File f = new File(rootPath
110: + "/lib/appserv-rt.jar"); // NOI18N
111: if (f.exists()) {
112: loader.addURL(f);
113: break;
114: }
115: }
116:
117: loaderMap.put(serverInstance, loader);
118: }
119: } catch (Exception ex) {
120: loader = null;
121: ex.printStackTrace(System.out);
122: }
123: }
124: return loader;
125: }
126:
127: public static boolean isRunningAppServer(JbiProject project) {
128: if (project == null) {
129: return false;
130: }
131:
132: JbiProjectProperties projectProperties = project
133: .getProjectProperties();
134: String serverInstance = (String) projectProperties
135: .get(JbiProjectProperties.J2EE_SERVER_INSTANCE);
136: return isRunningAppServer(serverInstance);
137: }
138:
139: public static boolean isRunningAppServer(String serverInstance) {
140: if (serverInstance == null) {
141: return false;
142: }
143:
144: J2eePlatform platform = Deployment.getDefault()
145: .getJ2eePlatform(serverInstance);
146:
147: if (!isAppServer(platform)) {
148: return false;
149: }
150:
151: // Can't get the ServerInstance directly from the platform! Get the info from layer.
152:
153: Properties properties = getServerInstanceProperties(serverInstance);
154:
155: String url = properties.getProperty(URL_ATTR);
156: String userName = properties.getProperty(USERNAME_ATTR);
157: String password = properties.getProperty(PASSWORD_ATTR);
158:
159: String plugin = Deployment.getDefault().getServerID(
160: serverInstance);
161:
162: if (url != null && userName != null && password != null
163: && plugin != null) {
164: StartServer startServer = getStartServer(plugin, url,
165: userName, password);
166: if (startServer != null) {
167: return startServer.isRunning();
168: }
169: }
170: return false;
171: }
172:
173: /**
174: * Starts the given App Server if it is not running. Optionally, blocks
175: * the current thread until the server is ready.
176: * <p>
177: * This should not be called from the Event Dispatch Thread.
178: *
179: * @param serverInstanceID server instance ID
180: * @param block if <code>true</code>, block the current thread
181: * until the server is ready; <code>false</code> otherwise.
182: */
183: public static void startServer(JbiProject project, boolean block) {
184: JbiProjectProperties projectProperties = project
185: .getProjectProperties();
186: String serverInstanceID = (String) projectProperties
187: .get(JbiProjectProperties.J2EE_SERVER_INSTANCE);
188: assert serverInstanceID != null;
189: startServer(serverInstanceID, block);
190: }
191:
192: /**
193: * Starts the given App Server if it is not running. Optionally, blocks
194: * the current thread until the server is ready.
195: * <p>
196: * This should not be called from the Event Dispatch Thread.
197: *
198: * @param serverInstanceID server instance ID
199: * @param block if <code>true</code>, block the current thread
200: * until the server is ready; <code>false</code> otherwise.
201: */
202: public static void startServer(String serverInstanceID,
203: boolean block) {
204:
205: assert !SwingUtilities.isEventDispatchThread() : "This should not be called from the EDT."; // NOI18N
206:
207: if (isRunningAppServer(serverInstanceID)) {
208: return;
209: }
210:
211: org.netbeans.modules.j2ee.deployment.impl.ServerInstance inst = null;
212:
213: try {
214: inst = ServerRegistry.getInstance().getServerInstance(
215: serverInstanceID);
216: } catch (Exception e) {
217: // NPE from command line deployment.
218: // Fine. Not supporting auto server start if deployed from command line.
219: return;
220: }
221:
222: if (inst == null) {
223: System.out.println("Bad target server ID: "
224: + serverInstanceID);
225: return;
226: }
227:
228: ServerString server = new ServerString(inst);
229:
230: org.netbeans.modules.j2ee.deployment.impl.ServerInstance serverInstance = server
231: .getServerInstance();
232: if (server == null || serverInstance == null) {
233: System.out
234: .println("Make sure a target server is set in project properties.");
235: }
236:
237: // Currently it is not possible to select target to which modules will
238: // be deployed. Lets use the first one.
239: // (This will start the server if the server is not running.)
240: ServerTarget targets[] = serverInstance.getTargets();
241:
242: if (block) {
243: while (!isRunningAppServer(serverInstanceID)) {
244: try {
245: Thread.sleep(1000);
246: } catch (InterruptedException ex) {
247: Exceptions.printStackTrace(ex);
248: }
249: }
250: }
251: }
252:
253: public static boolean isSelectedServer(JbiProject project) {
254:
255: AntProjectHelper antProjectHelper = project
256: .getAntProjectHelper();
257: String instance = antProjectHelper
258: .getStandardPropertyEvaluator().getProperty(
259: JbiProjectProperties.J2EE_SERVER_INSTANCE);
260:
261: if ((instance == null) || !JbiManager.isAppServer(instance)) {
262: String[] serverIDs = JbiManager.getAppServers();
263:
264: if (serverIDs.length < 1) {
265: NotifyDescriptor d = new NotifyDescriptor.Message(
266: NbBundle.getMessage(JbiActionProvider.class,
267: "MSG_NoInstalledServerError"), // NOI18N
268: NotifyDescriptor.ERROR_MESSAGE);
269: DialogDisplayer.getDefault().notify(d);
270: return false;
271: }
272:
273: NoSelectedServerWarning panel = new NoSelectedServerWarning(
274: serverIDs);
275:
276: Object[] options = new Object[] {
277: DialogDescriptor.OK_OPTION,
278: DialogDescriptor.CANCEL_OPTION };
279: DialogDescriptor desc = new DialogDescriptor(
280: panel,
281: NbBundle.getMessage(NoSelectedServerWarning.class,
282: "CTL_NoSelectedServerWarning_Title"), // NOI18N
283: true, options, options[0],
284: DialogDescriptor.DEFAULT_ALIGN, null, null);
285: Dialog dlg = DialogDisplayer.getDefault()
286: .createDialog(desc);
287: dlg.setVisible(true);
288:
289: if (desc.getValue() != options[0]) {
290: // cancel
291: return false;
292: } else {
293: instance = panel.getSelectedInstance();
294: if (instance != null) {
295: JbiProjectProperties projectProperties = project
296: .getProjectProperties();
297: projectProperties.put(
298: JbiProjectProperties.J2EE_SERVER_INSTANCE,
299: instance);
300: projectProperties.store();
301: }
302: }
303:
304: dlg.dispose();
305: }
306:
307: if (instance == null) {
308: NotifyDescriptor d = new NotifyDescriptor.Message(NbBundle
309: .getMessage(JbiActionProvider.class,
310: "MSG_NoSelectedServerError"), // NOI18N
311: NotifyDescriptor.ERROR_MESSAGE);
312: DialogDisplayer.getDefault().notify(d);
313: return false;
314: // } else if (!JbiManager.isRunningAppServer(instance)) {
315: // NotifyDescriptor d =
316: // new NotifyDescriptor.Message(
317: // NbBundle.getMessage(
318: // JbiActionProvider.class, "MSG_NoRunningServerError" // NOI18N
319: // ),
320: // NotifyDescriptor.ERROR_MESSAGE);
321: // DialogDisplayer.getDefault().notify(d);
322: // return false;
323: }
324:
325: return true;
326: }
327:
328: public static Properties getServerInstanceProperties(
329: String serverInstance) {
330: if (serverInstance == null) {
331: return null;
332: }
333:
334: Properties properties = new Properties();
335:
336: FileSystem defaultFileSystem = Repository.getDefault()
337: .getDefaultFileSystem();
338: FileObject dir = defaultFileSystem
339: .findResource("/J2EE/InstalledServers"); // NOI18N
340: FileObject[] ch = dir.getChildren();
341: String plugin = Deployment.getDefault().getServerID(
342: serverInstance);
343: for (int i = 0; i < ch.length; i++) {
344: FileObject file = ch[i];
345: String url = (String) ch[i].getAttribute(URL_ATTR);
346: if (url != null && url.equals(serverInstance)) {
347: String userName = (String) file
348: .getAttribute(USERNAME_ATTR);
349: String password = (String) file
350: .getAttribute(PASSWORD_ATTR);
351:
352: String tmp = url.substring(url.lastIndexOf("::") + 2); // NOI18N
353: String hostName = tmp.substring(0, tmp.indexOf(":")); // NOI18N
354: String port = tmp.substring(tmp.indexOf(":") + 1); // NOI18N
355:
356: properties.put(URL_ATTR, url);
357: properties.put(HOSTNAME_ATTR, hostName);
358: properties.put(PORT_ATTR, port);
359: properties.put(USERNAME_ATTR, userName);
360: properties.put(PASSWORD_ATTR, password);
361:
362: break;
363: }
364: }
365: return properties;
366: }
367:
368: private static StartServer getStartServer(String plugin,
369: String url, String userName, String password) {
370: try {
371: FileSystem defaultFileSystem = Repository.getDefault()
372: .getDefaultFileSystem();
373: FileObject file = defaultFileSystem
374: .findResource("/J2EE/DeploymentPlugins/" + plugin
375: + "/Factory.instance"); // NOI18N
376:
377: DataObject dob = DataObject.find(file);
378:
379: InstanceCookie cookie = (InstanceCookie) dob
380: .getCookie(InstanceCookie.class);
381:
382: DeploymentFactory deploymentFactory = (DeploymentFactory) cookie
383: .instanceCreate();
384: DeploymentManager deploymentManager = deploymentFactory
385: .getDeploymentManager(url, userName, password);
386:
387: file = defaultFileSystem
388: .findResource("J2EE/DeploymentPlugins/" + plugin
389: + "/OptionalFactory.instance"); // NOI18N
390:
391: dob = DataObject.find(file);
392:
393: cookie = (InstanceCookie) dob
394: .getCookie(InstanceCookie.class);
395:
396: OptionalDeploymentManagerFactory deploymentManagerFactory = (OptionalDeploymentManagerFactory) cookie
397: .instanceCreate();
398:
399: StartServer startServer = deploymentManagerFactory
400: .getStartServer(deploymentManager);
401:
402: return startServer;
403: } catch (Exception e) {
404: e.printStackTrace();
405: }
406:
407: return null;
408: }
409:
410: public static boolean isGlassFish(File candidate) {
411: //now test for AS 9 (J2EE 5.0) which should work for this plugin
412: File as9 = new File(candidate.getAbsolutePath()
413: + "/lib/dtds/sun-web-app_2_5-0.dtd"); // NOI18N
414: return as9.exists();
415: }
416:
417: public static boolean appServerInstalled() {
418: Deployment deployment = Deployment.getDefault();
419: String[] serverInstanceIDs = deployment.getServerInstanceIDs();
420: //String[] serverNames = new String[serverInstanceIDs.length];
421: //String[] serverURLs = new String[serverInstanceIDs.length];
422: for (int i = 0; i < serverInstanceIDs.length; i++) {
423: J2eePlatform platform = deployment
424: .getJ2eePlatform(serverInstanceIDs[i]);
425: if (platform != null) {
426: if (isAppServer(platform)) {
427: return true;
428: }
429: }
430: }
431: return false;
432: }
433:
434: public static String[] getAppServers() {
435: Deployment deployment = Deployment.getDefault();
436: String[] serverInstanceIDs = deployment.getServerInstanceIDs();
437: ArrayList arr = new ArrayList();
438: for (int i = 0; i < serverInstanceIDs.length; i++) {
439: // This is slow if the server instance is remote!
440: J2eePlatform platform = deployment
441: .getJ2eePlatform(serverInstanceIDs[i]);
442: if (platform != null) {
443: if (isAppServer(platform)) {
444: arr.add(serverInstanceIDs[i]);
445: }
446: }
447: }
448: return (String[]) arr.toArray(new String[arr.size()]);
449: }
450:
451: public static boolean isAppServer(String id) {
452: J2eePlatform platform = Deployment.getDefault()
453: .getJ2eePlatform(id);
454: // On Windows, the path should be case insensitive.
455: // An issue has been filed against j2eeserver at
456: // http://www.netbeans.org/issues/show_bug.cgi?id=84063
457: if (platform != null) {
458: return isAppServer(platform);
459: }
460: return false;
461: }
462:
463: static boolean isAppServer(J2eePlatform platform) {
464: File[] cps = platform.getClasspathEntries();
465: for (int j = 0; j < cps.length; j++) {
466: String path = cps[j].getPath();
467: if ((path.indexOf("javaee.jar") > 0) || // NOI18N
468: (path.indexOf("j2ee.jar") > 0)) { // NOI18N
469: return true;
470: }
471: }
472: return false;
473: }
474:
475: /**
476: * Used to get the netbeans classload of this class.
477: *
478: */
479: static class Empty {
480: // empty...
481: }
482: }
|