use of com.yahoo.config.ConfigInstance in project vespa by vespa-engine.
the class ConfigInstanceUtil method getNewInstance.
public static <T extends ConfigInstance> T getNewInstance(Class<T> type, String configId, ConfigPayload payload) {
T instance;
try {
ConfigTransformer<?> transformer = new ConfigTransformer<T>(type);
ConfigBuilder instanceBuilder = transformer.toConfigBuilder(payload);
Constructor<T> constructor = type.getConstructor(instanceBuilder.getClass());
instance = constructor.newInstance((ConfigInstance.Builder) instanceBuilder);
// Workaround for JDK7, where compilation fails due to fields being
// private and not accessible from T. Reference it as a
// ConfigInstance to work around it. See
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7022052 for
// more information.
ConfigInstance i = instance;
i.postInitialize(configId);
setConfigId(i, configId);
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException e) {
throw new IllegalArgumentException("Failed creating new instance of '" + type.getCanonicalName() + "' for config id '" + configId + "': " + Exceptions.toMessageString(e), e);
}
return instance;
}
use of com.yahoo.config.ConfigInstance in project vespa by vespa-engine.
the class AbstractConfigProducer method writeBuilder.
private void writeBuilder(File directory, Method m, ConfigInstance.Builder builder) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, IOException {
m.invoke(this, builder);
Class<?> configInstClass = builder.getClass().getEnclosingClass();
ConfigInstance inst = (ConfigInstance) configInstClass.getConstructor(builder.getClass()).newInstance(builder);
List<String> payloadList = ConfigInstance.serialize(inst);
File outfn = new File(directory, ConfigInstance.getDefName(inst.getClass()) + ".MODEL.cfg");
FileOutputStream out = new FileOutputStream(outfn);
for (String s : payloadList) {
out.write(Utf8.toBytes(s));
out.write('\n');
}
}
use of com.yahoo.config.ConfigInstance in project vespa by vespa-engine.
the class ConfigBuilderGeneratorTest method require_that_custom_classes_can_be_generated.
@Test
public void require_that_custom_classes_can_be_generated() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
String[] schema = new String[] { "namespace=foo.bar", "intval int", "stringval string" };
File tempDir = Files.createTempDir();
ConfigDefinitionKey key = new ConfigDefinitionKey("quux", "foo.bar");
ConfigCompiler compiler = new LazyConfigCompiler(tempDir);
ConfigInstance.Builder builder = compiler.compile(new ConfigDefinition(key.getName(), schema).generateClass()).newInstance();
assertNotNull(builder);
ConfigPayloadApplier<?> payloadApplier = new ConfigPayloadApplier<>(builder);
Slime slime = new Slime();
Cursor root = slime.setObject();
root.setString("intval", "3");
root.setString("stringval", "Hello, world");
payloadApplier.applyPayload(new ConfigPayload(slime));
String className = createClassName(key.getName());
ConfigInstance instance = (ConfigInstance) builder.getClass().getClassLoader().loadClass("com.yahoo." + key.getNamespace() + "." + className).getConstructor(new Class<?>[] { builder.getClass() }).newInstance(builder);
assertNotNull(instance);
assertEquals("intval 3\nstringval \"Hello, world\"", instance.toString());
}
use of com.yahoo.config.ConfigInstance in project vespa by vespa-engine.
the class ComponentClassTestCase method testCreateComponent.
@SuppressWarnings("unchecked")
@Test
public void testCreateComponent() throws NoSuchMethodException {
Map<ConfigKey, ConfigInstance> availableConfigs = new HashMap<>();
String configId = "testConfigId";
availableConfigs.put(new ConfigKey(StringConfig.class, configId), new StringConfig(new StringConfig.Builder()));
availableConfigs.put(new ConfigKey(IntConfig.class, configId), new IntConfig(new IntConfig.Builder()));
ComponentClass<TestComponent> testClass = new ComponentClass<>(TestComponent.class);
TestComponent component = testClass.createComponent(new ComponentId("test", new Version(1)), availableConfigs, configId);
assertEquals("test", component.getId().getName());
assertEquals(1, component.getId().getVersion().getMajor());
assertEquals(1, component.intVal);
assertEquals("_default_", component.stringVal);
}
use of com.yahoo.config.ConfigInstance in project vespa by vespa-engine.
the class ComponentClassTestCase method testNullIdComponent.
/**
* Verifies that ComponentClass sets the ComponentId when a component that takes a ComponentId as
* constructor argument fails to call super(id).
*/
@Test
public void testNullIdComponent() throws NoSuchMethodException {
ComponentClass<NullIdComponent> testClass = new ComponentClass<>(NullIdComponent.class);
NullIdComponent component = testClass.createComponent(new ComponentId("null-test", new Version(1)), new HashMap<ConfigKey, ConfigInstance>(), null);
assertEquals("null-test", component.getId().getName());
assertEquals(1, component.getId().getVersion().getMajor());
}
Aggregations