KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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.server;
12
13 import java.util.Dictionary JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.logging.Level JavaDoc;
16 import java.util.logging.Logger JavaDoc;
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 JavaDoc host;
28     private static int port = -1;
29     
30     public static void start(String JavaDoc webappName) throws CoreException {
31         Dictionary JavaDoc d = new Hashtable JavaDoc();
32         
33         // configure the port
34
d.put("http.port", new Integer JavaDoc(getPort())); //$NON-NLS-1$
35

36         // set the base URL
37
d.put("context.path", "/help"); //$NON-NLS-1$ //$NON-NLS-2$
38

39         // suppress Jetty INFO/DEBUG messages to stderr
40
Logger.getLogger("org.mortbay").setLevel(Level.WARNING); //$NON-NLS-1$
41

42         try {
43             JettyConfigurator.startServer(webappName, d);
44             ensureBundleStarted("org.eclipse.equinox.http.registry"); //$NON-NLS-1$
45
}
46         catch (Exception JavaDoc e) {
47             HelpBasePlugin.logError("An error occured while starting the help server", e); //$NON-NLS-1$
48
}
49     }
50
51     public static void stop(String JavaDoc webappName) throws CoreException {
52         try {
53             JettyConfigurator.stopServer(webappName);
54         }
55         catch (Exception JavaDoc e) {
56             HelpBasePlugin.logError("An error occured while stopping the help server", e); //$NON-NLS-1$
57
}
58     }
59
60     public static int getPort() {
61         if (port == -1) {
62             String JavaDoc portCommandLineOverride = HelpBasePlugin.getBundleContext().getProperty("server_port"); //$NON-NLS-1$
63
if (portCommandLineOverride != null && portCommandLineOverride.trim().length() > 0) {
64                 try {
65                     port = Integer.parseInt(portCommandLineOverride);
66                 }
67                 catch (NumberFormatException JavaDoc e) {
68                     String JavaDoc msg = "Help server port specified in VM arguments is invalid (" + portCommandLineOverride + ")"; //$NON-NLS-1$ //$NON-NLS-2$
69
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 JavaDoc getHost() {
80         if (host == null) {
81             String JavaDoc hostCommandLineOverride = HelpBasePlugin.getBundleContext().getProperty("server_host"); //$NON-NLS-1$
82
if (hostCommandLineOverride != null && hostCommandLineOverride.trim().length() > 0) {
83                 host = hostCommandLineOverride;
84             }
85             else {
86                 host = "127.0.0.1"; //$NON-NLS-1$
87
}
88         }
89         return host;
90     }
91     
92     private WebappManager() {
93     }
94     
95     /*
96      * Ensures that the bundle with the specified name and the highest available
97      * version is started.
98      */

99     private static void ensureBundleStarted(String JavaDoc 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