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

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

AEI2 0.99q:

  • Updates dialog: Updating the to-download-size on (un)checking updates to download
  • Updates dialog: Show download size of individual updates
  • Tool execution: Support for .NET tools (Mod_Info.cfg added ExeType flag)
  • Depot backend: Ignore mod nodes with an empty package number field
File size: 10.0 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.Date;
12import java.util.Enumeration;
13import java.util.HashMap;
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.depot.model.File;
21import net.oni2.aeinstaller.backend.depot.model.Node;
22import net.oni2.aeinstaller.backend.depot.model.NodeField_Body;
23import net.oni2.aeinstaller.backend.depot.model.NodeField_Upload;
24import net.oni2.aeinstaller.backend.depot.model.NodeMod;
25import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
26import net.oni2.aeinstaller.backend.depot.model.TaxonomyVocabulary;
27import net.oni2.aeinstaller.backend.network.FileDownloader;
28
29import org.apache.http.HttpResponse;
30import org.apache.http.client.methods.HttpHead;
31import org.apache.http.client.methods.HttpRequestBase;
32import org.apache.http.impl.client.DefaultHttpClient;
33import org.json.JSONArray;
34import org.json.JSONException;
35import org.json.JSONObject;
36
37import com.thoughtworks.xstream.XStream;
38import com.thoughtworks.xstream.io.xml.StaxDriver;
39
40/**
41 * @author Christian Illy
42 */
43public class DepotManager {
44 private static DepotManager instance = loadFromFile();
45
46 private HashMap<Integer, TaxonomyVocabulary> taxonomyVocabulary = new HashMap<Integer, TaxonomyVocabulary>();
47 private HashMap<Integer, TaxonomyTerm> taxonomyTerms = new HashMap<Integer, TaxonomyTerm>();
48
49 private HashMap<Integer, Node> nodes = new HashMap<Integer, Node>();
50 private HashMap<String, HashMap<Integer, Node>> nodesByType = new HashMap<String, HashMap<Integer, Node>>();
51
52 private HashMap<Integer, File> files = new HashMap<Integer, File>();
53
54 private int vocabId_type = -1;
55 private int vocabId_platform = -1;
56 private int vocabId_instmethod = -1;
57
58 /**
59 * @return Singleton instance
60 */
61 public static DepotManager getInstance() {
62 return instance;
63 }
64
65 /**
66 * Update local Depot information cache
67 */
68 public void updateInformation() {
69 try {
70 java.io.File zipName = new java.io.File(Paths.getDownloadPath(),
71 "jsoncache.zip");
72 FileDownloader fd = new FileDownloader(DepotConfig.getDepotUrl()
73 + "jsoncache/jsoncache.zip?ts="
74 + (new Date().getTime() / 1000), zipName);
75 fd.start();
76 while (fd.getState() != net.oni2.aeinstaller.backend.network.FileDownloader.EState.FINISHED) {
77 Thread.sleep(50);
78 }
79
80 ZipFile zf = null;
81 try {
82 zf = new ZipFile(zipName);
83 for (Enumeration<? extends ZipEntry> e = zf.entries(); e
84 .hasMoreElements();) {
85 ZipEntry ze = e.nextElement();
86 if (!ze.isDirectory()) {
87 BufferedReader input = new BufferedReader(
88 new InputStreamReader(zf.getInputStream(ze)));
89 StringBuffer json = new StringBuffer();
90
91 char data[] = new char[1024];
92 int dataRead;
93 while ((dataRead = input.read(data, 0, 1024)) != -1) {
94 json.append(data, 0, dataRead);
95 }
96
97 if (ze.getName().toLowerCase().contains("vocabulary")) {
98 initVocabulary(new JSONArray(json.toString()));
99 }
100 if (ze.getName().toLowerCase().contains("terms")) {
101 initTerms(new JSONArray(json.toString()));
102 }
103 if (ze.getName().toLowerCase().contains("nodes")) {
104 initNodes(new JSONArray(json.toString()));
105 }
106 if (ze.getName().toLowerCase().contains("files")) {
107 initFiles(new JSONArray(json.toString()));
108 }
109 }
110 }
111 } finally {
112 if (zf != null)
113 zf.close();
114 zipName.delete();
115 }
116
117 vocabId_type = getVocabulary(
118 DepotConfig.getVocabularyName_ModType()).getVid();
119 vocabId_platform = getVocabulary(
120 DepotConfig.getVocabularyName_Platform()).getVid();
121 vocabId_instmethod = getVocabulary(
122 DepotConfig.getVocabularyName_InstallType()).getVid();
123
124 saveToFile();
125 } catch (JSONException e) {
126 e.printStackTrace();
127 } catch (IOException e1) {
128 e1.printStackTrace();
129 } catch (InterruptedException e) {
130 e.printStackTrace();
131 }
132 }
133
134 private void initFiles(JSONArray ja) throws JSONException {
135 files = new HashMap<Integer, File>();
136 JSONObject jo;
137 for (int i = 0; i < ja.length(); i++) {
138 jo = ja.getJSONObject(i);
139 int fid = jo.getInt("fid");
140
141 File f = new File(jo);
142 files.put(fid, f);
143 }
144 }
145
146 private void initNodes(JSONArray ja) throws JSONException {
147 nodes = new HashMap<Integer, Node>();
148 nodesByType = new HashMap<String, HashMap<Integer, Node>>();
149 JSONObject jo;
150 for (int i = 0; i < ja.length(); i++) {
151 jo = ja.getJSONObject(i);
152
153 int nid = jo.getInt("nid");
154 String type = jo.getString("type");
155
156 Node n = null;
157 if (type.equalsIgnoreCase(DepotConfig.getNodeType_Mod()))
158 n = new NodeMod(jo);
159 else
160 n = new Node(jo);
161
162 nodes.put(nid, n);
163 if (!nodesByType.containsKey(type))
164 nodesByType.put(type, new HashMap<Integer, Node>());
165 nodesByType.get(type).put(nid, n);
166 }
167 }
168
169 private void initTerms(JSONArray ja) throws JSONException {
170 taxonomyTerms.clear();
171 JSONObject jo;
172 for (int i = 0; i < ja.length(); i++) {
173 jo = ja.getJSONObject(i);
174 TaxonomyTerm tt = new TaxonomyTerm(jo);
175 taxonomyTerms.put(tt.getTid(), tt);
176 }
177 }
178
179 private void initVocabulary(JSONArray ja) throws JSONException {
180 taxonomyVocabulary.clear();
181 JSONObject jo;
182 for (int i = 0; i < ja.length(); i++) {
183 jo = ja.getJSONObject(i);
184 TaxonomyVocabulary tv = new TaxonomyVocabulary(jo);
185 taxonomyVocabulary.put(tv.getVid(), tv);
186 }
187 }
188
189 /**
190 * @return Can we connect to the Depot?
191 */
192 public boolean checkConnection() {
193 HttpRequestBase httpQuery = null;
194
195 try {
196 DefaultHttpClient httpclient = new DefaultHttpClient();
197 httpQuery = new HttpHead(DepotConfig.getDepotUrl());
198
199 HttpResponse response = httpclient.execute(httpQuery);
200
201 int code = response.getStatusLine().getStatusCode();
202
203 return (code >= 200) && (code <= 299);
204 } catch (UnknownHostException e) {
205 } catch (UnsupportedEncodingException e) {
206 e.printStackTrace();
207 } catch (IOException e) {
208 e.printStackTrace();
209 } finally {
210 if (httpQuery != null)
211 httpQuery.releaseConnection();
212 }
213 return false;
214 }
215
216 private TaxonomyVocabulary getVocabulary(String name) {
217 for (TaxonomyVocabulary v : taxonomyVocabulary.values()) {
218 if (v.getName().equalsIgnoreCase(name))
219 return v;
220 }
221 return null;
222 }
223
224 /**
225 * @param vocabId
226 * Get all taxonomy terms of a given vocabulary
227 * @return TaxTerms
228 */
229 private Vector<TaxonomyTerm> getTaxonomyTermsByVocabulary(int vocabId) {
230 Vector<TaxonomyTerm> res = new Vector<TaxonomyTerm>();
231 for (TaxonomyTerm t : taxonomyTerms.values()) {
232 if (t.getVid() == vocabId)
233 res.add(t);
234 }
235 return res;
236 }
237
238 /**
239 * @return All defined types
240 */
241 public Vector<TaxonomyTerm> getTypes() {
242 return getTaxonomyTermsByVocabulary(getVocabIdType());
243 }
244
245 /**
246 * @param id
247 * Get taxonomy term by given ID
248 * @return TaxTerm
249 */
250 public TaxonomyTerm getTaxonomyTerm(int id) {
251 return taxonomyTerms.get(id);
252 }
253
254 /**
255 * Get all nodes of given node type
256 *
257 * @param nodeType
258 * Node type
259 * @return Nodes of type nodeType
260 */
261 public Vector<Node> getNodesByType(String nodeType) {
262 return new Vector<Node>(nodesByType.get(nodeType).values());
263 }
264
265 /**
266 * @return Mod-Nodes
267 */
268 public Vector<NodeMod> getModPackageNodes() {
269 Vector<NodeMod> result = new Vector<NodeMod>();
270 String instMethName = DepotConfig.getTaxonomyName_InstallType_Package();
271
272 Vector<Node> files = getNodesByType(DepotConfig.getNodeType_Mod());
273 for (Node n : files) {
274 if (n instanceof NodeMod) {
275 NodeMod nm = (NodeMod) n;
276 if (nm.getInstallMethod().getName()
277 .equalsIgnoreCase(instMethName)) {
278 try {
279 nm.getPackageNumber();
280 result.add(nm);
281 } catch (NumberFormatException e) {
282 System.err.println("Node " + nm.getNid() + " does not have a package number!!!");
283 }
284 }
285 }
286 }
287 return result;
288 }
289
290 /**
291 * @return VocabId of Platform vocabulary
292 */
293 public int getVocabIdPlatform() {
294 return vocabId_platform;
295 }
296
297 /**
298 * @return VocabId of Install method vocabulary
299 */
300 public int getVocabIdInstMethod() {
301 return vocabId_instmethod;
302 }
303
304 /**
305 * @return VocabId of Type vocabulary
306 */
307 public int getVocabIdType() {
308 return vocabId_type;
309 }
310
311 /**
312 * @param id
313 * ID of file to get
314 * @return the file
315 */
316 public File getFile(int id) {
317 return files.get(id);
318 }
319
320 /**
321 * Print stats about nodes and files
322 */
323 public void printStats() {
324 System.out.println("Nodes by type:");
325 for (String t : nodesByType.keySet()) {
326 System.out.println(" " + t + ": " + nodesByType.get(t).size());
327 }
328
329 System.out.println("Files: " + files.size());
330 }
331
332 private static XStream getXStream() {
333 XStream xs = new XStream(new StaxDriver());
334 xs.alias("Depot", DepotManager.class);
335 xs.alias("File", net.oni2.aeinstaller.backend.depot.model.File.class);
336 xs.alias("Node", Node.class);
337 xs.alias("NodeField_Body", NodeField_Body.class);
338 xs.alias("NodeField_Upload", NodeField_Upload.class);
339 xs.alias("NodeMod", NodeMod.class);
340 xs.alias("TaxonomyTerm", TaxonomyTerm.class);
341 xs.alias("TaxonomyVocabulary", TaxonomyVocabulary.class);
342 return xs;
343 }
344
345 /**
346 * Save Depot cache instance to file
347 */
348 private void saveToFile() {
349 try {
350 FileOutputStream fos = new FileOutputStream(
351 Settings.getDepotCacheFilename());
352 XStream xs = getXStream();
353 xs.toXML(this, fos);
354 fos.close();
355 } catch (FileNotFoundException e) {
356 e.printStackTrace();
357 } catch (IOException e) {
358 e.printStackTrace();
359 }
360 }
361
362 private static DepotManager loadFromFile() {
363 try {
364 FileInputStream fis = new FileInputStream(
365 Settings.getDepotCacheFilename());
366 XStream xs = getXStream();
367 Object obj = xs.fromXML(fis);
368 fis.close();
369 if (obj instanceof DepotManager)
370 return (DepotManager) obj;
371 } catch (FileNotFoundException e) {
372 } catch (IOException e) {
373 }
374 return new DepotManager();
375 }
376}
Note: See TracBrowser for help on using the repository browser.