1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.util.*; 15 import org.eclipse.osgi.framework.util.KeyedHashSet; 16 import org.osgi.framework.Version; 17 18 27 public class BundleRepository { 28 29 private ArrayList bundlesByInstallOrder; 30 31 32 private KeyedHashSet bundlesById; 33 34 35 private HashMap bundlesBySymbolicName; 36 37 public BundleRepository(int initialCapacity) { 38 bundlesByInstallOrder = new ArrayList(initialCapacity); 39 bundlesById = new KeyedHashSet(initialCapacity, true); 40 bundlesBySymbolicName = new HashMap(initialCapacity); 41 } 42 43 47 public List getBundles() { 48 return bundlesByInstallOrder; 49 } 50 51 56 public AbstractBundle getBundle(long bundleId) { 57 Long key = new Long (bundleId); 58 return (AbstractBundle) bundlesById.getByKey(key); 59 } 60 61 public AbstractBundle[] getBundles(String symbolicName) { 62 if (Constants.OSGI_SYSTEM_BUNDLE.equals(symbolicName)) 63 symbolicName = Constants.getInternalSymbolicName(); 64 return (AbstractBundle[]) bundlesBySymbolicName.get(symbolicName); 65 } 66 67 public AbstractBundle getBundle(String symbolicName, Version version) { 68 AbstractBundle[] bundles = getBundles(symbolicName); 69 if (bundles != null) { 70 if (bundles.length > 0) { 71 for (int i = 0; i < bundles.length; i++) { 72 if (bundles[i].getVersion().equals(version)) { 73 return bundles[i]; 74 } 75 } 76 } 77 } 78 return null; 79 } 80 81 public void add(AbstractBundle bundle) { 82 bundlesByInstallOrder.add(bundle); 83 bundlesById.add(bundle); 84 String symbolicName = bundle.getSymbolicName(); 85 if (symbolicName != null) { 86 AbstractBundle[] bundles = (AbstractBundle[]) bundlesBySymbolicName.get(symbolicName); 87 if (bundles == null) { 88 bundles = new AbstractBundle[1]; 91 bundles[0] = bundle; 92 bundlesBySymbolicName.put(symbolicName, bundles); 93 return; 94 } 95 96 ArrayList list = new ArrayList(bundles.length + 1); 97 Version newVersion = bundle.getVersion(); 99 boolean added = false; 100 for (int i = 0; i < bundles.length; i++) { 101 AbstractBundle oldBundle = bundles[i]; 102 Version oldVersion = oldBundle.getVersion(); 103 if (!added && newVersion.compareTo(oldVersion) >= 0) { 104 added = true; 105 list.add(bundle); 106 } 107 list.add(oldBundle); 108 } 109 if (!added) { 110 list.add(bundle); 111 } 112 113 bundles = new AbstractBundle[list.size()]; 114 list.toArray(bundles); 115 bundlesBySymbolicName.put(symbolicName, bundles); 116 } 117 } 118 119 public boolean remove(AbstractBundle bundle) { 120 boolean found = bundlesById.remove(bundle); 122 if (!found) 123 return false; 124 125 bundlesByInstallOrder.remove(bundle); 127 String symbolicName = bundle.getSymbolicName(); 129 if (symbolicName == null) 130 return true; 131 132 AbstractBundle[] bundles = (AbstractBundle[]) bundlesBySymbolicName.get(symbolicName); 133 if (bundles == null) 134 return true; 135 136 int numRemoved = 0; 139 for (int i = 0; i < bundles.length; i++) { 140 if (bundle == bundles[i]) { 141 numRemoved++; 142 bundles[i] = null; 143 } 144 } 145 if (numRemoved > 0) { 146 if (bundles.length - numRemoved <= 0) { 147 bundlesBySymbolicName.remove(symbolicName); 149 } else { 150 AbstractBundle[] newBundles = new AbstractBundle[bundles.length - numRemoved]; 152 int indexCnt = 0; 153 for (int i = 0; i < bundles.length; i++) { 154 if (bundles[i] != null) { 155 newBundles[indexCnt] = bundles[i]; 156 indexCnt++; 157 } 158 } 159 bundlesBySymbolicName.put(symbolicName, newBundles); 160 } 161 } 162 163 return true; 164 } 165 166 public void removeAllBundles() { 167 bundlesByInstallOrder.clear(); 168 bundlesById = new KeyedHashSet(); 169 bundlesBySymbolicName.clear(); 170 } 171 } 172 | Popular Tags |