gate.creole.AbstractResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gate-core Show documentation
Show all versions of gate-core Show documentation
GATE - general achitecture for text engineering - is open source
software capable of solving almost any text processing problem. This
artifact enables you to embed the core GATE Embedded with its essential
dependencies. You will able to use the GATE Embedded API and load and
store GATE XML documents. This artifact is the perfect dependency for
CREOLE plugins or for applications that need to customize the GATE
dependencies due to confict with their own dependencies or for lower
footprint.
The newest version!
/*
* AbstractResource.java
*
* Copyright (c) 1995-2012, The University of Sheffield. See the file
* COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
*
* This file is part of GATE (see http://gate.ac.uk/), and is free
* software, licenced under the GNU Library General Public License,
* Version 2, June 1991 (in the distribution as file licence.html,
* and also available at http://gate.ac.uk/gate/licence.html).
*
* Hamish Cunningham, 15/Oct/2000
*
* $Id: AbstractResource.java 19662 2016-10-10 08:03:37Z markagreenwood $
*/
package gate.creole;
import java.beans.BeanInfo;
import java.beans.EventSetDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import gate.Factory;
import gate.FeatureMap;
import gate.Gate;
import gate.Resource;
import gate.util.AbstractFeatureBearer;
import gate.util.Err;
import gate.util.GateException;
import gate.util.Strings;
import gate.util.Tools;
/** A convenience implementation of Resource with some default code.
*/
abstract public class AbstractResource
extends AbstractFeatureBearer implements Resource
{
static final long serialVersionUID = -9196293927841163321L;
/** Initialise this resource, and return it. */
@Override
public Resource init() throws ResourceInstantiationException {
return this;
} // init()
/** Sets the name of this resource*/
@Override
public void setName(String name){
this.name = name;
}
/** Returns the name of this resource*/
@Override
public String getName(){
return name;
}
protected String name;
/**
* releases the memory allocated to this resource
*/
@Override
public void cleanup(){
}
//Parameters utility methods
/**
* Gets the value of a parameter for a resource.
* @param resource the resource from which the parameter value will be
* obtained
* @param paramaterName the name of the parameter
* @return the current value of the parameter
*/
public static Object getParameterValue(Resource resource,
String paramaterName)
throws ResourceInstantiationException{
// get the beaninfo for the resource bean, excluding data about Object
BeanInfo resBeanInf = null;
try {
resBeanInf = getBeanInfo(resource.getClass());
} catch(Exception e) {
throw new ResourceInstantiationException(
"Couldn't get bean info for resource " + resource.getClass().getName()
+ Strings.getNl() + "Introspector exception was: " + e
);
}
PropertyDescriptor[] properties = resBeanInf.getPropertyDescriptors();
//find the property we're interested on
if(properties == null){
throw new ResourceInstantiationException(
"Couldn't get properties info for resource " +
resource.getClass().getName());
}
boolean done = false;
int i = 0;
Object value = null;
while(!done && i < properties.length){
PropertyDescriptor prop = properties[i];
if(prop.getName().equals(paramaterName)){
Method getMethod = prop.getReadMethod();
if(getMethod == null){
throw new ResourceInstantiationException(
"Couldn't get read accessor method for parameter " + paramaterName +
" in " + resource.getClass().getName());
}
// call the get method with the parameter value
Object[] args = new Object[0];
try {
value = getMethod.invoke(resource, args);
} catch(Exception e) {
throw new ResourceInstantiationException(
"couldn't invoke get method: " + e
);
}
done = true;
}//if(prop.getName().equals(paramaterName))
i++;
}//while(!done && i < properties.length)
if(done) return value;
else throw new ResourceInstantiationException(
"Couldn't find parameter named " + paramaterName +
" in " + resource.getClass().getName());
}
/**
* Sets the value for a specified parameter for a resource.
*
* @param resource the resource for which the parameter value will be set
* @param paramaterName the name for the parameter
* @param parameterValue the value the parameter will receive
*/
public static void setParameterValue(Resource resource, BeanInfo resBeanInf,
String paramaterName,
Object parameterValue)
throws ResourceInstantiationException{
PropertyDescriptor[] properties = resBeanInf.getPropertyDescriptors();
//find the property we're interested on
if(properties == null){
throw new ResourceInstantiationException(
"Couldn't get properties info for resource " +
resource.getClass().getName());
}
boolean done = false;
int i = 0;
while(!done && i < properties.length){
PropertyDescriptor prop = properties[i];
if(prop.getName().equals(paramaterName)){
Method setMethod = prop.getWriteMethod();
if(setMethod == null){
throw new ResourceInstantiationException(
"Couldn't get write accessor method for parameter " +
paramaterName + " in " + resource.getClass().getName());
}
// convert the parameter to the right type eg String -> URL
if(parameterValue != null){
Class> propertyType = prop.getPropertyType();
Class> typeToCreate = propertyType;
if(Parameter.substituteClasses.containsKey(propertyType)) {
typeToCreate = Parameter.substituteClasses.get(propertyType);
}
Class> paramType = parameterValue.getClass();
if(!propertyType.isAssignableFrom(paramType)) {
try {
Constructor> mostSpecificConstructor =
Tools.getMostSpecificConstructor(typeToCreate, paramType);
parameterValue = mostSpecificConstructor
.newInstance( new Object[]{parameterValue} );
} catch(Exception e) {
//this didn't work; if the parameter value is String
//try to use the Parameter implementation for finding the
//value
if(String.class.isAssignableFrom(paramType)){
ResourceData rData = Gate.getCreoleRegister().
get(resource.getClass().getName());
ParameterList pList = rData.getParameterList();
Parameter param = null;
Iterator> disjIter = pList.getInitimeParameters().iterator();
while(param == null && disjIter.hasNext()){
Iterator paramIter = disjIter.next().iterator();
while(param == null && paramIter.hasNext()){
Parameter aParam = paramIter.next();
if(aParam.getName().equals(paramaterName)) param = aParam;
}
}
disjIter = pList.getRuntimeParameters().iterator();
while(param == null && disjIter.hasNext()){
Iterator paramIter = disjIter.next().iterator();
while(param == null && paramIter.hasNext()){
Parameter aParam = paramIter.next();
if(aParam.getName().equals(paramaterName)) param = aParam;
}
}
if(param != null){
try{
parameterValue = param.calculateValueFromString(
(String)parameterValue);
}catch(ParameterException pe){
throw new ResourceInstantiationException(pe);
}
}else{
// if this happens it means that the class has the specified parameter
// name does indeed correspond to a getter/setter pair on the class
// but there is no associated CREOLE metadata for that parameter name
throw new ResourceInstantiationException("Property " + paramaterName +
" of resource class " + resource.getClass().getName() +
" is not declared as a CREOLE parameter. Automatic" +
" conversion of string values to other types is only" +
" supported for declared parameters.");
}
}else{
throw new ResourceInstantiationException(
"Error converting " + parameterValue.getClass() +
" to " + propertyType + ": " + e.toString()
);
}
}
}
}//if(parameterValue != null)
// call the set method with the parameter value
Object[] args = new Object[1];
args[0] = parameterValue;
try {
setMethod.invoke(resource, args);
} catch(Exception e) {
e.printStackTrace(Err.getPrintWriter());
throw new ResourceInstantiationException(
"couldn't invoke set method for " + paramaterName +
" on " + resource.getClass().getName() + ": " + e);
}
done = true;
}//if(prop.getName().equals(paramaterName))
i++;
}//while(!done && i < properties.length)
if(!done) throw new ResourceInstantiationException(
"Couldn't find parameter named " + paramaterName +
" in " + resource.getClass().getName());
}//public void setParameterValue(String paramaterName, Object parameterValue)
/**
* Sets the values for more parameters for a resource in one step.
*
* @param parameters a feature map that has parameter names as keys and
* parameter values as values.
*/
public static void setParameterValues(Resource resource,
FeatureMap parameters)
throws ResourceInstantiationException{
// get the beaninfo for the resource bean, excluding data about Object
BeanInfo resBeanInf = null;
try {
resBeanInf = getBeanInfo(resource.getClass());
} catch(Exception e) {
throw new ResourceInstantiationException(
"Couldn't get bean info for resource " + resource.getClass().getName()
+ Strings.getNl() + "Introspector exception was: " + e
);
}
Iterator