1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import org.eclipse.osgi.framework.util.KeyedHashSet; 18 import org.eclipse.osgi.service.resolver.*; 19 import org.osgi.framework.*; 20 import org.osgi.service.packageadmin.RequiredBundle; 21 22 29 public class BundleLoaderProxy implements RequiredBundle { 30 private BundleLoader loader; 32 final private BundleHost bundle; 34 final private BundleDescription description; 36 private boolean stale = false; 39 final private KeyedHashSet pkgSources; 41 42 public BundleLoaderProxy(BundleHost bundle, BundleDescription description) { 43 this.bundle = bundle; 44 this.description = description; 45 this.pkgSources = new KeyedHashSet(false); 46 } 47 48 synchronized BundleLoader getBundleLoader() { 49 if (loader != null) 50 return loader; 51 if (bundle.isResolved()) { 52 try { 53 if (bundle.getBundleId() == 0) loader = new SystemBundleLoader(bundle, this); 55 else 56 loader = new BundleLoader(bundle, this); 57 } catch (BundleException e) { 58 bundle.framework.publishFrameworkEvent(FrameworkEvent.ERROR, bundle, e); 59 return null; 60 } 61 } 62 return loader; 63 } 64 65 BundleLoader getBasicBundleLoader() { 66 return loader; 67 } 68 69 AbstractBundle getBundleHost() { 70 return bundle; 71 } 72 73 void setStale() { 74 stale = true; 75 } 76 77 boolean isStale() { 78 return stale; 79 } 80 81 public String toString() { 82 String symbolicName = bundle.getSymbolicName(); 83 StringBuffer sb = new StringBuffer (symbolicName == null ? bundle.getBundleData().getLocation() : symbolicName); 84 sb.append("; ").append(Constants.BUNDLE_VERSION_ATTRIBUTE); sb.append("=\"").append(description.getVersion().toString()).append("\""); return sb.toString(); 87 } 88 89 public org.osgi.framework.Bundle getBundle() { 90 if (isStale()) 91 return null; 92 93 return bundle; 94 } 95 96 public org.osgi.framework.Bundle[] getRequiringBundles() { 97 if (isStale()) 98 return null; 99 BundleDescription[] dependents = description.getDependents(); 101 if (dependents == null || dependents.length == 0) 102 return null; 103 ArrayList result = new ArrayList (dependents.length); 104 for (int i = 0; i < dependents.length; i++) 105 addRequirers(dependents[i], result); 106 return result.size() == 0 ? null : (Bundle[]) result.toArray(new org.osgi.framework.Bundle[result.size()]); 107 } 108 109 void addRequirers(BundleDescription dependent, ArrayList result) { 110 if (dependent.getHost() != null) return; 112 BundleLoaderProxy dependentProxy = getBundleLoader().getLoaderProxy(dependent); 113 if (dependentProxy == null) 114 return; if (result.contains(dependentProxy.bundle)) 116 return; BundleLoader dependentLoader = dependentProxy.getBundleLoader(); 118 BundleLoaderProxy[] requiredBundles = dependentLoader.requiredBundles; 119 int[] reexportTable = dependentLoader.reexportTable; 120 if (requiredBundles == null) 121 return; 122 int size = reexportTable == null ? 0 : reexportTable.length; 123 int reexportIndex = 0; 124 for (int i = 0; i < requiredBundles.length; i++) { 125 if (requiredBundles[i] == this) { 126 result.add(dependentProxy.bundle); 127 if (reexportIndex < size && reexportTable[reexportIndex] == i) { 128 reexportIndex++; 129 BundleDescription[] dependents = dependent.getDependents(); 130 if (dependents == null) 131 return; 132 for (int j = 0; j < dependents.length; j++) 133 dependentProxy.addRequirers(dependents[j], result); 134 } 135 return; 136 } 137 } 138 return; 139 } 140 141 public String getSymbolicName() { 142 return description.getSymbolicName(); 143 } 144 145 public Version getVersion() { 146 return description.getVersion(); 147 } 148 149 public boolean isRemovalPending() { 150 return description.isRemovalPending(); 151 } 152 153 BundleDescription getBundleDescription() { 154 return description; 155 } 156 157 PackageSource getPackageSource(String pkgName) { 158 PackageSource pkgSource = (PackageSource) pkgSources.getByKey(pkgName); 162 if (pkgSource == null) { 163 pkgSource = new SingleSourcePackage(pkgName, -1, this); 164 synchronized (pkgSources) { 165 pkgSources.add(pkgSource); 166 } 167 } 168 return pkgSource; 169 } 170 171 boolean inUse() { 172 return description.getDependents().length > 0; 173 } 174 175 boolean forceSourceCreation(ExportPackageDescription export) { 176 if (!export.isRoot()) 177 return true; 178 boolean strict = Constants.STRICT_MODE.equals(bundle.framework.adaptor.getState().getPlatformProperties()[0].get(Constants.OSGI_RESOLVER_MODE)); 179 return (export.getDirective(Constants.INCLUDE_DIRECTIVE) != null) || (export.getDirective(Constants.EXCLUDE_DIRECTIVE) != null) || (strict && export.getDirective(Constants.FRIENDS_DIRECTIVE) != null); 180 } 181 PackageSource createPackageSource(ExportPackageDescription export, boolean storeSource) { 188 PackageSource pkgSource = null; 189 if (!export.isRoot()) { 191 pkgSource = new ReexportPackageSource(export.getName()); 192 } else { 193 String includes = (String ) export.getDirective(Constants.INCLUDE_DIRECTIVE); 195 String excludes = (String ) export.getDirective(Constants.EXCLUDE_DIRECTIVE); 196 String [] friends = (String []) export.getDirective(Constants.FRIENDS_DIRECTIVE); 197 if (friends != null) { 198 boolean strict = Constants.STRICT_MODE.equals(bundle.framework.adaptor.getState().getPlatformProperties()[0].get(Constants.OSGI_RESOLVER_MODE)); 199 if (!strict) 200 friends = null; } 202 if (includes != null || excludes != null || friends != null) { 203 ExportPackageDescription[] exports = description.getExportPackages(); 204 int index = -1; 205 int first = -1; 206 for (int i = 0; i < exports.length; i++) { 207 if (first == -1 && exports[i].getName().equals(export.getName())) 208 first = i; 209 if (exports[i] == export && first != i) { 210 index = i; 211 break; 212 } 213 } 214 pkgSource = new FilteredSourcePackage(export.getName(), index, this, includes, excludes, friends); 215 } 216 } 217 218 if (storeSource) { 219 if (pkgSource != null && pkgSources.getByKey(export.getName()) == null) 224 synchronized (pkgSources) { 225 pkgSources.add(pkgSource); 226 } 227 } else { 228 if (pkgSource == null) 231 pkgSource = getPackageSource(export.getName()); 232 } 233 234 return pkgSource; 235 } 236 237 class ReexportPackageSource extends PackageSource { 238 public ReexportPackageSource(String id) { 239 super(id); 240 } 241 242 public synchronized SingleSourcePackage[] getSuppliers() { 243 PackageSource source = getBundleLoader().getPackageSource(id); 244 if (source == null) 245 return null; 246 return source.getSuppliers(); 247 } 248 249 public Class loadClass(String name) { 250 try { 251 return getBundleLoader().findClass(name, false); 252 } catch (ClassNotFoundException e) { 253 return null; 254 } 255 } 256 257 public URL getResource(String name) { 258 return getBundleLoader().findResource(name, false); 259 } 260 261 public Enumeration getResources(String name) throws IOException { 262 return getBundleLoader().findResources(name); 263 } 264 } 265 } 266 | Popular Tags |