1 11 12 package org.eclipse.osgi.internal.baseadaptor; 13 14 import java.io.*; 15 import java.net.URL ; 16 import java.util.Enumeration ; 17 import org.eclipse.osgi.baseadaptor.*; 18 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry; 19 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile; 20 import org.eclipse.osgi.baseadaptor.hooks.StorageHook; 21 import org.eclipse.osgi.framework.adaptor.*; 22 import org.eclipse.osgi.framework.debug.Debug; 23 import org.eclipse.osgi.framework.internal.core.Constants; 24 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 25 import org.eclipse.osgi.framework.util.Headers; 26 import org.osgi.framework.BundleException; 27 28 public class SystemBundleData extends BaseData { 29 private static final String OSGI_FRAMEWORK = "osgi.framework"; 31 public SystemBundleData(BaseAdaptor adaptor) throws BundleException { 32 super(0, adaptor); 33 File osgiBase = getOsgiBase(); 34 createBundleFile(osgiBase); 35 manifest = createManifest(osgiBase); 36 setMetaData(); 37 setLastModified(System.currentTimeMillis()); StorageHook[] hooks = adaptor.getHookRegistry().getStorageHooks(); 39 StorageHook[] instanceHooks = new StorageHook[hooks.length]; 40 for (int i = 0; i < hooks.length; i++) 41 instanceHooks[i] = hooks[i].create(this); 42 setStorageHooks(instanceHooks); 43 } 44 45 private File getOsgiBase() { 46 String frameworkLocation = FrameworkProperties.getProperty(SystemBundleData.OSGI_FRAMEWORK); 47 if (frameworkLocation != null) 48 return new File(frameworkLocation.substring(5)); 50 try { 51 URL url = getClass().getProtectionDomain().getCodeSource().getLocation(); 52 return new File(url.getPath()); 54 } catch (Throwable e) { 55 } 57 frameworkLocation = FrameworkProperties.getProperty("user.dir"); if (frameworkLocation != null) 59 return new File(frameworkLocation); 60 return null; 61 } 62 63 private Headers createManifest(File osgiBase) throws BundleException { 64 InputStream in = null; 65 66 if (osgiBase != null && osgiBase.exists()) 67 try { 68 BundleEntry entry = getBundleFile().getEntry(Constants.OSGI_BUNDLE_MANIFEST); 69 if (entry != null) 70 in = entry.getInputStream(); 71 } catch (IOException e) { 72 } 74 75 if (in == null) 79 in = getManifestAsResource(); 80 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 81 if (in == null) 82 Debug.println("Unable to find system bundle manifest " + Constants.OSGI_BUNDLE_MANIFEST); 84 if (in == null) 85 throw new BundleException(AdaptorMsg.SYSTEMBUNDLE_MISSING_MANIFEST); 86 return Headers.parseManifest(in); 87 } 88 89 private InputStream getManifestAsResource() { 90 ClassLoader cl = getClass().getClassLoader(); 91 try { 92 Enumeration manifests = cl != null ? cl.getResources(Constants.OSGI_BUNDLE_MANIFEST) : ClassLoader.getSystemResources(Constants.OSGI_BUNDLE_MANIFEST); 94 while (manifests.hasMoreElements()) { 95 URL url = (URL ) manifests.nextElement(); 96 try { 97 Headers headers = Headers.parseManifest(url.openStream()); 99 if ("true".equals(headers.get(Constants.ECLIPSE_SYSTEMBUNDLE))) return url.openStream(); 101 } catch (BundleException e) { 102 } 104 } 105 } catch (IOException e) { 106 } 108 return null; 109 } 110 111 private void createBundleFile(File osgiBase) { 112 if (osgiBase != null) 113 try { 114 bundleFile = getAdaptor().createBundleFile(osgiBase, this); 115 } catch (IOException e) { 116 } 118 else 119 bundleFile = new BundleFile(osgiBase) { 120 public File getFile(String path, boolean nativeCode) { 121 return null; 122 } 123 124 public BundleEntry getEntry(String path) { 125 return null; 126 } 127 128 public Enumeration getEntryPaths(String path) { 129 return null; 130 } 131 132 public void close() { 133 } 135 136 public void open() { 137 } 139 140 public boolean containsDir(String dir) { 141 return false; 142 } 143 }; 144 } 145 146 private void setMetaData() throws BundleException { 147 setLocation(Constants.SYSTEM_BUNDLE_LOCATION); 148 BaseStorageHook.loadManifest(this, manifest); 149 } 150 151 public BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, BundleProtectionDomain domain, String [] bundleclasspath) { 152 return null; 153 } 154 155 public File createGenerationDir() { 156 return null; 157 } 158 159 public String findLibrary(String libname) { 160 return null; 161 } 162 163 public void installNativeCode(String [] nativepaths) throws BundleException { 164 } 166 167 public File getDataFile(String path) { 168 return null; 169 } 170 171 public int getStartLevel() { 172 return 0; 173 } 174 175 public int getStatus() { 176 return Constants.BUNDLE_STARTED; 177 } 178 179 public void save() { 180 } 182 183 } 184 | Popular Tags |