View Javadoc

1   /***
2    * CremeAction.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   *
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2.1 of the License, or (at your option) any later version.
15   *
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   * Lesser General Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser General Public
22   * License along with this library; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   *
25   * contact information: dbregeon@sourceforge.net
26   */
27  package org.jcreme.sql;
28  
29  import java.lang.reflect.InvocationTargetException;
30  import java.lang.reflect.Method;
31  
32  /***
33   * This class enables to store a method call on an object. In particular it can
34   * be used to store a call that is to be performed in a part that has no other
35   * access to the subject object and has no knowledge (at compile time) of the
36   * method to call.
37   */
38  public class CremeAction {
39      /***
40       * The subject of the method to call.
41       */
42      private Object subject = null;
43  
44      /***
45       * The method to call on the subject.
46       */
47      private final Method method;
48  
49      /***
50       * The parameters to the method.
51       */
52      private Object parameters[] = new Object[0];
53  
54      /***
55       * The result from the call of the method on the subject with the
56       * parameters, once it has been done.
57       */
58      private Object result = null;
59  
60      /***
61       * Flag that signals whether the call was successfull or not (determines the
62       * validity of the result member).
63       */
64      private boolean runSuccessfull = false;
65  
66      /***
67       * Creates a new CremeAction
68       * 
69       * @param method
70       *            the method that will be called from this CremeAction.
71       * @throws IllegalArgumentException
72       *             if the method parameter is null.
73       */
74      public CremeAction(Method method) throws IllegalArgumentException {
75          if (method == null) {
76              throw new IllegalArgumentException("Null is not a valid method");
77          }
78          this.method = method;
79      }
80  
81      /***
82       * Enables to set the subject of the method call.
83       * 
84       * @param obj
85       *            the subject of the call.
86       */
87      public void setSubject(Object obj) {
88          this.subject = obj;
89      }
90  
91      /***
92       * Enables to set the parameters of the method call.
93       * 
94       * @param params
95       *            the parameters of the call.
96       */
97      public void setParameters(Object[] params) {
98          if (params != null) {
99              this.parameters = params;
100         } else {
101             this.parameters = new Object[0];
102         }
103     }
104 
105     /***
106      * This method makes the call of the method on the current subject with the
107      * current parameters.
108      * 
109      * @return true if the invocation was successfull, false otherwise.
110      * @throws InvocationTargetException
111      *             if the current subject is not valid for this call.
112      */
113     public boolean runAction() throws InvocationTargetException {
114         this.runSuccessfull = false;
115         try {
116             if (this.method != null) {
117                 this.result = this.method.invoke(this.subject, this.parameters);
118                 this.runSuccessfull = true;
119             }
120         } catch (IllegalAccessException e) {
121             e.printStackTrace();
122         } catch (IllegalArgumentException e) {
123             e.printStackTrace();
124         }
125         return this.runSuccessfull;
126     }
127 
128     /***
129      * Gives access to the result of the previous invocation.
130      * 
131      * @return the result of the previous invocation.
132      */
133     public Object getResult() {
134         return this.result;
135     }
136 
137     /***
138      * Gives access to the status of the previous invocation.
139      * 
140      * @return true if the previous invocation was successfull, false otherwise.
141      */
142     public boolean isSuccessfull() {
143         return this.runSuccessfull;
144     }
145 }