KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > registry > internal > HttpServiceTracker


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.registry.internal;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
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 JavaDoc httpRegistryManagers = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc service) {
65         // ignored
66
}
67
68     public synchronized void removedService(ServiceReference reference, Object JavaDoc 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 JavaDoc getService(Bundle bundle, ServiceRegistration registration) {
79             return new HttpContextExtensionServiceImpl(bundle);
80         }
81
82         public void ungetService(Bundle bundle, ServiceRegistration registration, Object JavaDoc service) {
83             // do nothing
84
}
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 JavaDoc 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