Search in sources :

Example 1 with ConfigInstance

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

the class ComponentClass method createComponent.

/**
 * Create an instance of this ComponentClass with the given configId. The configs needed by the component
 * must exist in the provided set of {@link com.yahoo.config.ConfigInstance}s.
 *
 * @param id                  The id of the component to create, never null.
 * @param availableConfigs    The set of available config instances.
 * @param configId            The config ID of the component, nullable.
 * @return A new instance of the class represented by this ComponentClass.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public T createComponent(ComponentId id, Map<ConfigKey, ConfigInstance> availableConfigs, String configId) {
    if (configId == null) {
        configId = System.getProperty("config.id");
    }
    boolean hasId = false;
    List<Object> params = new LinkedList<>();
    for (Class cc : constructor.parameters) {
        if (cc.equals(ComponentId.class)) {
            params.add(id);
            hasId = true;
        } else if (cc.getSuperclass().equals(ConfigInstance.class)) {
            ConfigKey key = new ConfigKey(cc, configId);
            if ((availableConfigs == null) || !availableConfigs.containsKey(key)) {
                throw new IllegalStateException("Could not resolve config instance '" + key + "' required to instantiate " + clazz);
            }
            params.add(availableConfigs.get(key));
        }
    }
    T component = construct(params.toArray());
    if (hasId && component.hasInitializedId() && !id.equals(component.getId())) {
        log.warning("Component with id '" + id + "' tried to set illegal component id: '" + component.getId() + "', or the component takes ComponentId as a constructor arg without calling super(id).");
    }
    // Enforce correct id - see bug #4036397
    component.initId(id);
    return component;
}
Also used : ConfigKey(com.yahoo.vespa.config.ConfigKey) ConfigInstance(com.yahoo.config.ConfigInstance)

Example 2 with ConfigInstance

use of com.yahoo.config.ConfigInstance 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 3 with ConfigInstance

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

the class InstanceResolver method getConfigClass.

/**
 * Returns a {@link ConfigInstance} of right type for given key using reflection
 *
 * @param  cKey a ConfigKey
 * @return a {@link ConfigInstance} or null if not available in classpath
 */
@SuppressWarnings("unchecked")
private static Class<? extends ConfigInstance> getConfigClass(ConfigDefinitionKey cKey, ClassLoader instanceLoader) {
    String className = createClassName(cKey.getName());
    String fullClassName = packageName(cKey) + "." + className;
    Class<?> clazz;
    try {
        clazz = instanceLoader != null ? instanceLoader.loadClass(fullClassName) : Class.forName(fullClassName);
    } catch (ClassNotFoundException e) {
        throw new ConfigurationRuntimeException("Could not find config class for key " + cKey, e);
    }
    if (!ConfigInstance.class.isAssignableFrom(clazz)) {
        throw new ConfigurationRuntimeException(fullClassName + " is not a ConfigInstance subclass, can not produce config for " + cKey);
    }
    return (Class<? extends ConfigInstance>) clazz;
}
Also used : ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) ConfigInstance(com.yahoo.config.ConfigInstance)

Example 4 with ConfigInstance

use of com.yahoo.config.ConfigInstance 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 5 with ConfigInstance

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

the class VespaModel method getConfigFromBuilder.

private ConfigPayload getConfigFromBuilder(ConfigKey configKey, ConfigBuilder builder, InnerCNode targetDef) {
    try {
        ConfigInstance instance = InstanceResolver.resolveToInstance(configKey, builder, targetDef);
        log.log(LogLevel.DEBUG, () -> "getConfigFromBuilder for " + configKey + ",instance=" + instance);
        return ConfigPayload.fromInstance(instance);
    } catch (ConfigurationRuntimeException e) {
        // This can happen in cases where services ask for config that no longer exist before they have been able
        // to reconfigure themselves. This happens for instance whenever jdisc reconfigures itself until
        // ticket 6599572 is fixed. When that happens, consider propagating a full error rather than empty payload
        // back to the client.
        log.log(LogLevel.INFO, "Error resolving instance for key '" + configKey + "', returning empty config: " + Exceptions.toMessageString(e));
        return ConfigPayload.fromBuilder(new ConfigPayloadBuilder());
    }
}
Also used : ConfigurationRuntimeException(com.yahoo.config.ConfigurationRuntimeException) ConfigPayloadBuilder(com.yahoo.vespa.config.ConfigPayloadBuilder) ConfigInstance(com.yahoo.config.ConfigInstance)

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