1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.*; 15 import java.net.URL ; 16 import java.util.Enumeration ; 17 import org.eclipse.osgi.framework.adaptor.*; 18 import org.eclipse.osgi.framework.adaptor.core.*; 19 import org.eclipse.osgi.framework.debug.Debug; 20 import org.eclipse.osgi.framework.internal.core.Constants; 21 import org.eclipse.osgi.framework.util.Headers; 22 import org.osgi.framework.BundleException; 23 24 public class SystemBundleData extends AbstractBundleData { 25 public static final String OSGI_FRAMEWORK = "osgi.framework"; 27 public SystemBundleData(AbstractFrameworkAdaptor adaptor) throws BundleException { 28 super(adaptor, 0); 29 File osgiBase = getOsgiBase(); 30 createBundleFile(osgiBase); 31 manifest = createManifest(osgiBase); 32 setMetaData(); 33 setLastModified(System.currentTimeMillis()); } 35 36 private File getOsgiBase() { 37 String frameworkLocation = System.getProperty(OSGI_FRAMEWORK); 38 if (frameworkLocation != null) 39 return new File(frameworkLocation.substring(5)); 41 try { 42 URL url = getClass().getProtectionDomain().getCodeSource().getLocation(); 43 return new File(url.getPath()); 45 } catch (Throwable e) { 46 } 48 frameworkLocation = System.getProperty("user.dir"); if (frameworkLocation != null) 50 return new File(frameworkLocation); 51 return null; 52 } 53 54 private Headers createManifest(File osgiBase) throws BundleException { 55 InputStream in = null; 56 57 if (osgiBase != null && osgiBase.exists()) { 58 try { 59 BundleEntry entry = baseBundleFile.getEntry(Constants.OSGI_BUNDLE_MANIFEST); 60 if (entry != null) 61 in = entry.getInputStream(); 62 } catch (IOException e) { 63 } 65 } 66 67 if (in == null) { 71 in = getManifestAsResource(); 72 } 73 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 74 if (in == null) { 75 Debug.println("Unable to find system bundle manifest " + Constants.OSGI_BUNDLE_MANIFEST); } 77 } 78 79 if (in == null) 80 throw new BundleException(AdaptorMsg.SYSTEMBUNDLE_MISSING_MANIFEST); 81 Headers systemManifest = Headers.parseManifest(in); 82 String exportPackages = adaptor.getExportPackages(); 85 String exportServices = adaptor.getExportServices(); 86 String providePackages = adaptor.getProvidePackages(); 87 if (exportPackages != null) 88 appendManifestValue(systemManifest, Constants.EXPORT_PACKAGE, exportPackages); 89 if (exportServices != null) 90 appendManifestValue(systemManifest, Constants.EXPORT_SERVICE, exportServices); 91 if (providePackages != null) 92 appendManifestValue(systemManifest, Constants.PROVIDE_PACKAGE, providePackages); 93 return systemManifest; 94 } 95 96 private InputStream getManifestAsResource() { 97 ClassLoader cl = getClass().getClassLoader(); 98 try { 99 Enumeration manifests = cl != null ? cl.getResources(Constants.OSGI_BUNDLE_MANIFEST) : ClassLoader.getSystemResources(Constants.OSGI_BUNDLE_MANIFEST); 101 while (manifests.hasMoreElements()) { 102 URL url = (URL ) manifests.nextElement(); 103 try { 104 Headers headers = Headers.parseManifest(url.openStream()); 106 if ("true".equals(headers.get(Constants.ECLIPSE_SYSTEMBUNDLE))) return url.openStream(); 108 } catch (BundleException e) { 109 } 111 } 112 } catch (IOException e) { 113 } 115 return null; 116 } 117 118 private void appendManifestValue(Headers systemManifest, String header, String append) { 119 String newValue = (String ) systemManifest.get(header); 120 if (newValue == null) { 121 newValue = append; 122 } else { 123 newValue += "," + append; } 125 systemManifest.set(header, null); 126 systemManifest.set(header, newValue); 127 } 128 129 private void createBundleFile(File osgiBase) { 130 if (osgiBase != null) 131 try { 132 baseBundleFile = adaptor.createBundleFile(osgiBase, this); 133 } catch (IOException e) { 134 } 136 else 137 baseBundleFile = new BundleFile(osgiBase) { 138 public File getFile(String path) { 139 return null; 140 } 141 142 public BundleEntry getEntry(String path) { 143 return null; 144 } 145 146 public Enumeration getEntryPaths(String path) { 147 return null; 148 } 149 150 public void close() { 151 } 153 154 public void open() { 155 } 157 158 public boolean containsDir(String dir) { 159 return false; 160 } 161 }; 162 } 163 164 private void setMetaData() throws BundleException { 165 setLocation(Constants.SYSTEM_BUNDLE_LOCATION); 166 loadFromManifest(); 167 } 168 169 public BundleClassLoader createClassLoader(ClassLoaderDelegate delegate, BundleProtectionDomain domain, String [] bundleclasspath) { 170 return null; 171 } 172 173 public File createGenerationDir() { 174 return null; 175 } 176 177 public String findLibrary(String libname) { 178 return null; 179 } 180 181 public void installNativeCode(String [] nativepaths) throws BundleException { 182 } 184 185 public File getDataFile(String path) { 186 return null; 187 } 188 189 public int getStartLevel() { 190 return 0; 191 } 192 193 public int getStatus() { 194 return 0; 195 } 196 197 public void save() { 198 } 200 201 public String [] getBundleSigners() { 202 return null; } 204 } 205 | Popular Tags |