Search in sources :

Example 1 with PropertiesResourceConfigurator

use of com.disney.groovity.conf.PropertiesResourceConfigurator in project groovity by disney.

the class GroovityBuilder method build.

/**
 * After setting all initialization parameters, call build(true) to construct, initialize and return the fully started Groovity,
 * or build(false) to initialize the Groovity without starting (e.g. to just build jar files without initing and starting classes)
 *
 * @return an initialized Groovity
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 * @throws IOException
 * @throws URISyntaxException
 */
public Groovity build(boolean start) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException, URISyntaxException {
    CopyOnWriteArrayList<Configurator> configurators = new CopyOnWriteArrayList<Configurator>();
    configurators.add(new SystemConfigurator());
    if (propsResource != null) {
        configurators.add(new PropertiesResourceConfigurator(propsResource));
    }
    if (propsFile != null) {
        configurators.add(new PropertiesFileConfigurator(propsFile));
    }
    if (propsURL != null) {
        configurators.add(new PropertiesURLConfigurator(propsURL));
    }
    if (configurator != null) {
        configurators.add(configurator);
    }
    Groovity groovity = new Groovity();
    groovity.setJarDirectory(jarDirectory);
    groovity.setJarPhases(jarPhases);
    groovity.setSourcePhases(sourcePhases);
    groovity.setCaseSensitive(caseSensitive);
    groovity.setArgsLookup(argsLookup);
    groovity.setAsyncThreads(asyncThreads);
    groovity.setScriptBaseClass(scriptBaseClass);
    groovity.setParentLoader(parentClassLoader);
    groovity.setConfigurator(new MultiConfigurator(configurators));
    final AtomicReference<BindingDecorator> bindingDecoratorRef = new AtomicReference<BindingDecorator>(bindingDecorator);
    if (defaultBinding != null) {
        bindingDecoratorRef.set(new BindingMapDecorator(new ConcurrentHashMap<String, Object>(defaultBinding), bindingDecoratorRef.get()));
    }
    ServiceLoader.load(BindingDecorator.class).forEach(decorator -> {
        decorator.setChainedDecorator(bindingDecoratorRef.get());
        bindingDecoratorRef.set(decorator);
    });
    groovity.setBindingDecorator(bindingDecoratorRef.get());
    if (httpClientBuilder == null) {
        httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
    }
    if (maxHttpConnPerRoute > 0) {
        httpClientBuilder.setMaxConnPerRoute(maxHttpConnPerRoute);
    }
    if (maxHttpConnTotal > 0) {
        httpClientBuilder.setMaxConnTotal(maxHttpConnTotal);
    }
    groovity.setHttpClient(httpClientBuilder.build());
    List<GroovitySourceLocator> locators = new ArrayList<GroovitySourceLocator>();
    if ((sourceLocations == null || sourceLocations.isEmpty()) && (sourceLocators == null || sourceLocators.isEmpty()) && jarDirectory == null) {
        throw new IllegalArgumentException("No groovity source locators or jar directories configured");
    }
    if (sourceLocators != null) {
        for (GroovitySourceLocator locator : sourceLocators) {
            locators.add(locator);
        }
    }
    if (sourceLocations != null) {
        for (URI location : sourceLocations) {
            AbstractGroovitySourceLocator sourceLocator = null;
            String scheme = location.getScheme();
            if (scheme != null) {
                scheme = scheme.toLowerCase();
                if (scheme.startsWith("http")) {
                    sourceLocator = new HttpGroovitySourceLocator(location);
                } else if (scheme.equals("classpath")) {
                    sourceLocator = new ClasspathGroovitySourceLocator(location.getPath());
                }
            }
            if (sourceLocator == null) {
                // file locator
                if (location.isAbsolute()) {
                    sourceLocator = new FileGroovitySourceLocator(new File(location));
                } else {
                    sourceLocator = new FileGroovitySourceLocator(new File(location.getPath()));
                }
            }
            sourceLocator.setInterval(sourcePollSeconds);
            locators.add(sourceLocator);
        }
    }
    groovity.setSourceLocators(locators.toArray(new GroovitySourceLocator[0]));
    if (start) {
        groovity.start();
    } else {
        groovity.init(false);
    }
    return groovity;
}
Also used : SystemConfigurator(com.disney.groovity.conf.SystemConfigurator) HttpGroovitySourceLocator(com.disney.groovity.source.HttpGroovitySourceLocator) PropertiesURLConfigurator(com.disney.groovity.conf.PropertiesURLConfigurator) PropertiesResourceConfigurator(com.disney.groovity.conf.PropertiesResourceConfigurator) PropertiesFileConfigurator(com.disney.groovity.conf.PropertiesFileConfigurator) MultiConfigurator(com.disney.groovity.conf.MultiConfigurator) Configurator(com.disney.groovity.conf.Configurator) SystemConfigurator(com.disney.groovity.conf.SystemConfigurator) PropertiesResourceConfigurator(com.disney.groovity.conf.PropertiesResourceConfigurator) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FileGroovitySourceLocator(com.disney.groovity.source.FileGroovitySourceLocator) MultiConfigurator(com.disney.groovity.conf.MultiConfigurator) URI(java.net.URI) AbstractGroovitySourceLocator(com.disney.groovity.source.AbstractGroovitySourceLocator) PropertiesURLConfigurator(com.disney.groovity.conf.PropertiesURLConfigurator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClasspathGroovitySourceLocator(com.disney.groovity.source.ClasspathGroovitySourceLocator) PropertiesFileConfigurator(com.disney.groovity.conf.PropertiesFileConfigurator) ClasspathGroovitySourceLocator(com.disney.groovity.source.ClasspathGroovitySourceLocator) GroovitySourceLocator(com.disney.groovity.source.GroovitySourceLocator) FileGroovitySourceLocator(com.disney.groovity.source.FileGroovitySourceLocator) AbstractGroovitySourceLocator(com.disney.groovity.source.AbstractGroovitySourceLocator) HttpGroovitySourceLocator(com.disney.groovity.source.HttpGroovitySourceLocator) File(java.io.File) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

Configurator (com.disney.groovity.conf.Configurator)1 MultiConfigurator (com.disney.groovity.conf.MultiConfigurator)1 PropertiesFileConfigurator (com.disney.groovity.conf.PropertiesFileConfigurator)1 PropertiesResourceConfigurator (com.disney.groovity.conf.PropertiesResourceConfigurator)1 PropertiesURLConfigurator (com.disney.groovity.conf.PropertiesURLConfigurator)1 SystemConfigurator (com.disney.groovity.conf.SystemConfigurator)1 AbstractGroovitySourceLocator (com.disney.groovity.source.AbstractGroovitySourceLocator)1 ClasspathGroovitySourceLocator (com.disney.groovity.source.ClasspathGroovitySourceLocator)1 FileGroovitySourceLocator (com.disney.groovity.source.FileGroovitySourceLocator)1 GroovitySourceLocator (com.disney.groovity.source.GroovitySourceLocator)1 HttpGroovitySourceLocator (com.disney.groovity.source.HttpGroovitySourceLocator)1 File (java.io.File)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1