@DwrPermission(admin = true) public List<StringStringPair> versionCheck() { if (UPGRADE_DOWNLOADER != null) return UPGRADE_DOWNLOADER.getModules(); try { // Create the request List<Module> modules = ModuleRegistry.getModules(); Module.sortByName(modules); Map<String, Object> json = new HashMap<String, Object>(); json.put("guid", Providers.get(ICoreLicense.class).getGuid()); json.put("description", SystemSettingsDao.getValue(SystemSettingsDao.INSTANCE_DESCRIPTION)); json.put("distributor", Common.envProps.getString("distributor")); json.put( "domain", ControllerUtils.getDomain(WebContextFactory.get().getHttpServletRequest())); Map<String, String> jsonModules = new HashMap<String, String>(); json.put("modules", jsonModules); jsonModules.put("core", Common.getVersion().getFullString()); for (Module module : modules) jsonModules.put(module.getName(), module.getVersion()); StringWriter stringWriter = new StringWriter(); new JsonWriter(Common.JSON_CONTEXT, stringWriter).writeObject(json); String requestData = stringWriter.toString(); // Send the request String baseUrl = Common.envProps.getString("store.url"); baseUrl += "/servlet/versionCheck"; HttpPost post = new HttpPost(baseUrl); post.setEntity(new StringEntity(requestData)); String responseData = HttpUtils4.getTextContent(Common.getHttpClient(), post); // Parse the response JsonTypeReader jsonReader = new JsonTypeReader(responseData); JsonObject root = jsonReader.read().toJsonObject(); List<StringStringPair> upgrades = new ArrayList<StringStringPair>(); for (Map.Entry<String, JsonValue> mod : root.entrySet()) { String name = mod.getKey(); String version = mod.getValue().toString(); upgrades.add(new StringStringPair(name, version)); } return upgrades; } catch (Exception e) { throw new ShouldNeverHappenException(e); } }
@Override public void run() { HttpClient httpClient = Common.getHttpClient(); for (StringStringPair mod : modules) { String name = mod.getKey(); String version = mod.getValue(); String filename = ModuleUtils.moduleFilename(name, version); String url = Common.envProps.getString("store.url") + "/" + ModuleUtils.downloadFilename(name, version); HttpGet get = new HttpGet(url); FileOutputStream out = null; try { String saveDir = Common.M2M2_HOME; if (!"core".equals(name)) saveDir += "/" + Constants.DIR_WEB + "/" + Constants.DIR_MODULES; out = new FileOutputStream(new File(saveDir, filename)); HttpUtils4.execute(httpClient, get, out); synchronized (results) { results.add(new StringStringPair(name, null)); } } catch (IOException e) { synchronized (results) { results.add(new StringStringPair(name, e.getMessage())); LOG.warn("Upgrade download error", e); } } finally { try { if (out != null) out.close(); } catch (IOException e) { // no op } } } finished = true; }