From: Chad B. <cwb...@us...> - 2008-04-08 16:29:14
|
User: cwbrandon Date: 08/04/08 09:29:07 Modified: andromda-jsf2/src/main/resources/templates/jsf2/flow ViewPopulator.java.vsl LogOutServlet.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/utils JsfUtils.java.vsl andromda-jsf2/src/main/resources/templates/jsf2/flow/crud ViewPopulator.java.vsl Log: more improvement to portlet form serialization Revision Changes Path 1.7 +9 -16 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/ViewPopulator.java.vsl Index: ViewPopulator.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/ViewPopulator.java.vsl,v retrieving revision 1.6 retrieving revision 1.7 diff -u -w -r1.6 -r1.7 --- ViewPopulator.java.vsl 8 Apr 2008 01:42:35 -0000 1.6 +++ ViewPopulator.java.vsl 8 Apr 2008 16:29:06 -0000 1.7 @@ -52,18 +52,11 @@ { // - we need to retrieve the faces context differently since we're outside of the // faces servlet - final LifecycleFactory lifecycleFactory = - (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); - final Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); - final FacesContextFactory facesContextFactory = - (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); - final FacesContext facesContext = - facesContextFactory.getFacesContext( - this.config.getServletContext(), - request, - response, - lifecycle); - populateFormAndViewVariables(facesContext, null); +#set ($facesContextUtilsFullyQualifiedName = "FacesContextUtils") +#if ($stringUtils.isNotBlank($managedBeansPackage)) +#set($facesContextUtilsFullyQualifiedName = "${managedBeansPackage}.${facesContextUtilsFullyQualifiedName}") +#end + populateFormAndViewVariables(${facesContextUtilsFullyQualifiedName}.getFacesContext(request, response), null); chain.doFilter(request, response); } @@ -108,7 +101,7 @@ // - if the form is still null, see if we can get it from a serialized state if (form == null) { - form = ${managedBeansPackage}.JsfUtils.getSerializedForm(session); + form = ${managedBeansPackage}.JsfUtils.getSerializedForm(facesContext); } #end if (form != null) @@ -143,13 +136,13 @@ // - if the form is still null, see if we can get it from a serialized state if (form == null) { - form = ${managedBeansPackage}.JsfUtils.getSerializedForm(session); + form = ${managedBeansPackage}.JsfUtils.getSerializedForm(facesContext); } else { // - pass any properties from the serialized form along org.andromda.presentation.jsf2.FormPopulator.populateForm( - org.andromda.presentation.jsf2.JsfUtils.getSerializedForm(session), form); + org.andromda.presentation.jsf2.JsfUtils.getSerializedForm(facesContext), form); } #end } @@ -181,7 +174,7 @@ // - serialize the form if (form != null) { - ${managedBeansPackage}.JsfUtils.serializeForm(session, form); + ${managedBeansPackage}.JsfUtils.serializeForm(facesContext, form); } #end #if (!$view.variables.empty || !$view.backingValueVariables.empty) 1.2 +8 -1 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/LogOutServlet.java.vsl Index: LogOutServlet.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/LogOutServlet.java.vsl,v retrieving revision 1.1 retrieving revision 1.2 diff -u -w -r1.1 -r1.2 --- LogOutServlet.java.vsl 5 Dec 2006 19:39:29 -0000 1.1 +++ LogOutServlet.java.vsl 8 Apr 2008 16:29:06 -0000 1.2 @@ -18,6 +18,8 @@ public class LogOutServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + /** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @@ -27,7 +29,12 @@ throws ServletException, IOException { #if ($formSerialization) - ${managedBeansPackage}.JsfUtils.deleteSerializedForm(request.getSession()); +#set ($facesContextUtilsFullyQualifiedName = "FacesContextUtils") +#if ($stringUtils.isNotBlank($managedBeansPackage)) +#set($facesContextUtilsFullyQualifiedName = "${managedBeansPackage}.${facesContextUtilsFullyQualifiedName}") +#end + ${managedBeansPackage}.JsfUtils.deleteSerializedForm( + ${facesContextUtilsFullyQualifiedName}.getFacesContext(request, response)); #end request.getSession().invalidate(); response.sendRedirect(request.getContextPath() + "${useCase.path}.${facesServletExtension}"); 1.4 +67 -29 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/JsfUtils.java.vsl Index: JsfUtils.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/utils/JsfUtils.java.vsl,v retrieving revision 1.3 retrieving revision 1.4 diff -u -w -r1.3 -r1.4 --- JsfUtils.java.vsl 8 Apr 2008 01:20:28 -0000 1.3 +++ JsfUtils.java.vsl 8 Apr 2008 16:29:07 -0000 1.4 @@ -4,8 +4,6 @@ #end import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; /** * Utilities used within the JSF Cartridge @@ -14,7 +12,8 @@ */ public class $className { - private static Log logger = LogFactory.getLog(JsfUtils.class); + private static org.apache.commons.logging.Log logger = + org.apache.commons.logging.LogFactory.getLog(JsfUtils.class); /** * The location of the temporary directoy used the JSF cartridge. @@ -45,15 +44,18 @@ /** * Attempts to serialize the given <code>form</code> to disk so that it * can later be retrieved when the user's session times out. - * @param session the session from which to create the serialization path. + * @param context the FacesContext from which to create the serialization path. * @param form the form to serialize. */ - public static void serializeForm(final $jsfUtils.sessionClassName session, final Object form) + public static void serializeForm(final javax.faces.context.FacesContext context, final Object form) { java.io.ObjectOutputStream objectStream = null; try { - final java.io.File serializationFile = new java.io.File(getFormSerializationPath(session)); + final String path = getFormSerializationPath(context); + if (StringUtils.isNotBlank(path)) + { + final java.io.File serializationFile = new java.io.File(getFormSerializationPath(context)); final java.io.File parent = serializationFile.getParentFile(); if (parent != null) { @@ -63,6 +65,7 @@ objectStream = new java.io.ObjectOutputStream(fileStream); objectStream.writeObject(form); } + } catch (final Exception exception) { logger.warn(exception); @@ -85,20 +88,24 @@ /** * Retrieves the current serialized form for the given session. - * @param session the session. + * @param context the FacesContext * @return the serialized form. * @throws Exception */ - public static Object getSerializedForm(final $jsfUtils.sessionClassName session) + public static Object getSerializedForm(final javax.faces.context.FacesContext context) { Object form = null; java.io.ObjectInputStream objectStream = null; try { - java.io.FileInputStream fileStream = new java.io.FileInputStream(getFormSerializationPath(session)); + final String path = getFormSerializationPath(context); + if (StringUtils.isNotBlank(path)) + { + java.io.FileInputStream fileStream = new java.io.FileInputStream(path); objectStream = new java.io.ObjectInputStream(fileStream); form = objectStream.readObject(); } + } catch (final Exception exception) { // - ignore if we couldn't retrieve the serialized form @@ -125,10 +132,13 @@ * * @param session the session for which to remove the serialized form. */ - public static void deleteSerializedForm(final $jsfUtils.sessionClassName session) + public static void deleteSerializedForm(final javax.faces.context.FacesContext context) + { + final String path = getFormSerializationPath(context); + if (StringUtils.isNotBlank(path)) { final java.io.File serializationFile = - new java.io.File(getFormSerializationPath(session)); + new java.io.File(getFormSerializationPath(context)); serializationFile.delete(); final java.io.File directory = serializationFile.getParentFile(); if (directory != null) @@ -136,20 +146,49 @@ directory.delete(); } } - + } +#if ($standalone) /** * The name of the file storing the serialized form. */ private static final String SERIALIZED_FORM_FILE_NAME = "/form.ser"; +#else + /** + * Pattern used to strip characters not used in the portlet serialization file. + */ + private static final String NAMESPACE_STRIP_PATTERN = "\\p{Punct}"; + + private static String getSerializationFileName(final Object response) + { + String fileName = null; + if (response instanceof javax.portlet.RenderResponse) + { + fileName = ((javax.portlet.RenderResponse)response).getNamespace().replaceAll(NAMESPACE_STRIP_PATTERN, ""); + } + return fileName; + } + +#end /** * Retrieves the path in which the serialized form will be stored. * @param session the session containing the unique id in which to create the path. * @return the path to which serialization occurs. */ - private static String getFormSerializationPath(final $jsfUtils.sessionClassName session) + private static String getFormSerializationPath(final javax.faces.context.FacesContext context) + { + final String sessionId = (($jsfUtils.sessionClassName)context.getExternalContext().getSession(true)).getId(); +#if ($standalone) + return TEMPORARY_DIRECTORY + sessionId + SERIALIZED_FORM_FILE_NAME; +#else + final String serializationFileName = getSerializationFileName(context.getExternalContext().getResponse()); + String path = null; + if (StringUtils.isNotBlank(serializationFileName)) { - return TEMPORARY_DIRECTORY + session.getId() + SERIALIZED_FORM_FILE_NAME; + path = TEMPORARY_DIRECTORY + sessionId + '/' + serializationFileName; + } + return path; +#end } /** @@ -201,5 +240,4 @@ } throw new RuntimeException("Parameter "+parameterName+" not found"); } - } 1.4 +8 -8 cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/crud/ViewPopulator.java.vsl Index: ViewPopulator.java.vsl =================================================================== RCS file: /cvsroot/andromdaplugins/cartridges/andromda-jsf2/src/main/resources/templates/jsf2/flow/crud/ViewPopulator.java.vsl,v retrieving revision 1.3 retrieving revision 1.4 diff -u -w -r1.3 -r1.4 --- ViewPopulator.java.vsl 18 Jan 2007 10:11:07 -0000 1.3 +++ ViewPopulator.java.vsl 8 Apr 2008 16:29:07 -0000 1.4 @@ -76,7 +76,7 @@ // - if the form is still null, see if we can get it from a serialized state if (form == null) { - form = ${managedBeansPackage}.JsfUtils.getSerializedForm(session); + form = ${managedBeansPackage}.JsfUtils.getSerializedForm(facesContext); } #end try @@ -95,7 +95,7 @@ // - serialize the form if (form != null) { - ${managedBeansPackage}.JsfUtils.serializeForm(session, form); + ${managedBeansPackage}.JsfUtils.serializeForm(facesContext, form); } #end } |