Example usage for org.apache.commons.configuration CompositeConfiguration addConfiguration

List of usage examples for org.apache.commons.configuration CompositeConfiguration addConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.configuration CompositeConfiguration addConfiguration.

Prototype

public void addConfiguration(Configuration config) 

Source Link

Document

Add a configuration.

Usage

From source file:org.wso2.andes.server.configuration.VirtualHostConfiguration.java

public ConfigurationPlugin getQueueConfiguration(AMQQueue queue) {
    VirtualHostConfiguration hostConfig = queue.getVirtualHost().getConfiguration();

    // First check if we have a named queue configuration (the easy case)
    if (Arrays.asList(hostConfig.getQueueNames()).contains(queue.getName())) {
        return null;
    }/*  w  w w .j  a v  a  2  s .  c  o  m*/

    // We don't have an explicit queue config we must find out what we need.
    ArrayList<Binding> bindings = new ArrayList<Binding>(queue.getBindings());

    List<AMQShortString> exchangeClasses = new ArrayList<AMQShortString>(bindings.size());

    //Remove default exchange
    for (int index = 0; index < bindings.size(); index++) {
        // Ignore the DEFAULT Exchange binding
        if (bindings.get(index).getExchange().getNameShortString()
                .equals(ExchangeDefaults.DEFAULT_EXCHANGE_NAME)) {
            bindings.remove(index);
        } else {
            exchangeClasses.add(bindings.get(index).getExchange().getType().getName());

            if (exchangeClasses.size() > 1) {
                // If we have more than 1 class of exchange then we can only use the global queue configuration.
                // and this will be returned from the default getQueueConfiguration
                return null;
            }
        }
    }

    // If we are just bound the the default exchange then use the default.
    if (bindings.isEmpty()) {
        return null;
    }

    // If we are bound to only one type of exchange then we are going
    // to have to resolve the configuration for that exchange.

    String exchangeName = bindings.get(0).getExchange().getType().getName().toString();

    // Lookup a Configuration handler for this Exchange.

    // Build the expected class name. <Exchangename>sConfiguration
    // i.e. TopicConfiguration or HeadersConfiguration
    String exchangeClass = "org.wso2.andes.server.configuration." + exchangeName.substring(0, 1).toUpperCase()
            + exchangeName.substring(1) + "Configuration";

    ExchangeConfigurationPlugin exchangeConfiguration = (ExchangeConfigurationPlugin) queue.getVirtualHost()
            .getConfiguration().getConfiguration(exchangeClass);

    // now need to perform the queue-topic-topics-queues magic.
    // So make a new ConfigurationObject that will hold all the configuration for this queue.
    ConfigurationPlugin queueConfig = new QueueConfiguration.QueueConfig();

    // Initialise the queue with any Global values we may have
    PropertiesConfiguration newQueueConfig = new PropertiesConfiguration();
    newQueueConfig.setProperty("name", queue.getName());

    try {
        //Set the queue name
        CompositeConfiguration mungedConf = new CompositeConfiguration();
        //Set the queue name
        mungedConf.addConfiguration(newQueueConfig);
        //Set the global queue configuration
        mungedConf.addConfiguration(getConfig().subset("queues"));

        // Set configuration
        queueConfig.setConfiguration("virtualhosts.virtualhost.queues", mungedConf);
    } catch (ConfigurationException e) {
        // This will not occur as queues only require a name.
        _logger.error("QueueConfiguration requirements have changed.");
    }

    // Merge any configuration the Exchange wishes to apply        
    if (exchangeConfiguration != null) {
        queueConfig.addConfiguration(exchangeConfiguration.getConfiguration(queue));
    }

    //Finally merge in any specific queue configuration we have.
    if (_queues.containsKey(queue.getName())) {
        queueConfig.addConfiguration(_queues.get(queue.getName()));
    }

    return queueConfig;
}

From source file:org.wso2.andes.server.security.auth.manager.PrincipalDatabaseAuthenticationManagerTest.java

