BundleContextImpl.java

00001 /*
00002  * Copyright (c) 2003-2006, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
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   // BundleContext interface
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       // Throw an NPE with a message to be really clear we do it 
00309       // intentionally.
00310       // A better solution would be to throw IllegalArgumentException,
00311       // but the OSGi ref impl throws NPE, and we want to keep as
00312       // close as possible
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       // Throw an NPE with a message to be really clear we do it 
00330       // intentionally.
00331       // A better solution would be to throw IllegalArgumentException,
00332       // but the OSGi ref impl throws NPE, and we want to keep as
00333       // close as possible
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   // Package methods
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 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4