KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > jetty > internal > Activator


1 /*******************************************************************************
2  * Copyright (c) 2005-2007 Cognos Incorporated, 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  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12
13 package org.eclipse.equinox.http.jetty.internal;
14
15 import java.io.File JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.util.Dictionary JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import org.osgi.framework.*;
20 import org.osgi.service.cm.ManagedServiceFactory;
21 import org.osgi.service.startlevel.StartLevel;
22
23 public class Activator implements BundleActivator {
24
25     private static final String JavaDoc JETTY_WORK_DIR = "jettywork"; //$NON-NLS-1$
26
private static final String JavaDoc DEFAULT_PID = "default"; //$NON-NLS-1$
27
private static final String JavaDoc MANAGED_SERVICE_FACTORY_PID = "org.eclipse.equinox.http.jetty.config"; //$NON-NLS-1$
28

29     // OSGi Http Service suggest these properties for setting the default ports
30
private static final String JavaDoc ORG_OSGI_SERVICE_HTTP_PORT = "org.osgi.service.http.port"; //$NON-NLS-1$
31
private static final String JavaDoc ORG_OSGI_SERVICE_HTTP_PORT_SECURE = "org.osgi.service.http.port.secure"; //$NON-NLS-1$
32

33     // controls whether start() should automatically start an Http Service based on BundleContext properties (default is true)
34
// Note: only used if the bundle is explicitly started (e.g. not "lazy" activated)
35
private static final String JavaDoc AUTOSTART = "org.eclipse.equinox.http.jetty.autostart"; //$NON-NLS-1$
36

37     // The staticServerManager is use by the start and stopServer methods and must be accessed in a static synchronized block
38
// to ensure it is correctly handled in terms of the bundle life-cycle.
39
private static HttpServerManager staticServerManager;
40
41     private HttpServerManager httpServerManager;
42     private ServiceRegistration registration;
43
44     public void start(BundleContext context) throws Exception JavaDoc {
45         File JavaDoc jettyWorkDir = new File JavaDoc(context.getDataFile(""), JETTY_WORK_DIR); //$NON-NLS-1$
46
jettyWorkDir.mkdir();
47         httpServerManager = new HttpServerManager(jettyWorkDir);
48
49         String JavaDoc autostart = context.getProperty(AUTOSTART);
50         if ((autostart == null || Boolean.valueOf(autostart).booleanValue()) && ! isBundleActivationPolicyUsed(context)) {
51             Dictionary JavaDoc defaultSettings = createDefaultSettings(context);
52             httpServerManager.updated(DEFAULT_PID, defaultSettings);
53         }
54
55         Dictionary JavaDoc dictionary = new Hashtable JavaDoc();
56         dictionary.put(Constants.SERVICE_PID, MANAGED_SERVICE_FACTORY_PID);
57
58         registration = context.registerService(ManagedServiceFactory.class.getName(), httpServerManager, dictionary);
59         setStaticServerManager(httpServerManager);
60     }
61
62     private boolean isBundleActivationPolicyUsed(BundleContext context) {
63         ServiceReference reference = context.getServiceReference(StartLevel.class.getName());
64         StartLevel sl = ((reference != null) ? (StartLevel) context.getService(reference) : null);
65         if (sl != null) {
66             try {
67                 Bundle bundle = context.getBundle();
68                 Method JavaDoc isBundleActivationPolicyUsed = StartLevel.class.getMethod("isBundleActivationPolicyUsed", new Class JavaDoc[] {Bundle.class}); //$NON-NLS-1$
69
Boolean JavaDoc result = (Boolean JavaDoc) isBundleActivationPolicyUsed.invoke(sl, new Object JavaDoc[] {bundle});
70                 return result.booleanValue();
71             } catch (Exception JavaDoc e) {
72                 // ignore
73
// Bundle Activation Policy only available in StartLevel Service 1.1
74
} finally {
75                 context.ungetService(reference);
76             }
77         }
78         return false;
79     }
80
81     public void stop(BundleContext context) throws Exception JavaDoc {
82         setStaticServerManager(null);
83         registration.unregister();
84         registration = null;
85
86         httpServerManager.shutdown();
87         httpServerManager = null;
88     }
89
90     private Dictionary JavaDoc createDefaultSettings(BundleContext context) {
91         final String JavaDoc PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty."; //$NON-NLS-1$
92
Dictionary JavaDoc defaultSettings = new Hashtable JavaDoc();
93
94         // PID
95
defaultSettings.put(Constants.SERVICE_PID, DEFAULT_PID);
96
97         // HTTP Enabled (default is true)
98
String JavaDoc httpEnabledProperty = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTP_ENABLED);
99         Boolean JavaDoc httpEnabled = (httpEnabledProperty == null) ? Boolean.TRUE : new Boolean JavaDoc(httpEnabledProperty);
100         defaultSettings.put(HttpServerManager.HTTP_ENABLED, httpEnabled);
101
102         // HTTP Port
103
String JavaDoc httpPortProperty = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTP_PORT);
104         if (httpPortProperty == null)
105             httpPortProperty = context.getProperty(ORG_OSGI_SERVICE_HTTP_PORT);
106
107         int httpPort = 80;
108         if (httpPortProperty != null) {
109             try {
110                 httpPort = Integer.parseInt(httpPortProperty);
111             } catch (NumberFormatException JavaDoc e) {
112                 //(log this) ignore and use default
113
}
114         }
115         defaultSettings.put(HttpServerManager.HTTP_PORT, new Integer JavaDoc(httpPort));
116
117         // HTTP Host (default is 0.0.0.0)
118
String JavaDoc httpHost = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTP_HOST);
119         if (httpHost != null)
120             defaultSettings.put(HttpServerManager.HTTP_HOST, httpHost);
121
122         // HTTPS Enabled (default is false)
123
Boolean JavaDoc httpsEnabled = new Boolean JavaDoc(context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTPS_ENABLED));
124         defaultSettings.put(HttpServerManager.HTTPS_ENABLED, httpsEnabled);
125
126         if (httpsEnabled.booleanValue()) {
127             // HTTPS Port
128
String JavaDoc httpsPortProperty = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTPS_PORT);
129             if (httpPortProperty == null)
130                 httpPortProperty = context.getProperty(ORG_OSGI_SERVICE_HTTP_PORT_SECURE);
131
132             int httpsPort = 443;
133             if (httpsPortProperty != null) {
134                 try {
135                     httpsPort = Integer.parseInt(httpsPortProperty);
136                 } catch (NumberFormatException JavaDoc e) {
137                     //(log this) ignore and use default
138
}
139             }
140             defaultSettings.put(HttpServerManager.HTTPS_PORT, new Integer JavaDoc(httpsPort));
141
142             // HTTPS Host (default is 0.0.0.0)
143
String JavaDoc httpsHost = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTPS_HOST);
144             if (httpsHost != null)
145                 defaultSettings.put(HttpServerManager.HTTPS_HOST, httpsHost);
146
147             // SSL SETTINGS
148
String JavaDoc keystore = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_KEYSTORE);
149             if (keystore != null)
150                 defaultSettings.put(HttpServerManager.SSL_KEYSTORE, keystore);
151
152             String JavaDoc password = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_PASSWORD);
153             if (password != null)
154                 defaultSettings.put(HttpServerManager.SSL_PASSWORD, password);
155
156             String JavaDoc keypassword = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_KEYPASSWORD);
157             if (keypassword != null)
158                 defaultSettings.put(HttpServerManager.SSL_KEYPASSWORD, keypassword);
159
160             String JavaDoc needclientauth = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_NEEDCLIENTAUTH);
161             if (needclientauth != null)
162                 defaultSettings.put(HttpServerManager.SSL_NEEDCLIENTAUTH, new Boolean JavaDoc(needclientauth));
163
164             String JavaDoc wantclientauth = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_WANTCLIENTAUTH);
165             if (wantclientauth != null)
166                 defaultSettings.put(HttpServerManager.SSL_WANTCLIENTAUTH, new Boolean JavaDoc(wantclientauth));
167
168             String JavaDoc protocol = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_PROTOCOL);
169             if (protocol != null)
170                 defaultSettings.put(HttpServerManager.SSL_PROTOCOL, protocol);
171
172             String JavaDoc algorithm = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_ALGORITHM);
173             if (algorithm != null)
174                 defaultSettings.put(HttpServerManager.SSL_ALGORITHM, algorithm);
175
176             String JavaDoc keystoretype = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_KEYSTORETYPE);
177             if (keystoretype != null)
178                 defaultSettings.put(HttpServerManager.SSL_KEYSTORETYPE, keystoretype);
179         }
180
181         // Servlet Context Path
182
String JavaDoc contextpath = context.getProperty(PROPERTY_PREFIX + HttpServerManager.CONTEXT_PATH);
183         if (contextpath != null)
184             defaultSettings.put(HttpServerManager.CONTEXT_PATH, contextpath);
185
186         // Session Inactive Interval (timeout)
187
String JavaDoc sessionInactiveInterval = context.getProperty(PROPERTY_PREFIX + HttpServerManager.CONTEXT_SESSIONINACTIVEINTERVAL);
188         if (sessionInactiveInterval != null) {
189             try {
190                 defaultSettings.put(HttpServerManager.CONTEXT_SESSIONINACTIVEINTERVAL, new Integer JavaDoc(sessionInactiveInterval));
191             } catch (NumberFormatException JavaDoc e) {
192                 //(log this) ignore
193
}
194         }
195
196         // Other Info
197
String JavaDoc otherInfo = context.getProperty(PROPERTY_PREFIX + HttpServerManager.OTHER_INFO);
198         if (otherInfo != null)
199             defaultSettings.put(HttpServerManager.OTHER_INFO, otherInfo);
200
201         return defaultSettings;
202     }
203
204     public synchronized static void startServer(String JavaDoc pid, Dictionary JavaDoc settings) throws Exception JavaDoc {
205         if (staticServerManager == null)
206             throw new IllegalStateException JavaDoc("Inactive"); //$NON-NLS-1$
207

208         staticServerManager.updated(pid, settings);
209     }
210
211     public synchronized static void stopServer(String JavaDoc pid) throws Exception JavaDoc {
212         if (staticServerManager != null)
213             staticServerManager.deleted(pid);
214     }
215
216     private synchronized static void setStaticServerManager(HttpServerManager httpServerManager) {
217         staticServerManager = httpServerManager;
218     }
219
220 }
221
Popular Tags