KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > adaptor > core > ServiceRegistryImpl


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.adaptor.core;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import org.osgi.framework.*;
17
18 /**
19  * A default implementation of the ServiceRegistry.
20  */

21 public class ServiceRegistryImpl implements org.eclipse.osgi.framework.adaptor.ServiceRegistry {
22
23     /** Published services by class name. Key is a String class name; Value is a ArrayList of ServiceRegistrations */
24     protected HashMap JavaDoc publishedServicesByClass;
25     /** All published services. Value is ServiceRegistrations */
26     protected ArrayList JavaDoc allPublishedServices;
27     /** Published services by BundleContext. Key is a BundleContext; Value is a ArrayList of ServiceRegistrations*/
28     protected HashMap JavaDoc publishedServicesByContext;
29
30     /**
31      * Initializes the internal data structures of this ServiceRegistry.
32      *
33      */

34     public void initialize() {
35         publishedServicesByClass = new HashMap JavaDoc(50);
36         publishedServicesByContext = new HashMap JavaDoc(50);
37         allPublishedServices = new ArrayList JavaDoc(50);
38     }
39
40     /**
41      * @see org.eclipse.osgi.framework.adaptor.ServiceRegistry#publishService(BundleContext, ServiceRegistration)
42      */

43     public void publishService(BundleContext context, ServiceRegistration serviceReg) {
44
45         // Add the ServiceRegistration to the list of Services published by BundleContext.
46
ArrayList JavaDoc contextServices = (ArrayList JavaDoc) publishedServicesByContext.get(context);
47         if (contextServices == null) {
48             contextServices = new ArrayList JavaDoc(10);
49             publishedServicesByContext.put(context, contextServices);
50         }
51         contextServices.add(serviceReg);
52
53         // Add the ServiceRegistration to the list of Services published by Class Name.
54
String JavaDoc[] clazzes = (String JavaDoc[]) serviceReg.getReference().getProperty(Constants.OBJECTCLASS);
55         int size = clazzes.length;
56
57         for (int i = 0; i < size; i++) {
58             String JavaDoc clazz = clazzes[i];
59
60             ArrayList JavaDoc services = (ArrayList JavaDoc) publishedServicesByClass.get(clazz);
61
62             if (services == null) {
63                 services = new ArrayList JavaDoc(10);
64                 publishedServicesByClass.put(clazz, services);
65             }
66
67             services.add(serviceReg);
68         }
69
70         // Add the ServiceRegistration to the list of all published Services.
71
allPublishedServices.add(serviceReg);
72     }
73
74     /**
75      * @see org.eclipse.osgi.framework.adaptor.ServiceRegistry#unpublishService(BundleContext, ServiceRegistration)
76      */

77     public void unpublishService(BundleContext context, ServiceRegistration serviceReg) {
78
79         // Remove the ServiceRegistration from the list of Services published by BundleContext.
80
ArrayList JavaDoc contextServices = (ArrayList JavaDoc) publishedServicesByContext.get(context);
81         if (contextServices != null) {
82             contextServices.remove(serviceReg);
83         }
84
85         // Remove the ServiceRegistration from the list of Services published by Class Name.
86
String JavaDoc[] clazzes = (String JavaDoc[]) serviceReg.getReference().getProperty(Constants.OBJECTCLASS);
87         int size = clazzes.length;
88
89         for (int i = 0; i < size; i++) {
90             String JavaDoc clazz = clazzes[i];
91             ArrayList JavaDoc services = (ArrayList JavaDoc) publishedServicesByClass.get(clazz);
92             services.remove(serviceReg);
93         }
94
95         // Remove the ServiceRegistration from the list of all published Services.
96
allPublishedServices.remove(serviceReg);
97
98     }
99
100     /**
101      * @see org.eclipse.osgi.framework.adaptor.ServiceRegistry#unpublishServices(BundleContext)
102      */

103     public void unpublishServices(BundleContext context) {
104         // Get all the Services published by the BundleContext.
105
ArrayList JavaDoc serviceRegs = (ArrayList JavaDoc) publishedServicesByContext.get(context);
106         if (serviceRegs != null) {
107             // Remove this list for the BundleContext
108
publishedServicesByContext.remove(context);
109             int size = serviceRegs.size();
110             for (int i = 0; i < size; i++) {
111                 ServiceRegistration serviceReg = (ServiceRegistration) serviceRegs.get(i);
112                 // Remove each service from the list of all published Services
113
allPublishedServices.remove(serviceReg);
114
115                 // Remove each service from the list of Services published by Class Name.
116
String JavaDoc[] clazzes = (String JavaDoc[]) serviceReg.getReference().getProperty(Constants.OBJECTCLASS);
117                 int numclazzes = clazzes.length;
118
119                 for (int j = 0; j < numclazzes; j++) {
120                     String JavaDoc clazz = clazzes[j];
121                     ArrayList JavaDoc services = (ArrayList JavaDoc) publishedServicesByClass.get(clazz);
122                     services.remove(serviceReg);
123                 }
124             }
125         }
126     }
127
128     /**
129      * @see org.eclipse.osgi.framework.adaptor.ServiceRegistry#lookupServiceReferences(String, Filter)
130      */

131     public ServiceReference[] lookupServiceReferences(String JavaDoc clazz, Filter filter) {
132         int size;
133         ArrayList JavaDoc references;
134         ArrayList JavaDoc serviceRegs;
135         if (clazz == null) /* all services */
136             serviceRegs = allPublishedServices;
137         else
138             /* services registered under the class name */
139             serviceRegs = (ArrayList JavaDoc) publishedServicesByClass.get(clazz);
140
141         if (serviceRegs == null)
142             return (null);
143
144         size = serviceRegs.size();
145
146         if (size == 0)
147             return (null);
148
149         references = new ArrayList JavaDoc(size);
150         for (int i = 0; i < size; i++) {
151             ServiceRegistration registration = (ServiceRegistration) serviceRegs.get(i);
152
153             ServiceReference reference = registration.getReference();
154             if ((filter == null) || filter.match(reference)) {
155                 references.add(reference);
156             }
157         }
158
159         if (references.size() == 0) {
160             return null;
161         }
162
163         return (ServiceReference[]) references.toArray(new ServiceReference[references.size()]);
164
165     }
166
167     /**
168      * @see org.eclipse.osgi.framework.adaptor.ServiceRegistry#lookupServiceReferences(BundleContext)
169      */

170     public ServiceReference[] lookupServiceReferences(BundleContext context) {
171         int size;
172         ArrayList JavaDoc references;
173         ArrayList JavaDoc serviceRegs = (ArrayList JavaDoc) publishedServicesByContext.get(context);
174
175         if (serviceRegs == null) {
176             return (null);
177         }
178
179         size = serviceRegs.size();
180
181         if (size == 0) {
182             return (null);
183         }
184
185         references = new ArrayList JavaDoc(size);
186         for (int i = 0; i < size; i++) {
187             ServiceRegistration registration = (ServiceRegistration) serviceRegs.get(i);
188
189             ServiceReference reference = registration.getReference();
190             references.add(reference);
191         }
192
193         if (references.size() == 0) {
194             return null;
195         }
196
197         return (ServiceReference[]) references.toArray(new ServiceReference[references.size()]);
198     }
199
200 }
Popular Tags