1 11 12 package org.eclipse.osgi.baseadaptor; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.*; 19 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry; 20 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile; 21 import org.eclipse.osgi.baseadaptor.hooks.*; 22 import org.eclipse.osgi.baseadaptor.loader.BaseClassLoader; 23 import org.eclipse.osgi.framework.adaptor.*; 24 import org.eclipse.osgi.framework.debug.Debug; 25 import org.eclipse.osgi.framework.internal.core.Constants; 26 import org.eclipse.osgi.framework.internal.protocol.bundleentry.Handler; 27 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 28 import org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader; 29 import org.eclipse.osgi.util.ManifestElement; 30 import org.osgi.framework.*; 31 32 40 public class BaseData implements BundleData { 41 private long id; 42 private BaseAdaptor adaptor; 43 private Bundle bundle; 44 private int startLevel = -1; 45 private int status = 0; 46 private StorageHook[] storageHooks; 47 private String location; 48 private long lastModified; 49 protected BundleFile bundleFile; 50 private boolean dirty = false; 51 protected Dictionary manifest; 52 protected String fileName; 54 protected Collection loadedNativeCode; 56 57 private String symbolicName; 59 private Version version; 60 private String activator; 61 private String classpath; 62 private String executionEnvironment; 63 private String dynamicImports; 64 private int type; 65 66 68 73 public BaseData(long id, BaseAdaptor adaptor) { 74 this.id = id; 75 this.adaptor = adaptor; 76 } 77 78 84 public BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, BundleProtectionDomain domain, String [] bundleclasspath) { 85 ClassLoadingHook[] hooks = adaptor.getHookRegistry().getClassLoadingHooks(); 86 ClassLoader parent = adaptor.getBundleClassLoaderParent(); 87 BaseClassLoader cl = null; 88 for (int i = 0; i < hooks.length && cl == null; i++) 89 cl = hooks[i].createClassLoader(parent, delegate, domain, this, bundleclasspath); 90 if (cl == null) 91 cl = new DefaultClassLoader(parent, delegate, domain, this, bundleclasspath); 92 return cl; 93 } 94 95 public final URL getEntry(String path) { 96 BundleEntry entry = getBundleFile().getEntry(path); 97 if (entry == null) 98 return null; 99 if (path.length() == 0 || path.charAt(0) != '/') 100 path = '/' + path; 101 try { 102 return new URL (Constants.OSGI_ENTRY_URL_PROTOCOL, Long.toString(id), 0, path, new Handler(entry)); 104 } catch (MalformedURLException e) { 105 return null; 106 } 107 } 108 109 public final Enumeration getEntryPaths(String path) { 110 return getBundleFile().getEntryPaths(path); 111 } 112 113 118 public String findLibrary(String libname) { 119 ClassLoadingHook[] hooks = adaptor.getHookRegistry().getClassLoadingHooks(); 120 String result = null; 121 for (int i = 0; i < hooks.length; i++) { 122 result = hooks[i].findLibrary(this, libname); 123 if (result != null) 124 break; 125 } 126 if (result != null) 128 synchronized (this) { 129 if (loadedNativeCode == null) 130 loadedNativeCode = new ArrayList(1); 131 if (loadedNativeCode.contains(result)) { 132 String temp = copyToTempLibrary(result); 134 if (temp != null) 135 result = temp; 136 } else { 137 loadedNativeCode.add(result); 138 } 139 } 140 return result; 141 } 142 143 private String copyToTempLibrary(String result) { 144 try { 145 return adaptor.getStorage().copyToTempLibrary(this, result); 146 } catch (IOException e) { 147 adaptor.getFrameworkLog().log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, e.getMessage(), 0, e, null)); 148 } 149 return null; 150 } 151 152 public void installNativeCode(String [] nativepaths) throws BundleException { 153 adaptor.getStorage().installNativeCode(this, nativepaths); 154 } 155 156 public File getDataFile(String path) { 157 return adaptor.getStorage().getDataFile(this, path); 158 } 159 160 public Dictionary getManifest() throws BundleException { 161 if (manifest == null) 162 manifest = adaptor.getStorage().loadManifest(this); 163 return manifest; 164 } 165 166 public long getBundleID() { 167 return id; 168 } 169 170 public final String getLocation() { 171 return location; 172 } 173 174 178 public final void setLocation(String location) { 179 this.location = location; 180 } 181 182 public final long getLastModified() { 183 return lastModified; 184 } 185 186 190 public final void setLastModified(long lastModified) { 191 this.lastModified = lastModified; 192 } 193 194 public void close() throws IOException { 195 if (bundleFile != null) 196 getBundleFile().close(); } 198 199 public void open() throws IOException { 200 getBundleFile().open(); 201 } 202 203 public final void setBundle(Bundle bundle) { 204 this.bundle = bundle; 205 } 206 207 211 public final Bundle getBundle() { 212 return bundle; 213 } 214 215 public int getStartLevel() { 216 return startLevel; 217 } 218 219 public int getStatus() { 220 return status; 221 } 222 223 228 public void setStartLevel(int value) { 229 startLevel = setPersistentData(value, true, startLevel); 230 } 231 232 237 public void setStatus(int value) { 238 status = setPersistentData(value, false, status); 239 } 240 241 private int setPersistentData(int value, boolean isStartLevel, int orig) { 242 StorageHook[] hooks = getStorageHooks(); 243 for (int i = 0; i < hooks.length; i++) 244 if (isStartLevel) { 245 if (hooks[i].forgetStartLevelChange(value)) 246 return value; 247 } else { 248 if (hooks[i].forgetStatusChange(value)) 249 return value; 250 } 251 if (value != orig) 252 dirty = true; 253 return value; 254 } 255 256 public void save() throws IOException { 257 adaptor.getStorage().save(this); 258 } 259 260 264 public boolean isDirty() { 265 return dirty; 266 } 267 268 272 public void setDirty(boolean dirty) { 273 this.dirty = dirty; 274 } 275 276 public final String getSymbolicName() { 277 return symbolicName; 278 } 279 280 284 public final void setSymbolicName(String symbolicName) { 285 this.symbolicName = symbolicName; 286 } 287 288 public final Version getVersion() { 289 return version; 290 } 291 292 296 public final void setVersion(Version version) { 297 this.version = version; 298 } 299 300 public final int getType() { 301 return type; 302 } 303 304 308 public final void setType(int type) { 309 this.type = type; 310 } 311 312 public final String [] getClassPath() throws BundleException { 313 ManifestElement[] classpathElements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, classpath); 314 return getClassPath(classpathElements); 315 } 316 317 public String getClassPathString() { 319 return classpath; 320 } 321 322 public void setClassPathString(String classpath) { 324 this.classpath = classpath; 325 } 326 327 public final String getActivator() { 328 return activator; 329 } 330 331 335 public final void setActivator(String activator) { 336 this.activator = activator; 337 } 338 339 public final String getExecutionEnvironment() { 340 return executionEnvironment; 341 } 342 343 347 public void setExecutionEnvironment(String executionEnvironment) { 348 this.executionEnvironment = executionEnvironment; 349 } 350 351 public final String getDynamicImports() { 352 return dynamicImports; 353 } 354 355 359 public void setDynamicImports(String dynamicImports) { 360 this.dynamicImports = dynamicImports; 361 } 362 363 368 public final boolean matchDNChain(String pattern) { 369 StorageHook[] hooks = getStorageHooks(); 370 for (int i = 0; i < hooks.length; i++) 371 if (hooks[i].matchDNChain(pattern)) 372 return true; 373 return false; 374 } 375 376 380 public final BaseAdaptor getAdaptor() { 381 return adaptor; 382 } 383 384 390 public synchronized BundleFile getBundleFile() throws IllegalArgumentException { 391 if (bundleFile == null) 392 try { 393 bundleFile = adaptor.createBundleFile(null, this); 394 } catch (IOException e) { 395 throw new IllegalArgumentException (e.getMessage()); 396 } 397 return bundleFile; 398 } 399 400 private static String [] getClassPath(ManifestElement[] classpath) { 401 if (classpath == null) { 402 if (Debug.DEBUG && Debug.DEBUG_LOADER) 403 Debug.println(" no classpath"); 405 return new String [] {"."}; } 407 408 ArrayList result = new ArrayList(classpath.length); 409 for (int i = 0; i < classpath.length; i++) { 410 if (Debug.DEBUG && Debug.DEBUG_LOADER) 411 Debug.println(" found classpath entry " + classpath[i].getValueComponents()); String [] paths = classpath[i].getValueComponents(); 413 for (int j = 0; j < paths.length; j++) { 414 result.add(paths[j]); 415 } 416 } 417 418 return (String []) result.toArray(new String [result.size()]); 419 } 420 421 426 public StorageHook getStorageHook(String key) { 427 if (storageHooks == null) 428 return null; 429 for (int i = 0; i < storageHooks.length; i++) 430 if (storageHooks[i].getKey().equals(key)) 431 return storageHooks[i]; 432 return null; 433 } 434 435 441 public void setStorageHooks(StorageHook[] storageHooks) { 442 if (this.storageHooks != null) 443 return; this.storageHooks = storageHooks; 445 } 446 447 451 public StorageHook[] getStorageHooks() { 452 return storageHooks == null ? new StorageHook[0] : storageHooks; 453 } 454 455 463 public File getExtractFile(String path) { 464 return adaptor.getStorage().getExtractFile(this, path); 465 } 466 467 472 public void setFileName(String fileName) { 474 this.fileName = fileName; 475 } 476 477 478 483 public String toString() { 484 String name = getSymbolicName(); 485 if (name == null) 486 return getLocation(); 487 Version ver = getVersion(); 488 if (ver == null) 489 return name; 490 return name+"_"+ver; } 492 } 493 | Popular Tags |