private ConfigurationPlugin getConfig(final String clazz, final String argName, final String argValue)
        throws Exception {
    final ConfigurationPlugin config = new PrincipalDatabaseAuthenticationManager.PrincipalDatabaseAuthenticationManagerConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();
    xmlconfig.addProperty("pd-auth-manager.principal-database.class", clazz);

    if (argName != null) {
        xmlconfig.addProperty("pd-auth-manager.principal-database.attributes.attribute.name", argName);
        xmlconfig.addProperty("pd-auth-manager.principal-database.attributes.attribute.value", argValue);
    }//from   ww  w .ja v  a  2 s. co m

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);
    config.setConfiguration("security", xmlconfig);
    return config;
}

From source file:org.wso2.andes.server.virtualhost.plugins.policies.TopicDeletePolicyConfigurationTest.java

/**
 * Test that with the correct configuration the deletePersistent option can
 * be enabled./*w  w w  .  j  a  v  a  2  s  . co m*/
 *
 * Test creates a new Configuration object and passes in the xml snippet
 * that the ConfigurationPlugin would receive during normal execution.
 * This is the XML that would be matched for this plugin:
 * <topicdelete>
 *   <delete-persistent>
 * <topicdelete>
 *
 * So it would be subset and passed in as just:
 *   <delete-persistent>
 *
 *
 * The property should therefore be enabled. 
 *
 */
public void testConfigDeletePersistent() {
    TopicDeletePolicyConfiguration config = new TopicDeletePolicyConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    xmlconfig.addProperty("delete-persistent", "");

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);

    try {
        config.setConfiguration("", composite);
    } catch (ConfigurationException e) {
        fail(e.getMessage());
    }

    assertTrue("A configured TopicDelete should delete persistent queues.", config.deletePersistent());
}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
 * Default Testing:/*from w w  w  .ja va  2s.  c  o m*/
 *
 * Provide a fully complete and valid configuration specifying 'delay' and
 * 'timeunit' and ensure that it is correctly processed.
 *
 * Ensure no exceptions are thrown and that we get the same values back that
 * were put into the configuration.
 */
public void testConfigLoadingValidConfig() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    long DELAY = 10;
    String TIMEUNIT = TimeUnit.MICROSECONDS.toString();
    xmlconfig.addProperty("delay", String.valueOf(DELAY));
    xmlconfig.addProperty("timeunit", TIMEUNIT);

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);

    try {
        config.setConfiguration("", composite);
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    assertEquals("Delay not correctly returned.", DELAY, config.getDelay());
    assertEquals("TimeUnit not correctly returned.", TIMEUNIT, String.valueOf(config.getTimeUnit()));
}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
   * Default Testing:/* ww w.  j av a 2s. c  om*/
   *
   * Test Missing TimeUnit value gets default.
   *
   * The TimeUnit value is optional and default to SECONDS.
   *
   * Test that if we do not specify a TimeUnit then we correctly get seconds.
   *
   * Also verify that relying on the default does not impact the setting of
   * the 'delay' value.
   *
   */
public void testConfigLoadingMissingTimeUnitDefaults() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    long DELAY = 10;
    xmlconfig.addProperty("delay", String.valueOf(DELAY));

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);
    try {
        config.setConfiguration("", composite);
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    assertEquals("Delay not correctly returned.", DELAY, config.getDelay());
    assertEquals("Default TimeUnit incorrect", TimeUnit.SECONDS, config.getTimeUnit());
}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
 * Input Testing://from www . ja  va 2 s.  c  o m
 *
 * TimeUnit parsing requires the String value be in UpperCase.
 * Ensure we can handle when the user doesn't know this.
 *
 * Same test as 'testConfigLoadingValidConfig' but checking that
 * the timeunit field is not case sensitive.
 * i.e. the toUpper is being correctly applied.
 */
