BundleArchiveImpl.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.bundlestorage.memory;
00036 
00037 import org.osgi.framework.*;
00038 import org.knopflerfish.framework.*;
00039 import java.io.*;
00040 import java.util.Enumeration;
00041 import java.util.Vector;
00042 import java.util.Hashtable;
00043 import java.util.StringTokenizer;
00044 import java.util.ArrayList;
00045 import java.util.List;
00046 import java.util.Properties;
00047 
00048 
00049 
00057 class BundleArchiveImpl implements BundleArchive
00058 {
00059 
00060   private Archive archive;
00061 
00062   private long id;
00063 
00064   private String location;
00065 
00066   private boolean startOnLaunch;
00067 
00068   private BundleStorageImpl storage;
00069 
00070   private Archive [] archives;
00071 
00072   private int startLevel = -1;
00073   private boolean bPersistent = false;
00074   private long lastModified;
00075 
00076   private ArrayList failedPath = null;
00077 
00082   BundleArchiveImpl(BundleStorageImpl bundleStorage, 
00083                     InputStream       is,
00084                     String            bundleLocation, 
00085                     long bundleId)
00086     throws Exception
00087   {
00088     archive       = new Archive(is);
00089     storage       = bundleStorage;
00090     id            = bundleId;
00091     location      = bundleLocation;
00092     startOnLaunch = false;
00093     setClassPath();
00094   }
00095 
00096 
00101   BundleArchiveImpl(BundleArchiveImpl old, InputStream is)
00102     throws Exception
00103   {
00104     location = old.location;
00105     storage = old.storage;
00106     id = old.id;
00107     startOnLaunch = old.startOnLaunch;
00108     bPersistent = old.bPersistent;
00109     archive = new Archive(is);
00110     setClassPath();
00111   }
00112 
00113 
00120   public String getAttribute(String key) {
00121     return archive.getAttribute(key);
00122   }
00123 
00124 
00128   public Hashtable getLocalizationEntries(String localeFile) {
00129     InputStream is = archive.getInputStream(localeFile);
00130     if (is != null) {
00131       Properties l = new Properties();
00132       try {
00133         l.load(is);
00134       } catch (IOException _ignore) { }
00135       try {
00136         is.close();
00137       } catch (IOException _ignore) { }
00138       return l;
00139     } else {
00140       return null;
00141     }
00142   }
00143 
00144 
00148   public HeaderDictionary getUnlocalizedAttributes() {
00149     return new HeaderDictionary(archive.manifest.getMainAttributes());
00150   }
00151   
00152 
00158   public long getBundleId() {
00159     return id;
00160   }
00161 
00167   public String getBundleLocation() {
00168     return location;
00169   }
00170 
00171 
00172   public int getStartLevel() {
00173     return startLevel;
00174   }
00175 
00176   
00177   public void setStartLevel(int level) {
00178     startLevel = level;
00179   }
00180 
00181 
00182   public void setPersistent(boolean b) {
00183     bPersistent = b;
00184   }
00185 
00186 
00187   public boolean isPersistent() {
00188     return bPersistent;
00189   }
00190 
00191   
00192   public long getLastModified() {
00193     return lastModified;
00194   }
00195 
00196 
00197   public void setLastModified(long timemillisecs) throws IOException{
00198           lastModified = timemillisecs;
00199   }
00200 
00201   
00211   public byte[] getClassBytes(Integer sub, String path) throws IOException {
00212     return archives[sub.intValue()].getClassBytes(path);
00213   }
00214 
00215 
00224   public Vector componentExists(String component, boolean onlyFirst) {
00225     Vector v = null;
00226     if (component.startsWith("/")) {
00227       component = component.substring(1);
00228     }
00229     for (int i = 0; i < archives.length; i++) {
00230       InputStream is = archives[i].getInputStream(component);
00231       if (is != null) {
00232         if(v == null) {
00233           v = new Vector();
00234         }
00235         v.addElement(new Integer(i));
00236         try {
00237             is.close();
00238         } catch (IOException ignore) { }
00239         if (onlyFirst) {
00240           break;
00241         }
00242       }
00243     }
00244     return v;
00245   }
00246     
00247 
00257   public InputStream getInputStream(String component, int ix) {
00258     if (component.startsWith("/")) {
00259       component = component.substring(1);
00260     }
00261 
00262     if(ix == -1) {
00263       return archive.getInputStream(component);
00264     } else {
00265       return archives[ix].getInputStream(component);
00266     }
00267   }
00268 
00269 
00276   public String getNativeLibrary(String libName) {
00277     return null;
00278   }
00279 
00280 
00286   public boolean getStartOnLaunchFlag() {
00287     return startOnLaunch;
00288   }
00289 
00290 
00296   public void setStartOnLaunchFlag(boolean value) throws IOException {
00297     if (startOnLaunch != value) {
00298       startOnLaunch = value;
00299     }
00300   }
00301 
00302 
00307   public void purge() {
00308     storage.removeArchive(this);
00309   }
00310 
00311 
00316   public void close() {
00317   }
00318 
00319 
00325   public List getFailedClassPathEntries() {
00326     return failedPath;
00327   }
00328 
00329   //
00330   // Private methods
00331   //
00332 
00333   private void setClassPath() throws IOException {
00334     String bcp = getAttribute(Constants.BUNDLE_CLASSPATH);
00335     
00336     if (bcp != null) {
00337       ArrayList a = new ArrayList();
00338       StringTokenizer st = new StringTokenizer(bcp, ",");
00339       while (st.hasMoreTokens()) {
00340         String path = st.nextToken().trim();
00341         if (".".equals(path)) {
00342                   a.add(archive);
00343           } 
00344           else if (path.endsWith(".jar")){
00345             try {
00346               a.add(archive.getSubArchive(path));
00347             } catch (IOException ioe) {
00348               if (failedPath == null) {
00349                 failedPath = new ArrayList(1);
00350               }
00351               failedPath.add(path);
00352             }
00353           }
00354           else{
00355             if(archive.subDirs == null){
00356               archive.subDirs = new ArrayList(1);
00357             }
00358           // NYI Check that it exists!
00359             archive.subDirs.add(path);
00360           }
00361       }
00362       archives = (Archive [])a.toArray(new Archive[a.size()]);
00363     } else {
00364       archives = new Archive[] { archive };
00365     }
00366   }
00367 
00368   
00369   public Enumeration findResourcesPath(String path) {
00370     return archive.findResourcesPath(path);
00371   }
00372 
00373   public String getJarLocation() {
00374     return null;
00375   }
00376 }

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