source: AE/installer2/src/net/oni2/aeinstaller/backend/depot/DepotManager.java@ 634

Last change on this file since 634 was 634, checked in by alloc, 12 years ago

AEI2 0.92b:\n- Using zipped Depot cache

File size: 12.3 KB
Line 
1package net.oni2.aeinstaller.backend.depot;
2
3import java.io.BufferedReader;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.UnsupportedEncodingException;
10import java.net.UnknownHostException;
11import java.util.Enumeration;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.Vector;
15import java.util.zip.ZipEntry;
16import java.util.zip.ZipFile;
17
18import net.oni2.aeinstaller.backend.Paths;
19import net.oni2.aeinstaller.backend.Settings;
20import net.oni2.aeinstaller.backend.Settings.Platform;
21import net.oni2.aeinstaller.backend.depot.model.File;
22import net.oni2.aeinstaller.backend.depot.model.Node;
23import net.oni2.aeinstaller.backend.depot.model.NodeField_Body;
24import net.oni2.aeinstaller.backend.depot.model.NodeField_Upload;
25import net.oni2.aeinstaller.backend.depot.model.NodeMod;
26import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
27import net.oni2.aeinstaller.backend.depot.model.TaxonomyVocabulary;
28import net.oni2.aeinstaller.backend.mods.ECompatiblePlatform;
29import net.oni2.aeinstaller.backend.network.FileDownloader;
30
31import org.apache.http.HttpResponse;
32import org.apache.http.client.methods.HttpGet;
33import org.apache.http.client.methods.HttpRequestBase;
34import org.apache.http.impl.client.DefaultHttpClient;
35import org.json.JSONArray;
36import org.json.JSONException;
37import org.json.JSONObject;
38
39import com.thoughtworks.xstream.XStream;
40import com.thoughtworks.xstream.io.xml.StaxDriver;
41
42/**
43 * @author Christian Illy
44 */
45public class DepotManager {
46 private static DepotManager instance = new DepotManager();
47
48 private HashMap<Integer, TaxonomyVocabulary> taxonomyVocabulary = new HashMap<Integer, TaxonomyVocabulary>();
49 private HashMap<Integer, TaxonomyTerm> taxonomyTerms = new HashMap<Integer, TaxonomyTerm>();
50
51 private HashMap<Integer, Node> nodes = new HashMap<Integer, Node>();
52 private HashMap<String, HashMap<Integer, Node>> nodesByType = new HashMap<String, HashMap<Integer, Node>>();
53
54 private HashMap<Integer, File> files = new HashMap<Integer, File>();
55
56 private int vocabId_type = -1;
57 private int vocabId_platform = -1;
58 private int vocabId_instmethod = -1;
59
60 /**
61 * @return Singleton instance
62 */
63 public static DepotManager getInstance() {
64 return instance;
65 }
66
67 /**
68 * Update local Depot information cache
69 *
70 * @param forceRefreshAll
71 * Force refreshing all data, even if it seems to be cached
72 */
73 public void updateInformation(boolean forceRefreshAll) {
74 taxonomyTerms.clear();
75 taxonomyVocabulary.clear();
76
77 nodes = new HashMap<Integer, Node>();
78 nodesByType = new HashMap<String, HashMap<Integer, Node>>();
79 files = new HashMap<Integer, File>();
80
81 try {
82 java.io.File zipName = new java.io.File(Paths.getDownloadPath(),
83 "jsoncache.zip");
84 FileDownloader fd = new FileDownloader(DepotConfig.getDepotUrl()
85 + "jsoncache/jsoncache.zip", zipName);
86 fd.start();
87 while (fd.getState() != net.oni2.aeinstaller.backend.network.FileDownloader.EState.FINISHED) {
88 Thread.sleep(50);
89 }
90
91 ZipFile zf = null;
92 try {
93 zf = new ZipFile(zipName);
94 for (Enumeration<? extends ZipEntry> e = zf.entries(); e
95 .hasMoreElements();) {
96 ZipEntry ze = e.nextElement();
97 if (!ze.isDirectory()) {
98 BufferedReader input = new BufferedReader(
99 new InputStreamReader(zf.getInputStream(ze)));
100 StringBuffer json = new StringBuffer();
101
102 char data[] = new char[1024];
103 int dataRead;
104 while ((dataRead = input.read(data, 0, 1024)) != -1) {
105 json.append(data, 0, dataRead);
106 }
107
108 if (ze.getName().toLowerCase().contains("vocabulary"))
109 initVocabulary(new JSONArray(json.toString()));
110 if (ze.getName().toLowerCase().contains("terms"))
111 initTerms(new JSONArray(json.toString()));
112 if (ze.getName().toLowerCase().contains("nodes"))
113 initNodes(new JSONArray(json.toString()));
114 if (ze.getName().toLowerCase().contains("files"))
115 initFiles(new JSONArray(json.toString()));
116 }
117 }
118 } finally {
119 if (zf != null)
120 zf.close();
121 zipName.delete();
122 }
123
124 vocabId_type = getVocabulary(
125 DepotConfig.getVocabularyName_ModType()).getVid();
126 vocabId_platform = getVocabulary(
127 DepotConfig.getVocabularyName_Platform()).getVid();
128 vocabId_instmethod = getVocabulary(
129 DepotConfig.getVocabularyName_InstallType()).getVid();
130 } catch (JSONException e) {
131 e.printStackTrace();
132 } catch (IOException e1) {
133 e1.printStackTrace();
134 } catch (InterruptedException e) {
135 e.printStackTrace();
136 }
137 }
138
139 private void initFiles(JSONArray ja) throws JSONException {
140 JSONObject jo;
141
142 for (int i = 0; i < ja.length(); i++) {
143 jo = ja.getJSONObject(i);
144
145 int fid = jo.getInt("fid");
146
147 File f = new File(jo);
148 files.put(fid, f);
149 }
150 }
151
152 private void initNodes(JSONArray ja) throws JSONException {
153 JSONObject jo;
154
155 for (int i = 0; i < ja.length(); i++) {
156 jo = ja.getJSONObject(i);
157
158 int nid = jo.getInt("nid");
159 String type = jo.getString("type");
160
161 Node n = null;
162 if (type.equalsIgnoreCase(DepotConfig.getNodeType_Mod()))
163 n = new NodeMod(jo);
164 else
165 n = new Node(jo);
166
167 nodes.put(nid, n);
168 if (!nodesByType.containsKey(type))
169 nodesByType.put(type, new HashMap<Integer, Node>());
170 nodesByType.get(type).put(nid, n);
171 }
172 }
173
174 private void initTerms(JSONArray ja) throws JSONException {
175 JSONObject jo;
176
177 for (int i = 0; i < ja.length(); i++) {
178 jo = ja.getJSONObject(i);
179 TaxonomyTerm tt = new TaxonomyTerm(jo);
180 taxonomyTerms.put(tt.getTid(), tt);
181 }
182 }
183
184 private void initVocabulary(JSONArray ja) throws JSONException {
185 JSONObject jo;
186
187 for (int i = 0; i < ja.length(); i++) {
188 jo = ja.getJSONObject(i);
189 TaxonomyVocabulary tv = new TaxonomyVocabulary(jo);
190 taxonomyVocabulary.put(tv.getVid(), tv);
191 }
192 }
193
194 /**
195 * @return Can we connect to the Depot?
196 */
197 public boolean checkConnection() {
198 HttpRequestBase httpQuery = null;
199
200 try {
201 DefaultHttpClient httpclient = new DefaultHttpClient();
202 httpQuery = new HttpGet(DepotConfig.getDepotUrl());
203
204 HttpResponse response = httpclient.execute(httpQuery);
205
206 int code = response.getStatusLine().getStatusCode();
207
208 return (code >= 200) && (code <= 299);
209 } catch (UnknownHostException e) {
210 } catch (UnsupportedEncodingException e) {
211 e.printStackTrace();
212 } catch (IOException e) {
213 e.printStackTrace();
214 } finally {
215 if (httpQuery != null)
216 httpQuery.releaseConnection();
217 }
218 return false;
219 }
220
221 /**
222 * @return All TaxVocabs
223 */
224 public Vector<TaxonomyVocabulary> getVocabulary() {
225 return new Vector<TaxonomyVocabulary>(taxonomyVocabulary.values());
226 }
227
228 /**
229 * @param id
230 * Get taxonomy vocabulary by given ID
231 * @return TaxVocab
232 */
233 public TaxonomyVocabulary getVocabulary(int id) {
234 return taxonomyVocabulary.get(id);
235 }
236
237 /**
238 * @param name
239 * Get taxonomy vocabulary by given name
240 * @return TaxVocab
241 */
242 public TaxonomyVocabulary getVocabulary(String name) {
243 for (TaxonomyVocabulary v : taxonomyVocabulary.values()) {
244 if (v.getName().equalsIgnoreCase(name))
245 return v;
246 }
247 return null;
248 }
249
250 /**
251 * @param vocabId
252 * Get all taxonomy terms of a given vocabulary
253 * @return TaxTerms
254 */
255 public Vector<TaxonomyTerm> getTaxonomyTermsByVocabulary(int vocabId) {
256 Vector<TaxonomyTerm> res = new Vector<TaxonomyTerm>();
257 for (TaxonomyTerm t : taxonomyTerms.values()) {
258 if (t.getVid() == vocabId)
259 res.add(t);
260 }
261 return res;
262 }
263
264 /**
265 * @param id
266 * Get taxonomy term by given ID
267 * @return TaxTerm
268 */
269 public TaxonomyTerm getTaxonomyTerm(int id) {
270 return taxonomyTerms.get(id);
271 }
272
273 /**
274 * @param name
275 * Get taxonomy term by given name
276 * @return TaxTerm
277 */
278 public TaxonomyTerm getTaxonomyTerm(String name) {
279 for (TaxonomyTerm t : taxonomyTerms.values()) {
280 if (t.getName().equalsIgnoreCase(name))
281 return t;
282 }
283 return null;
284 }
285
286 /**
287 * Get all nodes of given node type
288 *
289 * @param nodeType
290 * Node type
291 * @return Nodes of type nodeType
292 */
293 public Vector<Node> getNodesByType(String nodeType) {
294 return new Vector<Node>(nodesByType.get(nodeType).values());
295 }
296
297 /**
298 * Get a node by node id
299 *
300 * @param id
301 * Node id
302 * @return Node
303 */
304 public Node getNodeById(int id) {
305 return nodes.get(id);
306 }
307
308 /**
309 * Get a Mod-Node by a given package number
310 *
311 * @param packageNumber
312 * Package number to find
313 * @return The Mod-Node or null
314 */
315 public NodeMod getNodeByPackageNumber(int packageNumber) {
316 Vector<Node> files = getNodesByType(DepotConfig.getNodeType_Mod());
317 for (Node n : files) {
318 if (n instanceof NodeMod) {
319 NodeMod nm = (NodeMod) n;
320 if (nm.getPackageNumber() == packageNumber)
321 return nm;
322 }
323 }
324 return null;
325 }
326
327 /**
328 * @return Mod-Nodes
329 */
330 public Vector<NodeMod> getModPackageNodes() {
331 Vector<NodeMod> result = new Vector<NodeMod>();
332 String instMethName = DepotConfig.getTaxonomyName_InstallType_Package();
333
334 Vector<Node> files = getNodesByType(DepotConfig.getNodeType_Mod());
335 for (Node n : files) {
336 if (n instanceof NodeMod) {
337 NodeMod nm = (NodeMod) n;
338 if (nm.getInstallMethod().getName()
339 .equalsIgnoreCase(instMethName))
340 result.add(nm);
341 }
342 }
343 return result;
344 }
345
346 /**
347 * @param node
348 * Node to check validity on
349 * @param platform
350 * Platform to check against
351 * @return True if valid on platform
352 */
353 public boolean isModValidOnPlatform(NodeMod node, Settings.Platform platform) {
354 ECompatiblePlatform plat = node.getPlatform();
355 switch (plat) {
356 case BOTH:
357 return true;
358 case WIN:
359 return (platform == Platform.WIN)
360 || (platform == Platform.LINUX);
361 case MACOS:
362 return (platform == Platform.MACOS);
363 }
364 return false;
365 }
366
367 /**
368 * Checks if the given mod-node is of the given mod-type(s)
369 *
370 * @param node
371 * Node to check
372 * @param type
373 * Type(s) to check
374 * @param or
375 * If false check if all given types are included in node. If
376 * true checks if either of the given types is included.
377 * @return True if of given type(s)
378 */
379 public boolean isModOfType(NodeMod node, HashSet<Integer> type, boolean or) {
380 boolean matching = !or;
381 HashSet<TaxonomyTerm> terms = node.getTypes();
382
383 for (int t : type) {
384 if (or)
385 matching |= terms.contains(t);
386 else
387 matching &= terms.contains(t);
388 }
389 return matching;
390 }
391
392 /**
393 * @return VocabId of Platform vocabulary
394 */
395 public int getVocabIdPlatform() {
396 return vocabId_platform;
397 }
398
399 /**
400 * @return VocabId of Install method vocabulary
401 */
402 public int getVocabIdInstMethod() {
403 return vocabId_instmethod;
404 }
405
406 /**
407 * @return VocabId of Type vocabulary
408 */
409 public int getVocabIdType() {
410 return vocabId_type;
411 }
412
413 /**
414 * @param id
415 * ID of file to get
416 * @return the file
417 */
418 public File getFile(int id) {
419 return files.get(id);
420 }
421
422 /**
423 * Print stats about nodes and files
424 */
425 public void printStats() {
426 System.out.println("Nodes by type:");
427 for (String t : nodesByType.keySet()) {
428 System.out.println(" " + t + ": " + nodesByType.get(t).size());
429 }
430
431 System.out.println("Files: " + files.size());
432 }
433
434 private XStream getXStream() {
435 XStream xs = new XStream(new StaxDriver());
436 xs.alias("Depot", DepotManager.class);
437 xs.alias("File", net.oni2.aeinstaller.backend.depot.model.File.class);
438 xs.alias("Node", Node.class);
439 xs.alias("NodeField_Body", NodeField_Body.class);
440 xs.alias("NodeField_Upload", NodeField_Upload.class);
441 xs.alias("NodeMod", NodeMod.class);
442 xs.alias("TaxonomyTerm", TaxonomyTerm.class);
443 xs.alias("TaxonomyVocabulary", TaxonomyVocabulary.class);
444 return xs;
445 }
446
447 /**
448 * Save Depot cache instance to file
449 *
450 * @param f
451 * File to write to
452 */
453 public void saveToFile(java.io.File f) {
454 try {
455 FileOutputStream fos = new FileOutputStream(f);
456 XStream xs = getXStream();
457 xs.toXML(this, fos);
458 fos.close();
459 } catch (FileNotFoundException e) {
460 e.printStackTrace();
461 } catch (IOException e) {
462 e.printStackTrace();
463 }
464 }
465
466 /**
467 * Load Depot cache instance from file
468 *
469 * @param f
470 * File to read from
471 */
472 public void loadFromFile(java.io.File f) {
473 try {
474 FileInputStream fis = new FileInputStream(f);
475 XStream xs = getXStream();
476 Object obj = xs.fromXML(fis);
477 if (obj instanceof DepotManager)
478 instance = (DepotManager) obj;
479 fis.close();
480 } catch (FileNotFoundException e) {
481 } catch (IOException e) {
482 }
483 }
484}
Note: See TracBrowser for help on using the repository browser.