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

Quick Search    Search Deep

Source code: org/mentawai/core/ApplicationManager.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  import java.io.*;
23  
24  import org.mentawai.i18n.*;
25  import org.mentawai.list.*;
26  
27  /**
28   * The central abstract base manager which controls actions, filters, locales and data lists.
29   * You can use this class to register actions and filters through the loadActions() method.
30   * You can use this class to specify supported locales through the loadLocales() method.
31   * You can use this class to manage the data list loading process.
32   * You can use this class to initialize anything for your web application.
33   * 
34   * @author Sergio Oliveira
35   */
36  public abstract class ApplicationManager {
37      
38      private static String REALPATH;
39    
40    private Map actions = new HashMap();
41      private Map innerActions = new HashMap();
42    private List globalFilters = new LinkedList();
43      private List globalFiltersLast = new LinkedList();
44    private Map globalConsequences = new HashMap();
45      
46      public static void setRealPath(String realpath) {
47          REALPATH = realpath;
48      }
49      
50      /**
51       * Returns this web application's real path.
52       * For example: c:\program files\tomcat\webapps\myapplication
53       *
54       * @return The real path
55       */
56      public static String getRealPath() {
57          return REALPATH;
58      }
59       
60    /**
61     * Register an ActionConfig for the Mentawai controller.
62     * 
63     * @param ac The ActionConfig to register
64       * @throws IllegalStateException if you try to add an action config with no name (internal action config)
65     */
66    public void addActionConfig(ActionConfig ac) {
67          if (ac.getName() == null) throw new IllegalStateException("Cannot add an action config without a name!");
68          String innerAction = ac.getInnerAction();
69          if (innerAction == null) {
70          actions.put(ac.getName(), ac);
71          } else {
72              Map map = (Map) innerActions.get(ac.getName());
73              if (map == null) {
74                  map = new HashMap();
75                  innerActions.put(ac.getName(), map);
76              }
77              map.put(innerAction, ac);
78          }
79    }
80      
81      /**
82       * Override this method to do any initialization for your web application.
83       *
84       * @deprecated Use init(Context application) instead.
85       */
86      public void init() { }
87      
88      /**
89       * Override this method to do any initialization for your web application.
90       *
91       * @param application The application context of your web application.
92       * @since 1.1
93       */
94      public void init(Context application) { 
95          init(); // for deprecation...
96      }
97      
98    
99    /**
100    * Override this method to register actions and filters in this application manager.
101    */
102   public void loadActions() { }
103   
104   /**
105    * Override this method to specify the supported locales for your application.
106    */
107   public void loadLocales() {
108     LocaleManager.add(LocaleManager.DEFAULT_LOCALE);
109   }
110 
111   /**
112    * Override this method to control the data list loading process.
113    */
114   public void loadLists() throws IOException {
115     ListManager.init();
116   }
117   
118   /**
119    * Gets the ActionConfig with the given name or alias.
120    * 
121    * @param name The name of the ActionConfig
122    * @return The ActionConfig associated with the given name
123    */
124   public ActionConfig getActionConfig(String name) {
125     return (ActionConfig) actions.get(name);
126   }
127     
128   /**
129    * Gets the Inner ActionConfig with the given name and inner action.
130    * 
131    * @param name The name of the ActionConfig
132      * @param innerAction The inner action of the ActionConfig.
133    * @return The Inner ActionConfig associated with the given name and inner action.
134    */
135   public ActionConfig getActionConfig(String name, String innerAction) {
136         Map map = (Map) innerActions.get(name);
137         if (map != null) {
138             return (ActionConfig) map.get(innerAction);
139         }
140         return null;
141   }    
142   
143   /**
144    * Register a filter for all actions in this application manager.
145    * The filters registered with this method will be executed <i>before</i>
146      * the specific action filters.
147      *
148    * @param filter The filter to register as a global filter.
149    */
150   public void addGlobalFilter(Filter filter) {
151     addGlobalFilter(filter, false);
152   }
153     
154   /**
155    * Register a list of filters for all actions in this application manager.
156    * The filters registered with this method will be executed <i>before</i>
157      * the specific action filters.
158      *
159    * @param filters A list of filters.
160      * @since 1.1.1
161    */
162   public void addGlobalFilter(List filters) {
163     addGlobalFilter(filters, false);
164   }    
165     
166   /**
167    * Register a filter for all actions in this application manager.
168    * 
169    * @param filter The filter to register as a global filter.
170      * @param last true if you want this filter to be executed <i>after</i> the specific action filters.
171      * @since 1.1.1
172    */
173   public void addGlobalFilter(Filter filter, boolean last) {
174         if (last) {
175             globalFiltersLast.add(filter);
176         } else {
177         globalFilters.add(filter);
178         }
179   }    
180     
181   /**
182    * Register a list of filters for all actions in this application manager.
183    * 
184    * @param filters A list of filters.
185      * @param last true if you want these filters to be executed <i>after</i> the specific action filters.
186      * @since 1.1.1
187    */
188   public void addGlobalFilter(List filters, boolean last) {
189         Iterator iter = filters.iterator();
190         while(iter.hasNext()) {
191             Filter f = (Filter) iter.next();
192             addGlobalFilter(f, last);
193         }
194   }        
195   
196   /**
197    * Register a consequence for all actions in this application manager.
198    * A global consequence has precedence over action consequences.
199    * 
200    * @param result The result for what a global consequence will be registered
201    * @param c The consequence to register as a global consequence
202    */
203   public void addGlobalConsequence(String result, Consequence c) {
204     globalConsequences.put(result, c);
205   }
206   
207   /**
208    * Gets the global filters registered in this application manager.
209    * 
210      * @param last true if you want the global filters registered to be executed <i>after</i> the specific action filters.
211    * @return A java.util.List with all the filters registered in this application manager.
212      * @since 1.1.1
213    */
214   public List getGlobalFilters(boolean last) {
215         if (last) return globalFiltersLast;
216         return globalFilters;
217   }
218     
219   /**
220    * Gets all the global filters registered in this application manager.
221      * Note that it will sum up in a list the filters executed <i>before</i> and <i>after</i> the specific action filters.
222    * 
223    * @return A java.util.List with all the filters registered in this application manager.
224    */
225   public List getGlobalFilters() {
226         List list = new LinkedList();
227         list.addAll(getGlobalFilters(false));
228         list.addAll(getGlobalFilters(true));
229         return list;
230   }    
231   
232   /**
233    * Gets a global consequence associated with the result.
234    * 
235    * @param result The result for what to get a global consequence.
236    * @return A global consequence for the result.
237    */
238   public Consequence getGlobalConsequence(String result) {
239     return (Consequence) globalConsequences.get(result);
240   }
241     
242     /*
243      * This is useful for filter destroying in the Controller.
244      */
245     Set getAllFilters() {
246         Set filters = new HashSet();
247         filters.addAll(globalFilters);
248         filters.addAll(globalFiltersLast);
249         
250         Iterator iter = actions.values().iterator();
251         while(iter.hasNext()) {
252             ActionConfig ac = (ActionConfig) iter.next();
253             filters.addAll(ac.getFilters());
254         }
255         
256         iter = innerActions.values().iterator();
257         while(iter.hasNext()) {
258             ActionConfig ac = (ActionConfig) iter.next();
259             filters.addAll(ac.getFilters());
260         }
261         
262         return filters;
263     }
264         
265 }
266 
267     
268