00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.knopflerfish.framework;
00036
00037 import java.io.*;
00038
00039 import java.util.Dictionary;
00040 import java.util.List;
00041
00042 import org.osgi.framework.*;
00043
00051 public class BundleContextImpl
00052 implements BundleContext
00053 {
00054
00058 private final Framework framework;
00059
00063 private BundleImpl bundle;
00064
00065
00069 public BundleContextImpl(BundleImpl bundle) {
00070 this.bundle = bundle;
00071 framework = bundle.framework;
00072 }
00073
00074
00075
00076
00077
00083 public String getProperty(String key) {
00084 isBCvalid();
00085 return Framework.getProperty(key);
00086 }
00087
00088
00094 public Bundle installBundle(String location) throws BundleException {
00095 isBCvalid();
00096 return framework.bundles.install(location, null);
00097 }
00098
00099
00105 public Bundle installBundle(String location, InputStream in)
00106 throws BundleException
00107 {
00108 try {
00109 isBCvalid();
00110 return framework.bundles.install(location, in);
00111 } finally {
00112 if (in != null) {
00113 try {
00114 in.close();
00115 } catch (IOException ignore) {}
00116 }
00117 }
00118 }
00119
00120
00126 public Bundle getBundle() {
00127 isBCvalid();
00128 return bundle;
00129 }
00130
00131
00137 public Bundle getBundle(long id) {
00138 return framework.bundles.getBundle(id);
00139 }
00140
00141
00147 public Bundle[] getBundles() {
00148 List bl = framework.bundles.getBundles();
00149 return (Bundle[])bl.toArray(new Bundle [bl.size()]);
00150 }
00151
00152
00158 public void addServiceListener(ServiceListener listener, String filter)
00159 throws InvalidSyntaxException {
00160 isBCvalid();
00161 framework.listeners.addServiceListener(bundle, listener, filter);
00162 }
00163
00164
00170 public void addServiceListener(ServiceListener listener) {
00171 isBCvalid();
00172 try {
00173 framework.listeners.addServiceListener(bundle, listener, null);
00174 } catch (InvalidSyntaxException neverHappens) { }
00175 }
00176
00177
00183 public void removeServiceListener(ServiceListener listener) {
00184 isBCvalid();
00185 framework.listeners.removeServiceListener(bundle, listener);
00186 }
00187
00188
00194 public void addBundleListener(BundleListener listener) {
00195 isBCvalid();
00196 framework.listeners.addBundleListener(bundle, listener);
00197 }
00198
00199
00205 public void removeBundleListener(BundleListener listener) {
00206 isBCvalid();
00207 framework.listeners.removeBundleListener(bundle, listener);
00208 }
00209
00210
00216 public void addFrameworkListener(FrameworkListener listener) {
00217 isBCvalid();
00218 framework.listeners.addFrameworkListener(bundle, listener);
00219 }
00220
00221
00227 public void removeFrameworkListener(FrameworkListener listener) {
00228 isBCvalid();
00229 framework.listeners.removeFrameworkListener(bundle, listener);
00230 }
00231
00232
00238 public ServiceRegistration registerService(String[] clazzes,
00239 Object service,
00240 Dictionary properties) {
00241 isBCvalid();
00242 String [] classes = (String[]) clazzes.clone();
00243 return framework.services.register(bundle, classes, service, properties);
00244 }
00245
00246
00252 public ServiceRegistration registerService(String clazz,
00253 Object service,
00254 Dictionary properties) {
00255 isBCvalid();
00256 String [] classes = new String [] { clazz };
00257 return framework.services.register(bundle, classes, service, properties);
00258 }
00259
00260
00266 public ServiceReference[] getServiceReferences(String clazz, String filter)
00267 throws InvalidSyntaxException {
00268 isBCvalid();
00269 return framework.services.get(clazz, filter, bundle, true);
00270 }
00271
00277 public ServiceReference[] getAllServiceReferences(String clazz, String filter)
00278 throws InvalidSyntaxException {
00279 isBCvalid();
00280 return framework.services.get(clazz, filter, null, false);
00281 }
00282
00283
00289 public ServiceReference getServiceReference(String clazz) {
00290 isBCvalid();
00291 if (framework.perm.okGetServicePerm(clazz)) {
00292 return framework.services.get(bundle, clazz);
00293 } else {
00294 return null;
00295 }
00296 }
00297
00298
00304 public Object getService(ServiceReference reference) {
00305 isBCvalid();
00306
00307 if(reference == null) {
00308
00309
00310
00311
00312
00313 throw new NullPointerException("null ServiceReference is not valid input to getService()");
00314 }
00315
00316 return ((ServiceReferenceImpl)reference).getService(bundle);
00317 }
00318
00319
00325 public boolean ungetService(ServiceReference reference) {
00326 isBCvalid();
00327
00328 if(reference == null) {
00329
00330
00331
00332
00333
00334 throw new NullPointerException("null ServiceReference is not valid input to ungetService()");
00335 }
00336
00337 return ((ServiceReferenceImpl)reference).ungetService(bundle, true);
00338 }
00339
00340
00347 public File getDataFile(String filename) {
00348 isBCvalid();
00349 File dataRoot = bundle.getDataRoot();
00350 if (dataRoot != null) {
00351 if (!dataRoot.exists()) {
00352 dataRoot.mkdirs();
00353 }
00354 return new File(dataRoot, filename);
00355 } else {
00356 return null;
00357 }
00358 }
00359
00360
00372 public Filter createFilter(String filter) throws InvalidSyntaxException {
00373 isBCvalid();
00374 return new FilterImpl(filter);
00375 }
00376
00377
00378
00379
00380
00384 void invalidate() {
00385 bundle = null;
00386 }
00387
00388
00395 void isBCvalid() {
00396 if (bundle == null) {
00397 throw new IllegalStateException("This bundle context is no longer valid");
00398 }
00399 }
00400
00401
00402
00403 }