1 11 package org.eclipse.osgi.internal.baseadaptor; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 16 import org.eclipse.osgi.internal.resolver.*; 17 import org.eclipse.osgi.service.resolver.*; 18 import org.osgi.framework.BundleContext; 19 import org.osgi.framework.BundleException; 20 21 29 public class StateManager implements PlatformAdmin, Runnable { 30 33 public static boolean DEBUG = false; 34 37 public static boolean DEBUG_READER = false; 38 41 public static boolean DEBUG_PLATFORM_ADMIN = false; 42 45 public static boolean DEBUG_PLATFORM_ADMIN_RESOLVER = false; 46 49 public static boolean MONITOR_PLATFORM_ADMIN = false; 50 53 public static String PROP_NO_LAZY_LOADING = "osgi.noLazyStateLoading"; 57 public static String PROP_LAZY_UNLOADING_TIME = "osgi.lazyStateUnloadingTime"; private long expireTime = 300000; private long readStartupTime; 60 private StateImpl systemState; 61 private StateObjectFactoryImpl factory; 62 private long lastTimeStamp; 63 private boolean cachedState = false; 64 private File stateFile; 65 private File lazyFile; 66 private long expectedTimeStamp; 67 private BundleContext context; 68 private Thread dataManagerThread; 69 70 76 public StateManager(File stateFile, File lazyFile, BundleContext context) { 77 this(stateFile, lazyFile, context, -1); 79 } 80 81 89 public StateManager(File stateFile, File lazyFile, BundleContext context, long expectedTimeStamp) { 90 this.stateFile = stateFile; 91 this.lazyFile = lazyFile; 92 this.context = context; 93 this.expectedTimeStamp = expectedTimeStamp; 94 factory = new StateObjectFactoryImpl(); 95 } 96 97 103 public void shutdown(File stateFile, File lazyFile) throws IOException { 104 BundleDescription[] removalPendings = systemState.getRemovalPendings(); 105 if (removalPendings.length > 0) 106 systemState.resolve(removalPendings); 107 writeState(systemState, stateFile, lazyFile); 108 stopDataManager(); 109 } 110 111 117 public void update(File stateFile, File lazyFile) throws IOException { 118 BundleDescription[] removalPendings = systemState.getRemovalPendings(); 119 StateImpl state = systemState; 120 if (removalPendings.length > 0) { 121 state = (StateImpl) state.getFactory().createState(systemState); 122 state.setResolver(getResolver(System.getSecurityManager() != null)); 123 state.setPlatformProperties(FrameworkProperties.getProperties()); 124 state.resolve(false); 125 } 126 writeState(state, stateFile, lazyFile); 127 lastTimeStamp = state.getTimeStamp(); 128 } 130 131 private void readSystemState(File stateFile, File lazyFile, long expectedTimeStamp) { 132 if (stateFile == null || !stateFile.isFile()) 133 return; 134 if (DEBUG_READER) 135 readStartupTime = System.currentTimeMillis(); 136 try { 137 boolean lazyLoad = !Boolean.valueOf(FrameworkProperties.getProperty(PROP_NO_LAZY_LOADING)).booleanValue(); 138 systemState = factory.readSystemState(stateFile, lazyFile, lazyLoad, expectedTimeStamp); 139 if (systemState == null || !initializeSystemState()) { 141 systemState = null; 142 return; 143 } 144 cachedState = true; 145 try { 146 expireTime = Long.parseLong(FrameworkProperties.getProperty(PROP_LAZY_UNLOADING_TIME, Long.toString(expireTime))); 147 } catch (NumberFormatException nfe) { 148 expireTime = 0; 150 } 151 if (lazyLoad && expireTime > 0) 152 startDataManager(); 153 } catch (IOException ioe) { 154 ioe.printStackTrace(); 156 } finally { 157 if (DEBUG_READER) 158 System.out.println("Time to read state: " + (System.currentTimeMillis() - readStartupTime)); } 160 } 161 162 private synchronized void startDataManager() { 163 if (dataManagerThread != null) 164 return; 165 dataManagerThread = new Thread (this, "State Data Manager"); dataManagerThread.setDaemon(true); 167 dataManagerThread.start(); 168 } 169 170 174 public synchronized void stopDataManager() { 175 if (dataManagerThread == null) 176 return; 177 dataManagerThread.interrupt(); 178 dataManagerThread = null; 179 } 180 181 private void writeState(StateImpl state, File stateFile, File lazyFile) throws IOException { 182 if (state == null) 183 return; 184 if (cachedState && !saveNeeded()) 185 return; 186 state.fullyLoad(); factory.writeState(state, stateFile, lazyFile); 188 } 189 190 private boolean initializeSystemState() { 191 systemState.setResolver(getResolver(System.getSecurityManager() != null)); 192 lastTimeStamp = systemState.getTimeStamp(); 193 return !systemState.setPlatformProperties(FrameworkProperties.getProperties()); 194 } 195 196 201 public synchronized State createSystemState() { 202 if (systemState == null) { 203 systemState = factory.createSystemState(); 204 initializeSystemState(); 205 } 206 return systemState; 207 } 208 209 216 public synchronized State readSystemState() { 217 if (systemState == null) 218 readSystemState(stateFile, lazyFile, expectedTimeStamp); 219 return systemState; 220 } 221 222 228 public State getSystemState() { 229 return systemState; 230 } 231 232 239 public long getCachedTimeStamp() { 240 return lastTimeStamp; 241 } 242 243 public boolean saveNeeded() { 244 return systemState.getTimeStamp() != lastTimeStamp || systemState.dynamicCacheChanged(); 245 } 246 247 250 public State getState(boolean mutable) { 251 return mutable ? factory.createState(systemState) : new ReadOnlyState(systemState); 252 } 253 254 257 public State getState() { 258 return getState(true); 259 } 260 261 264 public StateObjectFactory getFactory() { 265 return factory; 266 } 267 268 271 public synchronized void commit(State state) throws BundleException { 272 throw new IllegalArgumentException ("PlatformAdmin.commit() not supported"); } 274 275 278 public Resolver getResolver() { 279 return getResolver(false); 280 } 281 282 private Resolver getResolver(boolean checkPermissions) { 283 return new org.eclipse.osgi.internal.module.ResolverImpl(context, checkPermissions); 284 } 285 286 289 public StateHelper getStateHelper() { 290 return StateHelperImpl.getInstance(); 291 } 292 293 public void run() { 294 long timeStamp = lastTimeStamp; while (true) { 296 try { 297 Thread.sleep(expireTime); 298 } catch (InterruptedException e) { 299 return; 300 } 301 if (systemState != null) 302 synchronized (systemState) { 303 if (timeStamp == systemState.getTimeStamp() && !systemState.dynamicCacheChanged()) 304 systemState.unloadLazyData(expireTime); 305 } 306 } 307 } 308 } 309 | Popular Tags |