public void testConfigLoadingValidConfigStrangeTimeUnit() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    long DELAY = 10;

    xmlconfig.addProperty("delay", DELAY);
    xmlconfig.addProperty("timeunit", "MiCrOsEcOnDs");

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);

    try {
        config.setConfiguration("", composite);
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    assertEquals("Delay not correctly returned.", DELAY, config.getDelay());
    assertEquals("TimeUnit not correctly returned.", TimeUnit.MICROSECONDS.toString(),
            String.valueOf(config.getTimeUnit()));

}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
 * Failure Testing:/*ww w . j  a v  a2 s. c om*/
 *
 * Test that delay must be long not a string value.
 * Provide a delay as a written value not a long. 'ten'.
 *
 * This should throw a configuration exception which is being trapped and
 * verified to be the right exception, a NumberFormatException.
 *
 */
public void testConfigLoadingInValidDelayString() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    xmlconfig.addProperty("delay", "ten");
    xmlconfig.addProperty("timeunit", TimeUnit.MICROSECONDS.toString());

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);

    try {
        config.setConfiguration("", composite);
        fail("Configuration should fail to validate");
    } catch (ConfigurationException e) {
        Throwable cause = e.getCause();

        assertEquals("Cause not correct", NumberFormatException.class, cause.getClass());
    }
}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
 * Failure Testing://from  w  w w.  j  a  va  2 s  .co m
 *
 * Test that negative delays are invalid.
 *
 * Delay must be a positive value as negative delay means doesn't make sense.
 *
 * Configuration exception with a useful message should be thrown here.
 *
 */
public void testConfigLoadingInValidDelayNegative() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    xmlconfig.addProperty("delay", "-10");
    xmlconfig.addProperty("timeunit", TimeUnit.MICROSECONDS.toString());

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);

    try {
        config.setConfiguration("", composite);
        fail("Configuration should fail to validate");
    } catch (ConfigurationException e) {
        Throwable cause = e.getCause();

        assertNotNull("Configuration Exception must not be null.", cause);
        assertEquals("Cause not correct", ConfigurationException.class, cause.getClass());
        assertEquals("Incorrect message.",
                "SlowConsumerDetectionConfiguration: 'delay' must be a Positive Long value.",
                cause.getMessage());
    }
}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
 * Failure Testing://from w  w w .  java2  s  .c o m
 *
 *  Test that delay cannot be 0.
 *
 * A zero delay means run constantly. This is not how VirtualHostTasks
 * are designed to be run so we dis-allow the use of 0 delay.
 *
 * Same test as 'testConfigLoadingInValidDelayNegative' but with a 0 value.
 *
 */
public void testConfigLoadingInValidDelayZero() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    xmlconfig.addProperty("delay", "0");
    xmlconfig.addProperty("timeunit", TimeUnit.MICROSECONDS.toString());

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);

    try {
        config.setConfiguration("", composite);
        fail("Configuration should fail to validate");
    } catch (ConfigurationException e) {
        Throwable cause = e.getCause();

        assertNotNull("Configuration Exception must not be null.", cause);
        assertEquals("Cause not correct", ConfigurationException.class, cause.getClass());
        assertEquals("Incorrect message.",
                "SlowConsumerDetectionConfiguration: 'delay' must be a Positive Long value.",
                cause.getMessage());
    }
}

From source file:org.wso2.andes.server.virtualhost.plugins.SlowConsumerDetectionConfigurationTest.java

/**
 * Failure Testing://from  w ww  . j ava2 s .  c o m
 *
 * Test that missing delay fails.
 * If we have no delay then we do not pick a default. So a Configuration
 * Exception is thrown.
 *
 * */
public void testConfigLoadingInValidMissingDelay() {
    SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration();

    XMLConfiguration xmlconfig = new XMLConfiguration();

    xmlconfig.addProperty("timeunit", TimeUnit.SECONDS.toString());

    // Create a CompositeConfiguration as this is what the broker uses
    CompositeConfiguration composite = new CompositeConfiguration();
    composite.addConfiguration(xmlconfig);
    try {
        config.setConfiguration("", composite);
        fail("Configuration should fail to validate");
    } catch (ConfigurationException e) {
        assertEquals("Incorrect message.",
                "SlowConsumerDetectionConfiguration: unable to configure invalid delay:null", e.getMessage());
    }
}