KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > faces > ExoFacesPortlet


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlet.faces;
6
7 import java.io.IOException JavaDoc;
8 import javax.faces.FactoryFinder;
9 import javax.faces.application.*;
10 import javax.faces.context.FacesContext;
11 import javax.faces.context.FacesContextFactory;
12 import javax.faces.lifecycle.Lifecycle;
13 import javax.faces.lifecycle.LifecycleFactory;
14 import javax.portlet.*;
15 import org.apache.commons.logging.Log;
16 import org.exoplatform.services.log.LogUtil;
17 import org.exoplatform.commons.utils.ExceptionUtil;
18 import org.exoplatform.portlet.faces.application.FacesPortletViewHandler;
19 import org.exoplatform.portlet.faces.application.PortletFacesData;
20
21 /**
22  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
23  * @since Dec 3, 2003 3:53:21 PM
24  * @version $Id: ExoFacesPortlet.java,v 1.27 2004/11/03 01:21:33 tuan08 Exp $
25  */

26 public class ExoFacesPortlet extends GenericPortlet {
27
28   private static final String JavaDoc COMPONENT_TAG_STACK_ATTR = "javax.faces.webapp.COMPONENT_TAG_STACK";
29   private static final String JavaDoc GLOBAL_ID_VIEW = "javax.faces.webapp.GLOBAL_ID_VIEW";
30   private static final String JavaDoc CURRENT_VIEW_ROOT = "javax.faces.webapp.CURRENT_VIEW_ROOT";
31
32   private Lifecycle facesLifecycle_ ;
33
34   public void init(PortletConfig config) throws PortletException {
35     super.init(config);
36     try {
37       XHTMLRendererConfiguration.confiure() ;
38       XHTMLMPRendererConfiguration.confiure() ;
39       LifecycleFactory lfactory = (LifecycleFactory) FactoryFinder.getFactory( FactoryFinder.LIFECYCLE_FACTORY );
40       facesLifecycle_ = lfactory.getLifecycle( LifecycleFactory.DEFAULT_LIFECYCLE );
41       ApplicationFactory factory =
42         (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
43       Application app = factory.getApplication();
44       ViewHandler handler = new FacesPortletViewHandler(app.getStateManager());
45       app.setViewHandler( handler );
46     } catch (Exception JavaDoc ex) {
47       getLog().error("Error: ", ex);
48     }
49   }
50
51   public void render (RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException JavaDoc {
52     //====================save portal jsf environment=====================
53
Object JavaDoc stack = request.getAttribute(COMPONENT_TAG_STACK_ATTR );
54     Object JavaDoc currentViewRoot = request.getAttribute(CURRENT_VIEW_ROOT );
55     Object JavaDoc globalIdView = request.getAttribute(GLOBAL_ID_VIEW);
56     //do not remove this
57
request.removeAttribute(CURRENT_VIEW_ROOT );
58     request.removeAttribute(COMPONENT_TAG_STACK_ATTR );
59     request.removeAttribute(GLOBAL_ID_VIEW);
60     //====================================================================
61
WindowState state = request.getWindowState();
62     if (!state.equals(WindowState.MINIMIZED)) {
63       response.setContentType("text/html") ;
64       processFacesLifeCycle(request, response) ;
65     }
66     //====================restore portal jsf environment=====================
67
request.setAttribute(COMPONENT_TAG_STACK_ATTR, stack);
68     request.setAttribute(CURRENT_VIEW_ROOT , currentViewRoot);
69     request.setAttribute(GLOBAL_ID_VIEW, globalIdView);
70     //====================================================================
71
}
72
73   public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException JavaDoc {
74     processFacesLifeCycle(request, response) ;
75   }
76
77   protected void processFacesLifeCycle(PortletRequest request, PortletResponse response) {
78     String JavaDoc lastViewId = null ;
79     FacesContext facesContext = null;
80     try {
81       PortletMode mode = request.getPortletMode() ;
82       PortletConfig config = getPortletConfig();
83       FacesContextFactory factory =
84         (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY );
85       facesContext = factory.getFacesContext( config, request, response, facesLifecycle_ );
86       PortletFacesData data = PortletFacesData.getPortletFacesData(facesContext) ;
87       lastViewId = request.getParameter(".view-id") ;
88       if (lastViewId != null) {
89         int idx = lastViewId.indexOf("?") ;
90         if(idx > 0) lastViewId = lastViewId.substring(0, idx);
91       } else {
92         lastViewId = data.getLastView(mode) ;
93       }
94       request.setAttribute("javax.servlet.include.path_info", lastViewId) ;
95       
96       facesLifecycle_.execute(facesContext);
97       facesLifecycle_.render(facesContext);
98       data.setLastView(mode, facesContext.getViewRoot().getViewId()) ;
99     } catch (Throwable JavaDoc t) {
100       t = ExceptionUtil.getRootCause(t) ;
101       logError(t, lastViewId) ;
102     } finally {
103       facesContext.release();
104     }
105   }
106
107   private void logError(Throwable JavaDoc t, String JavaDoc lastViewId) {
108     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
109     b.append("portlet is " + getPortletConfig().getPortletName()).append("\n") ;
110     b.append("Last View Id is " + lastViewId).append("\n") ;
111     b.append("ExoFacesPortlet Error: ") ;
112     getLog().error(b.toString(), t) ;
113   }
114   
115   private Log getLog() { return LogUtil.getLog("org.exoplatform.portlet.faces.faces"); }
116 }
Popular Tags