Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/remote/LookupStorage.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.remote; | |
2 | |
3 import java.io.File; | |
4 import java.io.Serializable; | |
5 import java.io.IOException; | |
6 import java.io.FileOutputStream; | |
7 import java.io.ObjectOutputStream; | |
8 import java.io.FileInputStream; | |
9 import java.io.ObjectInputStream; | |
10 | |
11 import java.util.logging.Level; | |
12 import java.util.BitSet; | |
13 | |
14 import net.jini.core.entry.Entry; | |
15 | |
16 import net.jini.config.Configuration; | |
17 import net.jini.config.ConfigurationException; | |
18 | |
19 import net.jini.id.Uuid; | |
20 | |
21 import net.jini.core.lookup.ServiceID; | |
22 | |
23 import net.jini.discovery.DiscoveryManagement; | |
24 | |
25 import net.jini.lookup.JoinManager; | |
26 | |
27 import org.dancres.util.BytePacker; | |
28 | |
29 import org.dancres.blitz.config.ConfigurationFactory; | |
30 | |
31 import org.dancres.blitz.meta.Registry; | |
32 import org.dancres.blitz.meta.RegistryAccessor; | |
33 import org.dancres.blitz.meta.RegistryFactory; | |
34 import org.dancres.blitz.meta.Initializer; | |
35 | |
36 import org.dancres.blitz.disk.Disk; | |
37 import org.dancres.blitz.disk.DiskTxn; | |
38 import org.dancres.blitz.disk.Syncable; | |
39 | |
40 /** | |
41 This class is responsible for managing JoinManagement state saving to | |
42 and reloading from disk as required. | |
43 | |
44 @todo Need logging somewhere | |
45 */ | |
46 public class LookupStorage implements Syncable { | |
47 public static final byte INIT_ATTRS = 1; | |
48 public static final byte INIT_LOCATORS = 2; | |
49 public static final byte INIT_GROUPS = 4; | |
50 | |
51 private static final String JOIN_STATE = "BlitzJoinState"; | |
52 | |
53 private static byte[] JOIN_STATE_KEY = new byte[12]; | |
54 | |
55 static { | |
56 BytePacker myPacker = BytePacker.getMSBPacker(JOIN_STATE_KEY); | |
57 myPacker.putInt(0, 1); | |
58 } | |
59 | |
60 private LookupData theData; | |
61 | |
62 private Registry theRegistry; | |
63 | |
64 public LookupStorage() throws ConfigurationException { | |
65 } | |
66 | |
67 /** | |
68 * Load state from disk or create from scratch | |
69 * | |
70 * Warning: not thread-safe | |
71 */ | |
72 public void init(Configuration aConfig) throws ConfigurationException { | |
73 try { | |
74 theRegistry = | |
75 RegistryFactory.get(JOIN_STATE, new RegistryInit(aConfig)); | |
76 Disk.add(this); | |
77 | |
78 DiskTxn myTxn = DiskTxn.newTxn(); | |
79 | |
80 theData = (LookupData) | |
81 theRegistry.getAccessor().load(JOIN_STATE_KEY); | |
82 | |
83 myTxn.commit(); | |
84 | |
85 // Display recovered settings for user | |
86 theData.dump(); | |
87 } catch (Exception anE) { | |
88 BlitzServiceImpl.theLogger.log(Level.SEVERE, | |
89 "Exceptioned loading state", | |
90 anE); | |
91 throw new ConfigurationException("Failed to load join state", | |
92 anE); | |
93 } | |
94 } | |
95 | |
96 /** | |
97 * Recover previous state (if it's not already loaded) and then | |
98 * overwrite selected settings. Make sure to call saveState | |
99 * | |
100 * Warning: not thread-safe | |
101 */ | |
102 public void reinit(Configuration aConfig, byte aFlags) | |
103 throws ConfigurationException { | |
104 | |
105 if (theRegistry == null) | |
106 init(aConfig); | |
107 | |
108 if ((aFlags & INIT_ATTRS) != 0) { | |
109 theData.initAttrs(aConfig); | |
110 } | |
111 | |
112 if ((aFlags & INIT_LOCATORS) != 0) { | |
113 theData.initLocators(aConfig); | |
114 } | |
115 | |
116 if ((aFlags & INIT_GROUPS) != 0) { | |
117 theData.initGroups(aConfig); | |
118 } | |
119 } | |
120 | |
121 public void sync() throws Exception { | |
122 // Always in sync | |
123 } | |
124 | |
125 public void close() throws Exception { | |
126 theRegistry.close(); | |
127 } | |
128 | |
129 /** | |
130 Recover a DiscoveryManagement object pre-configured with settings. | |
131 */ | |
132 DiscoveryManagement getDiscMgt() throws ConfigurationException { | |
133 return theData.getLookupDiscovery(); | |
134 } | |
135 | |
136 /** | |
137 Each JINI service has a unique (well, not quite) id | |
138 */ | |
139 ServiceID getServiceID() { | |
140 return theData.getServiceID(); | |
141 } | |
142 | |
143 Uuid getUuid() { | |
144 return theData.getUuid(); | |
145 } | |
146 | |
147 /** | |
148 Recover any descriptive attributes to be advertised as part of the | |
149 registration. | |
150 */ | |
151 Entry[] getAttributes() throws IOException { | |
152 return theData.getAttributes(); | |
153 } | |
154 | |
155 synchronized void sync(JoinManager aMgr) throws IOException { | |
156 theData.update(aMgr); | |
157 saveState(); | |
158 theData.dump(); | |
159 } | |
160 | |
161 public synchronized void saveState() throws IOException { | |
162 DiskTxn myTxn = DiskTxn.newTxn(); | |
163 | |
164 theRegistry.getAccessor().save(JOIN_STATE_KEY, theData); | |
165 | |
166 myTxn.commit(); | |
167 } | |
168 | |
169 private class RegistryInit implements Initializer { | |
170 private boolean wasExecuted; | |
171 | |
172 private Configuration theConfig; | |
173 | |
174 RegistryInit(Configuration aConfig) { | |
175 theConfig = aConfig; | |
176 } | |
177 | |
178 public void execute(RegistryAccessor anAccessor) throws IOException { | |
179 | |
180 LookupData myData = new LookupData(); | |
181 | |
182 try { | |
183 myData.init(theConfig); | |
184 } catch (ConfigurationException aCE) { | |
185 BlitzServiceImpl.theLogger.log(Level.SEVERE, | |
186 "Exceptioned init'ing state", | |
187 aCE); | |
188 throw new IOException("Failed to init state"); | |
189 } catch (Throwable aT) { | |
190 BlitzServiceImpl.theLogger.log(Level.SEVERE, | |
191 "Exceptioned init'ing state", | |
192 aT); | |
193 throw new IOException("Failed to init state"); | |
194 } | |
195 | |
196 myData.dump(); | |
197 | |
198 anAccessor.save(JOIN_STATE_KEY, myData); | |
199 } | |
200 } | |
201 } |