use of com.helger.settings.ISettings in project phoss-smp by phax.
the class SMPSettingsManagerXML method onRead.
@Override
@Nonnull
protected EChange onRead(@Nonnull final IMicroDocument aDoc) {
final SettingsMicroDocumentConverter<Settings> aConverter = new SettingsMicroDocumentConverter<>(ISettingsFactory.newInstance());
final ISettings aSettings = aConverter.convertToNative(aDoc.getDocumentElement());
m_aSettings.initFromSettings(aSettings);
return EChange.UNCHANGED;
}
use of com.helger.settings.ISettings in project ph-commons by phax.
the class ConfigFileBuilder method build.
@Nonnull
public ConfigFile build() {
if (m_aPaths.isEmpty())
throw new IllegalStateException("No config file path was provided!");
IReadableResource aRes = null;
ISettings aSettings = null;
for (final String sConfigPath : m_aPaths) {
// Support reading?
if (m_aResProvider.supportsReading(sConfigPath)) {
// Convert to resource
aRes = m_aResProvider.getReadableResource(sConfigPath);
if (aRes != null) {
// Open stream
final InputStream aIS = aRes.getInputStream();
if (aIS != null) {
// Read settings
aSettings = m_aSPP.readSettings(aIS);
if (aSettings != null)
break;
}
}
}
}
if (aSettings == null)
if (LOGGER.isWarnEnabled())
LOGGER.warn("Failed to resolve config file paths: " + m_aPaths);
return new ConfigFile(aSettings != null ? aRes : null, aSettings);
}
use of com.helger.settings.ISettings in project ph-commons by phax.
the class SettingsPersistenceProperties method writeSettings.
@Nonnull
public ESuccess writeSettings(@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS) {
ValueEnforcer.notNull(aOS, "OutputStream");
try {
final NonBlockingProperties aProps = new NonBlockingProperties();
// Must not be sorted, as Properties sorts them as it wishes...
for (final Map.Entry<String, Object> aEntry : aSettings.entrySet()) {
final String sName = aEntry.getKey();
final Object aValue = aEntry.getValue();
if (aValue instanceof ISettings)
throw new IllegalArgumentException("When saving settings to a Properties object, it may not contained nested settings! Now the key '" + sName + "' is mapped to a nested ISettings object!");
final String sValue = TypeConverter.convert(aValue, String.class);
aProps.put(sName, sValue);
}
// Does not close the output stream!
aProps.store(aOS, aSettings.getName());
return ESuccess.SUCCESS;
} catch (final IOException ex) {
LOGGER.error("Failed to write settings to properties file", ex);
return ESuccess.FAILURE;
} finally {
StreamHelper.close(aOS);
}
}
use of com.helger.settings.ISettings in project ph-commons by phax.
the class SettingsPersistenceJsonTest method testViceVersaConversion.
@Test
public void testViceVersaConversion() throws UnsupportedEncodingException {
// Name is important!
final ISettings aSrc = new Settings("anonymous");
aSrc.putIn("field1a", BigInteger.valueOf(1234));
aSrc.putIn("field1b", BigInteger.valueOf(-23423424));
aSrc.putIn("field2a", BigDecimal.valueOf(12.34));
aSrc.putIn("field2b", BigDecimal.valueOf(-2342.334599424));
aSrc.putIn("field3a", "My wonderbra string\n(incl newline)");
aSrc.putIn("field3b", "");
aSrc.putIn("field9a", Boolean.TRUE);
aSrc.putIn("field9b", StringParser.parseByteObj("5"));
aSrc.putIn("field9c", Character.valueOf('ä'));
aSrc.putIn("fieldxa", PDTFactory.getCurrentLocalDate());
aSrc.putIn("fieldxb", PDTFactory.getCurrentLocalTime());
aSrc.putIn("fieldxc", PDTFactory.getCurrentLocalDateTime());
aSrc.putIn("fieldxd", PDTFactory.getCurrentZonedDateTime());
aSrc.putIn("fieldxe", Duration.ofHours(5));
aSrc.putIn("fieldxf", Period.ofDays(3));
aSrc.putIn("fieldxg", "Any byte ärräy".getBytes(StandardCharsets.UTF_8.name()));
// null value
aSrc.putIn("fieldnull", null);
final SettingsPersistenceJson aSPP = new SettingsPersistenceJson();
final String sSrc = aSPP.writeSettings(aSrc);
assertNotNull(sSrc);
// The created object is different, because now all values are String typed!
final ISettings aDst1 = aSPP.readSettings(sSrc);
assertNotNull(aDst1);
// Reading the String typed version again should result in the same object
final ISettings aDst2 = aSPP.readSettings(aSPP.writeSettings(aDst1));
assertNotNull(aDst2);
if (false) {
// Differs because of different types
assertEquals(aDst1, aDst2);
}
assertNotNull(aDst2.getAsLocalDate("fieldxa"));
assertNotNull(aDst2.getAsLocalTime("fieldxb"));
assertNotNull(aDst2.getAsLocalDateTime("fieldxc"));
assertNotNull(aDst2.getConvertedValue("fieldxd", ZonedDateTime.class));
assertNotNull(aDst2.getConvertedValue("fieldxe", Duration.class));
assertNotNull(aDst2.getConvertedValue("fieldxf", Period.class));
}
use of com.helger.settings.ISettings in project ph-commons by phax.
the class SettingsPersistenceJson method readSettings.
@Nonnull
public ISettings readSettings(@Nonnull @WillClose final InputStream aIS) {
ValueEnforcer.notNull(aIS, "InputStream");
// Create the settings object
final ISettings aSettings = m_aSettingsFactory.apply(getReadSettingsName());
// Read the properties file from the input stream
final IJsonObject aProps = JsonReader.builder().source(aIS, m_aCharset).customizeCallback(aParser -> {
aParser.setRequireStringQuotes(false);
aParser.setAlwaysUseBigNumber(true);
}).readAsObject();
if (aProps != null)
for (final Map.Entry<String, IJson> aEntry : aProps) _recursiveReadSettings(aEntry.getKey(), aEntry.getValue(), aSettings);
return aSettings;
}
Aggregations