1 26 27 package com.opensugar.cube.serviceRegistry; 28 29 import com.opensugar.cube.AbstractCube; 30 import com.opensugar.cube.BundleImpl; 31 import com.opensugar.cube.ServiceRegistrationImpl; 32 import com.opensugar.cube.ServiceReferenceImpl; 33 import com.opensugar.cube.UnreachableCodeException; 34 import com.opensugar.cube.ldap.LDAPFilter; 35 36 import org.osgi.framework.ServiceReference; 37 import org.osgi.framework.ServiceRegistration; 38 import org.osgi.framework.InvalidSyntaxException; 39 import org.osgi.service.packageadmin.PackageAdmin; 40 import org.osgi.service.permissionadmin.PermissionAdmin; 41 42 import java.util.Dictionary; 43 import java.util.Hashtable; 44 import java.util.Vector; 45 import java.util.Enumeration; 46 47 public class ServiceRegistry { 48 49 private AbstractCube cube; 50 51 private Hashtable bundle_services; 53 54 public ServiceRegistry( AbstractCube cube ) { 55 this.cube = cube; 56 bundle_services = new Hashtable(); 57 } 58 59 public ServiceRegistrationImpl registerService( BundleImpl serviceProvider, String[] classNames, Object service, Dictionary properties ) { 61 cube.log( cube.LOG_DEBUG, serviceProvider.getLocationWithoutAdminPermissionCheck() + " registering service: " + classNames[ 0 ] ); 62 63 if ( serviceProvider.getBundleId() != 0 ) { 65 for ( int i = 0; i < classNames.length; i++ ) { 66 if ( classNames[ i ].equals( PackageAdmin.class.getName() ) ) { 67 throw new IllegalArgumentException( "Only system bundle may register PackageAdmin service" ); 68 } 69 else if ( classNames[ i ].equals( PermissionAdmin.class.getName() ) ) { 70 throw new IllegalArgumentException( "Only system bundle may register PermissionAdmin service" ); 71 } 72 } 73 } 74 75 ServiceRegistrationImpl serviceRegistration = new ServiceRegistrationImpl( serviceProvider, classNames, properties ); 76 ServiceReferenceImpl serviceReference = (ServiceReferenceImpl)serviceRegistration.getReference(); 77 ServiceRegistryEntry serviceRegistryEntry = new ServiceRegistryEntry( serviceRegistration, serviceReference, service ); 78 getServiceRegistryEntries( serviceProvider, true ).addElement( serviceRegistryEntry ); 79 return serviceRegistration; 80 } 81 82 public ServiceReferenceImpl[] getServiceReferences( String className, LDAPFilter filter ) { 84 Vector serviceReferences = new Vector(); 85 Enumeration enum = bundle_services.elements(); 86 Vector serviceRegistryEntries; 87 ServiceRegistryEntry serviceRegistryEntry; 88 while ( enum.hasMoreElements() ) { 89 serviceRegistryEntries = (Vector)enum.nextElement(); 90 for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) { 91 serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i ); 92 if ( serviceRegistryEntry.isServiceStillRegistered() && doFilter( serviceRegistryEntry, filter ) && serviceRegistryEntry.isImplemented( className ) ) { 94 serviceReferences.addElement( serviceRegistryEntry.getReference() ); 95 } 96 } 97 } 98 99 if ( serviceReferences.size() == 0 ) { 100 return null; 101 } 102 103 ServiceReferenceImpl[] ret = new ServiceReferenceImpl[ serviceReferences.size() ]; 104 serviceReferences.copyInto( ret ); 105 return ret; 106 } 107 108 public Object getService( ServiceReference reference, BundleImpl userBundle ) throws ServiceRegistryException { 115 Object serviceClass = reference.getProperty( "objectClass" ); 116 if ( serviceClass instanceof Object[] ) { 117 serviceClass = ( (Object[])serviceClass )[ 0 ]; 118 } 119 cube.log( cube.LOG_DEBUG, userBundle.getLocationWithoutAdminPermissionCheck() + " getting service: " + serviceClass ); 120 121 Enumeration enum = bundle_services.elements(); 122 Vector serviceRegistryEntries; 123 ServiceRegistryEntry serviceRegistryEntry; 124 while ( enum.hasMoreElements() ) { 125 serviceRegistryEntries = (Vector)enum.nextElement(); 126 for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) { 127 serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i ); 128 if ( serviceRegistryEntry.getReference().equals( reference ) ) { 130 return serviceRegistryEntry.getService( userBundle ); 135 } 136 } 137 } 138 return null; 139 } 140 141 public boolean ungetService( ServiceReference reference, BundleImpl userBundle ) { 143 Object serviceClass = reference.getProperty( "objectClass" ); 144 if ( serviceClass instanceof Object[] ) { 145 serviceClass = ( (Object[])serviceClass )[ 0 ]; 146 } 147 cube.log( cube.LOG_DEBUG, userBundle.getLocationWithoutAdminPermissionCheck() + " ungetting service: " + serviceClass ); 148 149 Enumeration enum = bundle_services.elements(); 150 Vector serviceRegistryEntries; 151 ServiceRegistryEntry serviceRegistryEntry; 152 while ( enum.hasMoreElements() ) { 153 serviceRegistryEntries = (Vector)enum.nextElement(); 154 for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) { 155 serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i ); 156 if ( serviceRegistryEntry.getReference().equals( reference ) ) { 157 return serviceRegistryEntry.ungetService( userBundle ); 158 } 159 } 160 } 161 162 throw new UnreachableCodeException(); 167 } 168 169 public ServiceRegistrationImpl[] getServicesRegisteredBy( BundleImpl bundle ) { 172 Vector serviceRegistrations = new Vector(); 173 Enumeration enum = bundle_services.elements(); 174 Vector serviceRegistryEntries; 175 ServiceRegistryEntry serviceRegistryEntry; 176 while ( enum.hasMoreElements() ) { 177 serviceRegistryEntries = (Vector)enum.nextElement(); 178 for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) { 179 serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i ); 180 if ( serviceRegistryEntry.isServiceStillRegistered() && serviceRegistryEntry.isProvidedBy( bundle ) ) { 182 serviceRegistrations.addElement( serviceRegistryEntry.getRegistration() ); 183 } 184 } 185 } 186 187 if ( serviceRegistrations.size() == 0 ) { 188 return null; 189 } 190 191 ServiceRegistrationImpl[] ret = new ServiceRegistrationImpl[ serviceRegistrations.size() ]; 192 serviceRegistrations.copyInto( ret ); 193 return ret; 194 } 195 196 public ServiceReferenceImpl[] getServicesUsedBy( BundleImpl bundle ) { 199 Vector serviceReferences = new Vector(); 200 Enumeration enum = bundle_services.elements(); 201 Vector serviceRegistryEntries; 202 ServiceRegistryEntry serviceRegistryEntry; 203 while ( enum.hasMoreElements() ) { 204 serviceRegistryEntries = (Vector)enum.nextElement(); 205 for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) { 206 serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i ); 207 if ( serviceRegistryEntry.isUsedBy( bundle ) ) { 209 serviceReferences.addElement( serviceRegistryEntry.getReference() ); 210 } 211 } 212 } 213 214 if ( serviceReferences.size() == 0 ) { 215 return null; 216 } 217 218 ServiceReferenceImpl[] ret = new ServiceReferenceImpl[ serviceReferences.size() ]; 219 serviceReferences.copyInto( ret ); 220 return ret; 221 } 222 223 public ServiceRegistryEntry getServiceRegistryEntry( ServiceRegistration serviceRegistration ) { 224 Vector serviceReferences = new Vector(); 225 Enumeration enum = bundle_services.elements(); 226 Vector serviceRegistryEntries; 227 ServiceRegistryEntry serviceRegistryEntry; 228 while ( enum.hasMoreElements() ) { 229 serviceRegistryEntries = (Vector)enum.nextElement(); 230 for ( int i = 0; i < serviceRegistryEntries.size(); i++ ) { 231 serviceRegistryEntry = (ServiceRegistryEntry)serviceRegistryEntries.elementAt( i ); 232 if ( serviceRegistryEntry.getRegistration().equals( serviceRegistration ) ) { 233 return serviceRegistryEntry; 234 } 235 } 236 } 237 throw new UnreachableCodeException(); 242 } 243 244 245 247 private Vector getServiceRegistryEntries( BundleImpl serviceProvider, boolean createIfNull ) { 251 if ( bundle_services.get( serviceProvider ) == null && createIfNull ) { 252 bundle_services.put( serviceProvider, new Vector() ); 253 } 254 return (Vector)bundle_services.get( serviceProvider ); 255 } 256 257 private boolean doFilter( ServiceRegistryEntry serviceRegistryEntry, LDAPFilter filter ) { 260 if ( filter != null ) { 261 try { 262 LDAPFilter caseInsensitiveFilter = new LDAPFilter( filter.getFilterString(), false ); 263 return caseInsensitiveFilter.match( serviceRegistryEntry.getReference() ); 264 } 265 catch ( InvalidSyntaxException e ) { 266 throw new UnreachableCodeException(); 269 } 270 } 271 return true; 272 } 273 274 } | Popular Tags |