protected static void restoreState(Object stateObject) { State state = (State) stateObject; SingularPropertiesImpl impl = SingularPropertiesImpl.get(); impl.setSingularServerHome(state.systemBackup.get(SYSTEM_PROPERTY_SINGULAR_SERVER_HOME)); impl.propertyMap = state.propertiesBackup; }
@Override public boolean isTrue(String key) { return "true".equalsIgnoreCase(getInternal(key)); }
/** * Limpa as propriedades da memoria e força recarga a partir da memória e classPath. */ public synchronized void reload() { LOGGER.info("Carregando configurações do Singular"); PropertyMap newProperties = readClasspathDefaults(); newProperties = readPropertiesFilesOverrides(newProperties); propertyMap = newProperties.consolidateAndFrozen(); }
/** * Copia as propriedades do arquivo para as properties internas. As propriedades previamente existentes serão * sobrepostas. Esse método é utilizado para testes unitários com difererentes contextos. */ @VisibleForTesting public synchronized void reloadAndOverrideWith(URL propertiesURL) { reload(); PropertyMap p = new PropertyMap(getProperties()); p.readProperties(propertiesURL); propertyMap = p; }
/** * Prints the content of map of properties to the system output identifying the source of each property. */ @Override public void debugContent() { getProperties().debugContent(); }
static SingularProperties get() { return SingularPropertiesImpl.get(); }
private synchronized PropertyMap getProperties() { //Faz leitura lazy das propriedades, pois no construtor da enum, as variáveis estáticas não estão disponíveis if (propertyMap == null) { reload(); } return propertyMap; }
private PropertyMap readPropertiesFilesOverrides(PropertyMap newProperties) { File confDir = findConfDir(); if (confDir == null) { return newProperties; } PropertyMap props = new PropertyMap(newProperties); for (String name : PROPERTIES_FILES_NAME) { props.readProperties(new File(confDir, name)); } return props; }
/** * Prints the content of map of properties to the specific output identifying the source of each property. */ @Override public void debugContent(@Nonnull Appendable out) { getProperties().debugContent(out); }
public static Object saveState() { SingularPropertiesImpl impl = SingularPropertiesImpl.get(); State state = new State(); state.propertiesBackup = impl.propertyMap; state.systemBackup.put(SYSTEM_PROPERTY_SINGULAR_SERVER_HOME, System.getProperty(SYSTEM_PROPERTY_SINGULAR_SERVER_HOME)); impl.propertyMap = new PropertyMap(impl.getProperties().consolidateAndFrozen()); return state; } }
@Override public boolean isFalse(String key) { return "false".equalsIgnoreCase(getInternal(key)); }
@Nullable private String getInternal(@Nonnull String key) { //se contém a chave ainda que esta seja com valor nulo PropertyEntry entry = getProperties().getEntry(key); if (entry != null) { return entry.getValue(); } return StringUtils.trimToNull(System.getProperties().getProperty(key)); }
/** * Runs the code ensuring that the properties context (in {@link SingularProperties#get()}) will be restore to * the previous state before the execution, also provides access to current properties to be changed within the * code. * <p>In shot, saves the properties, run the code (that may change the properties) and restore the properties * context to the previous state.</p> */ public static <EX extends Exception> void runInSandbox( @Nonnull IConsumerEx<SingularPropertiesImpl, EX> callable) throws EX { Object state = saveState(); try { callable.accept(SingularPropertiesImpl.get()); } finally { restoreState(state); } }
@Override @Nonnull public Optional<String> getPropertyOpt(@Nonnull String key) { return Optional.ofNullable(getInternal(key)); }
@VisibleForTesting public void setProperty(@Nonnull String key, @Nonnull String value) { if (getProperties().isFrozen()) { propertyMap = new PropertyMap(getProperties()); } propertyMap.add(key, value); }
/** * Looks for the property with the giving key. * <p>Never return empty String (in this case they became null) and also trims the resulting String.</p> * * @throws SingularPropertyException If the search results in a null value. */ @Override @Nonnull public String getProperty(@Nonnull String key) { String value = getInternal(key); if (value == null) { throw new SingularPropertyException("The property '" + key + "' is not set or it's value is null."); } return value; }