Search in sources :

Example 1 with ISettings

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;
}
Also used : SettingsMicroDocumentConverter(com.helger.settings.exchange.xml.SettingsMicroDocumentConverter) ISettings(com.helger.settings.ISettings) ISettings(com.helger.settings.ISettings) Settings(com.helger.settings.Settings) Nonnull(javax.annotation.Nonnull)

Example 2 with ISettings

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);
}
Also used : InputStream(java.io.InputStream) IReadableResource(com.helger.commons.io.resource.IReadableResource) ISettings(com.helger.settings.ISettings) Nonnull(javax.annotation.Nonnull)

Example 3 with ISettings

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);
    }
}
Also used : NonBlockingProperties(com.helger.commons.lang.NonBlockingProperties) IOException(java.io.IOException) ISettings(com.helger.settings.ISettings) Map(java.util.Map) Nonnull(javax.annotation.Nonnull)

Example 4 with ISettings

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));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Period(java.time.Period) Duration(java.time.Duration) ISettings(com.helger.settings.ISettings) ISettings(com.helger.settings.ISettings) Settings(com.helger.settings.Settings) Test(org.junit.Test)

Example 5 with ISettings

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;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) StreamHelper(com.helger.commons.io.stream.StreamHelper) WillClose(javax.annotation.WillClose) ISettingsPersistence(com.helger.settings.exchange.ISettingsPersistence) LoggerFactory(org.slf4j.LoggerFactory) IJsonObject(com.helger.json.IJsonObject) CollectionHelper(com.helger.commons.collection.CollectionHelper) ISettings(com.helger.settings.ISettings) JsonWriterSettings(com.helger.json.serialize.JsonWriterSettings) Charset(java.nio.charset.Charset) Nonempty(com.helger.commons.annotation.Nonempty) Map(java.util.Map) JsonReader(com.helger.json.serialize.JsonReader) Nonnull(javax.annotation.Nonnull) ISettingsFactory(com.helger.settings.factory.ISettingsFactory) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) IOException(java.io.IOException) JsonObject(com.helger.json.JsonObject) JsonWriter(com.helger.json.serialize.JsonWriter) ValueEnforcer(com.helger.commons.ValueEnforcer) TypeConverter(com.helger.commons.typeconvert.TypeConverter) IJson(com.helger.json.IJson) Comparator(java.util.Comparator) InputStream(java.io.InputStream) IJsonObject(com.helger.json.IJsonObject) ISettings(com.helger.settings.ISettings) Nonnull(javax.annotation.Nonnull)

Aggregations

ISettings (com.helger.settings.ISettings)8 Nonnull (javax.annotation.Nonnull)5 Settings (com.helger.settings.Settings)4 Test (org.junit.Test)3 NonBlockingProperties (com.helger.commons.lang.NonBlockingProperties)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Duration (java.time.Duration)2 Period (java.time.Period)2 ZonedDateTime (java.time.ZonedDateTime)2 Map (java.util.Map)2 ValueEnforcer (com.helger.commons.ValueEnforcer)1 Nonempty (com.helger.commons.annotation.Nonempty)1 CollectionHelper (com.helger.commons.collection.CollectionHelper)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 StreamHelper (com.helger.commons.io.stream.StreamHelper)1 ESuccess (com.helger.commons.state.ESuccess)1 TypeConverter (com.helger.commons.typeconvert.TypeConverter)1 IJson (com.helger.json.IJson)1 IJsonObject (com.helger.json.IJsonObject)1