Author: soh...@jb... Date: 2006-05-17 19:55:59 -0400 (Wed, 17 May 2006) New Revision: 4300 Added: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/BeginTransactionListener.java labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/EndTransactionListener.java Removed: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/TransactionFilter.java Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/ForumsJSFPortlet.java labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/JSFUtil.java labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/PortalUtil.java labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/AdminController.java labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/EditPost.java labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/forums-config.xml labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/jboss-portlet.xml labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/web.xml Log: Integrated a JSF PhaseListener for handling Transactions. Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/ForumsJSFPortlet.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/ForumsJSFPortlet.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/ForumsJSFPortlet.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -30,6 +30,7 @@ //portlet api import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; +import javax.portlet.PortletRequestDispatcher; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.PortletException; @@ -44,11 +45,33 @@ /** * Called by the portlet container to allow the portlet to process an action request. */ - public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException - { - this.setupRenderParameters(request,response); - super.processAction(request,response); + public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException + { + try + { + this.setupRenderParameters(request,response); + super.processAction(request,response); + } + catch(Throwable t) + { + throw new PortletException(t); + } } + + /** + * Called by the portlet container to allow the portlet to render its view + */ + public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException + { + try + { + super.doView(request,response); + } + catch(Throwable t) + { + throw new PortletException(t); + } + } /** * Not sure why this needs to be done... Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/JSFUtil.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/JSFUtil.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/JSFUtil.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -180,6 +180,23 @@ * * */ + public static boolean isErrorOccurred() + { + boolean errorOccurred = false; + + Iterator msgs = FacesContext.getCurrentInstance().getMessages(Constants.ERROR); + if(msgs!=null && msgs.hasNext()) + { + errorOccurred = true; + } + + return errorOccurred; + } + + /** + * + * + */ public static void setMessage(String id,String msg) { FacesMessage message = new FacesMessage( Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/PortalUtil.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/PortalUtil.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/PortalUtil.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -71,6 +71,21 @@ { }; + + /** + * + * + */ + public static boolean isRunningInPortal() + { + boolean isRunningInPortal = false; + Object o = FacesContext.getCurrentInstance().getExternalContext().getRequest(); + if(o instanceof PortletRequest) + { + isRunningInPortal = true; + } + return isRunningInPortal; + } /** * Deleted: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/TransactionFilter.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/TransactionFilter.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/TransactionFilter.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -1,148 +0,0 @@ -/* -* JBoss, Home of Professional Open Source -* Copyright 2005, JBoss Inc., and individual contributors as indicated -* by the @authors tag. See the copyright.txt in the distribution for a -* full listing of individual contributors. -* -* This is free software; you can redistribute it and/or modify it -* under the terms of the GNU Lesser General Public License as -* published by the Free Software Foundation; either version 2.1 of -* the License, or (at your option) any later version. -* -* This software is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this software; if not, write to the Free -* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA -* 02110-1301 USA, or see the FSF site: http://www.fsf.org. -*/ -package org.jboss.portlet.forums.ui; - -import org.apache.log4j.Logger; - -import javax.servlet.ServletException; -import javax.servlet.FilterChain; -import javax.servlet.ServletResponse; -import javax.servlet.ServletRequest; -import javax.servlet.FilterConfig; -import javax.servlet.Filter; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.transaction.UserTransaction; -import javax.transaction.Status; -import javax.transaction.SystemException; -import java.io.IOException; - -/** - * Wraps a servlet into a user transaction. Behaviour - * is similar as RequiresNew in EJB world. - * - * @author <a href="mailto:ju...@jb...">Julien Viet</a> - * @version $Revision: 1.1.1.1 $ - */ -public class TransactionFilter implements Filter -{ - - // Attributes ---------------------------------------------------- - - /** - * The filter config. - */ - private FilterConfig filterConfig; - - /** - * The JNDI context. - */ - private InitialContext ctx; - - // Static -------------------------------------------------------- - - private static Logger log = Logger.getLogger(TransactionFilter.class); - - // Filter implementation ----------------------------------------- - - public void init(FilterConfig filterConfig) throws ServletException - { - this.filterConfig = filterConfig; - try - { - ctx = new InitialContext(); - } - catch (NamingException e) - { - throw new ServletException(e); - } - } - - public void destroy() - { - filterConfig = null; - } - - public void doFilter(ServletRequest request, - ServletResponse response, - FilterChain chain) - throws ServletException, IOException - { - - UserTransaction tx = null; - - try - { - tx = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); - tx.begin(); - } - catch (Throwable t) - { - log.error("cannot start transaction, request aborted", t); - throw new ServletException(t); - } - - try - { - // do the job only if the - // transaction has started succesfully - chain.doFilter(request, response); - } - catch (Throwable t) - { - try - { - tx.setRollbackOnly(); - } - catch (IllegalStateException e) - { - log.error("SystemException while setting transaction for rollback only", e); - } - catch (SystemException e) - { - log.error("IllegalStateException while setting transaction for rollback only", e); - } - throw new ServletException(t); - } - finally - { - if (tx != null) - { - try - { - if (tx.getStatus() != Status.STATUS_MARKED_ROLLBACK) - { - tx.commit(); - } - else - { - tx.rollback(); - } - } - catch (Exception e) - { - log.error("cannot end transaction", e); - } - } - } - } -} Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/AdminController.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/AdminController.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/AdminController.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -43,8 +43,8 @@ * ui data associated with "Category" information * */ - private String categoryName = null; - private Category category = null; + private String categoryName = null; //this is used by the create new category usecase + private Category category = null; //this is used in the edit category usecase /** * ui data associated with "Forum" information @@ -177,7 +177,7 @@ String navState = null; boolean success = false; try - { + { JSFUtil.setMessage(Constants.FEEDBACK,"The Category \""+this.category.getTitle()+"\" was successfully updated."); navState = Constants.EDIT_CATEGORY; success = true; @@ -191,7 +191,7 @@ if(success) { //cleanup the state - this.categoryName = null; + this.category = null; } } return navState; Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/EditPost.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/EditPost.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/action/EditPost.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -317,10 +317,9 @@ { post.addAttachment((Attachment)itr.next()); } - - + //set the proper navigation state - navState = Constants.SUCCESS; + navState = Constants.SUCCESS; success = true; } Added: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/BeginTransactionListener.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/BeginTransactionListener.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/BeginTransactionListener.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -0,0 +1,95 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2005, JBoss Inc., and individual contributors as indicated +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* This is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as +* published by the Free Software Foundation; either version 2.1 of +* the License, or (at your option) any later version. +* +* This software is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this software; if not, write to the Free +* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +* 02110-1301 USA, or see the FSF site: http://www.fsf.org. +*/ +package org.jboss.portlet.forums.ui.event; + + +import javax.faces.event.PhaseEvent; +import javax.faces.event.PhaseId; +import javax.faces.event.PhaseListener; + +//transaction related +import javax.naming.InitialContext; +import javax.transaction.UserTransaction; + +// +import org.jboss.portlet.forums.ui.PortalUtil; + +/* + * Created on May 17, 2006 + * + * @author <a href="mailto:soh...@jb...">Sohil Shah</a> + */ +public class BeginTransactionListener implements PhaseListener +{ + /** + * The JNDI context. + */ + private InitialContext ctx = null; + + /** + * + */ + public BeginTransactionListener() throws Exception + { + this.ctx = new InitialContext(); + } + + /* + * + */ + public PhaseId getPhaseId() + { + return PhaseId.RESTORE_VIEW; + } + + /* + * + */ + public void beforePhase(PhaseEvent phaseEvent) + { + //check if the application is running in a portal + if(PortalUtil.isRunningInPortal()) + { + //no need....use the portal's transaction context + return; + } + + //handle transactions in a non-portal environment + try + { + UserTransaction tx = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); + tx.begin(); + } + catch (Throwable t) + { + throw new RuntimeException(t); + } + } + + /* + * + */ + public void afterPhase(PhaseEvent phaseEvent) + { + //leave this empty...nothing to do here + } +} Added: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/EndTransactionListener.java =================================================================== --- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/EndTransactionListener.java 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/event/EndTransactionListener.java 2006-05-17 23:55:59 UTC (rev 4300) @@ -0,0 +1,139 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2005, JBoss Inc., and individual contributors as indicated +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* This is free software; you can redistribute it and/or modify it +* under the terms of the GNU Lesser General Public License as +* published by the Free Software Foundation; either version 2.1 of +* the License, or (at your option) any later version. +* +* This software is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this software; if not, write to the Free +* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +* 02110-1301 USA, or see the FSF site: http://www.fsf.org. +*/ +package org.jboss.portlet.forums.ui.event; + + +import javax.faces.event.PhaseEvent; +import javax.faces.event.PhaseId; +import javax.faces.event.PhaseListener; + +//transaction related +import javax.naming.InitialContext; +import javax.transaction.UserTransaction; + +import org.jboss.portlet.forums.ui.JSFUtil; +import org.jboss.portlet.forums.ui.PortalUtil; + +/* + * Created on May 17, 2006 + * + * @author <a href="mailto:soh...@jb...">Sohil Shah</a> + */ +public class EndTransactionListener implements PhaseListener +{ + /** + * The JNDI context. + */ + private InitialContext ctx = null; + + /** + * + */ + public EndTransactionListener() throws Exception + { + this.ctx = new InitialContext(); + } + + /* + * + */ + public PhaseId getPhaseId() + { + return PhaseId.RENDER_RESPONSE; + } + + /* + * + */ + public void beforePhase(PhaseEvent phaseEvent) + { + //leave this empty, nothing to do here....... + } + + /* + * + */ + public void afterPhase(PhaseEvent phaseEvent) + { + UserTransaction tx = null; + boolean rollback = false; + + //check if the application is running in a portal + if(PortalUtil.isRunningInPortal()) + { + //no need....use the portal's transaction context + if(JSFUtil.isErrorOccurred()) + { + try + { + tx = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); + tx.setRollbackOnly(); + } + catch(Throwable t) + { + //dont worry....this accomplishes killing the transaction + //when an application error occurs + } + } + return; + } + + //handle transactions in a non-portal environment + try + { + tx = (UserTransaction) ctx.lookup("java:comp/UserTransaction"); + //check if an error occurred and transaction should be rolled back + if(JSFUtil.isErrorOccurred()) + { + //this means an error occurred + rollback = true; + } + } + catch(Throwable t) + { + rollback = true; + throw new RuntimeException(t); + } + finally + { + if(tx!=null) + { + try + { + if(rollback) + { + tx.rollback(); + } + else + { + tx.commit(); + } + } + catch(Exception e) + { + //if exceptions occur with transaction rollback and committ...not a whole lot you can do about it + e.printStackTrace(); + } + } + } + } +} Modified: labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/forums-config.xml =================================================================== --- labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/forums-config.xml 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/forums-config.xml 2006-05-17 23:55:59 UTC (rev 4300) @@ -23,6 +23,14 @@ <!--supported-locale>fr</supported-locale--> </locale-config> </application> + + <!-- phase listeners --> + <lifecycle> + <!-- starts before restoreView --> + <phase-listener>org.jboss.portlet.forums.ui.event.BeginTransactionListener</phase-listener> + <!-- starts after renderResponse --> + <phase-listener>org.jboss.portlet.forums.ui.event.EndTransactionListener</phase-listener> + </lifecycle> <!-- configuration for the shared EmptyController --> Modified: labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/jboss-portlet.xml =================================================================== --- labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/jboss-portlet.xml 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/jboss-portlet.xml 2006-05-17 23:55:59 UTC (rev 4300) @@ -156,7 +156,7 @@ </header-content> </portlet> <portlet> - <portlet-name>JSFForumsPortlet</portlet-name> + <portlet-name>JSFForumsPortlet</portlet-name> <transaction> <trans-attribute>Required</trans-attribute> </transaction> Modified: labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/web.xml =================================================================== --- labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/web.xml 2006-05-17 22:56:00 UTC (rev 4299) +++ labs/jbossforums/trunk/forums/src/resources/portal-forums-war/WEB-INF/web.xml 2006-05-17 23:55:59 UTC (rev 4300) @@ -10,11 +10,6 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> - <!-- MyFaces --> - <listener> - <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> - </listener> - <context-param> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value> @@ -53,17 +48,7 @@ <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/tomahawk.taglib.xml;/WEB-INF/forums.taglib.xml</param-value> </context-param> - - <!-- TransactionFilter --> - <filter> - <filter-name>TransactionFilter</filter-name> - <filter-class>org.jboss.portlet.forums.ui.TransactionFilter</filter-class> - </filter> - <filter-mapping> - <filter-name>TransactionFilter</filter-name> - <servlet-name>FacesServlet</servlet-name> - </filter-mapping> - + <!-- JSF Extensions filter --> <filter> <filter-name>MyFacesExtensionsFilter</filter-name> @@ -112,6 +97,12 @@ <url-pattern>/files</url-pattern> </servlet-mapping> + <!-- error handling --> + <!--error-page> + <error-code>500</error-code> + <location>/views/errors/error_body.jsf</location> + </error-page--> + <!-- secure the facelet views that make up the ui components of this jsf application --> <security-constraint> <display-name>constraints for all facelets</display-name> |