1 11 package org.eclipse.osgi.framework.adaptor.core; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import org.eclipse.osgi.internal.resolver.*; 16 import org.eclipse.osgi.service.resolver.*; 17 import org.osgi.framework.BundleContext; 18 import org.osgi.framework.BundleException; 19 20 28 public class StateManager implements PlatformAdmin, Runnable { 29 32 public static boolean DEBUG = false; 33 36 public static boolean DEBUG_READER = false; 37 40 public static boolean DEBUG_PLATFORM_ADMIN = false; 41 44 public static boolean DEBUG_PLATFORM_ADMIN_RESOLVER = false; 45 48 public static boolean MONITOR_PLATFORM_ADMIN = false; 49 52 public static String PROP_NO_LAZY_LOADING = "osgi.noLazyStateLoading"; 56 public static String PROP_LAZY_UNLOADING_TIME = "osgi.lazyStateUnloadingTime"; private long expireTime = 300000; private long readStartupTime; 59 private StateImpl systemState; 60 private StateObjectFactoryImpl factory; 61 private long lastTimeStamp; 62 private BundleInstaller installer; 63 private boolean cachedState = false; 64 private File stateFile; 65 private File lazyFile; 66 private long expectedTimeStamp; 67 private BundleContext context; 68 69 75 public StateManager(File stateFile, File lazyFile, BundleContext context) { 76 this(stateFile, lazyFile, context, -1); 78 } 79 80 88 public StateManager(File stateFile, File lazyFile, BundleContext context, long expectedTimeStamp) { 89 this.stateFile = stateFile; 90 this.lazyFile = lazyFile; 91 this.context = context; 92 this.expectedTimeStamp = expectedTimeStamp; 93 factory = new StateObjectFactoryImpl(); 94 } 95 96 102 public void shutdown(File stateFile, File lazyFile) throws IOException { 103 BundleDescription[] removalPendings = systemState.getRemovalPendings(); 104 if (removalPendings.length > 0) 105 systemState.resolve(removalPendings); 106 writeState(stateFile, lazyFile); 107 } 108 109 private void readSystemState(File stateFile, File lazyFile, long expectedTimeStamp) { 110 if (stateFile == null || !stateFile.isFile()) 111 return; 112 if (DEBUG_READER) 113 readStartupTime = System.currentTimeMillis(); 114 try { 115 boolean lazyLoad = !Boolean.valueOf(System.getProperty(PROP_NO_LAZY_LOADING)).booleanValue(); 116 systemState = factory.readSystemState(stateFile, lazyFile, lazyLoad, expectedTimeStamp); 117 if (systemState == null || !initializeSystemState()) { 119 systemState = null; 120 return; 121 } 122 cachedState = true; 123 try { 124 expireTime = Long.parseLong(System.getProperty(PROP_LAZY_UNLOADING_TIME, Long.toString(expireTime))); 125 } catch (NumberFormatException nfe) { 126 expireTime = 0; 128 } 129 if (lazyLoad && expireTime > 0) { 130 Thread t = new Thread (this, "State Data Manager"); t.setDaemon(true); 132 t.start(); 133 } 134 } catch (IOException ioe) { 135 ioe.printStackTrace(); 137 } finally { 138 if (DEBUG_READER) 139 System.out.println("Time to read state: " + (System.currentTimeMillis() - readStartupTime)); } 141 } 142 143 private void writeState(File stateFile, File lazyFile) throws IOException { 144 if (systemState == null) 145 return; 146 if (cachedState && lastTimeStamp == systemState.getTimeStamp()) 147 return; 148 systemState.fullyLoad(); factory.writeState(systemState, stateFile, lazyFile); 150 } 151 152 private boolean initializeSystemState() { 153 systemState.setResolver(getResolver(System.getSecurityManager() != null)); 154 lastTimeStamp = systemState.getTimeStamp(); 155 return !systemState.setPlatformProperties(System.getProperties()); 156 } 157 158 163 public synchronized State createSystemState() { 164 if (systemState == null) { 165 systemState = factory.createSystemState(); 166 initializeSystemState(); 167 } 168 return systemState; 169 } 170 171 178 public synchronized State readSystemState() { 179 if (systemState == null) 180 readSystemState(stateFile, lazyFile, expectedTimeStamp); 181 return systemState; 182 } 183 184 190 public State getSystemState() { 191 return systemState; 192 } 193 194 201 public long getCachedTimeStamp() { 202 return lastTimeStamp; 203 } 204 205 208 public State getState(boolean mutable) { 209 return mutable ? factory.createState(systemState) : new ReadOnlyState(systemState); 210 } 211 212 215 public State getState() { 216 return getState(true); 217 } 218 219 222 public StateObjectFactory getFactory() { 223 return factory; 224 } 225 226 229 public synchronized void commit(State state) throws BundleException { 230 if (installer == null) 232 throw new IllegalArgumentException ("PlatformAdmin.commit() not supported"); if (!(state instanceof UserState)) 234 throw new IllegalArgumentException ("Wrong state implementation"); if (state.getTimeStamp() != systemState.getTimeStamp()) 236 throw new BundleException(StateMsg.COMMIT_INVALID_TIMESTAMP); 237 StateDelta delta = state.compare(systemState); 238 BundleDelta[] changes = delta.getChanges(); 239 for (int i = 0; i < changes.length; i++) 240 if ((changes[i].getType() & BundleDelta.ADDED) > 0) 241 installer.installBundle(changes[i].getBundle()); 242 else if ((changes[i].getType() & BundleDelta.REMOVED) > 0) 243 installer.uninstallBundle(changes[i].getBundle()); 244 else if ((changes[i].getType() & BundleDelta.UPDATED) > 0) 245 installer.updateBundle(changes[i].getBundle()); 246 else { 247 } 249 } 250 251 254 public Resolver getResolver() { 255 return getResolver(false); 256 } 257 258 private Resolver getResolver(boolean checkPermissions) { 259 return new org.eclipse.osgi.internal.module.ResolverImpl(context, checkPermissions); 260 } 261 262 265 public StateHelper getStateHelper() { 266 return StateHelperImpl.getInstance(); 267 } 268 269 273 public BundleInstaller getInstaller() { 274 return installer; 275 } 276 277 282 public void setInstaller(BundleInstaller installer) { 283 this.installer = installer; 284 } 285 286 public void run() { 287 while (true) { 288 try { 289 Thread.sleep(expireTime); 290 } catch (InterruptedException e) { 291 return; 292 } 293 if (systemState != null && lastTimeStamp == systemState.getTimeStamp()) 294 systemState.unloadLazyData(expireTime); 295 } 296 } 297 } 298 | Popular Tags |