| 1 | package net.oni2.moddepot;
|
|---|
| 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.net.HttpURLConnection;
|
|---|
| 10 | import java.net.URL;
|
|---|
| 11 | import java.util.Date;
|
|---|
| 12 | import java.util.Enumeration;
|
|---|
| 13 | import java.util.HashMap;
|
|---|
| 14 | import java.util.Vector;
|
|---|
| 15 | import java.util.zip.ZipEntry;
|
|---|
| 16 | import java.util.zip.ZipFile;
|
|---|
| 17 |
|
|---|
| 18 | import net.oni2.ProxySettings;
|
|---|
| 19 | import net.oni2.httpfiledownloader.FileDownloader;
|
|---|
| 20 | import net.oni2.moddepot.model.File;
|
|---|
| 21 | import net.oni2.moddepot.model.Node;
|
|---|
| 22 | import net.oni2.moddepot.model.NodeField_Body;
|
|---|
| 23 | import net.oni2.moddepot.model.NodeField_Upload;
|
|---|
| 24 | import net.oni2.moddepot.model.NodeMod;
|
|---|
| 25 | import net.oni2.moddepot.model.TaxonomyTerm;
|
|---|
| 26 | import net.oni2.moddepot.model.TaxonomyVocabulary;
|
|---|
| 27 |
|
|---|
| 28 | import org.json.JSONArray;
|
|---|
| 29 | import org.json.JSONException;
|
|---|
| 30 | import org.json.JSONObject;
|
|---|
| 31 |
|
|---|
| 32 | import com.thoughtworks.xstream.XStream;
|
|---|
| 33 | import com.thoughtworks.xstream.XStreamException;
|
|---|
| 34 | import com.thoughtworks.xstream.io.xml.StaxDriver;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * @author Christian Illy
|
|---|
| 38 | */
|
|---|
| 39 | public 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 {
|
|---|
| 214 | HttpURLConnection con = (HttpURLConnection) new URL(
|
|---|
| 215 | DepotConfig.depotUrl).openConnection(ProxySettings
|
|---|
| 216 | .getInstance().getProxy());
|
|---|
| 217 | con.setRequestMethod("HEAD");
|
|---|
| 218 | int code = con.getResponseCode();
|
|---|
| 219 | return (code >= 200) && (code <= 299);
|
|---|
| 220 | } catch (IOException e) {
|
|---|
| 221 | System.out.println();
|
|---|
| 222 | System.out.println("Checking connection failed:");
|
|---|
| 223 | e.printStackTrace();
|
|---|
| 224 | System.out.println();
|
|---|
| 225 | }
|
|---|
| 226 | return false;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | private TaxonomyVocabulary getVocabulary(String name) {
|
|---|
| 230 | for (TaxonomyVocabulary v : taxonomyVocabulary.values()) {
|
|---|
| 231 | if (v.getName().equalsIgnoreCase(name))
|
|---|
| 232 | return v;
|
|---|
| 233 | }
|
|---|
| 234 | return null;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /**
|
|---|
| 238 | * @param vocabId
|
|---|
| 239 | * Get all taxonomy terms of a given vocabulary
|
|---|
| 240 | * @return TaxTerms
|
|---|
| 241 | */
|
|---|
| 242 | private Vector<TaxonomyTerm> getTaxonomyTermsByVocabulary(int vocabId) {
|
|---|
| 243 | Vector<TaxonomyTerm> res = new Vector<TaxonomyTerm>();
|
|---|
| 244 | for (TaxonomyTerm t : taxonomyTerms.values()) {
|
|---|
| 245 | if (t.getVid() == vocabId)
|
|---|
| 246 | res.add(t);
|
|---|
| 247 | }
|
|---|
| 248 | return res;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /**
|
|---|
| 252 | * @return All defined types
|
|---|
| 253 | */
|
|---|
| 254 | public Vector<TaxonomyTerm> getTypes() {
|
|---|
| 255 | return getTaxonomyTermsByVocabulary(vocabId_type);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /**
|
|---|
| 259 | * @param id
|
|---|
| 260 | * Get taxonomy term by given ID
|
|---|
| 261 | * @return TaxTerm
|
|---|
| 262 | */
|
|---|
| 263 | public TaxonomyTerm getTaxonomyTerm(int id) {
|
|---|
| 264 | return taxonomyTerms.get(id);
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | * Get all nodes of given node type
|
|---|
| 269 | *
|
|---|
| 270 | * @param nodeType
|
|---|
| 271 | * Node type
|
|---|
| 272 | * @return Nodes of type nodeType
|
|---|
| 273 | */
|
|---|
| 274 | public Vector<Node> getNodesByType(String nodeType) {
|
|---|
| 275 | if (nodesByType.get(nodeType) == null)
|
|---|
| 276 | return new Vector<Node>();
|
|---|
| 277 | return new Vector<Node>(nodesByType.get(nodeType).values());
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /**
|
|---|
| 281 | * @return Mod-Nodes
|
|---|
| 282 | */
|
|---|
| 283 | public Vector<NodeMod> getModPackageNodes() {
|
|---|
| 284 | Vector<NodeMod> result = new Vector<NodeMod>();
|
|---|
| 285 | String instMethName = DepotConfig.taxName_InstallType_Package;
|
|---|
| 286 |
|
|---|
| 287 | Vector<Node> files = getNodesByType(DepotConfig.nodeType_Package);
|
|---|
| 288 | for (Node n : files) {
|
|---|
| 289 | if (n instanceof NodeMod) {
|
|---|
| 290 | NodeMod nm = (NodeMod) n;
|
|---|
| 291 | if (nm.getInstallMethod().equalsIgnoreCase(instMethName)) {
|
|---|
| 292 | if (nm.getPackageNumber() >= 0)
|
|---|
| 293 | result.add(nm);
|
|---|
| 294 | else
|
|---|
| 295 | System.err.println("Node " + nm.getNid()
|
|---|
| 296 | + " does not have a package number!!!");
|
|---|
| 297 | }
|
|---|
| 298 | }
|
|---|
| 299 | }
|
|---|
| 300 | return result;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /**
|
|---|
| 304 | * @param id
|
|---|
| 305 | * ID of file to get
|
|---|
| 306 | * @return the file
|
|---|
| 307 | */
|
|---|
| 308 | public File getFile(int id) {
|
|---|
| 309 | return files.get(id);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | private static XStream getXStream() {
|
|---|
| 313 | XStream xs = new XStream(new StaxDriver());
|
|---|
| 314 | xs.alias("Depot", DepotManager.class);
|
|---|
| 315 | xs.alias("File", net.oni2.moddepot.model.File.class);
|
|---|
| 316 | xs.alias("Node", Node.class);
|
|---|
| 317 | xs.alias("NodeField_Body", NodeField_Body.class);
|
|---|
| 318 | xs.alias("NodeField_Upload", NodeField_Upload.class);
|
|---|
| 319 | xs.alias("NodeMod", NodeMod.class);
|
|---|
| 320 | xs.alias("TaxonomyTerm", TaxonomyTerm.class);
|
|---|
| 321 | xs.alias("TaxonomyVocabulary", TaxonomyVocabulary.class);
|
|---|
| 322 | return xs;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /**
|
|---|
| 326 | * Save Depot cache instance to file
|
|---|
| 327 | *
|
|---|
| 328 | * @param cacheFile
|
|---|
| 329 | * File to save to
|
|---|
| 330 | */
|
|---|
| 331 | public void saveToCacheFile(java.io.File cacheFile) {
|
|---|
| 332 | try {
|
|---|
| 333 | FileOutputStream fos = new FileOutputStream(cacheFile);
|
|---|
| 334 | XStream xs = getXStream();
|
|---|
| 335 | xs.toXML(this, fos);
|
|---|
| 336 | fos.close();
|
|---|
| 337 | } catch (FileNotFoundException e) {
|
|---|
| 338 | e.printStackTrace();
|
|---|
| 339 | } catch (IOException e) {
|
|---|
| 340 | e.printStackTrace();
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | /**
|
|---|
| 345 | * Load cache from file
|
|---|
| 346 | *
|
|---|
| 347 | * @param cacheFile
|
|---|
| 348 | * File to load
|
|---|
| 349 | */
|
|---|
| 350 | public static void loadFromCacheFile(java.io.File cacheFile) {
|
|---|
| 351 | try {
|
|---|
| 352 | FileInputStream fis = new FileInputStream(cacheFile);
|
|---|
| 353 | XStream xs = getXStream();
|
|---|
| 354 | Object obj = xs.fromXML(fis);
|
|---|
| 355 | fis.close();
|
|---|
| 356 | if (obj instanceof DepotManager)
|
|---|
| 357 | instance = (DepotManager) obj;
|
|---|
| 358 | } catch (XStreamException e) {
|
|---|
| 359 | } catch (FileNotFoundException e) {
|
|---|
| 360 | } catch (IOException e) {
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 | }
|
|---|