Example usage for org.apache.commons.configuration AbstractConfiguration getLong

List of usage examples for org.apache.commons.configuration AbstractConfiguration getLong

Introduction

In this page you can find the example usage for org.apache.commons.configuration AbstractConfiguration getLong.

Prototype

public Long getLong(String key, Long defaultValue) 

Source Link

Usage

From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java

static Supplier<?> getStaticSupplier(Class<?> type, String key, String defaultValue,
        AbstractConfiguration configuration) {
    if (type.isAssignableFrom(String.class)) {
        return Suppliers.ofInstance(configuration.getString(key, defaultValue));
    } else if (type.isAssignableFrom(Integer.class)) {
        return Suppliers.ofInstance(
                configuration.getInteger(key, defaultValue == null ? 0 : Integer.parseInt(defaultValue)));
    } else if (type.isAssignableFrom(Double.class)) {
        return Suppliers.ofInstance(
                configuration.getDouble(key, defaultValue == null ? 0.0 : Double.parseDouble(defaultValue)));
    } else if (type.isAssignableFrom(Long.class)) {
        return Suppliers.ofInstance(
                configuration.getLong(key, defaultValue == null ? 0L : Long.parseLong(defaultValue)));
    } else if (type.isAssignableFrom(Boolean.class)) {
        return Suppliers.ofInstance(configuration.getBoolean(key,
                defaultValue == null ? false : Boolean.parseBoolean(defaultValue)));
    }// w  w w . ja v a 2  s.  c  om
    throw new RuntimeException("Unsupported value type " + type.getCanonicalName());
}

From source file:me.ineson.demo.app.ConfigTest.java

@Test
public void testGetLong() {
    AbstractConfiguration mockAbstractConfiguration = mock(XMLConfiguration.class);
    when(mockAbstractConfiguration.getLong("testApp.key1", null)).thenReturn(LONG_1);
    Config config = new Config("testApp", mockAbstractConfiguration);

    Long value = config.getLong("key1");

    verify(mockAbstractConfiguration, times(1)).getLong("testApp.key1", null);
    verifyNoMoreInteractions(mockAbstractConfiguration);

    Assert.assertEquals(LONG_1, value);//from w w  w  .  j av  a 2  s .c o  m
}

From source file:me.ineson.demo.app.ConfigTest.java

@Test
public void testGetLongManadtorySuccess() {
    AbstractConfiguration mockAbstractConfiguration = mock(XMLConfiguration.class);
    when(mockAbstractConfiguration.getLong("testApp.key2", null)).thenReturn(LONG_2);
    Config config = new Config("testApp", mockAbstractConfiguration);

    Long value = config.getLongManadtory("key2");

    verify(mockAbstractConfiguration, times(1)).getLong("testApp.key2", null);
    verifyNoMoreInteractions(mockAbstractConfiguration);

    Assert.assertEquals(LONG_2, value);/*  www.ja va2s . c o m*/
}

From source file:me.ineson.demo.app.ConfigTest.java

/**
 * /*from   ww  w .  j  a v  a2  s  .  co m*/
 */
@Test
public void testGetLongManadtoryFail() {
    AbstractConfiguration mockAbstractConfiguration = mock(XMLConfiguration.class);

    when(mockAbstractConfiguration.getLong("testAppF.key3", null)).thenReturn(null);
    Config config = new Config("testAppF", mockAbstractConfiguration);

    try {
        config.getLongManadtory("key3");
        Assert.fail("Mandatory ");
    } catch (IllegalStateException e) {
        // Should fail
    }

    verify(mockAbstractConfiguration, times(1)).getLong("testAppF.key3", null);
    verifyNoMoreInteractions(mockAbstractConfiguration);
}

From source file:com.nesscomputing.jersey.filter.BodySizeLimitResourceFilterFactory.java

