@Override public void execute() throws BuildException { if (index == null) { throw new BuildException("Required attributes: file"); } try { JsonObjectBuilder jsonRoot = Json.createObjectBuilder(); JsonArrayBuilder jsonKeyrings = Json.createArrayBuilder(); getKeyRings().forEach(jsonKeyrings::add); jsonRoot.add("keyrings", jsonKeyrings); JsonArrayBuilder jsonPackages = Json.createArrayBuilder(); getPackages() .forEach( (p) -> { JsonObjectBuilder jsonPackage = Json.createObjectBuilder(); p.forEach( (k, v) -> { if (v instanceof Boolean) { jsonPackage.add(k, (Boolean) v); // Boolean } else if (v instanceof Number) { jsonPackage.add(k, ((Number) v).longValue()); // Integer } else if (v instanceof String[]) { JsonArrayBuilder array = Json.createArrayBuilder(); // String Array for (String s : (String[]) v) { array.add(s); } jsonPackage.add(k, array); } else if (v == null) { jsonPackage.addNull(k); // null } else { jsonPackage.add(k, v.toString()); // String } }); jsonPackages.add(jsonPackage); }); jsonRoot.add("packages", jsonPackages); log("Write Package Source: " + index); StringWriter json = new StringWriter(); try (JsonWriter writer = Json.createWriterFactory(singletonMap(JsonGenerator.PRETTY_PRINTING, true)) .createWriter(json)) { writer.writeObject(jsonRoot.build()); } Files.write(index.toPath(), json.toString().trim().getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new BuildException(e); } }
public List<String> getKeyRings() throws IOException { List<String> keys = new ArrayList<String>(); for (Resource resource : keyrings) { log("Include keyring: " + resource.getName()); String key = FileUtils.readFully( new InputStreamReader(resource.getInputStream(), StandardCharsets.US_ASCII)); if (key != null) { keys.add(key); } } return keys; }
public List<Map<String, Object>> getPackages() throws IOException { List<Map<String, Object>> packages = new ArrayList<Map<String, Object>>(); for (SPK spk : spks) { log("Include SPK: " + spk.file.getName()); // make sure file is cached locally if (spk.url != null) { log("Using " + spk.url); if (!spk.file.exists()) { spk.file.getParentFile().mkdirs(); } if (spk.url == null) { spk.url = spk.url; } Get get = new Get(); get.bindToOwner(this); get.setQuiet(true); get.setUseTimestamp(true); get.setSrc(spk.url); get.setDest(spk.file); get.execute(); } else { log("Using " + spk.file); } // import SPK INFO Map<String, Object> info = new LinkedHashMap<String, Object>(); TarFileSet tar = new TarFileSet(); tar.setProject(getProject()); tar.setSrc(spk.file); tar.setIncludes(INFO); for (Resource resource : tar) { if (INFO.equals(resource.getName())) { String text = FileUtils.readFully( new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)); for (String line : text.split("\\R")) { String[] s = line.split("=", 2); if (s.length == 2) { if (s[1].startsWith("\"") && s[1].endsWith("\"")) { s[1] = s[1].substring(1, s[1].length() - 1); } importSpkInfo(info, s[0], s[1]); } } } } log(String.format("Imported %d fields from SPK: %s", info.size(), info.keySet())); // add thumbnails and snapshots if (spk.thumbnail.size() > 0) { info.put(THUMBNAIL, spk.thumbnail.toArray(new String[0])); } if (spk.snapshot.size() > 0) { info.put(SNAPSHOT, spk.snapshot.toArray(new String[0])); } // add user-defined fields info.putAll(spk.infoList); // automatically generate file size and checksum fields if (!info.containsKey(LINK)) { info.put(LINK, spk.url); } info.put(MD5, md5(spk.file)); info.put(SIZE, spk.file.length()); packages.add(info); } return packages; }