Search in sources :

Example 1 with ConfigRestService

use of com.peterphi.std.guice.config.rest.iface.ConfigRestService in project stdlib by petergeneric.

the class GuiceFactory method applyNetworkConfiguration.

/**
 * Add to the configuration any properties defined by the network configuration endpoint (if network config is enabled in a
 * previous property)
 *
 * @param config
 */
private static void applyNetworkConfiguration(final GuiceConfig config) {
    final String configEndpoint = config.get(GuiceProperties.CONFIG_ENDPOINT, null);
    final Boolean configSkip = config.getBoolean(GuiceProperties.CONFIG_SKIP, false);
    if (configEndpoint != null && !configSkip) {
        final boolean useMoxy = config.getBoolean(GuiceProperties.MOXY_ENABLED, true);
        final JAXBContextResolver jaxb = new JAXBContextResolver(new JAXBSerialiserFactory(useMoxy));
        final ResteasyClientFactoryImpl clientFactory = new ResteasyClientFactoryImpl(null, null, null, jaxb);
        try {
            final ResteasyProxyClientFactoryImpl proxyFactory = new ResteasyProxyClientFactoryImpl(clientFactory, config);
            final ConfigRestService client = proxyFactory.getClient(ConfigRestService.class);
            // We set it in the config because otherwise the NetworkConfigReloadDaemon won't be able to load the config
            if (config.get(GuiceProperties.CONFIG_PATH) == null) {
                config.set(GuiceProperties.CONFIG_PATH, "services/" + config.get(GuiceProperties.SERVLET_CONTEXT_NAME, "unknown-service"));
            }
            // Get the config path to read
            final String path = config.get(GuiceProperties.CONFIG_PATH);
            final ConfigPropertyData data = client.read(path, config.get(GuiceProperties.INSTANCE_ID), null);
            for (ConfigPropertyValue property : data.properties) {
                config.set(property.name, property.value);
            }
            // Let others know that the configuration data is coming from a network source
            config.set(GuiceProperties.CONFIG_SOURCE, GuiceConstants.CONFIG_SOURCE_NETWORK);
            if (data.revision != null)
                config.set(GuiceProperties.CONFIG_REVISION, data.revision);
        } finally {
            clientFactory.shutdown();
        }
    } else {
        // Config is not coming from the network
        config.set(GuiceProperties.CONFIG_SOURCE, GuiceConstants.CONFIG_SOURCE_LOCAL);
    }
}
Also used : ConfigRestService(com.peterphi.std.guice.config.rest.iface.ConfigRestService) ResteasyClientFactoryImpl(com.peterphi.std.guice.restclient.resteasy.impl.ResteasyClientFactoryImpl) JAXBSerialiserFactory(com.peterphi.std.util.jaxb.JAXBSerialiserFactory) ConfigPropertyData(com.peterphi.std.guice.config.rest.types.ConfigPropertyData) JAXBContextResolver(com.peterphi.std.guice.restclient.resteasy.impl.JAXBContextResolver) ConfigPropertyValue(com.peterphi.std.guice.config.rest.types.ConfigPropertyValue) ResteasyProxyClientFactoryImpl(com.peterphi.std.guice.restclient.resteasy.impl.ResteasyProxyClientFactoryImpl)

Aggregations

ConfigRestService (com.peterphi.std.guice.config.rest.iface.ConfigRestService)1 ConfigPropertyData (com.peterphi.std.guice.config.rest.types.ConfigPropertyData)1 ConfigPropertyValue (com.peterphi.std.guice.config.rest.types.ConfigPropertyValue)1 JAXBContextResolver (com.peterphi.std.guice.restclient.resteasy.impl.JAXBContextResolver)1 ResteasyClientFactoryImpl (com.peterphi.std.guice.restclient.resteasy.impl.ResteasyClientFactoryImpl)1 ResteasyProxyClientFactoryImpl (com.peterphi.std.guice.restclient.resteasy.impl.ResteasyProxyClientFactoryImpl)1 JAXBSerialiserFactory (com.peterphi.std.util.jaxb.JAXBSerialiserFactory)1