use of com.disney.groovity.conf.Configurator in project groovity by disney.
the class Groovity method addConfigurator.
public void addConfigurator(Configurator toAdd) {
if (this.configurator == null) {
this.configurator = new MultiConfigurator(new CopyOnWriteArrayList<Configurator>());
}
if (!(configurator instanceof MultiConfigurator)) {
CopyOnWriteArrayList<Configurator> nc = new CopyOnWriteArrayList<Configurator>();
nc.add(configurator);
this.configurator = new MultiConfigurator(nc);
}
toAdd.init();
((MultiConfigurator) configurator).getConfigurators().add(toAdd);
configureAll();
}
use of com.disney.groovity.conf.Configurator 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;
}
use of com.disney.groovity.conf.Configurator in project groovity by disney.
the class TestCoreGroovity method testConf.
@Test
public void testConf() throws Exception {
Binding binding = new Binding();
CharArrayWriter writer = new CharArrayWriter();
binding.setVariable("out", writer);
Script confScript = groovity.load("/conf", binding);
GroovityClassLoader gcl = (GroovityClassLoader) confScript.getClass().getClassLoader();
groovity.run("/conf", binding);
String result = writer.toString();
Assert.assertEquals("|false||0||test|||", result);
writer.reset();
gcl.configure(new Configurator() {
@Override
public void init() {
}
@Override
public void destroy() {
}
@Override
public void configure(String sourcePath, Set<String> propertyNames, BiConsumer<String, String> propertySetter) {
propertySetter.accept("testDefaultBoolean", "true");
propertySetter.accept("testBooleanType", "foo");
propertySetter.accept("testDefaultInteger", "99");
propertySetter.accept("testIntegerType", "111");
propertySetter.accept("testDefaultString", "bar");
propertySetter.accept("testStringType", "zzz");
propertySetter.accept("testNull", "qq");
}
});
groovity.run("/conf", binding);
result = writer.toString();
Assert.assertEquals("|true|false|99|111|bar|zzz|qq|", result);
writer.reset();
logRecords.clear();
gcl.configure(new Configurator() {
@Override
public void init() {
}
@Override
public void destroy() {
}
@Override
public void configure(String sourcePath, Set<String> propertyNames, BiConsumer<String, String> propertySetter) {
propertySetter.accept("testDefaultInteger", "badOnPurpose");
propertySetter.accept("testIntegerType", "badOnPurpose");
}
});
groovity.run("/conf", binding);
result = writer.toString();
Assert.assertEquals("|false||0||test|||", result);
Assert.assertEquals(3, logRecords.size());
Assert.assertEquals(Level.WARNING, logRecords.get(0).getLevel());
Assert.assertEquals(Level.SEVERE, logRecords.get(1).getLevel());
Assert.assertEquals(Level.SEVERE, logRecords.get(2).getLevel());
}
Aggregations