1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*******************************************************************************
* Copyright (c) 2003, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Rob Harrop - SpringSource Inc. (bug 247522)
*******************************************************************************/
package org.eclipse.osgi.internal.baseadaptor;
import java.io.File;
import java.io.IOException;
import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
import org.eclipse.osgi.internal.resolver.*;
import org.eclipse.osgi.service.resolver.*;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
/**
* The StateManager manages the system state for the framework. It also provides the implementation
* to the PlatformAdmin service.
* <p>
* Clients may extend this class.
* </p>
* @since 3.1
*/
public class StateManager implements PlatformAdmin, Runnable {
/**
* General debug flag
*/
public static boolean DEBUG = false;
/**
* Reader debug flag
*/
public static boolean DEBUG_READER = false;
/**
* PlatformAdmin debug flag
*/
public static boolean DEBUG_PLATFORM_ADMIN = false;
/**
* PlatformAdmin resolver debug flag
*/
public static boolean DEBUG_PLATFORM_ADMIN_RESOLVER = false;
/**
* Monitor PlatformAdmin debug flag
*/
public static boolean MONITOR_PLATFORM_ADMIN = false;
/**
* System property used to disable lazy state loading
*/
public static String PROP_NO_LAZY_LOADING = "osgi.noLazyStateLoading"; //$NON-NLS-1$
/**
* System property used to specify to amount time before lazy data can be flushed from memory
*/
public static String PROP_LAZY_UNLOADING_TIME = "osgi.lazyStateUnloadingTime"; //$NON-NLS-1$
private long expireTime = 300000; // default to five minutes
private long readStartupTime;
private StateImpl systemState;
private final StateObjectFactoryImpl factory;
private long lastTimeStamp;
private boolean cachedState = false;
private final File stateFile;
private final File lazyFile;
private final long expectedTimeStamp;
private final BundleContext context;
private Thread dataManagerThread;
/**
* Constructs a StateManager using the specified files and context
* @param stateFile a file with the data required to persist in memory
* @param lazyFile a file with the data that may be lazy loaded and can be flushed from memory
* @param context the bundle context of the system bundle
*/
public StateManager(File stateFile, File lazyFile, BundleContext context) {
// a negative timestamp means no timestamp checking
this(stateFile, lazyFile, context, -1);
}
/**
* Constructs a StateManager using the specified files and context
* @param stateFile a file with the data required to persist in memory
* @param lazyFile a file with the data that may be lazy loaded and can be flushed from memory
* @param context the bundle context of the system bundle
* @param expectedTimeStamp the expected timestamp of the persisted system state. A negative
* value indicates that no timestamp checking is done
*/
public StateManager(File stateFile, File lazyFile, BundleContext context, long expectedTimeStamp) {
this.stateFile = stateFile;
this.lazyFile = lazyFile;
this.context = context;
this.expectedTimeStamp = expectedTimeStamp;
factory = new StateObjectFactoryImpl();
}
/**
* Shutsdown the state manager. If the timestamp of the system state has changed
* @param saveStateFile
* @param saveLazyFile
* @throws IOException
*/
public void shutdown(File saveStateFile, File saveLazyFile) throws IOException {
writeState(systemState, saveStateFile, saveLazyFile);
stopDataManager();
}
/**
* Update the given target files with the state data in memory.
* @param updateStateFile
* @param updateLazyFile
* @throws IOException
*/
public void update(File updateStateFile, File updateLazyFile) throws IOException {
writeState(systemState, updateStateFile, updateLazyFile);
// Need to use the timestamp of the original state here
lastTimeStamp = systemState.getTimeStamp();
// TODO consider updating the state files for lazy loading
}
private void internalReadSystemState() {
if (stateFile == null || !stateFile.isFile())
return;
if (DEBUG_READER)
readStartupTime = System.currentTimeMillis();
try {
boolean lazyLoad = !Boolean.valueOf(FrameworkProperties.getProperty(PROP_NO_LAZY_LOADING)).booleanValue();
systemState = factory.readSystemState(context, stateFile, lazyFile, lazyLoad, expectedTimeStamp);
// problems in the cache (corrupted/stale), don't create a state object
if (systemState == null || !initializeSystemState()) {
systemState = null;
return;
}
cachedState = true;
try {
expireTime = Long.parseLong(FrameworkProperties.getProperty(PROP_LAZY_UNLOADING_TIME, Long.toString(expireTime)));
} catch (NumberFormatException nfe) {
// default to not expire
expireTime = 0;
}
if (lazyLoad && expireTime > 0)
startDataManager();
} catch (IOException ioe) {
// TODO: how do we log this?
ioe.printStackTrace();
} finally {
if (DEBUG_READER)
System.out.println("Time to read state: " + (System.currentTimeMillis() - readStartupTime)); //$NON-NLS-1$
}
}
private synchronized void startDataManager() {
if (dataManagerThread != null)
return;
dataManagerThread = new Thread(this, "State Data Manager"); //$NON-NLS-1$
dataManagerThread.setDaemon(true);
dataManagerThread.start();
}
/**
* Stops the active data manager thread which is used to unload unused
* state objects from memory.
*/
public synchronized void stopDataManager() {
if (dataManagerThread == null)
return;
dataManagerThread.interrupt();
dataManagerThread = null;
}
private void writeState(StateImpl state, File saveStateFile, File saveLazyFile) throws IOException {
if (state == null)
return;
if (cachedState && !saveNeeded())
return;
state.fullyLoad(); // make sure we are fully loaded before saving
factory.writeState(state, saveStateFile, saveLazyFile);
}
private boolean initializeSystemState() {
systemState.setResolver(createResolver(System.getSecurityManager() != null));
lastTimeStamp = systemState.getTimeStamp();
return !systemState.setPlatformProperties(FrameworkProperties.getProperties());
}
/**
* Creates a new State used by the system. If the system State already
* exists then a new system State is not created.
* @return the State used by the system.
*/
public synchronized State createSystemState() {
if (systemState == null) {
systemState = factory.createSystemState(context);
initializeSystemState();
}
return systemState;
}
/**
* Reads the State used by the system. If the system State already
* exists then the system State is not read from a cache. If the State could
* not be read from a cache then <code>null</code> is returned.
* @return the State used by the system or <code>null</code> if the State
* could not be read from a cache.
*/
public synchronized State readSystemState() {
if (systemState == null)
internalReadSystemState();
return systemState;
}
/**
* Returns the State used by the system. If the system State does
* not exist then <code>null</code> is returned.
* @return the State used by the system or <code>null</code> if one
* does not exist.
*/
public State getSystemState() {
return systemState;
}
/**
* Returns the cached time stamp of the system State. This value is the
* original time stamp of the system state when it was created or read from
* a cache.
* @see State#getTimeStamp()
* @return the cached time stamp of the system State
*/
public long getCachedTimeStamp() {
return lastTimeStamp;
}
public boolean saveNeeded() {
return systemState.getTimeStamp() != lastTimeStamp || systemState.dynamicCacheChanged();
}
/**
* @see PlatformAdmin#getState(boolean)
*/
public State getState(boolean mutable) {
return mutable ? factory.createState(systemState) : new ReadOnlyState(systemState);
}
/**
* @see PlatformAdmin#getState()
*/
public State getState() {
return getState(true);
}
/**
* @see PlatformAdmin#getFactory()
*/
public StateObjectFactory getFactory() {
return factory;
}
/**
* @throws BundleException
* @see PlatformAdmin#commit(State)
*/
public synchronized void commit(State state) throws BundleException {
throw new IllegalArgumentException("PlatformAdmin.commit() not supported"); //$NON-NLS-1$
}
/**
* @see PlatformAdmin#getResolver()
* @deprecated
*/
public Resolver getResolver() {
return createResolver(false);
}
/**
* @see PlatformAdmin#createResolver()
*/
public Resolver createResolver() {
return createResolver(false);
}
private Resolver createResolver(boolean checkPermissions) {
return new org.eclipse.osgi.internal.module.ResolverImpl(checkPermissions);
}
/**
* @see PlatformAdmin#getStateHelper()
*/
public StateHelper getStateHelper() {
return StateHelperImpl.getInstance();
}
public void run() {
long timeStamp = lastTimeStamp; // cache the original timestamp incase of updates
while (true) {
try {
Thread.sleep(expireTime);
} catch (InterruptedException e) {
return;
}
if (systemState != null)
synchronized (systemState) {
if (!systemState.unloadLazyData(timeStamp))
return;
}
}
}
public void addDisabledInfo(DisabledInfo disabledInfo) {
if (systemState == null)
throw new IllegalStateException(); // should never happen
systemState.addDisabledInfo(disabledInfo);
}
public void removeDisabledInfo(DisabledInfo disabledInfo) {
if (systemState == null)
throw new IllegalStateException(); // should never happen
systemState.removeDisabledInfo(disabledInfo);
}
}
|