source: java/ModDepotAccess/src/net/oni2/moddepot/DepotManager.java@ 864

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

AEI2.08, AEIUpdater:

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