comparison src/org/dancres/blitz/remote/LookupData.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.Serializable;
4 import java.io.IOException;
5
6 import java.util.ArrayList;
7
8 import java.util.logging.Level;
9 import java.util.logging.Logger;
10
11 import net.jini.core.entry.Entry;
12
13 import net.jini.core.discovery.LookupLocator;
14
15 import net.jini.id.Uuid;
16 import net.jini.id.UuidFactory;
17
18 import net.jini.core.lookup.ServiceID;
19
20 import net.jini.config.ConfigurationException;
21 import net.jini.config.Configuration;
22 import net.jini.config.NoSuchEntryException;
23
24 import net.jini.lookup.JoinManager;
25
26 import net.jini.discovery.DiscoveryManagement;
27 import net.jini.discovery.DiscoveryLocatorManagement;
28 import net.jini.discovery.DiscoveryGroupManagement;
29 import net.jini.discovery.LookupDiscoveryManager;
30
31 import java.rmi.MarshalledObject;
32
33 import org.dancres.blitz.config.ConfigurationFactory;
34
35 import org.dancres.blitz.Logging;
36
37 /**
38 All data related to join management is held in this class.
39 Objects of this class are persisted to disk whenever their state
40 is updated and used to restore join management information next time
41 the service is run. LookupStorage delegates most of it's method calls
42 to this class.
43 */
44 class LookupData implements Serializable {
45 private static Logger theLogger =
46 Logging.newLogger("org.dancres.blitz.remote.LookupData");
47
48 static final long serialVersionUID = -6636356018279188568L;
49
50 private String[] theGroups;
51 private MarshalledObject theAttributes;
52 private LookupLocator[] theLocators;
53
54 private Uuid theUuid;
55
56 /*
57 No need to save this to disk it's derived from theUuid
58 */
59 private transient ServiceID theServiceId;
60
61 private transient Entry[] theStdAttrs;
62
63 LookupData() {
64 }
65
66 /**
67 Invoked the first time the service is exported. The initial
68 state is recovered from the Configuration file and then persisted
69 to disk. After that, the config file is only used to locate the
70 saved state which is then used in preference to the config file
71 for all join management related data.
72 */
73 void init(Configuration aConfig) throws ConfigurationException {
74 initAttrs(aConfig);
75 initGroups(aConfig);
76 initLocators(aConfig);
77 initUuid(aConfig);
78 }
79
80 void initAttrs(Configuration aConfig) throws ConfigurationException {
81 Entry[] myUserAttrs = new Entry[0];
82
83 try {
84 myUserAttrs =
85 (Entry[])
86 aConfig.getEntry(ConfigurationFactory.BLITZ_MODULE,
87 "initialAttrs",
88 Entry[].class);
89 } catch (NoSuchEntryException aNSEE) {
90 } catch (Throwable aT) {
91 throw new ConfigurationException("Failed to load initAttrs",
92 aT);
93 }
94
95 try {
96 theAttributes = new MarshalledObject(myUserAttrs);
97 } catch (IOException anIOE) {
98 throw new ConfigurationException("Failed to store attributes",
99 anIOE);
100 }
101 }
102
103 void initGroups(Configuration aConfig) throws ConfigurationException {
104 theGroups = (String[])
105 aConfig.getEntry(ConfigurationFactory.BLITZ_MODULE,
106 "initialGroups",
107 String[].class,
108 null);
109 }
110
111 void initLocators(Configuration aConfig) throws ConfigurationException {
112 theLocators =
113 (LookupLocator[])
114 aConfig.getEntry(ConfigurationFactory.BLITZ_MODULE,
115 "initialLocators",
116 LookupLocator[].class,
117 new LookupLocator[0]);
118 }
119
120 void initUuid(Configuration aConfig) throws ConfigurationException {
121 theUuid = UuidFactory.generate();
122 }
123
124 /**
125 Update our state from the JoinManager instance passed in.
126 */
127 void update(JoinManager aMgr) throws IOException {
128 theGroups = getDGM(aMgr).getGroups();
129 theLocators = getDLM(aMgr).getLocators();
130 theAttributes = new MarshalledObject(aMgr.getAttributes());
131 }
132
133 private boolean ignore(Entry anAttr) {
134 for (int i = 0; i < theStdAttrs.length; i++) {
135 if (anAttr.getClass().equals(theStdAttrs[i].getClass())) {
136 return true;
137 }
138 }
139
140 return false;
141 }
142
143 private DiscoveryGroupManagement getDGM(JoinManager aMgr) {
144 return (DiscoveryGroupManagement) aMgr.getDiscoveryManager();
145 }
146
147 private DiscoveryLocatorManagement getDLM(JoinManager aMgr) {
148 return (DiscoveryLocatorManagement) aMgr.getDiscoveryManager();
149 }
150
151 void dump() {
152 theLogger.log(Level.INFO, "ServiceID: " + getServiceID());
153 theLogger.log(Level.INFO, "Groups: ");
154 if (theGroups == null)
155 theLogger.log(Level.INFO, "All groups");
156 else {
157 for (int i = 0; i < theGroups.length; i++) {
158 theLogger.log(Level.INFO, theGroups[i]);
159 }
160 }
161
162 theLogger.log(Level.INFO, "Locators: ");
163 if ((theLocators == null) || (theLocators.length == 0))
164 theLogger.log(Level.INFO, "None");
165 else {
166 for (int i = 0; i < theLocators.length; i++) {
167 theLogger.log(Level.INFO, theLocators[i].toString());
168 }
169 }
170 }
171
172 Entry[] getAttributes() throws IOException {
173 try {
174 if (theStdAttrs == null) {
175 String myName = null;
176
177 try {
178 myName =
179 (String)
180 ConfigurationFactory.getEntry("name", String.class);
181 } catch (NoSuchEntryException aNSEE) {
182 // Doesn't matter
183 }
184
185 theStdAttrs = BlitzProxy.getDefaultAttrs(myName);
186 }
187
188 Entry[] myAttrs = (Entry[]) theAttributes.get();
189
190 ArrayList mySaveable = new ArrayList();
191
192 for (int i = 0; i < myAttrs.length; i++) {
193 Entry myAttr = myAttrs[i];
194
195 if (! ignore(myAttr))
196 mySaveable.add(myAttr);
197 }
198
199 myAttrs = new Entry[mySaveable.size()];
200
201 myAttrs = (Entry[]) mySaveable.toArray(myAttrs);
202
203
204 Entry[] myMergedAttrs = new Entry[myAttrs.length +
205 theStdAttrs.length];
206
207 System.arraycopy(theStdAttrs, 0, myMergedAttrs, 0,
208 theStdAttrs.length);
209 System.arraycopy(myAttrs, 0, myMergedAttrs,
210 theStdAttrs.length, myAttrs.length);
211
212 myAttrs = myMergedAttrs;
213
214 return myAttrs;
215 } catch (Exception anE) {
216 IOException myIOE =
217 new IOException("Failed to restore attributes");
218 myIOE.initCause(anE);
219 throw myIOE;
220 }
221 }
222
223 DiscoveryManagement getLookupDiscovery()
224 throws ConfigurationException {
225 try {
226 return new LookupDiscoveryManager(theGroups, theLocators,
227 null);
228 } catch (IOException anIOE) {
229 BlitzServiceImpl.theLogger.log(Level.SEVERE,
230 "Got IO problems from LDM",
231 anIOE);
232 throw new ConfigurationException("IO exception in LDM", anIOE);
233 }
234 }
235
236 Uuid getUuid() {
237 return theUuid;
238 }
239
240 synchronized ServiceID getServiceID() {
241 if (theServiceId == null) {
242 theServiceId =
243 new ServiceID(theUuid.getMostSignificantBits(),
244 theUuid.getLeastSignificantBits());
245 }
246
247 return theServiceId;
248 }
249 }