1 11 12 package org.eclipse.osgi.framework.internal.defaultadaptor; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.security.AccessController ; 17 import java.security.PrivilegedActionException ; 18 import java.security.PrivilegedExceptionAction ; 19 import java.util.ArrayList ; 20 import org.eclipse.osgi.framework.adaptor.BundleData; 21 import org.eclipse.osgi.framework.adaptor.core.*; 22 import org.eclipse.osgi.framework.debug.Debug; 23 import org.eclipse.osgi.framework.log.FrameworkLog; 24 import org.osgi.framework.BundleException; 25 import org.osgi.framework.FrameworkEvent; 26 27 49 public class DefaultAdaptor extends AbstractFrameworkAdaptor { 50 public static final String METADATA_ADAPTOR_NEXTID = "METADATA_ADAPTOR_NEXTID"; public static final String METADATA_ADAPTOR_IBSL = "METADATA_ADAPTOR_IBSL"; 53 public static final String METADATA_BUNDLE_GEN = "METADATA_BUNDLE_GEN"; public static final String METADATA_BUNDLE_LOC = "METADATA_BUNDLE_LOC"; public static final String METADATA_BUNDLE_REF = "METADATA_BUNDLE_REF"; public static final String METADATA_BUNDLE_NAME = "METADATA_BUNDLE_NAME"; public static final String METADATA_BUNDLE_NCP = "METADATA_BUNDLE_NCP"; public static final String METADATA_BUNDLE_ABSL = "METADATA_BUNDLE_ABSL"; public static final String METADATA_BUNDLE_STATUS = "METADATA_BUNDLE_STATUS"; public static final String METADATA_BUNDLE_METADATA = "METADATA_BUNDLE_METADATA"; public static final String METADATA_LAST_MODIFIED = "METADATA_LAST_MODIFIED"; 63 66 protected MetaData fwMetadata; 67 68 public DefaultAdaptor(String [] args) { 69 super(args); 70 } 71 72 75 public BundleData[] getInstalledBundles() { 76 String list[] = getBundleStoreRootDir().list(); 77 78 if (list == null) { 79 return null; 80 } 81 ArrayList bundleDatas = new ArrayList (list.length); 82 83 84 for (int i = 0; i < list.length; i++) { 85 try { 86 DefaultBundleData data; 87 88 long id = -1; 89 try { 90 id = Long.parseLong(list[i]); 91 } catch (NumberFormatException nfe) { 92 continue; 93 } 94 data = (DefaultBundleData) getElementFactory().createBundleData(this, id); 95 loadMetaDataFor(data); 96 data.initializeExistingBundle(); 97 98 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 99 Debug.println("BundleData created: " + data); } 101 processExtension(data, EXTENSION_INITIALIZE); 102 bundleDatas.add(data); 103 } catch (BundleException e) { 104 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 105 Debug.println("Unable to open Bundle[" + list[i] + "]: " + e.getMessage()); Debug.printStackTrace(e); 107 } 108 } catch (IOException e) { 109 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 110 Debug.println("Unable to open Bundle[" + list[i] + "]: " + e.getMessage()); Debug.printStackTrace(e); 112 } 113 } 114 } 115 116 return (BundleData[]) bundleDatas.toArray(new BundleData[bundleDatas.size()]); 117 } 118 119 public void setInitialBundleStartLevel(int value) { 120 super.setInitialBundleStartLevel(value); 121 try { 122 persistInitialBundleStartLevel(value); 123 } catch (IOException e) { 124 eventPublisher.publishFrameworkEvent(FrameworkEvent.ERROR, context.getBundle(), e); 125 } 126 } 127 128 protected void persistInitialBundleStartLevel(int value) throws IOException { 129 fwMetadata.setInt(METADATA_ADAPTOR_IBSL, value); 130 try { 131 AccessController.doPrivileged(new PrivilegedExceptionAction () { 132 public Object run() throws Exception { 133 fwMetadata.save(); 134 return null; 135 } 136 }); 137 } catch (PrivilegedActionException e) { 138 if (e.getException() instanceof IOException ) { 139 throw (IOException )e.getException(); 140 } 141 throw (RuntimeException )e.getException(); 142 } 143 } 144 145 public AdaptorElementFactory getElementFactory() { 146 if (elementFactory == null) 147 elementFactory = new DefaultElementFactory(); 148 return elementFactory; 149 } 150 151 protected void loadMetaDataFor(DefaultBundleData data) throws IOException { 152 MetaData bundleMetaData = (new MetaData(new File (data.getBundleStoreDir(), ".bundle"), "Bundle metadata")); bundleMetaData.load(); 154 155 data.setLocation(bundleMetaData.get(METADATA_BUNDLE_LOC, null)); 156 data.setFileName(bundleMetaData.get(METADATA_BUNDLE_NAME, null)); 157 data.setGeneration(bundleMetaData.getInt(METADATA_BUNDLE_GEN, -1)); 158 data.setNativePaths(bundleMetaData.get(METADATA_BUNDLE_NCP, null)); 159 data.setStartLevel(bundleMetaData.getInt(METADATA_BUNDLE_ABSL, 1)); 160 data.setStatus(bundleMetaData.getInt(METADATA_BUNDLE_STATUS, 0)); 161 data.setReference(bundleMetaData.getBoolean(METADATA_BUNDLE_REF, false)); 162 data.setLastModified(bundleMetaData.getLong(METADATA_LAST_MODIFIED, 0)); 163 164 if (data.getGeneration() == -1 || data.getFileName() == null || data.getLocation() == null) { 165 throw new IOException (AdaptorMsg.ADAPTOR_STORAGE_EXCEPTION); } 167 } 168 169 public void saveMetaDataFor(AbstractBundleData data) throws IOException { 170 MetaData bundleMetadata = (new MetaData(new File (((DefaultBundleData) data).createBundleStoreDir(), ".bundle"), "Bundle metadata")); bundleMetadata.load(); 172 173 bundleMetadata.set(METADATA_BUNDLE_LOC, data.getLocation()); 174 bundleMetadata.set(METADATA_BUNDLE_NAME, data.getFileName()); 175 bundleMetadata.setInt(METADATA_BUNDLE_GEN, data.getGeneration()); 176 String nativePaths = data.getNativePathsString(); 177 if (nativePaths != null) { 178 bundleMetadata.set(METADATA_BUNDLE_NCP, nativePaths); 179 } 180 bundleMetadata.setInt(METADATA_BUNDLE_ABSL, data.getStartLevel()); 181 bundleMetadata.setInt(METADATA_BUNDLE_STATUS, data.getStatus()); 182 bundleMetadata.setBoolean(METADATA_BUNDLE_REF, data.isReference()); 183 bundleMetadata.setLong(METADATA_LAST_MODIFIED, data.getLastModified()); 184 185 bundleMetadata.save(); 186 } 187 188 protected void persistNextBundleID(long id) throws IOException { 189 fwMetadata.setLong(METADATA_ADAPTOR_NEXTID, nextId); 190 fwMetadata.save(); 191 } 192 193 protected void initializeMetadata() throws IOException { 194 fwMetadata = new MetaData(getMetaDataFile(), "Framework metadata"); fwMetadata.load(); 196 nextId = fwMetadata.getLong(METADATA_ADAPTOR_NEXTID, 1); 197 initialBundleStartLevel = fwMetadata.getInt(METADATA_ADAPTOR_IBSL, 1); 198 } 199 200 protected FrameworkLog createFrameworkLog() { 201 return new DefaultLog(); 202 } 203 } 204 | Popular Tags |