KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > resolver > StateManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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         // a negative timestamp means no timestamp checking
32
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         //systemState should not be set to null as when the framework
43
//is restarted from a shutdown state, the systemState variable will
44
//not be reset, resulting in a null pointer exception
45
//systemState = null;
46
}
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             // TODO: log before bailing
58
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             // problems in the cache (corrupted/stale), don't create a state object
66
if (systemState == null)
67                 return;
68             initializeSystemState();
69             cachedState = true;
70         } catch (IOException ioe) {
71             // TODO: how do we log this?
72
ioe.printStackTrace();
73         } finally {
74             if (DEBUG_READER)
75                 System.out.println("Time to read state: " + (System.currentTimeMillis() - readStartupTime)); //$NON-NLS-1$
76
}
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         // no installer have been provided - commit not supported
117
if (installer == null)
118             throw new IllegalArgumentException JavaDoc("PlatformAdmin.commit() not supported"); //$NON-NLS-1$
119
if (!(state instanceof UserState))
120             throw new IllegalArgumentException JavaDoc("Wrong state implementation"); //$NON-NLS-1$
121
if (state.getTimeStamp() != systemState.getTimeStamp())
122             throw new BundleException(StateMsg.formatter.getString("COMMIT_INVALID_TIMESTAMP")); //$NON-NLS-1$
123
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                 // bug in StateDelta#getChanges
134
}
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