KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > appserver > WebappManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.appserver;
12
13 import java.io.*;
14 import java.net.*;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.util.NLS;
18 import org.osgi.framework.*;
19
20 /**
21  * Singleton class to be called by clients to run a webapp.
22  *
23  * @since 2.1
24  * @deprecated This internal interface is no longer used by the Eclipse Help
25  * system and should not be used by anyone else. It is likely to be removed
26  * in a future release.
27  * Use the HTTP service implementation provided by Equinox that is based
28  * on Jetty, see http://www.eclipse.org/equinox/server.
29  */

30 public class WebappManager {
31     private static boolean applicationsStarted = false;
32
33     /**
34      * Private constructor, so no instances can be created.
35      *
36      * @see java.lang.Object#Object()
37      */

38     private WebappManager() {
39     }
40
41     /**
42      * Runs a webapp on the server. The webapp is defined in a plugin and the
43      * path is relative to the plugin directory.
44      * <p>
45      * It is assumed that webapp names are unique. It is suggested to create
46      * unique web app names by prefixing them with the plugin id.
47      * </p>
48      *
49      * @param webappName
50      * the name of the web app (also knowns as application context)
51      * @param pluginId
52      * plugin that defines the webapp
53      * @param path
54      * webapp relative path to the plugin directory
55      */

56     public static void start(String JavaDoc webappName, String JavaDoc pluginId, IPath path)
57             throws CoreException {
58
59         IPath webappPath = getWebappPath(pluginId, path);
60
61         // we get the server before constructing the class loader, so
62
// class loader exposed by the server is available to the webapps.
63
IWebappServer server = AppserverPlugin.getDefault().getAppServer();
64         applicationsStarted = true;
65         server.start(webappName, webappPath, new PluginClassLoaderWrapper(
66                 pluginId));
67     }
68
69     /**
70      * Stops the specified webapp.
71      */

72     public static void stop(String JavaDoc webappName) throws CoreException {
73         if (!applicationsStarted) {
74             // do not obtain (start) appserver when no reason
75
return;
76         }
77         AppserverPlugin.getDefault().getAppServer().stop(webappName);
78     }
79
80     /**
81      * Returns the port number the app server listens on.
82      *
83      * @return integer port number, 0 if server not started
84      */

85     public static int getPort() {
86         try {
87             return AppserverPlugin.getDefault().getAppServer().getPort();
88         } catch (CoreException e) {
89             return 0;
90         }
91     }
92
93     /**
94      * Returns the host name or ip the app server runs on.
95      *
96      * @return String representaion of host name of IP, null if server not
97      * started yet
98      */

99     public static String JavaDoc getHost() {
100         try {
101             return AppserverPlugin.getDefault().getAppServer().getHost();
102         } catch (CoreException e) {
103             return null;
104         }
105     }
106
107     /**
108      * @param pluginId
109      * @param path
110      * webapp path relative to the plugin directory
111      * @return String absolute webapp path
112      */

113     private static IPath getWebappPath(String JavaDoc pluginId, IPath path)
114             throws CoreException {
115
116         Bundle bundle = Platform.getBundle(pluginId);
117         if (bundle == null) {
118             throw new CoreException(new Status(IStatus.ERROR,
119                     AppserverPlugin.PLUGIN_ID, IStatus.OK, NLS.bind(AppserverResources.Appserver_cannotFindPlugin, pluginId), null));
120         }
121
122         // Note: we just look for one webapp directory.
123
// If needed, may want to use the locale specific path.
124
URL webappURL = FileLocator.find(bundle, path, null);
125         if (webappURL == null) {
126             throw new CoreException(new Status(IStatus.ERROR,
127                     AppserverPlugin.PLUGIN_ID, IStatus.OK, NLS.bind(AppserverResources.Appserver_cannotFindPath, pluginId, path.toOSString()), null));
128         }
129
130         try {
131             String JavaDoc webappLocation = FileLocator.toFileURL(
132                     FileLocator.resolve(webappURL)).getFile();
133             return new Path(webappLocation);
134         } catch (IOException ioe) {
135             throw new CoreException(new Status(IStatus.ERROR,
136                     AppserverPlugin.PLUGIN_ID, IStatus.OK, NLS.bind(AppserverResources.Appserver_cannotResolvePath, pluginId, path.toOSString()), ioe));
137         }
138     }
139 }
140
Popular Tags