
fitnesse.components.ComponentFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fitnesse Show documentation
Show all versions of fitnesse Show documentation
The fully integrated standalone wiki, and acceptance testing framework.
The newest version!
// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.components;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import fitnesse.ConfigurationParameter;
/**
* Create components for FitNesse.
*
* Components have one of the following constructors:
*
* Component(fitnesse.componentsComponentFactory componentFactory)
* Component(java.util.Properties properties)
* Component()
*
*
* Components requested by parameter/type/name are instantiated once for the application.
*/
public class ComponentFactory {
private final Properties properties;
private final ClassLoader classLoader;
private Map components;
public ComponentFactory(Properties properties) {
this(properties, ClassLoader.getSystemClassLoader());
}
public ComponentFactory(Properties properties, ClassLoader classLoader) {
this.properties = properties;
this.classLoader = classLoader;
this.components = new HashMap<>();
}
public T createComponent(String componentType, Class defaultComponent) throws ComponentInstantiationException {
if (components.containsKey(componentType)) {
return (T) components.get(componentType);
}
String componentClassName = properties.getProperty(componentType);
Class> componentClass;
try {
if (componentClassName != null)
componentClass = lookupComponentClass(componentClassName);
else
componentClass = defaultComponent;
} catch (Exception e) {
throw new ComponentInstantiationException("Unable to look up component for type '" + componentType + "' with classname '" + componentClassName + "'", e);
}
if (componentClass != null) {
T component = (T) createComponent(componentClass);
components.put(componentType, component);
return component;
}
return null;
}
public T createComponent(Class componentClass) throws ComponentInstantiationException {
try {
try {
Constructor> constructor = componentClass.getConstructor(ComponentFactory.class);
return (T) constructor.newInstance(this);
} catch (NoSuchMethodException e) {
// no problem, we can deal with some other constructors as well
}
try {
Constructor> constructor = componentClass.getConstructor(Properties.class);
return (T) constructor.newInstance(properties);
} catch (NoSuchMethodException e) {
Constructor> constructor = componentClass.getConstructor();
return (T) constructor.newInstance();
}
} catch (Exception e) {
throw new ComponentInstantiationException("Unable to instantiate component for type " + componentClass.getName(), e);
}
}
public T createComponentForClassName(String className) {
Class clazz;
try {
clazz = lookupComponentClass(className);
} catch (ClassNotFoundException e) {
throw new ComponentInstantiationException("Unable to load class named " + className, e);
}
return createComponent(clazz);
}
public Class lookupComponentClass(String className) throws ClassNotFoundException {
return (Class) classLoader.loadClass(className);
}
public T createComponent(ConfigurationParameter componentType, Class defaultComponent) {
return createComponent(componentType.getKey(), defaultComponent);
}
public T createComponent(ConfigurationParameter componentType) {
return createComponent(componentType, null);
}
public String getProperty(String key) {
return properties.getProperty(key);
}
public String getProperty(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy