Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mentawai/core/ActionConfig.java


1   /*
2    * Mentawai Web Framework http://mentawai.lohis.com.br/
3    * Copyright (C) 2005  Sergio Oliveira Jr. (sergio.oliveira.jr@gmail.com)
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package org.mentawai.core;
20  
21  import java.util.*;
22  
23  /**
24   * An ActionConfig links together an action implementation, an action name or alias, action results and action consequences.
25   * It makes it possible for an action implementation to be re-used in different situations with different names and consequences.
26   * 
27   * @author Sergio Oliveira
28   */
29  public class ActionConfig {
30    
31    private Class actionClass;
32    private String name = null;
33    private Map consequences = new HashMap();
34      private Map innerConsequences = new HashMap();
35      private List filters = new LinkedList();
36      private String innerAction = null;
37      
38    /**
39     * Creates an internal ActionConfig for the given action implementation.
40       * This action config does not have a name and cannot be called by the outside world.
41       * Trying to add an internal action config to the application manager will throw an exception.
42       * An internal action config may be useful for action chaining, in other words,
43       * you may want to chain an action with another action that is not available to the outside world.
44     * 
45     * @param klass The action implementation to use
46     */
47    public ActionConfig(Class klass) {
48      this.actionClass = klass;
49    }
50      
51  
52    /**
53     * Creates an ActionConfig with the given name for the given action implementation.
54     * 
55     * @param name The name or alias of this ActionConfig
56     * @param klass The action implementation to use
57     */
58    public ActionConfig(String name, Class klass) {
59      this.actionClass = klass;
60      this.name = cutSlash(name);
61    }
62      
63    /**
64     * Creates an ActionConfig with the given name for the given inner action implementation.
65       * Notice that this action config is specific to an inner action.
66     * 
67     * @param name The name or alias of this ActionConfig
68     * @param klass The action implementation to use
69       * @param innerAction The inner action to use
70     */
71    public ActionConfig(String name, Class klass, String innerAction) {
72      this.actionClass = klass;
73      this.name = cutSlash(name);
74          this.innerAction = innerAction;
75    }    
76    
77    /**
78     * Adds a consequence for the given result.
79     * An action must have a consequence for each of its possible results.
80     * 
81     * @param result A possible result of this ActionConfig
82     * @param c The consequence for this result
83       * @return this action config for method chaining. Ex: addConsequence().addConsequence();
84     */
85    public ActionConfig addConsequence(String result, Consequence c) {
86      consequences.put(result, c);
87          return this;
88    }
89      
90      private String cutSlash(String name) {
91          if (name.startsWith("/") && name.length() > 1) {
92              return name.substring(1, name.length());
93          }
94          return name;
95      }
96      
97    /**
98     * Adds a consequence for the given result of the given inner action.
99     * An inner action can have a consequence for each of its possible results.
100      * If you don't define consequences for an inner action, 
101      * the consequences of the main action (execute() method) is used instead.
102    * 
103    * @param result A possible result of this ActionConfig
104      * @param innerAction The inner action that can return this result.
105    * @param c The consequence for this result
106      * @return this action config for method chaining Ex: addConsequence().addConsequence();
107      * @throws IllegalStateException If this method is called for a action config specific to an inner action
108    */
109   public ActionConfig addConsequence(String result, String innerAction, Consequence c) {
110         if (this.innerAction != null) throw new IllegalStateException("Calling addConsequence(result,innerAction,c) is illegal for inner action configs!");
111         Map map = (Map) innerConsequences.get(innerAction);
112         if (map == null) {
113             map = new HashMap();
114             innerConsequences.put(innerAction, map);
115         }
116         map.put(result, c);
117         return this;
118   }
119     
120     /**
121      * Adds a filter for the action.
122      *
123      * @param filter The filter to add for this action.
124      * @return this action config for method chaining Ex: addConsequence().addFilter();
125      */
126     public ActionConfig addFilter(Filter filter) {
127         return addFilter(filter, null);
128     }
129     
130     /**
131      * Adds a filter for this inner action.
132      *
133      * @param filter The filter to add for this inner action.
134      * @param innerAction the inner action
135      * @return this action config for method chaining Ex: addConsequence().addFilter();
136      * @since 1.1.1
137      */
138     public ActionConfig addFilter(Filter filter, String innerAction) {
139         Object [] array = new Object[2];
140         array[0] = innerAction;
141         array[1] = filter;
142         filters.add(array);
143         return this;
144     }
145     
146     /**
147      * Adds a list of filter for the action.
148      *
149      * @param filters A list of filters.
150      * @return this action config for method chaining Ex: addConsequence().addFilter();
151      */
152     public ActionConfig addFilter(List filters) {
153         return addFilter(filters, null);
154     }    
155     
156     /**
157      * Adds a list of filter for the inner action.
158      *
159      * @param filters A list of filters
160      * @param innerAction the inner action
161      * @return this action config for method chaining Ex: addConsequence().addFilter();
162      * @since 1.1.1
163      */
164     public ActionConfig addFilter(List filters, String innerAction) {
165         Iterator iter = filters.iterator();
166         while(iter.hasNext()) {
167             Filter f = (Filter) iter.next();
168             addFilter(f, innerAction);
169         }
170         return this;
171     }        
172     
173     /**
174      * Returns the filters for this action.
175      *
176      * @return The filters for this action.
177      */
178     public List getFilters() {
179         List list = new ArrayList(filters.size());
180         Iterator iter = filters.iterator();
181         while(iter.hasNext()) {
182             Object [] array = (Object []) iter.next();
183             list.add(array[1]);
184         }
185         return list;
186     }
187     
188     /**
189      * Returns the filters for this inner action.
190      *
191      * @param innerAction the inner action.
192      * @return The filters for this action.
193      * @since 1.1.1
194      */
195     public List getFilters(String innerAction) {
196         List list = new ArrayList(filters.size());
197         Iterator iter = filters.iterator();
198         while(iter.hasNext()) {
199             Object [] array = (Object []) iter.next();
200             if (array[0] == null || array[0].equals(innerAction)) {
201                 list.add(array[1]);
202             }
203         }
204         return list;
205     }    
206   
207   /**
208    * Gets the name or alias of this ActionConfig.
209    * 
210    * @return The name or alias of this ActionConfig.
211    */
212   public String getName() { return name; }
213   
214     /**
215      * Gets the inner action that this action config represents.
216      *
217      * @return The inner action name that his action represents.
218      */
219     public String getInnerAction() {
220         return innerAction;
221     }
222     
223     void setInnerAction(String innerAction) {
224         this.innerAction = innerAction;
225     }
226   
227   /**
228    * Gets the consequence for the given result.
229    * 
230    * @param result The result for what to get the consequence
231    * @return The consequence associated with the result.
232    */
233   public Consequence getConsequence(String result) {
234     return (Consequence) consequences.get(result);
235   }
236     
237   /**
238    * Gets the consequence for the given result of the given inner action.
239    * 
240    * @param result The result for what to get the consequence
241      * @param innerAction The innerAction from where to get the consequence.
242    * @return The consequence associated with the result and the inner action.
243    */
244   public Consequence getConsequence(String result, String innerAction) {
245         Map map = (Map) innerConsequences.get(innerAction);
246         if (map != null) {
247             return (Consequence) map.get(result);
248         }
249     return null;
250   }
251 
252     /**
253      * Returns an action instance to be used with this request.
254      * Mentawai creates a new action instance for each request.
255      * You can extend ActionConfig and override this class to integrage Mentawai with other IoC containers.
256      * 
257      * @return The action instance to use for the request.
258      */
259     public Action getAction() {
260         try {
261             return (Action) actionClass.newInstance();
262         } catch(Exception e) {
263             e.printStackTrace();
264         }
265         return null;
266     }
267     
268     /**
269      * Returns the name of this ActionConfig.
270      * Ex: /HelloWorld, /customers/add, etc.
271      *
272      * @return The name of this action.
273      */
274     public String toString() {
275         return name;
276     }
277 }
278 
279     
280   
281   
282