org.wso2.carbon.config.ConfigurationException Java Examples

The following examples show how to use org.wso2.carbon.config.ConfigurationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From msf4j with Apache License 2.0 6 votes vote down vote up
public static TransportsConfiguration resolveTransportsNSConfiguration(Object transportsConfig)
        throws ConfigurationException {

    TransportsConfiguration transportsConfiguration;

    if (transportsConfig instanceof Map) {
        LinkedHashMap httpConfig = ((LinkedHashMap) ((Map) transportsConfig).get("http"));
        if (httpConfig != null) {
            String configYaml = new Yaml().dump(httpConfig);
            Yaml yaml = new Yaml(new CustomClassLoaderConstructor(TransportsConfiguration.class,
                    TransportsConfiguration.class.getClassLoader()));
            yaml.setBeanAccess(BeanAccess.FIELD);
            transportsConfiguration = yaml.loadAs(configYaml, TransportsConfiguration.class);

        } else {
            transportsConfiguration = new TransportsConfiguration();
        }
    } else {
        throw new ConfigurationException("The first level config under 'transports' namespace should be " +
                "a map.");
    }
    return transportsConfiguration;
}
 
Example #2
Source File: TransportConfigurationTest.java    From msf4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get the {@code TransportsConfiguration} represented by a particular configuration file
 *
 * @param configFileLocation configuration file location
 * @return TransportsConfiguration represented by a particular configuration file
 */
public TransportsConfiguration getConfiguration(String configFileLocation) {
    TransportsConfiguration transportsConfiguration;

    File file = new File(configFileLocation);
    if (file.exists()) {
        try {
        transportsConfiguration =
                ConfigProviderFactory.getConfigProvider(Paths.get(configFileLocation), null)
                .getConfigurationObject(WSO2_TRANSPORT_HTTP_CONFIG_NAMESPACE, TransportsConfiguration.class);
        } catch (ConfigurationException e) {
            throw new RuntimeException(
                    "Error while loading " + configFileLocation + " configuration file", e);
        }
    } else { // return a default config
        transportsConfiguration = new TransportsConfiguration();
    }

    return transportsConfiguration;
}
 
Example #3
Source File: ConfigProviderOSGITest.java    From carbon-kernel with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigurationProvider() throws ConfigurationException {
    CarbonConfiguration carbonConfiguration = configProvider.getConfigurationObject(
            CarbonConfiguration.class);
    String id = carbonConfiguration.getId();
    String name = carbonConfiguration.getName();
    String tenant = carbonConfiguration.getTenant();

    Assert.assertEquals(id, "carbon-kernel", "id should be carbon-kernel");
    Assert.assertEquals(name, "WSO2 Carbon Kernel", "name should be WSO2 Carbon Kernel");
    Assert.assertEquals(tenant, "default", "tenant should be default");

    Map secureVaultConfiguration =
            (LinkedHashMap) configProvider.getConfigurationObject("wso2.securevault");

    Assert.assertEquals(((LinkedHashMap) (secureVaultConfiguration.get("secretRepository"))).get("type"),
            "org.wso2.carbon.secvault.repository.DefaultSecretRepository",
            "Default secret repository would be " +
                    "org.wso2.carbon.secvault.repository.DefaultSecretRepository");

    Assert.assertEquals(((LinkedHashMap) (secureVaultConfiguration.get("masterKeyReader"))).get("type"),
            "org.wso2.carbon.secvault.reader.DefaultMasterKeyReader",
            "Default master key reader would be " +
                    "org.wso2.carbon.secvault.reader.DefaultMasterKeyReader");
}
 
Example #4
Source File: Main.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
/**
 * Loads configurations during the broker start up.
 * method will try to <br/>
 *  (1) Load the configuration file specified in 'broker.file' (e.g. -Dbroker.file={FilePath}). <br/>
 *  (2) If -Dbroker.file is not specified, the broker.yaml file exists in current directory and load it. <br/>
 *
 * <b>Note: </b> if provided configuration file cannot be read broker will not start.
 * @param startupContext startup context of the broker
 */
private static void initConfigProvider(StartupContext startupContext) throws ConfigurationException {
    Path brokerYamlFile;
    String brokerFilePath = System.getProperty(BrokerCoreConfiguration.SYSTEM_PARAM_BROKER_CONFIG_FILE);
    if (brokerFilePath == null || brokerFilePath.trim().isEmpty()) {
        // use current path.
        brokerYamlFile = Paths.get("", BrokerCoreConfiguration.BROKER_FILE_NAME).toAbsolutePath();
    } else {
        brokerYamlFile = Paths.get(brokerFilePath).toAbsolutePath();
    }

    ConfigProvider configProvider = ConfigProviderFactory.getConfigProvider(brokerYamlFile);
    startupContext.registerService(BrokerConfigProvider.class,
                                   (BrokerConfigProvider) configProvider::getConfigurationObject);
}
 
Example #5
Source File: CarbonConfigAdapter.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
private <T> T getConfig(String s, Class<T> aClass) throws ConfigurationException {
    try {
        return configProvider.getConfigurationObject(s, aClass);
    } catch (Exception e) {
        throw new ConfigurationException("Error while reading metrics config", e);
    }
}
 
Example #6
Source File: CarbonConfigAdapterTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = ConfigurationException.class)
public void testGetInvalidConfigurationObject() throws Exception {
    CarbonConfigAdapter carbonConfigAdapter = new CarbonConfigAdapter(configProvider);
    TestConfig configurationObject = carbonConfigAdapter.getConfigurationObject("invalid.namespace",
                                                                                TestConfig.class);
    Assert.assertEquals(configurationObject.getTestField(), NAMESPACE_KEY);
}
 
