Search in sources :

Example 6 with ConfigInstance

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;
}
Also used : ConfigBuilder(com.yahoo.config.ConfigBuilder) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigBuilder(com.yahoo.config.ConfigBuilder) ConfigInstance(com.yahoo.config.ConfigInstance)

Example 7 with ConfigInstance

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');
    }
}
Also used : ConfigInstance(com.yahoo.config.ConfigInstance)

Example 8 with ConfigInstance

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());
}
Also used : ConfigDefinitionKey(com.yahoo.vespa.config.ConfigDefinitionKey) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor) ConfigPayload(com.yahoo.vespa.config.ConfigPayload) ConfigPayloadApplier(com.yahoo.vespa.config.ConfigPayloadApplier) File(java.io.File) ConfigInstance(com.yahoo.config.ConfigInstance) Test(org.junit.Test)

Example 9 with ConfigInstance

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);
}
Also used : ConfigKey(com.yahoo.vespa.config.ConfigKey) HashMap(java.util.HashMap) IntConfig(com.yahoo.config.core.IntConfig) StringConfig(com.yahoo.config.core.StringConfig) Version(com.yahoo.component.Version) ComponentId(com.yahoo.component.ComponentId) ComponentClass(com.yahoo.component.provider.ComponentClass) ConfigInstance(com.yahoo.config.ConfigInstance) Test(org.junit.Test)

Example 10 with ConfigInstance

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());
}
Also used : ConfigKey(com.yahoo.vespa.config.ConfigKey) Version(com.yahoo.component.Version) ComponentId(com.yahoo.component.ComponentId) ComponentClass(com.yahoo.component.provider.ComponentClass) ConfigInstance(com.yahoo.config.ConfigInstance) Test(org.junit.Test)

Aggregations

ConfigInstance (com.yahoo.config.ConfigInstance)10 ConfigurationRuntimeException (com.yahoo.config.ConfigurationRuntimeException)3 ConfigKey (com.yahoo.vespa.config.ConfigKey)3 Test (org.junit.Test)3 ComponentId (com.yahoo.component.ComponentId)2 Version (com.yahoo.component.Version)2 ComponentClass (com.yahoo.component.provider.ComponentClass)2 Builder (com.yahoo.config.ConfigInstance.Builder)2 ConfigBuilder (com.yahoo.config.ConfigBuilder)1 IntConfig (com.yahoo.config.core.IntConfig)1 StringConfig (com.yahoo.config.core.StringConfig)1 Cursor (com.yahoo.slime.Cursor)1 Slime (com.yahoo.slime.Slime)1 ConfigDefinitionKey (com.yahoo.vespa.config.ConfigDefinitionKey)1 ConfigPayload (com.yahoo.vespa.config.ConfigPayload)1 ConfigPayloadApplier (com.yahoo.vespa.config.ConfigPayloadApplier)1 ConfigPayloadBuilder (com.yahoo.vespa.config.ConfigPayloadBuilder)1 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1