@Override
public List<ResourceFilter> create(AbstractMethod am) {
    BodySizeLimit annotation = ObjectUtils.firstNonNull(am.getAnnotation(BodySizeLimit.class),
            am.getResource().getAnnotation(BodySizeLimit.class));

    final AbstractConfiguration ac = config.getConfiguration();
    final Method realMethod = am.getMethod();
    Class<?> resourceClass = am.getResource().getResourceClass();
    Map<String, Long> foundValues = Maps.newLinkedHashMap();
    do {//from ww w  . j a va 2 s  .c  om
        final String classConfigKey = "ness.filter.max-body-size." + resourceClass.getName();
        try {
            resourceClass.getMethod(realMethod.getName(), realMethod.getParameterTypes());
        } catch (NoSuchMethodException | SecurityException e) {
            LOG.trace(e);
            continue;
        }
        final String methodKey = classConfigKey + "." + realMethod.getName();
        foundValues.put(methodKey, ac.getLong(methodKey, null));
        foundValues.put(classConfigKey, ac.getLong(classConfigKey, null));
    } while ((resourceClass = resourceClass.getSuperclass()) != null);

    if (annotation != null) {
        foundValues.put("annotation " + annotation, annotation.value());
    }
    foundValues.put("default", defaultSizeLimit);

    for (Entry<String, Long> e : foundValues.entrySet()) {
        final Long value = e.getValue();
        if (value != null) {

            String message = "[%s] => %s";
            Object[] args = {
                    Joiner.on("], [").withKeyValueSeparator(" = ").useForNull("null").join(foundValues),
                    value };

            if (value != defaultSizeLimit || annotation != null) {
                LOG.debug(message, args);
            } else {
                LOG.trace(message, args);
            }

            return Collections.<ResourceFilter>singletonList(new Filter(value));
        }
    }

    throw new IllegalStateException("No value found for " + am + ": " + foundValues);
}

From source file:org.skb.util.types.composite.util.TSPropertyMap.java

public String loadFromFile(String fn) {
    try {//from  w  ww .  ja va  2  s.  c om
        AbstractConfiguration cfg;
        String prefix = "";
        if (fn.endsWith(".ini")) {
            cfg = new INIConfiguration(fn);
            prefix = "tribe.";
        } else if (fn.endsWith(".xml"))
            cfg = new XMLConfiguration(fn);
        else if (fn.endsWith(".properties"))
            cfg = new PropertiesConfiguration(fn);
        else
            return "unknown configuration file format, use '.ini' or '.xml' or '.properties'";

        File file = new File(fn);
        if (!file.canRead())
            return "can't read configuration file <" + fn + ">";

        Iterator<?> it;
        if (prefix != "")
            it = cfg.getKeys(prefix);
        else
            it = cfg.getKeys();
        while (it.hasNext()) {
            String p = it.next().toString();
            if (this.containsKey(p)) {
                String type = this.get(p, TSPropertyMap.pmValType).toString();
                if (type.equals(TSRepository.TString.TS_ATOMIC_JAVA_STRING))
                    this.put(p, TSPropertyMap.pmValValueFile, cfg.getString(p));
                else if (type.equals(TSRepository.TString.TS_ATOMIC_JAVA_BOOLEAN))
                    this.put(p, TSPropertyMap.pmValValueFile, cfg.getBoolean(p, false));
                else if (type.equals(TSRepository.TString.TS_ATOMIC_JAVA_INTEGER))
                    this.put(p, TSPropertyMap.pmValValueFile, cfg.getInteger(p, 0));
                else if (type.equals(TSRepository.TString.TS_ATOMIC_JAVA_DOUBLE))
                    this.put(p, TSPropertyMap.pmValValueFile, cfg.getDouble(p));
                else if (type.equals(TSRepository.TString.TS_ATOMIC_JAVA_LONG))
                    this.put(p, TSPropertyMap.pmValValueFile, cfg.getLong(p, 0));
                //                 else
                //                    System.err.println("TSPropMap, loadfromfile, unknown type <"+type+"> for <"+p+">");
            }

        }
    } catch (Exception e) {
        //           ReportManager repMgr=ReportManager.getInstance();
        //           repMgr.reportErrorNoFile(e.toString());
    }
    return null;
}