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;
}
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);
}
}
}
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;
}
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);
}
}
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());
}
}
Aggregations