void put(final URI uri, ArtifactData data) throws Exception { reporter.trace("put %s %s", uri, data); File tmp = createTempFile(repoDir, "mtp", ".whatever"); tmp.deleteOnExit(); try { copy(uri.toURL(), tmp); byte[] sha = SHA1.digest(tmp).digest(); reporter.trace("SHA %s %s", uri, Hex.toHexString(sha)); ArtifactData existing = get(sha); if (existing != null) { reporter.trace("existing"); xcopy(existing, data); return; } File meta = new File(repoDir, Hex.toHexString(sha) + ".json"); File file = new File(repoDir, Hex.toHexString(sha)); rename(tmp, file); reporter.trace("file %s", file); data.file = file.getAbsolutePath(); data.sha = sha; data.busy = false; CommandData cmddata = parseCommandData(data); if (cmddata.bsn != null) { data.name = cmddata.bsn + "-" + cmddata.version; } else data.name = Strings.display(cmddata.title, cmddata.bsn, cmddata.name, uri); codec.enc().to(meta).put(data); reporter.trace("TD = " + data); } finally { tmp.delete(); reporter.trace("puted %s %s", uri, data); } }
public Object get() throws Exception { try { return codec.decode(null, this); } finally { if (!keepOpen) close(); } }
@SuppressWarnings("unchecked") public <T> T get(Class<T> clazz) throws Exception { try { return (T) codec.decode(clazz, this); } finally { if (!keepOpen) close(); } }
@SuppressWarnings("unchecked") public <T> T get(TypeReference<T> ref) throws Exception { try { return (T) codec.decode(ref.getType(), this); } finally { if (!keepOpen) close(); } }
/** * @param clazz * @param dataFile * @return * @throws Exception */ private <T> T getData(Class<T> clazz, File dataFile) throws Exception { try { return codec.dec().from(dataFile).get(clazz); } catch (Exception e) { // e.printStackTrace(); // System.out.println("Cannot read data file "+dataFile+": " + // IO.collect(dataFile)); return null; } }
public static void toJSON(ConfigurationAdmin admin, Writer osw, String filter) throws Exception { Configuration[] list = admin.listConfigurations(filter); Encoder encoder = codec.enc().to(osw); Protocol p = new Protocol(); p.version = 1; p.date = new Date(); p.size = list.length; encoder.put(p).append('\n'); if (list != null) for (Configuration c : list) { Dictionary<String, Object> d = c.getProperties(); Export export = new Export(); export.values = new HashMap<String, Object>(); export.factoryPid = c.getFactoryPid(); export.pid = c.getPid(); for (Enumeration<String> e = d.keys(); e.hasMoreElements(); ) { String k = e.nextElement(); Object v = d.get(k); if (!(v instanceof String)) { if (export.types == null) export.types = new HashMap<String, Type>(); Type type = new Type(); Class<?> clazz = v.getClass(); if (v instanceof Collection) { Collection<?> coll = (Collection<?>) v; clazz = String.class; if (coll.size() > 0) type.vectorOf = shortName(coll.iterator().next().getClass()); else type.vectorOf = shortName(String.class); } else if (v.getClass().isArray()) { type.arrayOf = shortName(clazz.getComponentType()); } else type.scalar = shortName(v.getClass()); export.types.put(k, type); } export.values.put(k, v); } encoder.mark().put(export); // encoder.put(encoder.digest()); encoder.append('\n'); } osw.flush(); }
public static void fromJSON(Reader r, ConfigurationAdmin admin) throws Exception { Decoder decoder = codec.dec().from(r); Protocol p = decoder.get(Protocol.class); for (int i = 0; i < p.size; i++) { Export e = decoder.get(Export.class); Configuration c; if (e.factoryPid != null) { c = admin.createFactoryConfiguration(e.factoryPid, "?*"); } else { c = admin.getConfiguration(e.pid, "?*"); } Dictionary<String, Object> d = getDictionary(e.types, e.values); c.update(d); } }
public ArtifactData get(byte[] sha) throws Exception { String name = Hex.toHexString(sha); File data = IO.getFile(repoDir, name + ".json"); reporter.trace("artifact data file %s", data); if (data.isFile()) { // Bin + metadata ArtifactData artifact = codec.dec().from(data).get(ArtifactData.class); artifact.file = IO.getFile(repoDir, name).getAbsolutePath(); return artifact; } File bin = IO.getFile(repoDir, name); if (bin.exists()) { // Only bin ArtifactData artifact = new ArtifactData(); artifact.file = bin.getAbsolutePath(); artifact.sha = sha; return artifact; } return null; }
private void storeData(File dataFile, Object o) throws Exception { codec.enc().to(dataFile).put(o); }