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.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
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
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 }