KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.IExtension;
16 import org.eclipse.core.runtime.IExtensionPoint;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.Plugin;
20 import org.eclipse.core.runtime.Status;
21 import org.osgi.framework.BundleContext;
22
23 /**
24  */

25 public class AppserverPlugin extends Plugin {
26     public final static String JavaDoc PLUGIN_ID = "org.eclipse.help.appserver"; //$NON-NLS-1$
27
public final static String JavaDoc HOST_KEY = "host"; //$NON-NLS-1$
28
public final static String JavaDoc PORT_KEY = "port"; //$NON-NLS-1$
29
private final static String JavaDoc APP_SERVER_EXTENSION_ID = PLUGIN_ID + ".server"; //$NON-NLS-1$
30
private static final String JavaDoc APP_SERVER_CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
31
private static final String JavaDoc APP_SERVER_DEFAULT_ATTRIBUTE = "default"; //$NON-NLS-1$
32
// singleton object
33
private static AppserverPlugin plugin;
34 // private static BundleContext bundleContext;
35
private IWebappServer appServer;
36     private String JavaDoc contributingServerPlugin;
37     private String JavaDoc hostAddress;
38     private int port;
39     /**
40      */

41     public static AppserverPlugin getDefault() {
42         return plugin;
43     }
44     /**
45      * Returns the instance of WebappServer.
46      */

47     public synchronized IWebappServer getAppServer() throws CoreException {
48         if (appServer == null) {
49             createWebappServer();
50             startWebappServer();
51         }
52         return appServer;
53     }
54     /**
55      * Logs an Error message with an exception.
56      */

57     public static synchronized void logError(String JavaDoc message, Throwable JavaDoc ex) {
58         if (message == null)
59             message = ""; //$NON-NLS-1$
60
Status errorStatus = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK,
61                 message, ex);
62         AppserverPlugin.getDefault().getLog().log(errorStatus);
63     }
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
68      */

69     public void stop(BundleContext context) throws Exception JavaDoc {
70         if (appServer != null) {
71             appServer.stop();
72         }
73         plugin = null;
74 // bundleContext = null;
75
super.stop(context);
76     }
77     /*
78      * (non-Javadoc)
79      *
80      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
81      */

82     public void start(BundleContext context) throws Exception JavaDoc {
83         super.start(context);
84         plugin = this;
85 // bundleContext = context;
86
}
87     /**
88      * Returns the plugin ID that contributes the server implementation
89      *
90      * @return String
91      */

92     public String JavaDoc getContributingServerPlugin() {
93         return contributingServerPlugin;
94     }
95     private void createWebappServer() throws CoreException {
96         // Initializes the app server by getting an instance via
97
// app-server the extension point
98
// get the app server extension from the system plugin registry
99
IExtensionPoint point = Platform.getExtensionRegistry()
100                 .getExtensionPoint(APP_SERVER_EXTENSION_ID);
101         if (point != null) {
102             IExtension[] extensions = point.getExtensions();
103             if (extensions.length != 0) {
104                 // We need to pick up the non-default configuration
105
IConfigurationElement[] elements = extensions[0]
106                         .getConfigurationElements();
107                 if (elements.length == 0)
108                     return;
109                 IConfigurationElement serverElement = null;
110                 for (int i = 0; i < elements.length; i++) {
111                     String JavaDoc defaultValue = elements[i]
112                             .getAttribute(APP_SERVER_DEFAULT_ATTRIBUTE);
113                     if (defaultValue == null || defaultValue.equals("false")) { //$NON-NLS-1$
114
serverElement = elements[i];
115                         break;
116                     }
117                 }
118                 // if all the servers are default, then pick the first one
119
if (serverElement == null)
120                     serverElement = elements[0];
121                 // Instantiate the app server
122
try {
123                     appServer = (IWebappServer) serverElement
124                             .createExecutableExtension(APP_SERVER_CLASS_ATTRIBUTE);
125                     contributingServerPlugin = serverElement
126                             .getContributor().getName();
127                 } catch (CoreException e) {
128                     getLog().log(e.getStatus());
129                     throw e;
130                 }
131             }
132         }
133     }
134     private void startWebappServer() throws CoreException {
135         // Initialize host and port from preferences
136
hostAddress = getPluginPreferences().getString(HOST_KEY);
137         if ("".equals(hostAddress)) { //$NON-NLS-1$
138
hostAddress = null;
139         }
140         port = getPluginPreferences().getInt(PORT_KEY);
141         // apply host and port overrides passed as command line arguments
142
try {
143             String JavaDoc hostCommandLineOverride = System.getProperty("server_host"); //$NON-NLS-1$
144
if (hostCommandLineOverride != null
145                     && hostCommandLineOverride.trim().length() > 0) {
146                 hostAddress = hostCommandLineOverride;
147             }
148         } catch (Exception JavaDoc e) {
149         }
150         try {
151             String JavaDoc portCommandLineOverride = System.getProperty("server_port"); //$NON-NLS-1$
152
if (portCommandLineOverride != null
153                     && portCommandLineOverride.trim().length() > 0) {
154                 port = Integer.parseInt(portCommandLineOverride);
155             }
156         } catch (Exception JavaDoc e) {
157         }
158         if (appServer == null)
159             throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
160                     IStatus.OK,
161                     AppserverResources.Appserver_start, null));
162         appServer.start(port, hostAddress);
163     }
164 }
165
Popular Tags