001package com.ganteater.ae;
002
003import java.io.InputStream;
004import java.lang.reflect.InvocationTargetException;
005import java.lang.reflect.Method;
006import java.net.URL;
007import java.util.Properties;
008
009import org.apache.commons.lang.ClassUtils;
010import org.apache.commons.lang.StringUtils;
011
012public class OperationHolder {
013
014        private Method method;
015        private Class<?> operationClass;
016        private Properties prop = new Properties();
017
018        public OperationHolder(Class<?> operationClass, Method method)
019                        throws InstantiationException, IllegalAccessException {
020                this.method = method;
021                this.operationClass = operationClass;
022                try {
023                        URL resource = operationClass.getResource(ClassUtils.getShortClassName(operationClass) + ".properties");
024                        InputStream openStream = resource.openStream();
025                        prop.load(openStream);
026                } catch (Exception e) {
027                        //
028                }
029        }
030
031        public Method getMethod() {
032                return method;
033        }
034
035        public void setMethod(Method method) {
036                this.method = method;
037        }
038
039        public Object invoke(Object instance, Object[] args)
040                        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
041                return method.invoke(instance, args);
042        }
043
044        public Object newInstance() throws InstantiationException, IllegalAccessException {
045                return operationClass.newInstance();
046        }
047
048        public String getParameterName(int i) {
049                String name = prop.getProperty(method.getName() + ".param[" + i + "]");
050                return StringUtils.defaultIfBlank(name, "arg" + (i + 1));
051        }
052
053        public String getReturnDescription() {
054                String description = prop.getProperty(method.getName() + ".return");
055                return StringUtils.defaultIfBlank(description, "");
056        }
057
058        public String getDescription() {
059                String description = prop.getProperty(method.getName() + ".description", "");
060                description = StringUtils.trim(StringUtils.substringBefore(description, "."));
061                description = StringUtils.trim(StringUtils.substringBefore(description, "<p"));
062                return description;
063        }
064
065        public Class<?> getOperationClass() {
066                return operationClass;
067        }
068
069}