1 11 package org.eclipse.osgi.internal.resolver; 12 13 import java.io.*; 14 import org.eclipse.osgi.service.resolver.*; 15 import org.osgi.framework.BundleException; 16 17 public class StateManager implements PlatformAdmin { 18 public static boolean DEBUG = false; 19 public static boolean DEBUG_READER = false; 20 public static boolean DEBUG_PLATFORM_ADMIN = false; 21 public static boolean DEBUG_PLATFORM_ADMIN_RESOLVER = false; 22 public static boolean MONITOR_PLATFORM_ADMIN = false; 23 private long readStartupTime; 24 private StateImpl systemState; 25 private StateObjectFactoryImpl factory; 26 private long lastTimeStamp; 27 private BundleInstaller installer; 28 private boolean cachedState = false; 29 30 public StateManager(File stateLocation) { 31 this(stateLocation, -1); 33 } 34 35 public StateManager(File stateLocation, long expectedTimeStamp) { 36 factory = new StateObjectFactoryImpl(); 37 readState(stateLocation, expectedTimeStamp); 38 } 39 40 public void shutdown(File stateLocation) throws IOException { 41 writeState(stateLocation); 42 } 47 48 private void readState(File stateLocation, long expectedTimeStamp) { 49 if (!stateLocation.isFile()) 50 return; 51 if (DEBUG_READER) 52 readStartupTime = System.currentTimeMillis(); 53 FileInputStream fileInput; 54 try { 55 fileInput = new FileInputStream(stateLocation); 56 } catch (FileNotFoundException e) { 57 e.printStackTrace(); 59 return; 60 } 61 DataInputStream input = null; 62 try { 63 input = new DataInputStream(new BufferedInputStream(fileInput, 65536)); 64 systemState = factory.readSystemState(input, expectedTimeStamp); 65 if (systemState == null) 67 return; 68 initializeSystemState(); 69 cachedState = true; 70 } catch (IOException ioe) { 71 ioe.printStackTrace(); 73 } finally { 74 if (DEBUG_READER) 75 System.out.println("Time to read state: " + (System.currentTimeMillis() - readStartupTime)); } 77 } 78 79 private void writeState(File stateLocation) throws IOException { 80 if (systemState == null) 81 return; 82 if (cachedState && lastTimeStamp == systemState.getTimeStamp()) 83 return; 84 DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(stateLocation))); 85 factory.writeState(systemState, output); 86 } 87 88 public StateImpl createSystemState() { 89 systemState = factory.createSystemState(); 90 initializeSystemState(); 91 return systemState; 92 } 93 94 private void initializeSystemState() { 95 systemState.setResolver(new ResolverImpl()); 96 lastTimeStamp = systemState.getTimeStamp(); 97 } 98 99 public StateImpl getSystemState() { 100 return systemState; 101 } 102 103 public State getState(boolean mutable) { 104 return mutable ? factory.createState(systemState) : new ReadOnlyState(systemState); 105 } 106 107 public State getState() { 108 return getState(true); 109 } 110 111 public StateObjectFactory getFactory() { 112 return factory; 113 } 114 115 public synchronized void commit(State state) throws BundleException { 116 if (installer == null) 118 throw new IllegalArgumentException ("PlatformAdmin.commit() not supported"); if (!(state instanceof UserState)) 120 throw new IllegalArgumentException ("Wrong state implementation"); if (state.getTimeStamp() != systemState.getTimeStamp()) 122 throw new BundleException(StateMsg.formatter.getString("COMMIT_INVALID_TIMESTAMP")); StateDelta delta = state.compare(systemState); 124 BundleDelta[] changes = delta.getChanges(); 125 for (int i = 0; i < changes.length; i++) 126 if ((changes[i].getType() & BundleDelta.ADDED) > 0) 127 installer.installBundle(changes[i].getBundle()); 128 else if ((changes[i].getType() & BundleDelta.REMOVED) > 0) 129 installer.uninstallBundle(changes[i].getBundle()); 130 else if ((changes[i].getType() & BundleDelta.UPDATED) > 0) 131 installer.updateBundle(changes[i].getBundle()); 132 else { 133 } 135 } 136 137 public Resolver getResolver() { 138 return new ResolverImpl(); 139 } 140 141 public StateHelper getStateHelper() { 142 return StateHelperImpl.getInstance(); 143 } 144 145 public BundleInstaller getInstaller() { 146 return installer; 147 } 148 149 public void setInstaller(BundleInstaller installer) { 150 this.installer = installer; 151 } 152 } | Popular Tags |