BundleStorageImpl.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.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 /* BundleArchive */ 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   // Package methods
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   // Private methods
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 }

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