1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import org.osgi.framework.*; 17 18 21 public class ServiceRegistryImpl implements org.eclipse.osgi.framework.adaptor.ServiceRegistry { 22 23 24 protected HashMap publishedServicesByClass; 25 26 protected ArrayList allPublishedServices; 27 28 protected HashMap publishedServicesByContext; 29 30 34 public void initialize() { 35 publishedServicesByClass = new HashMap (50); 36 publishedServicesByContext = new HashMap (50); 37 allPublishedServices = new ArrayList (50); 38 } 39 40 43 public void publishService(BundleContext context, ServiceRegistration serviceReg) { 44 45 ArrayList contextServices = (ArrayList ) publishedServicesByContext.get(context); 47 if (contextServices == null) { 48 contextServices = new ArrayList (10); 49 publishedServicesByContext.put(context, contextServices); 50 } 51 contextServices.add(serviceReg); 52 53 String [] clazzes = ((ServiceRegistrationImpl) serviceReg).clazzes; 55 int size = clazzes.length; 56 57 for (int i = 0; i < size; i++) { 58 String clazz = clazzes[i]; 59 60 ArrayList services = (ArrayList ) publishedServicesByClass.get(clazz); 61 62 if (services == null) { 63 services = new ArrayList (10); 64 publishedServicesByClass.put(clazz, services); 65 } 66 67 services.add(serviceReg); 68 } 69 70 allPublishedServices.add(serviceReg); 72 } 73 74 77 public void unpublishService(BundleContext context, ServiceRegistration serviceReg) { 78 79 ArrayList contextServices = (ArrayList ) publishedServicesByContext.get(context); 81 if (contextServices != null) { 82 contextServices.remove(serviceReg); 83 } 84 85 String [] clazzes = ((ServiceRegistrationImpl) serviceReg).clazzes; 87 int size = clazzes.length; 88 89 for (int i = 0; i < size; i++) { 90 String clazz = clazzes[i]; 91 ArrayList services = (ArrayList ) publishedServicesByClass.get(clazz); 92 services.remove(serviceReg); 93 } 94 95 allPublishedServices.remove(serviceReg); 97 98 } 99 100 103 public void unpublishServices(BundleContext context) { 104 ArrayList serviceRegs = (ArrayList ) publishedServicesByContext.get(context); 106 if (serviceRegs != null) { 107 publishedServicesByContext.remove(context); 109 int size = serviceRegs.size(); 110 for (int i = 0; i < size; i++) { 111 ServiceRegistrationImpl serviceReg = (ServiceRegistrationImpl) serviceRegs.get(i); 112 allPublishedServices.remove(serviceReg); 114 115 String [] clazzes = serviceReg.clazzes; 117 int numclazzes = clazzes.length; 118 119 for (int j = 0; j < numclazzes; j++) { 120 String clazz = clazzes[j]; 121 ArrayList services = (ArrayList ) publishedServicesByClass.get(clazz); 122 services.remove(serviceReg); 123 } 124 } 125 } 126 } 127 128 131 public ServiceReference[] lookupServiceReferences(String clazz, Filter filter) { 132 int size; 133 ArrayList references; 134 ArrayList serviceRegs; 135 if (clazz == null) 136 serviceRegs = allPublishedServices; 137 else 138 139 serviceRegs = (ArrayList ) 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 (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 170 public ServiceReference[] lookupServiceReferences(BundleContext context) { 171 int size; 172 ArrayList references; 173 ArrayList serviceRegs = (ArrayList ) 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 (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 } 201 | Popular Tags |