1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 import org.eclipse.osgi.framework.debug.Debug; 17 import org.eclipse.osgi.util.NLS; 18 import org.osgi.framework.*; 19 20 25 26 public class ServiceUse { 27 29 protected ServiceFactory factory; 30 32 protected Object service; 33 34 protected BundleContextImpl context; 35 36 protected ServiceRegistrationImpl registration; 37 38 protected int useCount; 39 40 41 42 50 protected ServiceUse(BundleContextImpl context, ServiceRegistrationImpl registration) { 51 this.context = context; 52 this.registration = registration; 53 this.useCount = 0; 54 55 Object service = registration.service; 56 if (service instanceof ServiceFactory) { 57 factory = (ServiceFactory) service; 58 this.service = null; 59 } else { 60 this.factory = null; 61 this.service = service; 62 } 63 } 64 65 100 protected Object getService() { 101 if ((useCount == 0) && (factory != null)) { 102 AbstractBundle factorybundle = registration.context.bundle; 103 Object service; 104 105 try { 106 service = AccessController.doPrivileged(new PrivilegedAction () { 107 public Object run() { 108 return factory.getService(context.bundle, registration); 109 } 110 }); 111 } catch (Throwable t) { 112 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 113 Debug.println(factory + ".getService() exception: " + t.getMessage()); Debug.printStackTrace(t); 115 } 116 context.framework.adaptor.handleRuntimeError(t); 118 BundleException be = new BundleException(NLS.bind(Msg.SERVICE_FACTORY_EXCEPTION, factory.getClass().getName(), "getService"), t); context.framework.publishFrameworkEvent(FrameworkEvent.ERROR, factorybundle, be); 120 return (null); 121 } 122 123 if (service == null) { 124 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 125 Debug.println(factory + ".getService() returned null."); } 127 128 BundleException be = new BundleException(NLS.bind(Msg.SERVICE_OBJECT_NULL_EXCEPTION, factory.getClass().getName())); 129 context.framework.publishFrameworkEvent(FrameworkEvent.ERROR, factorybundle, be); 130 131 return (null); 132 } 133 134 String [] clazzes = registration.clazzes; 135 String invalidService = BundleContextImpl.checkServiceClass(clazzes, service); 136 if (invalidService != null) { 137 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 138 Debug.println("Service object is not an instanceof " + invalidService); } 140 throw new IllegalArgumentException (NLS.bind(Msg.SERVICE_NOT_INSTANCEOF_CLASS_EXCEPTION, invalidService)); 141 } 142 this.service = service; 143 } 144 145 useCount++; 146 147 return (this.service); 148 } 149 150 178 protected boolean ungetService() { 179 if (useCount == 0) { 180 return (true); 181 } 182 183 useCount--; 184 185 if (useCount == 0) { 186 if (factory != null) { 187 try { 188 AccessController.doPrivileged(new PrivilegedAction () { 189 public Object run() { 190 factory.ungetService(context.bundle, registration, service); 191 192 return null; 193 } 194 }); 195 } catch (Throwable t) { 196 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 197 Debug.println(factory + ".ungetService() exception"); Debug.printStackTrace(t); 199 } 200 201 AbstractBundle factorybundle = registration.context.bundle; 202 BundleException be = new BundleException(NLS.bind(Msg.SERVICE_FACTORY_EXCEPTION, factory.getClass().getName(), "ungetService"), t); context.framework.publishFrameworkEvent(FrameworkEvent.ERROR, factorybundle, be); 204 } 205 206 service = null; 207 } 208 209 return (true); 210 } 211 212 return (false); 213 } 214 215 224 protected void releaseService() { 225 if ((useCount > 0) && (factory != null)) { 226 try { 227 AccessController.doPrivileged(new PrivilegedAction () { 228 public Object run() { 229 factory.ungetService(context.bundle, registration, service); 230 231 return null; 232 } 233 }); 234 } catch (Throwable t) { 235 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 236 Debug.println(factory + ".ungetService() exception"); Debug.printStackTrace(t); 238 } 239 240 AbstractBundle factorybundle = registration.context.bundle; 241 BundleException be = new BundleException(NLS.bind(Msg.SERVICE_FACTORY_EXCEPTION, factory.getClass().getName(), "ungetService"), t); context.framework.publishFrameworkEvent(FrameworkEvent.ERROR, factorybundle, be); 243 } 244 245 service = null; 246 } 247 248 useCount = 0; 249 } 250 } 251 | Popular Tags |