/** * Creates a deep copy of this object that includes all of the properties except for the parent configuration. If * the <code>keepId</code> flag is <code>false</code>, then the id property is not copied. * * @param keepId A flag indicating whether or not the id should be copied * @return A copy of this object */ public RawConfiguration deepCopy(boolean keepId) { RawConfiguration copy = new RawConfiguration(); if (keepId) { copy.id = this.id; } copy.path = this.path; if (this.contents != null) { copy.setContents(this.getContents(), this.getSha256()); } copy.errorMessage = this.errorMessage; return copy; }
public void save(RawConfiguration config) throws Exception { File f = new File(config.getPath()); FileUtils.writeStringToFile(f, config.getContents()); }
public RawConfigurationBuilder withPath(String path) { rawConfig.setPath(path); return this; }
public RawConfigurationBuilder() { rawConfig = new RawConfiguration(); rawConfig.setConfiguration(getMap()); }
public void mergeRawConfig(Configuration from, RawConfiguration existingConfig, RawConfiguration toUpdate) throws Exception { Augeas aug = null; try { String lens = configMap.get(existingConfig.getPath()); aug = createAugeas(lens, existingConfig.getContents()); String file = getFile(aug); for (String pathSuffix : nodePaths.get(existingConfig.getPath())) { String propName = ("/files" + existingConfig.getPath() + "/" + pathSuffix).substring(rootNodePath .length()); String propValue = translator.getPropertyValue(propName, from); aug.set("/files" + file + "/" + pathSuffix, propValue); } aug.save(); toUpdate.setPath(existingConfig.getPath()); String contents = FileUtils.readFileToString(new File(file)); String sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(contents); toUpdate.setContents(contents, sha256); } finally { if (aug != null) { aug.close(); } } }
private void mergedStructuredIntoRaws(Configuration configuration, ResourceConfigurationFacet facet) { Set<RawConfiguration> rawConfigs = facet.loadRawConfigurations(); if (rawConfigs == null) { return; } prepareConfigForMergeIntoRaws(configuration, rawConfigs); Queue<RawConfiguration> queue = new LinkedList<RawConfiguration>(rawConfigs); while (!queue.isEmpty()) { RawConfiguration originalRaw = queue.poll(); RawConfiguration mergedRaw = facet.mergeRawConfiguration(configuration, originalRaw); if (mergedRaw != null) { //TODO bypass validation of structured config for template values String contents = mergedRaw.getContents(); String sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(contents); mergedRaw.setContents(contents, sha256); updateRawConfig(configuration, originalRaw, mergedRaw); } } }
/** * Will return the persisted Resource Configuration update, or null if the specified Configuration * is identical to the currently persisted Configuration. */ private ResourceConfigurationUpdate persistNewAgentReportedResourceConfiguration(Resource resource, Configuration liveConfig) throws ConfigurationUpdateStillInProgressException { if (liveConfig.getRawConfigurations() != null) { for (RawConfiguration raw : liveConfig.getRawConfigurations()) { MessageDigestGenerator sha256Generator = new MessageDigestGenerator(MessageDigestGenerator.SHA_256); sha256Generator.add(raw.getContents().getBytes()); raw.setSha256(sha256Generator.getDigestString()); } } /* * NOTE: We pass the overlord, since this is a system side-effect. here, the system * and *not* the user, is choosing to persist the most recent configuration because it was different * from the last known value. again, the user isn't attempting to change the value; instead, *JON* * is triggering save based on the semantics that we want to provide for configuration updates. * For the same reason, we pass null as the subject. */ ResourceConfigurationUpdate update = this.configurationManager .persistResourceConfigurationUpdateInNewTransaction(this.subjectManager.getOverlord(), resource.getId(), liveConfig, ConfigurationUpdateStatus.SUCCESS, null, false); // resource.setResourceConfiguration(liveConfig.deepCopy(false)); resource.setResourceConfiguration(liveConfig.deepCopyWithoutProxies()); return update; }
public RawConfigurationBuilder withContents(String content, String sha256) { rawConfig.setContents(content, sha256); return this; }
/** * Replaces the contents of the raw configuration. The SHA-256 hash returned from {@link #getSha256()} will be * updated as well. * * @param contents The new contents * @param sha256 the digest string representing the sha256 hash of the given contents */ public void setContents(String contents, String sha256) { this.contents = contents; this.errorMessage = null; setSha256(sha256); }
private void createDeepCopyOfRawConfigs(Configuration copy, boolean keepId) { if (rawConfigurations==null) { return; } for (RawConfiguration rawConfig : rawConfigurations) { copy.addRawConfiguration(rawConfig.deepCopy(keepId)); } }
private void mergeRawsIntoStructured(Configuration configuration, ResourceConfigurationFacet facet) { Configuration structuredConfig = facet.loadStructuredConfiguration(); if (structuredConfig != null) { prepareConfigForMergeIntoStructured(configuration, structuredConfig); for (RawConfiguration rawConfig : configuration.getRawConfigurations()) { String contents = rawConfig.getContents(); String sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(contents); rawConfig.setContents(contents, sha256); structuredConfig.addRawConfiguration(rawConfig); facet.mergeStructuredConfiguration(rawConfig, configuration); } } }
public void mergeStructured(RawConfiguration from, Configuration toUpdate) throws Exception { Augeas aug = null; try { String lens = configMap.get(from.getPath()); aug = createAugeas(lens, from.getContents()); String file = getFile(aug); for (String pathSuffix : nodePaths.get(from.getPath())) { String propName = ("/files" + from.getPath() + "/" + pathSuffix).substring(rootNodePath.length()); String augeasPath = "/files" + file + "/" + pathSuffix; Property property = translator.createProperty(propName, augeasPath, aug); toUpdate.put(property); } } finally { if (aug != null) { aug.close(); } } }