1 16 package org.apache.cocoon.webapps.session.components; 17 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Map ; 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 ; 42 43 49 public final class DefaultFormManager 50 extends AbstractLogEnabled 51 implements Serviceable, Component, FormManager, ThreadSafe, Contextualizable { 52 53 54 private static final String ATTRIBUTE_INPUTXML_STORAGE = "org.apache.cocoon.webapps.session.InputXMLStorage"; 55 56 57 private ServiceManager manager; 58 59 60 private Context context; 61 62 65 private SessionContext getContext(String 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 getContextFragment(String context, String 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 94 public DocumentFragment registerInputField(String contextName, 95 String path, 96 String name, 97 String formName) 98 throws ProcessingException { 99 if (this.getLogger().isDebugEnabled() ) { 101 this.getLogger().debug("BEGIN registerInputField context="+contextName+", path="+path+", name="+name+", formName="+formName); 102 } 103 104 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 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 inputFields = (Map )session.getAttribute(ATTRIBUTE_INPUTXML_STORAGE); 131 if (inputFields == null) { 132 inputFields = new HashMap (10); 133 session.setAttribute(ATTRIBUTE_INPUTXML_STORAGE, inputFields); 134 } 135 inputFields.put(name, new Object [] {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 151 private void processInputFields(Map objectModel) { 152 if (objectModel.containsKey(this.getClass().getName())) { 154 return; 155 } 156 objectModel.put(this.getClass().getName(), "done"); 157 158 if (this.getLogger().isDebugEnabled() ) { 160 this.getLogger().debug("BEGIN processInputFields"); 161 } 162 163 final Request request = ObjectModelHelper.getRequest( objectModel ); 164 final String 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 inputFields = (Map )session.getAttribute(ATTRIBUTE_INPUTXML_STORAGE); 170 if (inputFields != null) { 171 final Enumeration keys = request.getParameterNames(); 172 String currentKey; 173 Object [] contextAndPath; 174 175 while (keys.hasMoreElements()) { 176 currentKey = (String )keys.nextElement(); 177 if (inputFields.containsKey(currentKey)) { 178 contextAndPath = (Object [])inputFields.get(currentKey); 179 inputFields.remove(currentKey); 180 181 SessionContext context = (SessionContext)contextAndPath[0]; 182 String path = (String )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 207 public void processInputFields() { 208 final Map objectModel = ContextHelper.getObjectModel(this.context); 209 this.processInputFields( objectModel ) ; 210 } 211 212 215 public void contextualize(Context context) throws ContextException { 216 this.context = context; 217 } 218 219 222 public void service(ServiceManager manager) throws ServiceException { 223 this.manager = manager; 224 } 225 226 } 227 | Popular Tags |