KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > servlet > 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.servlet.internal;
14
15 import java.util.*;
16 import javax.servlet.ServletConfig JavaDoc;
17 import org.osgi.framework.*;
18 import org.osgi.service.http.HttpService;
19
20 public class Activator implements BundleActivator {
21
22     private static final String JavaDoc DEFAULT_SERVICE_DESCRIPTION = "Equinox Servlet Bridge"; //$NON-NLS-1$
23
private static final String JavaDoc DEFAULT_SERVICE_VENDOR = "Eclipse.org"; //$NON-NLS-1$
24

25     private static BundleContext context;
26     private static Map serviceRegistrations = new HashMap();
27
28     public void start(BundleContext context) throws Exception JavaDoc {
29         startHttpServiceProxy(context);
30     }
31
32     public void stop(BundleContext context) throws Exception JavaDoc {
33         stopHttpServiceProxy(context);
34     }
35
36     private static synchronized void startHttpServiceProxy(BundleContext bundleContext) {
37         context = bundleContext;
38         Object JavaDoc[] proxyServlets = serviceRegistrations.keySet().toArray();
39         for (int i = 0; i < proxyServlets.length; ++i) {
40             ServiceRegistration registration = registerHttpService((ProxyServlet) proxyServlets[i]);
41             serviceRegistrations.put(proxyServlets[i], registration);
42         }
43     }
44
45     private static synchronized void stopHttpServiceProxy(BundleContext bundleContext) {
46         Object JavaDoc[] proxyServlets = serviceRegistrations.keySet().toArray();
47         for (int i = 0; i < proxyServlets.length; ++i) {
48             ServiceRegistration registration = (ServiceRegistration) serviceRegistrations.put(proxyServlets[i], null);
49             registration.unregister();
50         }
51         context = null;
52     }
53
54     static synchronized void addProxyServlet(ProxyServlet proxyServlet) {
55         ServiceRegistration registration = null;
56         if (context != null)
57             registration = registerHttpService(proxyServlet);
58
59         serviceRegistrations.put(proxyServlet, registration);
60     }
61
62     private static ServiceRegistration registerHttpService(ProxyServlet proxyServlet) {
63         HttpServiceFactory factory = new HttpServiceFactory(proxyServlet);
64         Dictionary serviceProperties = new Hashtable(2);
65         ServletConfig JavaDoc config = proxyServlet.getServletConfig();
66         Enumeration initparameterNames = config.getInitParameterNames();
67         while (initparameterNames.hasMoreElements()) {
68             String JavaDoc name = (String JavaDoc) initparameterNames.nextElement();
69             serviceProperties.put(name, config.getInitParameter(name));
70         }
71
72         if (serviceProperties.get(Constants.SERVICE_VENDOR) == null)
73             serviceProperties.put(Constants.SERVICE_VENDOR, DEFAULT_SERVICE_VENDOR);
74
75         if (serviceProperties.get(Constants.SERVICE_DESCRIPTION) == null)
76             serviceProperties.put(Constants.SERVICE_DESCRIPTION, DEFAULT_SERVICE_DESCRIPTION);
77
78         return context.registerService(HttpService.class.getName(), factory, serviceProperties);
79     }
80
81     static synchronized void removeProxyServlet(ProxyServlet proxyServlet) {
82         ServiceRegistration registration = (ServiceRegistration) serviceRegistrations.remove(proxyServlet);
83         if (registration != null)
84             registration.unregister();
85     }
86 }
87
Popular Tags