1 12 13 package org.eclipse.equinox.http.registry.internal; 14 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import org.eclipse.core.runtime.IExtensionRegistry; 18 import org.eclipse.equinox.http.registry.HttpContextExtensionService; 19 import org.osgi.framework.*; 20 import org.osgi.service.http.HttpContext; 21 import org.osgi.service.http.HttpService; 22 import org.osgi.service.packageadmin.PackageAdmin; 23 import org.osgi.util.tracker.ServiceTracker; 24 25 public class HttpServiceTracker extends ServiceTracker { 26 27 private BundleContext context; 28 private PackageAdmin packageAdmin; 29 private IExtensionRegistry registry; 30 31 private ServiceRegistration registration; 32 Map httpRegistryManagers = new HashMap (); 33 34 public HttpServiceTracker(BundleContext context, PackageAdmin packageAdmin, IExtensionRegistry registry) { 35 super(context, HttpService.class.getName(), null); 36 this.context = context; 37 this.packageAdmin = packageAdmin; 38 this.registry = registry; 39 } 40 41 public void open() { 42 super.open(); 43 registration = context.registerService(HttpContextExtensionService.class.getName(), new HttpContextExtensionServiceFactory(), null); 44 } 45 46 public void close() { 47 registration.unregister(); 48 registration = null; 49 super.close(); 50 } 51 52 public synchronized Object addingService(ServiceReference reference) { 53 HttpService httpService = (HttpService) super.addingService(reference); 54 if (httpService == null) 55 return null; 56 57 HttpRegistryManager httpRegistryManager = new HttpRegistryManager(reference, httpService, packageAdmin, registry); 58 httpRegistryManager.start(); 59 httpRegistryManagers.put(reference, httpRegistryManager); 60 61 return httpService; 62 } 63 64 public void modifiedService(ServiceReference reference, Object service) { 65 } 67 68 public synchronized void removedService(ServiceReference reference, Object service) { 69 HttpRegistryManager httpRegistryManager = (HttpRegistryManager) httpRegistryManagers.remove(reference); 70 if (httpRegistryManager != null) { 71 httpRegistryManager.stop(); 72 } 73 super.removedService(reference, service); 74 } 75 76 public class HttpContextExtensionServiceFactory implements ServiceFactory { 77 78 public Object getService(Bundle bundle, ServiceRegistration registration) { 79 return new HttpContextExtensionServiceImpl(bundle); 80 } 81 82 public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { 83 } 85 } 86 87 public class HttpContextExtensionServiceImpl implements HttpContextExtensionService { 88 89 private Bundle bundle; 90 91 public HttpContextExtensionServiceImpl(Bundle bundle) { 92 this.bundle = bundle; 93 } 94 95 public HttpContext getHttpContext(ServiceReference httpServiceReference, String httpContextId) { 96 synchronized (HttpServiceTracker.this) { 97 HttpRegistryManager httpRegistryManager = (HttpRegistryManager) httpRegistryManagers.get(httpServiceReference); 98 if (httpRegistryManager == null) 99 return null; 100 101 return httpRegistryManager.getHttpContext(httpContextId, bundle); 102 } 103 } 104 } 105 } 106 | Popular Tags |