SystemBundle.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 import java.net.URL;
00039 import java.security.ProtectionDomain;
00040 import java.util.Dictionary;
00041 import java.util.Enumeration;
00042 import java.util.Hashtable;
00043 
00044 import org.knopflerfish.framework.permissions.PermissionAdminImpl;
00045 import org.osgi.framework.*;
00046 import org.osgi.service.packageadmin.PackageAdmin;
00047 import org.osgi.service.permissionadmin.PermissionAdmin;
00048 
00049 import org.osgi.util.tracker.ServiceTracker;
00050 import org.osgi.service.startlevel.StartLevel;
00051 
00060 public class SystemBundle extends BundleImpl {
00061 
00062 
00066   private final static String SYSPKG_FILE = Constants.FRAMEWORK_SYSTEMPACKAGES + ".file";
00067 
00071   private final static String EXPORT13 =
00072     "org.knopflerfish.framework.system.export.all_13";
00073 
00077   private final static String EXPORT14 =
00078     "org.knopflerfish.framework.system.export.all_14";
00079 
00083   private final static String EXPORT15 =
00084     "org.knopflerfish.framework.system.export.all_15";
00085 
00089   private final String exportPackageString;
00090 
00091 
00096   SystemBundle(Framework fw, ProtectionDomain pd) {
00097     super(fw, 0, Constants.SYSTEM_BUNDLE_LOCATION, pd,
00098           Constants.SYSTEM_BUNDLE_SYMBOLICNAME, new Version(Main.readVersion()));
00099     state = STARTING;
00100     StringBuffer sp = new StringBuffer(System.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES, ""));
00101     if (sp.length() > 0) {
00102       sp.append(",");
00103     }
00104     
00105     if("true".equals(System.getProperty(EXPORT13, "").trim())) {
00106       addSysPackagesFromFile(sp, "packages1.3.txt");
00107     }
00108     
00109     if("true".equals(System.getProperty(EXPORT14, "").trim())) {
00110       addSysPackagesFromFile(sp, "packages1.4.txt");
00111     }
00112     
00113     if("true".equals(System.getProperty(EXPORT15, "").trim())) {
00114       addSysPackagesFromFile(sp, "packages1.5.txt");
00115     }
00116     
00117     addSysPackagesFromFile(sp, System.getProperty(SYSPKG_FILE, null));
00118     addSystemPackages(sp);
00119     
00120     exportPackageString = sp.toString();
00121     
00122     bpkgs = new BundlePackages(this, 0, exportPackageString, null, null, null);
00123     bpkgs.registerPackages();
00124     bpkgs.resolvePackages();
00125   }
00126 
00127 
00131   void addSystemPackages(StringBuffer sp) {
00132     // Set up org.osgi.framework package
00133     String name = Bundle.class.getName();
00134     name = name.substring(0, name.lastIndexOf('.'));
00135     sp.append(name + ";" + Constants.VERSION_ATTRIBUTE +
00136           "=" + Framework.SPEC_VERSION);
00137 
00138     // Set up packageadmin package
00139     name = PackageAdmin.class.getName();
00140     name = name.substring(0, name.lastIndexOf('.'));
00141     sp.append("," + name + ";" + Constants.VERSION_ATTRIBUTE +
00142           "=" +  PackageAdminImpl.SPEC_VERSION);
00143 
00144     // Set up permissionadmin package
00145     name = PermissionAdmin.class.getName();
00146     name = name.substring(0, name.lastIndexOf('.'));
00147     sp.append("," + name + ";" + Constants.VERSION_ATTRIBUTE +
00148           "=" +  PermissionAdminImpl.SPEC_VERSION);
00149 
00150     // Set up startlevel package
00151     name = StartLevel.class.getName();
00152     name = name.substring(0, name.lastIndexOf('.'));
00153     sp.append("," + name + ";" + Constants.VERSION_ATTRIBUTE +
00154           "=" +  StartLevelImpl.SPEC_VERSION);
00155 
00156     // Set up tracker package
00157     name = ServiceTracker.class.getName();
00158     name = name.substring(0, name.lastIndexOf('.'));
00159     sp.append("," + name + ";" + Constants.VERSION_ATTRIBUTE +
00160           "=" +  "1.3.1");
00161 
00162     // Set up URL package
00163     name = org.osgi.service.url.URLStreamHandlerService.class.getName();
00164     name = name.substring(0, name.lastIndexOf('.'));
00165     sp.append("," + name + ";" + Constants.VERSION_ATTRIBUTE +
00166           "=" +  "1.0");
00167   }
00168 
00169 
00173   void addSysPackagesFromFile(StringBuffer sp, String sysPkgFile) {
00174 
00175     if(sysPkgFile != null) {
00176       File f = new File(sysPkgFile);
00177       if(!f.exists() || !f.isFile()) {
00178     throw new RuntimeException("System package file '" + sysPkgFile +
00179                    "' does not exists");
00180       } else {
00181     if(Debug.packages) {
00182       Debug.println("adding system packages from file " + sysPkgFile);
00183     }
00184     BufferedReader in = null;
00185     try {
00186       in = new BufferedReader(new FileReader(sysPkgFile));
00187       String line;
00188       for(line = in.readLine(); line != null;
00189           line = in.readLine()) {
00190         line = line.trim();
00191         if(line.length() > 0 && !line.startsWith("#")) {
00192           sp.append(line);
00193           sp.append(",");
00194         }
00195       }
00196     } catch (IOException e) {
00197       throw new IllegalArgumentException("Failed to read " + sysPkgFile + ": " + e);
00198     } finally {
00199       try {   in.close();  } catch (Exception ignored) { }
00200     }
00201       }
00202     }
00203   }
00204 
00205 
00206   public boolean hasPermission(Object permission) {
00207     // we have them all
00208     return true;
00209   }
00210 
00211   //
00212   // Bundle interface
00213   //
00214 
00220   synchronized public void start() throws BundleException
00221   {
00222     secure.checkExecuteAdminPerm(this);
00223   }
00224 
00225 
00231   public void stop() throws BundleException {
00232     stop(0);    
00233   }
00234 
00235 
00236   synchronized public void stop(int exitcode) throws BundleException {
00237     secure.checkExecuteAdminPerm(this);
00238     secure.callMainShutdown(exitcode);
00239   }
00240 
00241 
00247   synchronized public void update(InputStream in) throws BundleException {
00248     secure.checkLifecycleAdminPerm(this);
00249     secure.callMainRestart();
00250   }
00251 
00252 
00258   synchronized public void uninstall() throws BundleException {
00259     secure.checkLifecycleAdminPerm(this);
00260     throw new BundleException("uninstall of System bundle is not allowed");
00261   }
00262 
00263 
00269   public Dictionary getHeaders() {
00270     return getHeaders(null);
00271   }
00272 
00273   public Dictionary getHeaders(String locale) {
00274     secure.checkMetadataAdminPerm(this);
00275     Hashtable headers = new Hashtable();
00276     headers.put(Constants.BUNDLE_NAME, Constants.SYSTEM_BUNDLE_LOCATION);
00277     headers.put(Constants.EXPORT_PACKAGE, exportPackageString);
00278     return headers;
00279   }
00280 
00281 
00287   public Enumeration findEntries(String path, String filePattern, boolean recurse) {
00288     return null;
00289   }
00290 
00291 
00295   public URL getEntry(String name) {
00296     return null;
00297   }
00298 
00299 
00303   public Enumeration getEntryPaths(String path) {
00304     return null;
00305   }
00306 
00307 
00308   //
00309   // Package method
00310   //
00311 
00315   ClassLoader getClassLoader() {
00316     return getClass().getClassLoader();
00317   }
00318 
00319   void setBundleContext(BundleContextImpl bc) {
00320     bundleContext = bc;
00321   }
00322 
00326   void systemActive() {
00327     state = ACTIVE;
00328   }
00329 
00330 
00334   void systemShuttingdown() {
00335     state = STOPPING;
00336   }
00337 
00338 
00342   private boolean isInClassPath(BundleImpl extension) {
00343     String cps = extension.isBootClassPathExtension() ? 
00344       "sun.boot.class.path" : "java.class.path";
00345     String cp = System.getProperty(cps);
00346     String[] scp = Util.splitwords(cp, ":");
00347     String path = extension.archive.getJarLocation();
00348 
00349     for (int i = 0; i < scp.length; i++) {
00350       if (scp[i].equals(path)) {
00351         return true;
00352       }
00353     }
00354 
00355     return false;
00356   }
00357 
00358 
00363   void attachFragment(BundleImpl extension) {
00364     // NYI! Plugin VM specific functionality, dynamic classpath additions
00365     if (isInClassPath(extension)) {
00366       super.attachFragment(extension);
00367     } else {
00368       throw new UnsupportedOperationException("Extension can not be dynamicly activated");
00369     }
00370   }
00371 
00372 
00380   protected void readLocalization(String locale, Hashtable localization_entries) {
00381 
00382     String[] parts = Util.splitwords(locale, "_");
00383     String tmploc = parts[0];
00384     int o = 0;
00385     
00386     do {
00387       if (fragments != null) {
00388         for (int i = fragments.size() - 1; i >= 0; i--) {
00389           BundleImpl b = (BundleImpl)fragments.get(i);
00390           // NYI! Get correct archive
00391           Hashtable tmp = b.archive.getLocalizationEntries(tmploc);
00392           if (tmp != null) {
00393             localization_entries.putAll(tmp);
00394           }
00395         }
00396       }
00397       // NYI! read localization from framework?
00398       
00399       if (++o >= parts.length) {
00400         break;
00401       }
00402       tmploc = tmploc + "_" + parts[o];
00403       
00404     } while (true);
00405   }
00406 }

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