@Override public void scrubRegistrationProperties() { if (!exist()) { return; } try { PropertiesConfiguration config = new PropertiesConfiguration(); config.setIOFactory(new FilteringOutputWriterFactory()); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); layout.setLineSeparator("\n"); layout.load(config, reader()); try (FileWriter out = new FileWriter(this.configFile)) { layout.save(config, out); } loadProperties(); } catch (ConfigurationException | IOException e) { LOG.warn("[Agent Auto Registration] Unable to scrub registration key.", e); } }
/** * Returns the header comment of the represented properties file in a * canonical form. With the {@code commentChar} parameter it can be * specified whether comment characters should be stripped or be always * present. * * @param commentChar determines the presence of comment characters * @return the header comment (can be <b>null</b>) */ public String getCanonicalHeaderComment(final boolean commentChar) { return constructCanonicalComment(getHeaderComment(), commentChar); }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.PropertiesConfigurationLayout; public class PropertiesReader { public static void main(String args[]) throws ConfigurationException, FileNotFoundException { File file = new File(args[0] + ".properties"); PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config); layout.load(new InputStreamReader(new FileInputStream(file))); config.setProperty("test", "testValue"); layout.save(propsFile.newWriter()); } }
private PropertiesConfiguration loadPropertiesFile(File file, PropertiesConfiguration config) { PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStreamReader is = new InputStreamReader(new FileInputStream(file))) { layout.load(config, is); } catch (ConfigurationException | FileNotFoundException e) { LOGGER.log(Level.WARNING, "Failed to load " + file.getPath(), e); } catch (IOException e) { e.printStackTrace(); } config.setLayout(layout); return config; } }
public void setValue(String property, String propertyValue) { PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStream input = new FileInputStream(props)) { layout.load(config, new InputStreamReader(input, UTF_8)); config.setProperty(property, propertyValue); layout.save(config, new FileWriter(props, false)); } catch (Exception e) { LOGGER.error(e.getMessage()); } } }
/** * Creates a standard layout object. This configuration is initialized with * such a standard layout. * * @return the newly created layout object */ private PropertiesConfigurationLayout createLayout() { return new PropertiesConfigurationLayout(); }
/** * Copies the data from the given layout object. * * @param c the layout object to copy */ private void copyFrom(final PropertiesConfigurationLayout c) { for (final String key : c.getKeys()) { final PropertyLayoutData data = c.layoutData.get(key); layoutData.put(key, data.clone()); } setHeaderComment(c.getHeaderComment()); setFooterComment(c.getFooterComment()); }
/** * {@inheritDoc} This implementation delegates to the associated layout * object which does the actual saving. Note that, analogous to * {@link #read(Reader)}, this method does not do any synchronization. * * @since 2.0 */ @Override public void write(final Writer out) throws ConfigurationException, IOException { getLayout().save(this, out); }
/** * {@inheritDoc} This implementation delegates to the associated layout * object which does the actual loading. Note that this method does not * do any synchronization. This lies in the responsibility of the caller. * (Typically, the caller is a {@code FileHandler} object which takes * care for proper synchronization.) * * @since 2.0 */ @Override public void read(final Reader in) throws ConfigurationException, IOException { getLayout().load(this, in); }
/** * Checks if parts of the passed in comment can be used as header comment. * This method checks whether a header comment can be defined (i.e. whether * this is the first comment in the loaded file). If this is the case, it is * searched for the latest blanc line. This line will mark the end of the * header comment. The return value is the index of the first line in the * passed in list, which does not belong to the header comment. * * @param commentLines the comment lines * @return the index of the next line after the header comment */ private int checkHeaderComment(final List<String> commentLines) { if (loadCounter.get() == 1 && layoutData.isEmpty()) { // This is the first comment. Search for blanc lines. int index = commentLines.size() - 1; while (index >= 0 && commentLines.get(index).length() > 0) { index--; } if (getHeaderComment() == null) { setHeaderComment(extractComment(commentLines, 0, index - 1)); } return index + 1; } return 0; }
.getPropertyName()); int blancLines = 0; int idx = checkHeaderComment(reader.getCommentLines()); while (idx < reader.getCommentLines().size() && reader.getCommentLines().get(idx).length() < 1) blancLines++; final String comment = extractComment(reader.getCommentLines(), idx, reader.getCommentLines().size() - 1); final PropertyLayoutData data = fetchLayoutData(reader .getPropertyName()); if (contained) setFooterComment(extractComment(reader.getCommentLines(), 0, reader .getCommentLines().size() - 1));
/** * Return the comment header. * * @return the comment header * @since 1.1 */ public String getHeader() { beginRead(false); try { return getLayout().getHeaderComment(); } finally { endRead(); } }
layoutData.containsKey(event.getPropertyName()); final PropertyLayoutData data = fetchLayoutData(event.getPropertyName()); data.setSingleLine(!contained); clear(); fetchLayoutData(event.getPropertyName());
public String getValue(String property) { String propertyValue = EMPTY; PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStream input = new FileInputStream(props)) { layout.load(config, new InputStreamReader(input, UTF_8)); propertyValue = config.getString(property); } catch (Exception e) { LOGGER.error(e.getMessage()); } return propertyValue; }
File file = new File("src/test/resources/1automation.properties"); PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config); layout.load(new InputStreamReader(new FileInputStream(file))); FileWriter fw = new FileWriter("src/test/resources/1automation.properties",false); config.setProperty("myssi.admin.name", "testValue"); layout.save(fw);
/** * Creates a copy of this object. * * @return the copy */ @Override public Object clone() { final PropertiesConfiguration copy = (PropertiesConfiguration) super.clone(); if (layout != null) { copy.setLayout(new PropertiesConfigurationLayout(layout)); } return copy; }
CombinedConfiguration props = new CombinedConfiguration(); final PropertiesConfiguration defaultsProps = new PropertiesConfiguration(new File("/tmp/default.properties")); final PropertiesConfiguration customProps = new PropertiesConfiguration(new File("/tmp/custom.properties")); props.setNodeCombiner(new OverrideCombiner()); props.addConfiguration(customProps); //first should be loaded the override values props.addConfiguration(defaultsProps); // last your 'default' values PropertiesConfiguration finalFile = new PropertiesConfiguration(); finalFile.append(props); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(finalFile, defaultsProps.getLayout()); //here we copy the layout from the 'base file' layout.save(new FileWriter(new File("/tmp/app.properties")));
FileBasedSecStoreConfig(File userFile, File roleFile) throws Exception { Configurations configs = new Configurations(); userBuilder = configs.propertiesBuilder(userFile); roleBuilder = configs.propertiesBuilder(roleFile); userConfig = userBuilder.getConfiguration(); roleConfig = roleBuilder.getConfiguration(); String roleHeader = roleConfig.getLayout().getHeaderComment(); String userHeader = userConfig.getLayout().getHeaderComment(); if (userHeader == null) { if (userConfig.isEmpty()) { //clean and reset header userConfig.clear(); userConfig.setHeader(LICENSE_HEADER); } } if (roleHeader == null) { if (roleConfig.isEmpty()) { //clean and reset header roleConfig.clear(); roleConfig.setHeader(LICENSE_HEADER); } } }
public Map<String, String> getKeysAndValues() { Map<String, String> kvp = newLinkedHashMap(); PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStream input = new FileInputStream(props)) { layout.load(config, new InputStreamReader(input, UTF_8)); config.getKeys().forEachRemaining(item -> kvp.put(item, config.getString(item))); } catch (Exception e) { LOGGER.error(e.getMessage()); } return kvp; }
public BinanceAutoBlacklist() { cache = new HashMap<>(); scheduler = Executors.newSingleThreadScheduledExecutor(); settings = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); settings.setLayout(layout); if (!settingsFile.exists()) { settings.setProperty("enabled", enabled = true); settings.setProperty("interval", interval = 30); settings.setProperty("market", market = "BTC"); settings.setProperty("days", days = 14); settings.setProperty("clear", clear = true); try (FileWriter fw = new FileWriter(settingsFile, false)) { settings.write(fw); } catch (ConfigurationException | IOException e) { LOGGER.log(Level.WARNING, "Failed to save " + settingsFile.getName(), e); } LOGGER.info("Created " + settingsFile.getPath()); currentInterval = interval; start(); } else { updateSettings(); } }