1 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 25 public class AppserverPlugin extends Plugin { 26 public final static String PLUGIN_ID = "org.eclipse.help.appserver"; public final static String HOST_KEY = "host"; public final static String PORT_KEY = "port"; private final static String APP_SERVER_EXTENSION_ID = PLUGIN_ID + ".server"; private static final String APP_SERVER_CLASS_ATTRIBUTE = "class"; private static final String APP_SERVER_DEFAULT_ATTRIBUTE = "default"; private static AppserverPlugin plugin; 34 private IWebappServer appServer; 36 private String contributingServerPlugin; 37 private String hostAddress; 38 private int port; 39 41 public static AppserverPlugin getDefault() { 42 return plugin; 43 } 44 47 public synchronized IWebappServer getAppServer() throws CoreException { 48 if (appServer == null) { 49 createWebappServer(); 50 startWebappServer(); 51 } 52 return appServer; 53 } 54 57 public static synchronized void logError(String message, Throwable ex) { 58 if (message == null) 59 message = ""; Status errorStatus = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, 61 message, ex); 62 AppserverPlugin.getDefault().getLog().log(errorStatus); 63 } 64 69 public void stop(BundleContext context) throws Exception { 70 if (appServer != null) { 71 appServer.stop(); 72 } 73 plugin = null; 74 super.stop(context); 76 } 77 82 public void start(BundleContext context) throws Exception { 83 super.start(context); 84 plugin = this; 85 } 87 92 public String getContributingServerPlugin() { 93 return contributingServerPlugin; 94 } 95 private void createWebappServer() throws CoreException { 96 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 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 defaultValue = elements[i] 112 .getAttribute(APP_SERVER_DEFAULT_ATTRIBUTE); 113 if (defaultValue == null || defaultValue.equals("false")) { serverElement = elements[i]; 115 break; 116 } 117 } 118 if (serverElement == null) 120 serverElement = elements[0]; 121 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 hostAddress = getPluginPreferences().getString(HOST_KEY); 137 if ("".equals(hostAddress)) { hostAddress = null; 139 } 140 port = getPluginPreferences().getInt(PORT_KEY); 141 try { 143 String hostCommandLineOverride = System.getProperty("server_host"); if (hostCommandLineOverride != null 145 && hostCommandLineOverride.trim().length() > 0) { 146 hostAddress = hostCommandLineOverride; 147 } 148 } catch (Exception e) { 149 } 150 try { 151 String portCommandLineOverride = System.getProperty("server_port"); if (portCommandLineOverride != null 153 && portCommandLineOverride.trim().length() > 0) { 154 port = Integer.parseInt(portCommandLineOverride); 155 } 156 } catch (Exception 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 |