Example #7
Source File: HTTPMonitoringConfigBuilder.java    From msf4j with Apache License 2.0 5 votes vote down vote up
public static HTTPMonitoringConfig build() {
    HTTPMonitoringConfig configurationObject;
    ConfigProvider configProvider = AnalyticUtils.getConfigurationProvider();

    try {
        configurationObject =
                configProvider.getConfigurationObject(HTTPMonitoringConfig.class);
    } catch (ConfigurationException e) {
        throw new RuntimeException(
                "Error while loading " + HTTPMonitoringConfig.class.getName() + " from config provider", e);
    }

    return configurationObject;
}
 
Example #8
Source File: PropertyResolveConfigProviderOSGITest.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyResolve() throws ConfigurationException {
    CarbonConfiguration carbonConfiguration = configProvider.getConfigurationObject(
            CarbonConfiguration.class);
    String id = carbonConfiguration.getId();
    String name = carbonConfiguration.getName();
    String tenant = carbonConfiguration.getTenant();

    Assert.assertEquals(id, "test-carbon-kernel", "id should be test-carbon-kernel");
    Assert.assertEquals(name, "Test WSO2 Carbon Kernel",
            "name should be Test WSO2 Carbon Kernel");
    Assert.assertEquals(tenant, "default", "tenant should be default");

}
 
Example #9
Source File: CarbonConfigProviderImpl.java    From carbon-kernel with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T getConfigurationObject(Class<T> configClass) throws ConfigurationException {
    try {
        return configClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ConfigurationException("Error while creating configuration Instance : "
                + configClass.getSimpleName(), e);
    }
}
 
Example #10
Source File: CarbonConfigAdapter.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T getConfigurationObject(Class<T> aClass) throws ConfigurationException {
    return getConfig(aClass.getCanonicalName(), aClass);
}
 
Example #11
Source File: CarbonConfigAdapter.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Override
public Object getConfigurationObject(String s) throws ConfigurationException {
    return getConfig(s, Object.class);
}
 
Example #12
Source File: CarbonConfigAdapter.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T getConfigurationObject(String s, Class<T> aClass) throws ConfigurationException {
    return getConfig(s, aClass);
}
 
Example #13
Source File: MicroservicesServerSC.java    From msf4j with Apache License 2.0 4 votes vote down vote up
@Reference(
        name = "carbon.config.provider",
        service = ConfigProvider.class,
        cardinality = ReferenceCardinality.MANDATORY,
        policy = ReferencePolicy.DYNAMIC,
        unbind = "unregisterConfigProvider"
)
protected void registerConfigProvider(ConfigProvider configProvider) {
    DataHolder.getInstance().setConfigProvider(configProvider);
    try {
        final TransportsConfiguration transportsConfiguration;
        Object transportConf = configProvider.getConfigurationObject(STREAMLINED_TRANSPORT_NAMESPACE);
        if (transportConf != null) {
            transportsConfiguration = Utils.resolveTransportsNSConfiguration(transportConf);
        } else {
            transportsConfiguration = configProvider.getConfigurationObject
                    (MSF4JConstants.WSO2_TRANSPORT_HTTP_CONFIG_NAMESPACE, TransportsConfiguration.class);
        }

        CarbonConfiguration carbonConfig = configProvider.getConfigurationObject(CarbonConfiguration.class);
        transportsConfiguration.getListenerConfigurations().forEach(
                listenerConfiguration -> listenerConfiguration.setPort(
                        listenerConfiguration.getPort() + carbonConfig.getPortsConfig().getOffset()));

        Set<ListenerConfiguration> listenerConfigurations =
                transportsConfiguration.getListenerConfigurations();
        if (listenerConfigurations.isEmpty()) {
            listenerConfigurations = new HashSet<>();
            listenerConfigurations.add(new ListenerConfiguration());
        }

        ServerBootstrapConfiguration serverBootstrapConfiguration =
                HttpConnectorUtil.getServerBootstrapConfiguration(transportsConfiguration.getTransportProperties());
        HttpWsConnectorFactory connectorFactory = new DefaultHttpWsConnectorFactory();
        listenerConfigurations.forEach(listenerConfiguration -> {
            ServerConnector serverConnector =
                    connectorFactory.createServerConnector(serverBootstrapConfiguration, listenerConfiguration);
            MicroservicesRegistryImpl microservicesRegistry = new MicroservicesRegistryImpl();
            Map<String, MicroservicesRegistryImpl> microservicesRegistries =
                    DataHolder.getInstance().getMicroservicesRegistries();
            Dictionary<String, String> properties = new Hashtable<>();
            properties.put(MSF4JConstants.CHANNEL_ID, serverConnector.getConnectorID());
            microservicesRegistries.put(serverConnector.getConnectorID(), microservicesRegistry);
            DataHolder.getInstance().getBundleContext()
                    .registerService(MicroservicesRegistry.class, microservicesRegistry, properties);
            listenerConfigurationMap.put(serverConnector.getConnectorID(), listenerConfiguration);
            serverConnectors.add(serverConnector);
        });
    } catch (ConfigurationException e) {
        log.error("Error while loading TransportsConfiguration", e);
        throw new RuntimeException("Error while loading TransportsConfiguration", e);
    }
    StartupServiceUtils.updateServiceCache("wso2-microservices-server", ConfigProvider.class);
}
 
Example #14
Source File: CarbonConfigProviderImpl.java    From carbon-kernel with Apache License 2.0 4 votes vote down vote up
@Override
public Object getConfigurationObject(String namespace) throws ConfigurationException {
    return null;
}