KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > functions > SetFunction


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.functions;
11
12 import java.util.Arrays JavaDoc;
13 import java.lang.reflect.*;
14
15 import org.mmbase.util.logging.*;
16
17 /**
18  * A SetFunction is a {@link Function} which wraps precisely one method of a class. It is used as one function of a 'set' of functions.
19  *
20  * @author Michiel Meeuwissen
21  * @author Daniel Ockeloen
22  * @author Pierre van Rooden
23  * @version $Id: SetFunction.java,v 1.13.2.1 2006/10/26 11:41:56 michiel Exp $
24  * @since MMBase-1.8
25  * @see FunctionSets
26  */

27 class SetFunction extends AbstractFunction {
28     private static final Logger log = Logging.getLoggerInstance(SetFunction.class);
29
30     /**
31      * If type is 'class' the method must be static, or if it is not static, there will ie instantiated <em>one</em> object.
32      */

33     public static final int TYPE_CLASS = 1;
34
35     /**
36      * If type is 'class' the method must not be static, and on every call to getFunctionValue, a new object is instantiated.
37      */

38     public static final int TYPE_INSTANCE = 2;
39
40     private Method functionMethod = null;
41     private Object JavaDoc functionInstance = null;
42     private int type = TYPE_CLASS;
43
44     public SetFunction(String JavaDoc name, Parameter[] def, ReturnType returnType, String JavaDoc className, String JavaDoc methodName) {
45         super(name, def, returnType);
46         initialize(className, methodName);
47     }
48     public SetFunction(String JavaDoc name, Parameter[] def ,String JavaDoc className, String JavaDoc methodName) {
49         super(name, def, null);
50         initialize(className, methodName);
51     }
52
53     /**
54      */

55     public Object JavaDoc getFunctionValue(Parameters parameters) {
56         parameters.checkRequiredParameters();
57         try {
58             return functionMethod.invoke(getInstance(), parameters.toArray());
59         } catch (IllegalAccessException JavaDoc iae) {
60             log.error("Function call failed (method not available) : " + name +", method: " + functionMethod +
61                        ", instance: " + getInstance() +", parameters: " + parameters);
62             return null;
63         } catch (InvocationTargetException ite) {
64             Throwable JavaDoc te = ite.getTargetException();
65             if (te instanceof RuntimeException JavaDoc) {
66                 throw (RuntimeException JavaDoc) te;
67             } else {
68                 throw new RuntimeException JavaDoc(te); // throw the actual exception that occurred
69
}
70         } catch (IllegalArgumentException JavaDoc iae) {
71             String JavaDoc mes =
72                 "Function call failed (method not available) : " + name +", method: " + functionMethod +
73                 ", instance: " + getInstance() +", parameters: " + parameters;
74             throw new RuntimeException JavaDoc(mes, iae);
75
76         }
77     }
78
79     public void setType(String JavaDoc t) {
80         if (t.equalsIgnoreCase("instance")) {
81             type = TYPE_INSTANCE;
82         } else {
83             type = TYPE_CLASS;
84         }
85     }
86
87
88     protected Object JavaDoc getInstance() {
89         if (functionInstance != null || type == TYPE_CLASS) return functionInstance;
90         try {
91             return functionMethod.getDeclaringClass().newInstance();
92         } catch(Exception JavaDoc e) {
93             throw new RuntimeException JavaDoc("Can't create an function instance : " + functionMethod.getDeclaringClass().getName(), e);
94         }
95     }
96
97     /**
98      * Initializes the function by creating an instance of the function class, and
99      * locating the method to call.
100      */

101     private void initialize(String JavaDoc className, String JavaDoc methodName) {
102         Class JavaDoc functionClass;
103         try {
104             functionClass = Class.forName(className);
105         } catch(Exception JavaDoc e) {
106             throw new RuntimeException JavaDoc("Can't create an application function class : " + className + " " + e.getMessage(), e);
107         }
108         try {
109             functionMethod = functionClass.getMethod(methodName, createParameters().toClassArray());
110         } catch(NoSuchMethodException JavaDoc e) {
111             throw new RuntimeException JavaDoc("Function method not found : " + className + "." + methodName + "(" + Arrays.asList(getParameterDefinition()) +")", e);
112         }
113         if (Modifier.isStatic(functionMethod.getModifiers())) {
114             functionInstance = null;
115         } else {
116             if (type != TYPE_INSTANCE) {
117                 try {
118                     functionInstance = functionMethod.getDeclaringClass().newInstance();
119                 } catch(Exception JavaDoc e) {
120                     throw new RuntimeException JavaDoc("Can't create an function instance : " + functionMethod.getDeclaringClass().getName(), e);
121                 }
122             }
123         }
124         if (returnType == null) returnType = new ReturnType(functionMethod.getReturnType(), functionMethod.getReturnType().getClass().getName());
125
126     String JavaDoc returni = functionMethod.getReturnType().getName();
127     String JavaDoc returnx = returnType.getDataType().getTypeAsClass().getName();
128     if (returni.equals("boolean")) { returni = "java.lang.Boolean"; }
129         if (!returni.equals(returnx)) {
130             log.warn("Return value of function " + className + "." + methodName + "(" + returni + ") does not match method return type as specified in XML: (" + returnx + ")");
131         }
132     }
133 }
134
Popular Tags