1 11 package org.eclipse.help.internal.server; 12 13 import java.util.Dictionary ; 14 import java.util.Hashtable ; 15 import java.util.logging.Level ; 16 import java.util.logging.Logger ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.equinox.http.jetty.JettyConfigurator; 21 import org.eclipse.help.internal.base.HelpBasePlugin; 22 import org.osgi.framework.Bundle; 23 import org.osgi.framework.BundleException; 24 25 public class WebappManager { 26 27 private static String host; 28 private static int port = -1; 29 30 public static void start(String webappName) throws CoreException { 31 Dictionary d = new Hashtable (); 32 33 d.put("http.port", new Integer (getPort())); 36 d.put("context.path", "/help"); 39 Logger.getLogger("org.mortbay").setLevel(Level.WARNING); 42 try { 43 JettyConfigurator.startServer(webappName, d); 44 ensureBundleStarted("org.eclipse.equinox.http.registry"); } 46 catch (Exception e) { 47 HelpBasePlugin.logError("An error occured while starting the help server", e); } 49 } 50 51 public static void stop(String webappName) throws CoreException { 52 try { 53 JettyConfigurator.stopServer(webappName); 54 } 55 catch (Exception e) { 56 HelpBasePlugin.logError("An error occured while stopping the help server", e); } 58 } 59 60 public static int getPort() { 61 if (port == -1) { 62 String portCommandLineOverride = HelpBasePlugin.getBundleContext().getProperty("server_port"); if (portCommandLineOverride != null && portCommandLineOverride.trim().length() > 0) { 64 try { 65 port = Integer.parseInt(portCommandLineOverride); 66 } 67 catch (NumberFormatException e) { 68 String msg = "Help server port specified in VM arguments is invalid (" + portCommandLineOverride + ")"; HelpBasePlugin.logError(msg, e); 70 } 71 } 72 if (port == -1) { 73 port = SocketUtil.findUnusedLocalPort(); 74 } 75 } 76 return port; 77 } 78 79 public static String getHost() { 80 if (host == null) { 81 String hostCommandLineOverride = HelpBasePlugin.getBundleContext().getProperty("server_host"); if (hostCommandLineOverride != null && hostCommandLineOverride.trim().length() > 0) { 83 host = hostCommandLineOverride; 84 } 85 else { 86 host = "127.0.0.1"; } 88 } 89 return host; 90 } 91 92 private WebappManager() { 93 } 94 95 99 private static void ensureBundleStarted(String symbolicName) throws BundleException { 100 Bundle bundle = Platform.getBundle(symbolicName); 101 if (bundle != null) { 102 if (bundle.getState() == Bundle.RESOLVED) { 103 bundle.start(Bundle.START_TRANSIENT); 104 } 105 } 106 } 107 } 108 | Popular Tags |