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

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

AEI2 0.98:

  • Reduced number+size of HTTP queries
  • Removed "submitter" field
  • Added output of dependencies to be selected before continuing
File size: 9.9 KB
RevLine 
[591]1package net.oni2.aeinstaller.backend.depot;
2
[634]3import java.io.BufferedReader;
[591]4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
[634]8import java.io.InputStreamReader;
[621]9import java.io.UnsupportedEncodingException;
10import java.net.UnknownHostException;
[634]11import java.util.Enumeration;
[591]12import java.util.HashMap;
13import java.util.Vector;
[634]14import java.util.zip.ZipEntry;
15import java.util.zip.ZipFile;
[591]16
[634]17import net.oni2.aeinstaller.backend.Paths;
[591]18import net.oni2.aeinstaller.backend.Settings;
19import net.oni2.aeinstaller.backend.depot.model.File;
20import net.oni2.aeinstaller.backend.depot.model.Node;
21import net.oni2.aeinstaller.backend.depot.model.NodeField_Body;
22import net.oni2.aeinstaller.backend.depot.model.NodeField_Upload;
23import net.oni2.aeinstaller.backend.depot.model.NodeMod;
24import net.oni2.aeinstaller.backend.depot.model.TaxonomyTerm;
25import net.oni2.aeinstaller.backend.depot.model.TaxonomyVocabulary;
[634]26import net.oni2.aeinstaller.backend.network.FileDownloader;
[591]27
[621]28import org.apache.http.HttpResponse;
[641]29import org.apache.http.client.methods.HttpHead;
[621]30import org.apache.http.client.methods.HttpRequestBase;
31import org.apache.http.impl.client.DefaultHttpClient;
[591]32import org.json.JSONArray;
33import org.json.JSONException;
34import org.json.JSONObject;
35
36import com.thoughtworks.xstream.XStream;
37import com.thoughtworks.xstream.io.xml.StaxDriver;
38
39/**
40 * @author Christian Illy
41 */
42public class DepotManager {
[635]43 private static DepotManager instance = loadFromFile();
[591]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 */
[634]70 public void updateInformation(boolean forceRefreshAll) {
[591]71 try {
[634]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);
[633]79 }
[591]80
[634]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();
[591]91
[634]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 }
[591]97
[635]98 if (ze.getName().toLowerCase().contains("vocabulary")) {
[634]99 initVocabulary(new JSONArray(json.toString()));
[635]100 }
101 if (ze.getName().toLowerCase().contains("terms")) {
[634]102 initTerms(new JSONArray(json.toString()));
[635]103 }
104 if (ze.getName().toLowerCase().contains("nodes")) {
[634]105 initNodes(new JSONArray(json.toString()));
[635]106 }
107 if (ze.getName().toLowerCase().contains("files")) {
[634]108 initFiles(new JSONArray(json.toString()));
[635]109 }
[634]110 }
111 }
112 } finally {
113 if (zf != null)
114 zf.close();
115 zipName.delete();
[591]116 }
117
[592]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();
[635]124
125 saveToFile();
[591]126 } catch (JSONException e) {
127 e.printStackTrace();
[634]128 } catch (IOException e1) {
129 e1.printStackTrace();
130 } catch (InterruptedException e) {
[591]131 e.printStackTrace();
132 }
133 }
134
[634]135 private void initFiles(JSONArray ja) throws JSONException {
[635]136 files = new HashMap<Integer, File>();
[634]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 {
[635]148 nodes = new HashMap<Integer, Node>();
149 nodesByType = new HashMap<String, HashMap<Integer, Node>>();
[634]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 {
[635]171 taxonomyTerms.clear();
[634]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 {
[635]181 taxonomyVocabulary.clear();
[634]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
[591]190 /**
[621]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();
[641]198 httpQuery = new HttpHead(DepotConfig.getDepotUrl());
[621]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 }
[635]216
217 private TaxonomyVocabulary getVocabulary(String name) {
[591]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 */
[635]230 private Vector<TaxonomyTerm> getTaxonomyTermsByVocabulary(int vocabId) {
[591]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 /**
[635]240 * @return All defined types
241 */
242 public Vector<TaxonomyTerm> getTypes() {
243 return getTaxonomyTermsByVocabulary(getVocabIdType());
244 }
245
246 /**
[591]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() {
[592]270 Vector<NodeMod> result = new Vector<NodeMod>();
[600]271 String instMethName = DepotConfig.getTaxonomyName_InstallType_Package();
[591]272
[592]273 Vector<Node> files = getNodesByType(DepotConfig.getNodeType_Mod());
[591]274 for (Node n : files) {
275 if (n instanceof NodeMod) {
276 NodeMod nm = (NodeMod) n;
[600]277 if (nm.getInstallMethod().getName()
278 .equalsIgnoreCase(instMethName))
[591]279 result.add(nm);
280 }
281 }
282 return result;
283 }
284
285 /**
[600]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 /**
[591]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
[635]327 private static XStream getXStream() {
[591]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 */
[635]343 private void saveToFile() {
[591]344 try {
[635]345 FileOutputStream fos = new FileOutputStream(
346 Settings.getDepotCacheFilename());
[591]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
[635]357 private static DepotManager loadFromFile() {
[591]358 try {
[635]359 FileInputStream fis = new FileInputStream(
360 Settings.getDepotCacheFilename());
[591]361 XStream xs = getXStream();
362 Object obj = xs.fromXML(fis);
[635]363 fis.close();
[591]364 if (obj instanceof DepotManager)
[635]365 return (DepotManager) obj;
[591]366 } catch (FileNotFoundException e) {
367 } catch (IOException e) {
368 }
[635]369 return new DepotManager();
[591]370 }
371}
Note: See TracBrowser for help on using the repository browser.