KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > components > DefaultFormManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.session.components;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.component.Component;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.cocoon.ProcessingException;
32 import org.apache.cocoon.components.ContextHelper;
33 import org.apache.cocoon.environment.ObjectModelHelper;
34 import org.apache.cocoon.environment.Request;
35 import org.apache.cocoon.environment.Session;
36 import org.apache.cocoon.webapps.session.ContextManager;
37 import org.apache.cocoon.webapps.session.FormManager;
38 import org.apache.cocoon.webapps.session.SessionConstants;
39 import org.apache.cocoon.webapps.session.SessionManager;
40 import org.apache.cocoon.webapps.session.context.SessionContext;
41 import org.w3c.dom.DocumentFragment JavaDoc;
42
43 /**
44  * Form handling
45  *
46  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
47  * @version CVS $Id: DefaultFormManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
48 */

49 public final class DefaultFormManager
50 extends AbstractLogEnabled
51 implements Serviceable, Component, FormManager, ThreadSafe, Contextualizable {
52
53     /** This session attribute is used to store the information for the inputxml tags */
54     private static final String JavaDoc ATTRIBUTE_INPUTXML_STORAGE = "org.apache.cocoon.webapps.session.InputXMLStorage";
55
56     /** The <code>ServiceManager</code> */
57     private ServiceManager manager;
58
59     /** The context */
60     private Context context;
61     
62     /**
63      * Get the context
64      */

65     private SessionContext getContext(String JavaDoc name)
66     throws ProcessingException {
67         ContextManager contextManager = null;
68         try {
69             contextManager = (ContextManager) this.manager.lookup(ContextManager.ROLE);
70             return contextManager.getContext( name );
71         } catch (ServiceException ce ) {
72             throw new ProcessingException("Unable to lookup context manager.", ce);
73         } finally {
74             this.manager.release(contextManager);
75         }
76     }
77     
78     private DocumentFragment JavaDoc getContextFragment(String JavaDoc context, String JavaDoc path)
79     throws ProcessingException {
80         SessionManager sessionManager = null;
81         try {
82             sessionManager = (SessionManager) this.manager.lookup(SessionManager.ROLE);
83             return sessionManager.getContextFragment( context, path );
84         } catch (ServiceException ce ) {
85             throw new ProcessingException("Unable to lookup session manager.", ce);
86         } finally {
87             this.manager.release(sessionManager);
88         }
89     }
90     
91     /**
92      * @see FormManager#registerInputField(String, String, String, String)
93      */

94     public DocumentFragment JavaDoc registerInputField(String JavaDoc contextName,
95                                                String JavaDoc path,
96                                                String JavaDoc name,
97                                                String JavaDoc formName)
98     throws ProcessingException {
99         // synchronized
100
if (this.getLogger().isDebugEnabled() ) {
101             this.getLogger().debug("BEGIN registerInputField context="+contextName+", path="+path+", name="+name+", formName="+formName);
102         }
103
104         // test arguments
105
if (contextName == null) {
106             throw new ProcessingException("SessionManager.registerInputField: Context Name is required");
107         }
108         if (path == null) {
109             throw new ProcessingException("SessionManager.registerInputField: Path is required");
110         }
111         if (name == null) {
112             throw new ProcessingException("SessionManager.registerInputField: Name is required");
113         }
114         if (formName == null) {
115             throw new ProcessingException("SessionManager.registerInputField: Form is required");
116         }
117
118         DocumentFragment JavaDoc value = null;
119         SessionContext context = this.getContext(contextName);
120         if (context == null) {
121             throw new ProcessingException("SessionManager.registerInputField: Context not found " + contextName);
122         }
123         final Request request = ContextHelper.getRequest(this.context);
124         Session session = request.getSession(false);
125         if (session == null) {
126             throw new ProcessingException("SessionManager.registerInputField: Session is required for context " + contextName);
127         }
128
129         synchronized(session) {
130             Map JavaDoc inputFields = (Map JavaDoc)session.getAttribute(ATTRIBUTE_INPUTXML_STORAGE);
131             if (inputFields == null) {
132                 inputFields = new HashMap JavaDoc(10);
133                 session.setAttribute(ATTRIBUTE_INPUTXML_STORAGE, inputFields);
134             }
135             inputFields.put(name, new Object JavaDoc[] {context, path, formName});
136             value = context.getXML(path);
137         }
138
139         if (this.getLogger().isDebugEnabled() ) {
140             this.getLogger().debug("END registerInputField value="+value);
141         }
142         return value;
143     }
144
145     /**
146      * Process all input fields.
147      * The fields are removed even if the request did not contain
148      * any values.
149      * This is a private method and should not be invoked directly.
150      */

151     private void processInputFields(Map JavaDoc objectModel) {
152         // we only want to invoke the testing once per request
153
if (objectModel.containsKey(this.getClass().getName())) {
154             return;
155         }
156         objectModel.put(this.getClass().getName(), "done");
157
158         // synchronized
159
if (this.getLogger().isDebugEnabled() ) {
160             this.getLogger().debug("BEGIN processInputFields");
161         }
162
163         final Request request = ObjectModelHelper.getRequest( objectModel );
164         final String JavaDoc formName = request.getParameter(SessionConstants.SESSION_FORM_PARAMETER);
165         if ( null != formName ) {
166             final Session session = request.getSession(false);
167             if (session != null) {
168                 synchronized(session) {
169                     final Map JavaDoc inputFields = (Map JavaDoc)session.getAttribute(ATTRIBUTE_INPUTXML_STORAGE);
170                     if (inputFields != null) {
171                         final Enumeration JavaDoc keys = request.getParameterNames();
172                         String JavaDoc currentKey;
173                         Object JavaDoc[] contextAndPath;
174
175                         while (keys.hasMoreElements()) {
176                             currentKey = (String JavaDoc)keys.nextElement();
177                             if (inputFields.containsKey(currentKey)) {
178                                 contextAndPath = (Object JavaDoc[])inputFields.get(currentKey);
179                                 inputFields.remove(currentKey);
180
181                                 SessionContext context = (SessionContext)contextAndPath[0];
182                                 String JavaDoc path = (String JavaDoc)contextAndPath[1];
183
184                                 if (formName.equals(contextAndPath[2])) {
185                                     try {
186                                         context.setXML(path,
187                                                      this.getContextFragment(SessionConstants.REQUEST_CONTEXT, "/parameter/"+currentKey));
188                                     } catch (ProcessingException ignore) {
189                                         this.getLogger().warn("Exception during processing of input fields.", ignore);
190                                     }
191                                 }
192                             }
193                         }
194                     }
195                 }
196             }
197         }
198
199         if (this.getLogger().isDebugEnabled() ) {
200             this.getLogger().debug("END processInputFields");
201         }
202     }
203
204     /* (non-Javadoc)
205      * @see org.apache.cocoon.webapps.session.FormManager#processInputFields()
206      */

207     public void processInputFields() {
208         final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
209         this.processInputFields( objectModel ) ;
210     }
211     
212     /* (non-Javadoc)
213      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
214      */

215     public void contextualize(Context context) throws ContextException {
216         this.context = context;
217     }
218
219     /* (non-Javadoc)
220      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
221      */

222     public void service(ServiceManager manager) throws ServiceException {
223         this.manager = manager;
224     }
225
226 }
227
Popular Tags