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