Search in sources :

Example 1 with Builder

use of com.yahoo.config.ConfigInstance.Builder in project vespa by vespa-engine.

the class Derived method exportBuilderConfig.

/**
 * Checks what this is a producer of, instantiate that and export to writer
 */
// TODO move to ReflectionUtil, and move that to unexported pkg
private void exportBuilderConfig(Writer writer) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IOException {
    for (Class<?> intf : getClass().getInterfaces()) {
        if (ConfigInstance.Producer.class.isAssignableFrom(intf)) {
            Class<?> configClass = intf.getEnclosingClass();
            String builderClassName = configClass.getCanonicalName() + "$Builder";
            Class<?> builderClass = Class.forName(builderClassName);
            ConfigInstance.Builder builder = (Builder) builderClass.newInstance();
            Method getConfig = getClass().getMethod("getConfig", builderClass);
            getConfig.invoke(this, builder);
            ConfigInstance inst = (ConfigInstance) configClass.getConstructor(builderClass).newInstance(builder);
            List<String> payloadL = ConfigInstance.serialize(inst);
            String payload = StringUtilities.implodeMultiline(payloadL);
            writer.write(payload);
        }
    }
}
Also used : Builder(com.yahoo.config.ConfigInstance.Builder) Method(java.lang.reflect.Method) ConfigInstance(com.yahoo.config.ConfigInstance) Builder(com.yahoo.config.ConfigInstance.Builder)

Example 2 with Builder

use of com.yahoo.config.ConfigInstance.Builder in project vespa by vespa-engine.

the class VespaModel method getConfig.

/**
 * Populates an instance of configClass with config produced by configProducer.
 */
public static <CONFIGTYPE extends ConfigInstance> CONFIGTYPE getConfig(Class<CONFIGTYPE> configClass, ConfigProducer configProducer) {
    try {
        Builder builder = newBuilder(configClass);
        populateConfigBuilder(builder, configProducer);
        return newConfigInstance(configClass, builder);
    } catch (Exception e) {
        throw new RuntimeException("Failed getting config for class " + configClass.getName(), e);
    }
}
Also used : ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) VespaDomBuilder(com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder) Builder(com.yahoo.config.ConfigInstance.Builder) ConfigPayloadBuilder(com.yahoo.vespa.config.ConfigPayloadBuilder) ConfigBuilder(com.yahoo.config.ConfigBuilder) VespaModelBuilder(com.yahoo.vespa.model.builder.VespaModelBuilder) SAXException(org.xml.sax.SAXException) ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) IOException(java.io.IOException)

Example 3 with Builder

use of com.yahoo.config.ConfigInstance.Builder in project vespa by vespa-engine.

the class VespaModel method getConfig.

/**
 * Resolves config of the given type and config id, by first instantiating the correct {@link com.yahoo.config.ConfigInstance.Builder},
 * calling {@link #getConfig(com.yahoo.config.ConfigInstance.Builder, String)}. The default values used will be those of the config
 * types in the model.
 *
 * @param clazz The type of config
 * @param configId The config id
 * @return A config instance of the given type
 */
public <CONFIGTYPE extends ConfigInstance> CONFIGTYPE getConfig(Class<CONFIGTYPE> clazz, String configId) {
    try {
        ConfigInstance.Builder builder = newBuilder(clazz);
        getConfig(builder, configId);
        return newConfigInstance(clazz, builder);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) SAXException(org.xml.sax.SAXException) ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) IOException(java.io.IOException) Builder(com.yahoo.config.ConfigInstance.Builder) ConfigInstance(com.yahoo.config.ConfigInstance)

Example 4 with Builder

use of com.yahoo.config.ConfigInstance.Builder in project vespa by vespa-engine.

the class VespaModel method createBuilder.

public ConfigInstance.Builder createBuilder(ConfigDefinitionKey key, ConfigDefinition targetDef) {
    String className = createClassName(key.getName());
    Class<?> clazz;
    final String fullClassName = InstanceResolver.packageName(key) + "." + className;
    final String builderName = fullClassName + "$Builder";
    final String producerName = fullClassName + "$Producer";
    ClassLoader classLoader = getConfigClassLoader(producerName);
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
        if (logDebug()) {
            log.log(LogLevel.DEBUG, "No producer found to get classloader from for " + fullClassName + ". Using default");
        }
    }
    try {
        clazz = classLoader.loadClass(builderName);
    } catch (ClassNotFoundException e) {
        if (logDebug()) {
            log.log(LogLevel.DEBUG, "Tried to load " + builderName + ", not found, trying with generic builder");
        }
        // return compiler.compile(targetDef.generateClass()).newInstance();
        return new GenericConfig.GenericConfigBuilder(key, new ConfigPayloadBuilder());
    }
    Object i;
    try {
        i = clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ConfigurationRuntimeException(e);
    }
    if (!(i instanceof ConfigInstance.Builder)) {
        throw new ConfigurationRuntimeException(fullClassName + " is not a ConfigInstance.Builder, can not produce config for the name '" + key.getName() + "'.");
    }
    return (ConfigInstance.Builder) i;
}
Also used : ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) VespaDomBuilder(com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder) Builder(com.yahoo.config.ConfigInstance.Builder) ConfigPayloadBuilder(com.yahoo.vespa.config.ConfigPayloadBuilder) ConfigBuilder(com.yahoo.config.ConfigBuilder) VespaModelBuilder(com.yahoo.vespa.model.builder.VespaModelBuilder) GenericConfig(com.yahoo.vespa.config.GenericConfig) ConfigPayloadBuilder(com.yahoo.vespa.config.ConfigPayloadBuilder)

Aggregations

Builder (com.yahoo.config.ConfigInstance.Builder)4 ConfigurationRuntimeException (com.yahoo.config.ConfigurationRuntimeException)3 ConfigBuilder (com.yahoo.config.ConfigBuilder)2 ConfigInstance (com.yahoo.config.ConfigInstance)2 ConfigPayloadBuilder (com.yahoo.vespa.config.ConfigPayloadBuilder)2 VespaModelBuilder (com.yahoo.vespa.model.builder.VespaModelBuilder)2 VespaDomBuilder (com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder)2 IOException (java.io.IOException)2 SAXException (org.xml.sax.SAXException)2 GenericConfig (com.yahoo.vespa.config.GenericConfig)1 Method (java.lang.reflect.Method)1