1 12 13 package org.eclipse.equinox.http.servlet.internal; 14 15 import java.util.*; 16 import javax.servlet.ServletConfig ; 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 DEFAULT_SERVICE_DESCRIPTION = "Equinox Servlet Bridge"; private static final String DEFAULT_SERVICE_VENDOR = "Eclipse.org"; 25 private static BundleContext context; 26 private static Map serviceRegistrations = new HashMap(); 27 28 public void start(BundleContext context) throws Exception { 29 startHttpServiceProxy(context); 30 } 31 32 public void stop(BundleContext context) throws Exception { 33 stopHttpServiceProxy(context); 34 } 35 36 private static synchronized void startHttpServiceProxy(BundleContext bundleContext) { 37 context = bundleContext; 38 Object [] 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 [] 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 config = proxyServlet.getServletConfig(); 66 Enumeration initparameterNames = config.getInitParameterNames(); 67 while (initparameterNames.hasMoreElements()) { 68 String name = (String ) 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 |