1 26 27 package com.opensugar.cube.serviceRegistry; 28 29 import com.opensugar.cube.BundleImpl; 30 import com.opensugar.cube.ServiceReferenceImpl; 31 32 import org.osgi.framework.Bundle; 33 import org.osgi.framework.ServiceRegistration; 34 import org.osgi.framework.ServiceReference; 35 import org.osgi.framework.ServiceFactory; 36 import org.osgi.framework.Constants; 37 38 import java.util.Vector ; 39 import java.util.Hashtable ; 40 import java.util.Hashtable ; 41 42 public class ServiceRegistryEntry { 45 46 private ServiceRegistration registration; 48 private Object service; 50 private Vector users; 52 private ServiceReferenceImpl reference; 57 58 private Hashtable cachedServiceInstances; 60 61 public ServiceRegistryEntry( ServiceRegistration registration, ServiceReferenceImpl reference, Object service ) { 62 this.registration = registration; 63 this.reference = reference; 64 this.service = service; 65 66 users = new Vector (); 67 cachedServiceInstances = new Hashtable (); 68 } 69 70 protected ServiceRegistration getRegistration() { 72 return registration; 73 } 74 75 protected ServiceReferenceImpl getReference() { 77 return reference; 78 } 79 80 protected synchronized Object getService( Bundle userBundle ) throws ServiceRegistryException { 86 registration.getReference(); 88 89 Object serviceInstance; 90 if ( service instanceof ServiceFactory ) { 91 if ( cachedServiceInstances.get( userBundle ) != null ) { 93 return cachedServiceInstances.get( userBundle ); 95 } 96 else { 97 try { 100 serviceInstance = ( (ServiceFactory)service ).getService( userBundle, registration ); 101 } 102 catch( Exception e ) { 103 throw new ServiceRegistryException( "Exception occured while calling service factory", e ); 104 } 105 106 if ( serviceInstance == null ) { 107 throw new ServiceRegistryException( "Service factory returned a null object" ); 108 } 109 110 try { 113 String [] objectClass = (String [])reference.getProperty( "objectClass" ); 114 BundleImpl bundle; 115 for ( int i = 0; i < objectClass.length; i++ ) { 116 bundle = (BundleImpl)reference.getBundle(); 117 if ( !bundle.getClassLoader().loadClass( objectClass[ i ] ).isInstance( serviceInstance ) ) { 118 throw new ServiceRegistryException( "Invalid service type created by service factory" ); 119 } 120 } 121 } 122 catch( ClassNotFoundException e ) { 123 throw new ServiceRegistryException( "Class not found while checking service created by service factory", e ); 124 } 125 126 cachedServiceInstances.put( userBundle, serviceInstance ); 128 } 129 } 130 else { 131 serviceInstance = service; 133 } 134 addUser( userBundle ); 136 return serviceInstance; 138 } 139 140 protected synchronized boolean ungetService( Bundle userBundle ) { 142 return removeUser( userBundle ); 143 } 144 145 protected boolean isImplemented( String className ) { 147 if ( className == null ) { 148 return true; 149 } 150 151 String [] classNames = (String [])reference.getProperty( Constants.OBJECTCLASS ); 152 for ( int i = 0; i < classNames.length; i++ ) { 153 if ( classNames[ i ].equals( className ) ) { 154 return true; 155 } 156 } 157 return false; 158 } 159 160 protected boolean isProvidedBy( Bundle bundle ) { 162 if ( reference.getBundle().getBundleId() == bundle.getBundleId() ) { 163 return true; 164 } 165 return false; 166 } 167 168 protected synchronized boolean isUsedBy( Bundle bundle ) { 170 if ( users.indexOf( bundle ) != -1 ) { 171 return true; 172 } 173 return false; 174 } 175 176 protected boolean isServiceStillRegistered() { 178 try { 179 registration.getReference(); 182 return true; 183 } 184 catch( IllegalStateException e ) { 185 return false; 186 } 187 } 188 189 public synchronized void serviceUnregistered() { 196 while ( users.size() > 0 ) { 197 removeUser( (Bundle)users.elementAt( 0 ) ); 198 } 199 } 200 201 203 private synchronized void addUser( Bundle user ) { 205 if ( users.indexOf( user ) == -1 ) { 206 reference.addUsingBundle( user ); 211 } 212 213 users.addElement( user ); 217 } 218 219 private synchronized boolean removeUser( Bundle user ) { 223 if ( users.indexOf( user ) == -1 ) { 227 return false; 228 } 229 230 users.removeElement( user ); 231 if ( users.indexOf( user ) == -1 ) { 232 if ( service instanceof ServiceFactory ) { 235 Object serviceInstance = cachedServiceInstances.get( user ); 236 ( (ServiceFactory)service ).ungetService( user, registration, serviceInstance ); 237 } 238 cachedServiceInstances.remove( user ); 240 241 reference.removeUsingBundle( user ); 246 } 247 248 return true; 249 } 250 251 } | Popular Tags |