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.knopflerfish.framework.*;
00038 import java.io.*;
00039 import java.util.*;
00040
00047 public class BundleStorageImpl implements BundleStorage {
00048
00052 private long nextFreeId = 1;
00053
00057 private ArrayList archives = new ArrayList();
00058
00064 public BundleStorageImpl() {
00065 }
00066
00074 public BundleArchive insertBundleJar(String location, InputStream is)
00075 throws Exception
00076 {
00077 long id = nextFreeId++;
00078 BundleArchive ba = new BundleArchiveImpl(this, is, location, id);
00079 archives.add(ba);
00080 return ba;
00081 }
00082
00083
00093 public BundleArchive updateBundleArchive(BundleArchive old, InputStream is)
00094 throws Exception
00095 {
00096 return new BundleArchiveImpl((BundleArchiveImpl)old, is);
00097 }
00098
00099
00108 public void replaceBundleArchive(BundleArchive oldBA, BundleArchive newBA)
00109 throws Exception
00110 {
00111 int pos;
00112 long id = oldBA.getBundleId();
00113 synchronized (archives) {
00114 pos = find(id);
00115 if (pos >= archives.size() || archives.get(pos) != oldBA) {
00116 throw new Exception("replaceBundleJar: Old bundle archive not found, pos=" + pos);
00117 }
00118 archives.set(pos, newBA);
00119 }
00120 }
00121
00122
00128 public BundleArchive [] getAllBundleArchives() {
00129 synchronized (archives) {
00130 return (BundleArchive [])archives.toArray(new BundleArchive[archives.size()]);
00131 }
00132 }
00133
00134
00141 public List getStartOnLaunchBundles() {
00142 ArrayList res = new ArrayList();
00143 for (Iterator i = archives.iterator(); i.hasNext(); ) {
00144 BundleArchive ba = (BundleArchive)i.next();
00145 if (ba.getStartOnLaunchFlag()) {
00146 res.add(ba.getBundleLocation());
00147 }
00148 }
00149 return res;
00150 }
00151
00152
00153
00154
00155
00162 boolean removeArchive(BundleArchive ba) {
00163 synchronized (archives) {
00164 int pos = find(ba.getBundleId());
00165 if (archives.get(pos) == ba) {
00166 archives.remove(pos);
00167 return true;
00168 } else {
00169 return false;
00170 }
00171 }
00172 }
00173
00174
00175
00176
00177
00178
00185 private int find(long id) {
00186 int lb = 0;
00187 int ub = archives.size();
00188 int x = 0;
00189 while (lb != ub) {
00190 x = (lb + ub) / 2;
00191 long xid = ((BundleArchive)archives.get(x)).getBundleId();
00192 if (id <= xid) {
00193 ub = x;
00194 } else {
00195 lb = x+1;
00196 }
00197 }
00198 return lb;
00199 }
00200
00201 }