1 26 27 package com.opensugar.cube; 28 29 import com.opensugar.cube.ldap.LDAPFilter; 30 import com.opensugar.cube.serviceRegistry.ServiceRegistryException; 31 32 import org.osgi.framework.BundleContext; 33 import org.osgi.framework.Bundle; 34 import org.osgi.framework.BundleException; 35 import org.osgi.framework.ServiceListener; 36 import org.osgi.framework.InvalidSyntaxException; 37 import org.osgi.framework.BundleListener; 38 import org.osgi.framework.FrameworkListener; 39 import org.osgi.framework.ServiceRegistration; 40 import org.osgi.framework.ServiceReference; 41 import org.osgi.framework.ServiceEvent; 42 import org.osgi.framework.BundleEvent; 43 import org.osgi.framework.ServiceFactory; 44 import org.osgi.framework.ServicePermission; 45 import org.osgi.framework.FrameworkEvent; 46 import org.osgi.framework.Constants; 47 import org.osgi.framework.Filter; 48 49 import java.io.InputStream ; 50 import java.io.File ; 51 import java.io.FileOutputStream ; 52 import java.io.IOException ; 53 import java.util.Dictionary ; 54 import java.util.Vector ; 55 import java.util.Hashtable ; 56 import java.net.URL ; 57 58 public class BundleContextImpl implements BundleContext { 59 60 public String getProperty( String key ) { 61 Util.checkNullParameter( key, getClass().getName(), "getProperty", "key" ); 62 63 String value = cube.getProperty( key ); 64 if ( value != null ) { 65 return value; 66 } 67 else { 68 try { 72 cube.checkPropertyPermission( key, "read" ); 73 return (String )System.getProperties().get( key ); 74 } 75 catch ( SecurityException e ) { 76 return null; 77 } 78 } 79 } 80 81 public Bundle getBundle() { 82 checkBundleContextIsNotStopped(); 83 84 return bundle; 85 } 86 87 public Bundle installBundle( String location ) throws BundleException, SecurityException { 88 Util.checkNullParameter( location, getClass().getName(), "installBundle", "location" ); 89 90 return installBundle( location, null ); 91 } 92 93 public Bundle installBundle( String location, InputStream in ) throws BundleException, SecurityException { 94 Util.checkNullParameter( location, getClass().getName(), "installBundle", "location" ); 95 96 cube.checkAdminPermission(); 97 98 return cube.installBundle( location, in ); 99 } 100 101 public Bundle getBundle( long id ) { 102 return cube.getBundle( id ); 103 } 104 105 public Bundle[] getBundles() { 106 return cube.getBundles(); 107 } 108 109 public Filter createFilter( String filter ) throws InvalidSyntaxException { 110 if ( filter == null ) { 111 throw new NullPointerException ( "Filter string is null" ); 112 } 113 return new LDAPFilter( filter, false ); 115 } 116 117 public void addServiceListener( ServiceListener listener, String filterString ) throws InvalidSyntaxException { 118 Util.checkNullParameter( listener, getClass().getName(), "addServiceListener", "listener" ); 119 121 checkBundleContextIsNotStopped(); 122 123 cube.addServiceListener( bundle, listener, filterString ); 124 } 125 126 public void addServiceListener( ServiceListener listener ) { 127 Util.checkNullParameter( listener, getClass().getName(), "addServiceListener", "listener" ); 128 129 checkBundleContextIsNotStopped(); 130 131 cube.addServiceListener( bundle, listener ); 132 } 133 134 public void removeServiceListener( ServiceListener listener ) { 135 Util.checkNullParameter( listener, getClass().getName(), "removeServiceListener", "listener" ); 136 137 checkBundleContextIsNotStopped(); 138 139 cube.removeServiceListener( bundle, listener ); 140 } 141 142 public void addBundleListener( BundleListener listener ) { 143 Util.checkNullParameter( listener, getClass().getName(), "addBundleListener", "listener" ); 144 145 checkBundleContextIsNotStopped(); 146 147 cube.addBundleListener( bundle, listener ); 148 } 149 150 public void removeBundleListener( BundleListener listener ) { 151 Util.checkNullParameter( listener, getClass().getName(), "removeBundleListener", "listener" ); 152 153 checkBundleContextIsNotStopped(); 154 155 cube.removeBundleListener( bundle, listener ); 156 } 157 158 public void addFrameworkListener( FrameworkListener listener ) { 159 Util.checkNullParameter( listener, getClass().getName(), "addFrameworkListener", "listener" ); 160 161 checkBundleContextIsNotStopped(); 162 163 cube.addFrameworkListener( bundle, listener ); 164 } 165 166 public void removeFrameworkListener( FrameworkListener listener ) { 167 Util.checkNullParameter( listener, getClass().getName(), "removeFrameworkListener", "listener" ); 168 169 checkBundleContextIsNotStopped(); 170 171 cube.removeFrameworkListener( bundle, listener ); 172 } 173 174 public ServiceRegistration registerService( String [] classNames, Object service, Dictionary properties ) { 175 Util.checkNullArrayParameter( classNames, getClass().getName(), "registerService", "classNames" ); 176 Util.checkNullParameter( service, getClass().getName(), "registerService", "service" ); 177 179 if ( properties == null ) { 180 properties = new Hashtable (); 181 } 182 properties.put( Constants.SERVICE_ID, generateServiceId() ); 183 properties.put( Constants.OBJECTCLASS, classNames ); 184 185 if ( !( service instanceof ServiceFactory ) ) { 186 Class clazz; 187 for ( int i = 0; i < classNames.length; i++ ) { 188 try { 189 clazz = bundle.getClassLoader().loadClass( classNames[ i ] ); 190 if ( !clazz.isInstance( service ) ) { 191 throw new IllegalArgumentException ( "Error while registering service: " + service.getClass().getName() + " is not an instance of " + clazz.getName() ); 192 } 193 } 194 catch( ClassNotFoundException e ) { 195 getCube().log( getCube().LOG_ERROR, "classNotFound: " + getBundle().getLocation(), e ); 196 throw new IllegalArgumentException ( "Invalid class name: " + classNames[ i ] ); 197 } 198 } 199 } 200 201 cube.checkServicePermissionForAll( classNames, ServicePermission.REGISTER ); 202 203 208 ServiceRegistration serviceRegistration = cube.getServiceRegistry().registerService( bundle, classNames, service, properties ); 209 cube.fireServiceEvent( ServiceEvent.REGISTERED, serviceRegistration.getReference() ); 210 return serviceRegistration; 211 } 212 213 public ServiceRegistration registerService( String className, Object service, Dictionary properties ) { 214 String [] classNames = { className }; 215 return registerService( classNames, service, properties ); 216 } 217 228 public ServiceReference[] getServiceReferences( String className, String filterString ) throws InvalidSyntaxException { 230 233 String [] classNames = { className }; 234 try { 235 cube.checkServicePermissionForAll( classNames, ServicePermission.GET ); 236 } 237 catch( SecurityException e ) { 238 return null; 239 } 240 241 checkBundleContextIsNotStopped(); 243 245 LDAPFilter filter = null; 246 if ( filterString != null ) { 247 filter = new LDAPFilter( filterString, false ); 249 } 250 return cube.getServiceRegistry().getServiceReferences( className, filter ); 251 } 252 253 public ServiceReference getServiceReference( String className ) { 254 256 String [] classNames = { className }; 257 try { 258 ServiceReference[] serviceReferences = getServiceReferences( className, null ); 260 if ( serviceReferences != null && serviceReferences.length > 0 ) { 261 int largestRanking = Integer.MIN_VALUE; 263 Object val; 264 int ranking; 265 for ( int i = 0; i < serviceReferences.length; i++ ) { 266 val = serviceReferences[ i ].getProperty( Constants.SERVICE_RANKING ); 267 if ( val != null ) { 269 ranking = Integer.valueOf( val.toString() ).intValue(); 270 if ( ranking > largestRanking ) { 271 largestRanking = ranking; 272 } 273 } 274 } 275 Vector largestRankingServices = new Vector (); 276 if ( largestRanking == Integer.MIN_VALUE ) { 277 for ( int i = 0; i < serviceReferences.length; i++ ) { 278 largestRankingServices.addElement( serviceReferences[ i ] ); 279 } 280 } 281 else { 282 for ( int i = 0; i < serviceReferences.length; i++ ) { 283 val = serviceReferences[ i ].getProperty( Constants.SERVICE_RANKING ); 284 if ( val != null && Integer.valueOf( val.toString() ).intValue() == largestRanking ) { 286 largestRankingServices.addElement( serviceReferences[ i ] ); 287 } 288 } 289 } 290 291 ServiceReference largestRankingServiceWithSmallestId = null; 294 ServiceReference sr; 295 long smallestId = Long.MAX_VALUE; 296 long id; 297 for ( int i = 0; i < largestRankingServices.size(); i++ ) { 298 sr = (ServiceReference)largestRankingServices.elementAt( i ); 299 id = ( (Long )serviceReferences[ i ].getProperty( Constants.SERVICE_ID ) ).longValue(); 300 if ( id < smallestId ) { 301 largestRankingServiceWithSmallestId = sr; 302 smallestId = id; 303 } 304 } 305 return largestRankingServiceWithSmallestId; 306 } 307 } 308 catch( InvalidSyntaxException e ) { 309 throw new UnreachableCodeException(); 311 } 312 return null; 313 } 314 315 public Object getService( ServiceReference reference ) { 316 Util.checkNullParameter( reference, getClass().getName(), "getService", "reference" ); 317 318 cube.checkServicePermissionForAtLeastOne( (String [])reference.getProperty( Constants.OBJECTCLASS ), ServicePermission.GET ); 319 320 checkBundleContextIsNotStopped(); 321 322 try { 323 Object service = cube.getServiceRegistry().getService( reference, bundle ); 324 return service; 325 } 326 catch( IllegalStateException e ) { 327 return null; 329 } 330 catch( ServiceRegistryException e ) { 331 cube.fireFrameworkErrorEvent( getBundle(), e ); 335 return null; 336 } 337 } 338 339 public boolean ungetService( ServiceReference reference ) { 340 Util.checkNullParameter( reference, getClass().getName(), "ungetService", "reference" ); 341 342 checkBundleContextIsNotStopped(); 343 344 return cube.getServiceRegistry().ungetService( reference, bundle ); 345 } 346 347 public File getDataFile( String fileName ) { 348 Util.checkNullParameter( fileName, getClass().getName(), "getDataFile", "fileName" ); 349 350 checkBundleContextIsNotStopped(); 351 352 File dataDirectory = cube.getBundleDataDirectory( bundle.getBundleId() ); 353 cube.checkFilePermission( dataDirectory.getAbsolutePath() + File.separator + "-", "read,write,execute,delete" ); 354 if ( fileName.length() == 0 ) { 355 return dataDirectory; 356 } 357 return new File ( dataDirectory, fileName ); 358 } 359 360 362 private static long lastAssignedServiceId = 0; 363 364 private AbstractCube cube; 365 private BundleImpl bundle; 366 private boolean stopped; 367 368 protected BundleContextImpl( AbstractCube cube, BundleImpl bundle ) { 369 this.cube = cube; 370 this.bundle = bundle; 371 stopped = false; 372 } 373 374 public AbstractCube getCube() { 375 return cube; 376 } 377 378 protected void setStopped() { 379 stopped = true; 380 } 381 382 384 private void checkBundleContextIsNotStopped() { 385 if ( stopped ) { 386 throw new IllegalStateException ( "Bundle context has been stopped (bundle id: " + bundle.getBundleId() + ")" ); 387 } 388 } 389 390 private static synchronized Long generateServiceId() { 391 long id = lastAssignedServiceId + 1; 392 lastAssignedServiceId = id; 393 return new Long ( id ); 394 } 395 396 } | Popular Tags |