Search in sources :

Example 6 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class BootstrapManagerTest method shouldLoadPropertiesInTheCorrectOrder.

@PrepareForTest(ConfigPropertiesUtils.class)
@Test
public void shouldLoadPropertiesInTheCorrectOrder() throws IOException {
    PowerMockito.mockStatic(ConfigPropertiesUtils.class);
    Iterable<ConfigLocation> configLocations = new ArrayList<ConfigLocation>();
    Properties properties = createProperties();
    properties.put(SQL_URL, "");
    when(environment.getConfigDir()).thenReturn(null);
    when(environment.getBootstrapProperties()).thenReturn(properties);
    when(configLocationFileStore.getAll()).thenReturn(configLocations);
    when(ConfigPropertiesUtils.getDefaultPropertiesFile(ConfigLocation.FileAccessType.READABLE, configLocations, BootstrapManager.BOOTSTRAP_PROPERTIES)).thenThrow(new MotechConfigurationException("Error loading file from config locations"));
    try {
        bootstrapManager.loadBootstrapConfig();
    } catch (MotechConfigurationException e) {
    // Ignore error because invocation order is to be verified.
    }
    InOrder inOrder = Mockito.inOrder(environment, configLocationFileStore);
    inOrder.verify(environment).getConfigDir();
    inOrder.verify(environment).getBootstrapProperties();
    inOrder.verify(configLocationFileStore).getAll();
}
Also used : InOrder(org.mockito.InOrder) ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) ArrayList(java.util.ArrayList) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) Properties(java.util.Properties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class QueueURLValidator method validateComposite.

private void validateComposite(URI brokerURL, String value) throws URISyntaxException {
    URISupport.CompositeData data = parseComposite(brokerURL);
    String scheme = data.getScheme();
    if (scheme != null && ("failover".equals(scheme) || "fanout".equals(scheme) || "vm".equals(scheme))) {
        for (URI uri : data.getComponents()) {
            validateUriContainSpecificScheme(brokerURL.toString(), uri);
        }
    } else if (scheme != null) {
        isValidUri(brokerURL.toString(), value);
    } else {
        throw new MotechConfigurationException(URL_INVALID);
    }
}
Also used : MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) URISupport(org.apache.activemq.util.URISupport) URI(java.net.URI) URISupport.isCompositeURI(org.apache.activemq.util.URISupport.isCompositeURI)

Example 8 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class QueueURLValidator method validate.

/**
 * Checks whether given URL is valid.
 *
 * @param queueUrl  the URL to be validated
 * @throws MotechConfigurationException if queueUrl is null, empty or invalid
 */
public void validate(String queueUrl) {
    if (StringUtils.isBlank(queueUrl)) {
        throw new MotechConfigurationException("Queue URL cannot be null or empty.");
    }
    try {
        String value = queueUrl.replace("localhost", "127.0.0.1");
        URI brokerURL = new URI(value);
        if (isCompositeURI(brokerURL)) {
            validateComposite(brokerURL, value);
        } else {
            isValidUri(queueUrl, value);
        }
    } catch (URISyntaxException e) {
        throw new MotechConfigurationException(URL_INVALID, e);
    }
}
Also used : MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISupport.isCompositeURI(org.apache.activemq.util.URISupport.isCompositeURI)

Example 9 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class ConfigLocationFileStore method save.

private void save(List<String> configLocations) {
    try {
        propertiesConfiguration.setProperty(CONFIG_LOCATION_PROPERTY_KEY, StringUtils.join(configLocations, ","));
        propertiesConfiguration.save();
    } catch (ConfigurationException e) {
        String errorMessage = String.format("Could not save %s in this location %s.", propertiesConfiguration.getFileName(), propertiesConfiguration.getBasePath());
        throw new MotechConfigurationException(errorMessage, e);
    }
}
Also used : ConfigurationException(org.apache.commons.configuration.ConfigurationException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException)

Example 10 with MotechConfigurationException

use of org.motechproject.config.core.exception.MotechConfigurationException in project motech by motech.

the class ConfigFileMonitor method init.

/**
 * Initializes the configuration file monitor. This method will be automatically called after creation and
 * dependency injection. It is done to make sure that injected dependencies are set and ready to use.
 */
@PostConstruct
public void init() throws IOException {
    BootstrapConfig bootstrapConfig = configurationService.loadBootstrapConfig();
    if (bootstrapConfig != null && bootstrapConfig.getConfigSource() == ConfigSource.FILE) {
        // allow custom monitors to be injected
        if (fileMonitor == null) {
            fileMonitor = new DefaultFileMonitor(this);
            // allow raw configs, which are one directory down, under /raw/, to be monitored
            fileMonitor.setRecursive(true);
        }
        fileMonitor.setDelay(DELAY);
        final List<File> files = new ArrayList<>();
        try {
            files.addAll(configLoader.findExistingConfigs());
        } catch (MotechConfigurationException ex) {
            LOGGER.error(ex.getMessage(), ex);
            return;
        }
        configurationService.processExistingConfigs(files);
        startFileMonitor();
    }
}
Also used : BootstrapConfig(org.motechproject.config.core.domain.BootstrapConfig) ArrayList(java.util.ArrayList) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) DefaultFileMonitor(org.apache.commons.vfs2.impl.DefaultFileMonitor) File(java.io.File) PostConstruct(javax.annotation.PostConstruct)

Aggregations

MotechConfigurationException (org.motechproject.config.core.exception.MotechConfigurationException)19 IOException (java.io.IOException)6 Test (org.junit.Test)5 Properties (java.util.Properties)4 ConfigLocation (org.motechproject.config.core.domain.ConfigLocation)4 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 URI (java.net.URI)2 URISupport (org.apache.activemq.util.URISupport)2 URISupport.isCompositeURI (org.apache.activemq.util.URISupport.isCompositeURI)2 ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 BootstrapConfig (org.motechproject.config.core.domain.BootstrapConfig)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Resource (org.springframework.core.io.Resource)2 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1