KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > container > struts > PicoRequestProcessorImplementation


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * ---------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * ---------------------------------------------------------
30  * $Log: PicoRequestProcessorImplementation.java,v $
31  * Revision 1.1 2005/04/11 10:18:56 colinmacleod
32  * Added tiles support to request processor.
33  * Updated for new PicoContianerFactory singleton.
34  *
35  * ---------------------------------------------------------
36  */

37 package com.ivata.groupware.container.struts;
38
39 import java.io.IOException JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import javax.servlet.ServletContext JavaDoc;
43 import javax.servlet.ServletException JavaDoc;
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import javax.servlet.http.HttpSession JavaDoc;
47
48 import org.apache.log4j.Logger;
49 import org.apache.struts.action.Action;
50 import org.apache.struts.action.ActionForm;
51 import org.apache.struts.action.ActionMapping;
52 import org.apache.struts.action.ActionServlet;
53 import org.apache.struts.config.FormBeanConfig;
54 import org.apache.struts.config.ModuleConfig;
55 import org.picocontainer.PicoContainer;
56
57 import com.ivata.groupware.admin.security.Security;
58 import com.ivata.groupware.admin.security.server.SecuritySession;
59 import com.ivata.groupware.container.PicoContainerFactory;
60 import com.ivata.mask.Mask;
61 import com.ivata.mask.MaskFactory;
62 import com.ivata.mask.field.FieldValueConvertorFactory;
63 import com.ivata.mask.persistence.PersistenceManager;
64 import com.ivata.mask.persistence.PersistenceSession;
65 import com.ivata.mask.util.StringHandling;
66 import com.ivata.mask.util.SystemException;
67 import com.ivata.mask.valueobject.ValueObject;
68 import com.ivata.mask.web.browser.Browser;
69 import com.ivata.mask.web.field.DefaultFieldWriterFactory;
70 import com.ivata.mask.web.field.FieldWriterFactory;
71 import com.ivata.mask.web.struts.InputMaskForm;
72 import com.ivata.mask.web.struts.MaskRequestProcessorImplementation;
73
74
75 /**
76  * <p>
77  * Overridden to extend the create action and create action form methods.
78  * </p>
79  *
80  * @since ivata groupware 0.10 (11-Mar-2005)
81  * @author Colin MacLeod <colin.macleod@ivata.com>
82  * @version $Revision: 1.1 $
83  */

