All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javabuilders.handler.GlobalVariablePropertyHandler Maven / Gradle / Ivy

The newest version!
package org.javabuilders.handler;

import org.apache.commons.beanutils.PropertyUtils;
import org.javabuilders.BuildException;
import org.javabuilders.BuildProcess;
import org.javabuilders.BuilderConfig;
import org.javabuilders.Node;

/**
 * Generic handler for those cases where the property value is defined as a reference to a
 * global variable, instead of a value in the YAML file
 * @author Jacek Furmankiewicz
 *
 */
public class GlobalVariablePropertyHandler extends AbstractPropertyHandler {

	private static final GlobalVariablePropertyHandler instance = new GlobalVariablePropertyHandler();
	
	/**
	 * @return Singleton
	 */
	public static GlobalVariablePropertyHandler getInstance() {return instance;}
	
	private GlobalVariablePropertyHandler() {}
	
	/* (non-Javadoc)
	 * @see org.javabuilders.handler.IPropertyHandler#handle(org.javabuilders.BuilderConfig, org.javabuilders.BuildProcess, org.javabuilders.Node, java.lang.String)
	 */
	public void handle(BuilderConfig config, BuildProcess process, Node node,
			String key) throws BuildException {
		
		Object main = node.getMainObject();
		String name = node.getStringProperty(key);
		
		try {
			Class type = PropertyUtils.getPropertyDescriptor(main, key).getPropertyType();
			Object value = config.getGlobalVariable(name, type);
			PropertyUtils.setProperty(main, key, value);
		} catch (BuildException ex) {
			throw ex;
		} catch (Exception e) {
			throw new BuildException("Unable to access or set property \"{0}\" : {1}", key, e);
		}
	}

	/* (non-Javadoc)
	 * @see org.javabuilders.IApplicable#getApplicableClass()
	 */
	public Class getApplicableClass() {
		return Object.class;
	}

}