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