84
85 public class PicoRequestProcessorImplementation extends
86         MaskRequestProcessorImplementation {
87     /**
88      * Logger for this class.
89      */

90     private static final Logger logger = Logger
91             .getLogger(PicoRequestProcessorImplementation.class);
92
93     /**
94      * <p>
95      * Specifies the path to the mask config file, relative to the webapp root.
96      * </p>
97      */

98     public static final String JavaDoc MASKS_FILE_NAME = "/WEB-INF/ivataMasks.xml";
99     private Security security;
100     /**
101      * <p>
102      * Get a valid persistence manager for the super class constructor.
103      * </p>
104      *
105      * @return valid hibernate persistence manager from the default pico
106      * container.
107      */

108     static PersistenceManager getPersistenceManager() throws SystemException {
109         PicoContainer container = PicoContainerFactory.getInstance()
110             .getGlobalContainer();
111         PersistenceManager persistenceManager =
112             (PersistenceManager) container
113                 .getComponentInstance(PersistenceManager.class);
114         assert(persistenceManager != null);
115         return persistenceManager;
116     }
117     /**
118      * <p>
119      * Get a request processor implementation.
120      * </p>
121      *
122      * @return valid hibernate persistence manager from the default pico
123      * container.
124      */

125     static MaskRequestProcessorImplementation
126             getRequestProcessorImplementation() {
127         try {
128             MaskRequestProcessorImplementation implementation =
129                 (MaskRequestProcessorImplementation)
130                 PicoContainerFactory.getInstance().instantiateOrOverride(
131                         PicoRequestProcessorImplementation.class);
132             assert(implementation != null);
133             return implementation;
134         } catch (Exception JavaDoc e) {
135             logger.error("Cannot intantiate pico request processor "
136                     + "implementation", e);
137             throw new RuntimeException JavaDoc(e);
138         }
139     }
140     /**
141      * <p>
142      * Initializes the mask factory, the value object locator and the the
143      * standard field value convertors.
144      * </p>
145      *
146      * @param maskFactoryParam
147      * needed to access the masks and groups of masks.
148      * @param persistenceManagerParam
149      * used to locate the value objects by their shared base class.
150      */

151     public PicoRequestProcessorImplementation(final Security security,
152             final MaskFactory maskFactoryParam,
153             final PersistenceManager persistenceManagerParam,
154             final FieldValueConvertorFactory fieldValueConvertorFactory) {
155         super (maskFactoryParam, persistenceManagerParam,
156                 fieldValueConvertorFactory);
157         this.security = security;
158     }
159     /**
160      * <p>
161      * Create an action. This method re-implemented to create an action in a
162      * <strong>PicoContainer</strong> friendly way.
163      * </p>
164      *
165      * @param classNameParam full path and name of the action class to be created.
166      * @param request request for which we are creating an action.
167      * @param response response we are sending.
168      * @param mapping <strong>Struts</strong> mapping.
169      * @return newly created action, or <code>null</code> if we can't create one.
170      * @throws IOException if the action cannot be created.
171      */

172     protected Action createAction(final
173            String JavaDoc classNameParam,
174            final HttpServletRequest JavaDoc request,
175            final HttpServletResponse JavaDoc response,
176            final ActionMapping mapping,
177            final Map JavaDoc actions,
178            final ActionServlet servlet)
179            throws IOException JavaDoc, SystemException {
180         String JavaDoc className = classNameParam;
181         // Acquire the Action instance we will be using (if there is one)
182
Action instance = null;
183
184         // Return any existing Action instance of this class
185
instance = (Action) actions.get(className);
186
187         if (instance != null) {
188             return (instance);
189         }
190
191         // initialize the mask factory, if necessary
192
MaskFactory maskFactory = PicoContainerFactory.getInstance()
193             .getMaskFactory();
194         synchronized (maskFactory) {
195             // if the mask factory is not configured, create the field writer
196
// and initialize the mask factory...
197
if (!maskFactory.isConfigured()) {
198                 ServletContext JavaDoc context = servlet.getServletContext();
199                 FieldWriterFactory fieldWriterFactory = new
200                         DefaultFieldWriterFactory(getPersistenceManager(),
201                                 "/mask/find.action");
202                 context.setAttribute(FieldWriterFactory.APPLICATION_ATTRIBUTE,
203                         fieldWriterFactory);
204                 maskFactory.readConfiguration(context.getResourceAsStream(
205                         MASKS_FILE_NAME));
206             }
207         }
208
209         HttpSession JavaDoc session = request.getSession();
210         SecuritySession securitySession = (SecuritySession)
211             session.getAttribute("securitySession");
212         PicoContainer container;
213         // if there is a session available, use the container from that
214
if (securitySession != null) {
215             container = securitySession.getContainer();
216         } else {
217             // we timed out? login as guest
218
securitySession = security.loginGuest();
219             session.setAttribute("securitySession", securitySession);
220             container = securitySession.getContainer();
221             // for now, create a browser with no javascript support
222
Browser browser = new Browser(request.getHeader("User-Agent"), null);
223             session.setAttribute("browser", browser);
224         }
225         instance = (Action)
226             PicoContainerFactory.getInstance()
227                 .instantiateOrOverride(container, className);
228         if (instance == null) {
229             throw new SystemException("Could not instantiate this class");
230         }
231         actions.put(className, instance);
232
233         return instance;
234     }
235
236     /**
237      * <p>
238      * Get the appropriate action form for the parameters.
239      * </p>
240      *
241      * @param request the servlet request being processed.
242      * @param mapping the action mapping for the current request.
243      * @param moduleConfig the module configuration.
244      * @param servlet the action servlet.
245      * @return ActionForm valid action form instance, or <code>null</code> if
246      * none is appropriate.
247      */

248     protected ActionForm createActionForm(final
249             FormBeanConfig config,
250             final HttpServletRequest JavaDoc request,
251             final HttpServletResponse JavaDoc response,
252             final ActionMapping mapping)
253             throws SystemException {
254         ActionForm instance = null;
255         HttpSession JavaDoc session = request.getSession();
256         String JavaDoc attribute = mapping.getAttribute();
257         String JavaDoc name = mapping.getName();
258
259         if (attribute == null) {
260             return null;
261         }
262         if (config == null) {
263             return null;
264         }
265
266         // see if there is an existing instance we can reuse
267
if ("request".equals(mapping.getScope())) {
268             instance = (ActionForm) request.getAttribute(attribute);
269         } else {
270             instance = (ActionForm) session.getAttribute(attribute);
271         }
272
273         if (instance != null) {
274             return instance;
275         }
276
277         // if it gets down here, we need to create a new instance
278
SecuritySession securitySession = (SecuritySession)
279             session.getAttribute("securitySession");
280         // watch for session timeout
281
String JavaDoc type;
282         if (securitySession == null) {
283             return null;
284         }
285         PicoContainer container = securitySession.getContainer();
286         // use the value object class/idstring, mask and base class to make an
287
// input mask, if we have them - note you need either the id or the
288
// class of the value object - you don't need both
289
String JavaDoc valueObjectClassName = request.getParameter("valueObject.class.name");
290         String JavaDoc valueObjectIdString = request.getParameter("valueObject.idString");
291         String JavaDoc baseClassName = request.getParameter("baseClass.name");
292         String JavaDoc maskName = request.getParameter("mask.name");
293
294         if ("com.ivata.mask.web.struts.InputMaskForm"
295                 .equals(config.getType())) {
296             assert (!(StringHandling.isNullOrEmpty(valueObjectClassName)
297                         && StringHandling.isNullOrEmpty(valueObjectIdString)));
298             assert (!StringHandling.isNullOrEmpty(baseClassName));
299             assert (!StringHandling.isNullOrEmpty(maskName));
300
301             try {
302                 Class JavaDoc valueObjectClass = Class.forName(valueObjectClassName);
303                 Class JavaDoc baseClass = Class.forName(baseClassName);
304                 MaskFactory maskFactory = PicoContainerFactory.getInstance()
305                     .getMaskFactory();
306                 Mask mask = maskFactory.getMask(valueObjectClass, maskName);
307                 ValueObject valueObject;
308                 // if there is no id, make a new value object
309
if (StringHandling.isNullOrEmpty(valueObjectIdString)) {
310                     valueObject = (ValueObject)
311                         PicoContainerFactory.getInstance()
312                             .instantiateOrOverride(container,
313                                 valueObjectClassName);
314
315                     // otherwise use hibernate to find an existing instance
316
} else {
317                     PersistenceManager persistenceManager =
318                         getPersistenceManager();
319                     PersistenceSession persistenceSession =
320                         persistenceManager.openSession(securitySession);
321                     try {
322                         valueObject = persistenceManager.findByPrimaryKey(
323                                 persistenceSession, valueObjectClass,
324                                 valueObjectIdString);
325                     } finally {
326                         persistenceSession.close();
327                     }
328                 }
329                 instance = new InputMaskForm(valueObject, mask, baseClass);
330             } catch (ClassNotFoundException JavaDoc e1) {
331                 throw new SystemException(e1);
332             }
333         } else {
334             // this is for non-ivata-masks forms. they are constructed via
335
// pico directly
336
try {
337                 instance = (ActionForm)
338                     PicoContainerFactory.getInstance()
339                         .instantiateOrOverride(container,
340                             config.getType());
341             } catch (SystemException e) {
342                 return null;
343             }
344         }
345         return (instance);
346     }
347     /**
348      * Refer to {@link }.
349      *
350      * @param servletParam
351      * @param moduleConfigParam
352      * @throws javax.servlet.ServletException
353      * @see org.apache.struts.action.RequestProcessor#init(org.apache.struts.action.ActionServlet, org.apache.struts.config.ModuleConfig)
354      */

355     public void init(ActionServlet servletParam, ModuleConfig moduleConfigParam)
356             throws ServletException JavaDoc {
357     }
358
359 }
360
Popular Tags