empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1327528 - in /empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2: app/ components/ pages/
Date Wed, 18 Apr 2012 14:46:22 GMT
Author: doebele
Date: Wed Apr 18 14:46:21 2012
New Revision: 1327528

URL: http://svn.apache.org/viewvc?rev=1327528&view=rev
Log:
EMPIREDB-140
improved PagePhaseListener

Added:
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplicationFactory.java
  (with props)
Modified:
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/AppStartupListener.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplication.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesUtils.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ControlTag.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinitions.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PagePhaseListener.java

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/AppStartupListener.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/AppStartupListener.java?rev=1327528&r1=1327527&r2=1327528&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/AppStartupListener.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/AppStartupListener.java
Wed Apr 18 14:46:21 2012
@@ -50,14 +50,14 @@ public class AppStartupListener implemen
             Application app = ((PostConstructApplicationEvent) event).getApplication();
             if (!(app instanceof FacesApplication))
                 throw new AbortProcessingException("Error: Application is not a "+FacesApplication.class.getName()+"
instance. Please create a ApplicationFactory!");
-            // Create an Init application
+            // Create and Init application
             ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
             FacesApplication jsfApp = (FacesApplication)app;
             jsfApp.init(servletContext);
             // Set Servlet Attribute
             servletContext.setAttribute(jsfApp.getApplicationBeanName(), jsfApp);
             // done
-            log.info("Application initialization complete!");
+            jsfApp.initComplete();
         }
         else if (event instanceof PreDestroyApplicationEvent)
         {

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplication.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplication.java?rev=1327528&r1=1327527&r2=1327528&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplication.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplication.java
Wed Apr 18 14:46:21 2012
@@ -52,25 +52,61 @@ public abstract class FacesApplication e
 {
     private static final Logger    log    = LoggerFactory.getLogger(FacesApplication.class);
     
-    public final String APPLICATION_ATTRIBUTE   = "app";
+    public final String APPLICATION_ATTRIBUTE   = "facesApp";
     public final String CONNECTION_ATTRIBUTE    = "dbConnections";
     
     protected TextResolver[] textResolvers = null; 
     
+    protected FacesApplication(AppStartupListener startupListener)
+    {   // subscribe
+        subscribeToEvent(javax.faces.event.PostConstructApplicationEvent.class, startupListener);
+    }   
+    
     protected FacesApplication()
-    {
-    }
+    {   // subscribe
+        this(new AppStartupListener());
+    }   
     
     public String getApplicationBeanName()
     {
         return APPLICATION_ATTRIBUTE;
     }
-
+    
     protected abstract DataSource getAppDataSource(DBDatabase db);
     
     public abstract void init(ServletContext fc);
+
+    public void initComplete()
+    {
+        // Check Text resolvers
+        if (textResolvers==null)
+        {
+            log.info("TextResolvers not initialized. Using default.");
+            
+            int count = 0;
+            Iterator<Locale> locales = getSupportedLocales();
+            for (count=0; locales.hasNext(); count++) { locales.next(); }
+            
+            // get message bundles
+            String messageBundle = this.getMessageBundle();
+            textResolvers = new TextResolver[count];
+            locales = getSupportedLocales();
+            for (int i=0; locales.hasNext(); i++)
+            {
+                Locale locale = locales.next();
+                textResolvers[i] = new TextResolver(ResourceBundle.getBundle(messageBundle,
locale));
+            }
+        }
+        // done
+        log.info("FacesApplication initialization complete");
+    }
     
     /* Context handling */
+    
+    public void onChangeView(final FacesContext fc, String previousViewId, String viewId)
+    {
+        // allow custom view change logic
+    }
 
     public void addJavascriptCall(final FacesContext fc, String function)
     {

Added: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplicationFactory.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplicationFactory.java?rev=1327528&view=auto
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplicationFactory.java
(added)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplicationFactory.java
Wed Apr 18 14:46:21 2012
@@ -0,0 +1,67 @@
+/*
+ * ESTEAM Software GmbH, 18.04.2012
+ */
+package org.apache.empire.jsf2.app;
+
+import java.util.Map;
+
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+import javax.faces.context.FacesContext;
+
+import org.apache.empire.exceptions.InternalException;
+import org.apache.empire.exceptions.InvalidArgumentException;
+import org.apache.empire.exceptions.ItemExistsException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.sun.faces.application.InjectionApplicationFactory;
+
+public abstract class FacesApplicationFactory extends ApplicationFactory
+{
+    private static final Logger  log = LoggerFactory.getLogger(FacesApplicationFactory.class);
+
+    private Class<? extends FacesApplication> applicationClass;
+    
+    private volatile Application application;
+    
+    protected FacesApplicationFactory(Class<? extends FacesApplication> applicationClass)
+    {
+        this.applicationClass  = applicationClass;
+    }
+
+    @Override
+    public Application getApplication()
+    {
+        if (application == null)
+        {   try
+            {   // Create Application
+                application = applicationClass.newInstance();
+            }
+            catch (InstantiationException e)
+            {
+                throw new InternalException(e);
+            }
+            catch (IllegalAccessException e)
+            {
+                throw new InternalException(e);
+            }
+            // InjectionApplicationFactory.setApplicationInstance(application);
+            Map<String, Object> appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
+            appMap.put(InjectionApplicationFactory.class.getName(), application);
+            // log
+            log.info("Fin2Application Application instance created");
+        }
+        return application;
+    }
+
+    @Override
+    public void setApplication(Application application)
+    {
+        if (this.application != null)
+            throw new ItemExistsException(this.application);
+        if (!(application instanceof FacesApplication))
+            throw new InvalidArgumentException("application", application);
+        this.application = application;
+    }
+}

Propchange: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesApplicationFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesUtils.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesUtils.java?rev=1327528&r1=1327527&r2=1327528&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesUtils.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/FacesUtils.java
Wed Apr 18 14:46:21 2012
@@ -34,6 +34,7 @@ import javax.servlet.http.HttpServletReq
 
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.jsf2.pages.Page;
+import org.apache.empire.jsf2.pages.PageDefinition;
 import org.apache.empire.jsf2.utils.ParameterMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -82,7 +83,7 @@ public class FacesUtils
         return context.getExternalContext().getRequestParameterMap().get(param);
     }
 
-    public static void redirectDirectly(final FacesContext fc,final String url)
+    public static void redirectDirectly(final FacesContext fc, final String url)
     {
         try
         {
@@ -95,6 +96,13 @@ public class FacesUtils
         }
     }
 
+    public static void redirectDirectly(final FacesContext fc, final PageDefinition page)
+    {
+        String ctxPath = fc.getExternalContext().getRequestContextPath();
+        String pageURI = ctxPath + page.getPath().replace(".xhtml", ".iface");
+        FacesUtils.redirectDirectly(fc, pageURI);
+    }
+    
     /* Connection */
     
     public Connection getConnection(final FacesContext fc, DBDatabase db) 

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ControlTag.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ControlTag.java?rev=1327528&r1=1327527&r2=1327528&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ControlTag.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/components/ControlTag.java
Wed Apr 18 14:46:21 2012
@@ -46,11 +46,14 @@ public class ControlTag extends UIInput 
     public static abstract class ControlSeparatorComponent extends javax.faces.component.UIComponentBase
     {
         private ControlTag control = null;
-        
+
+        /*
         protected ControlSeparatorComponent()
         {
-            log.info("ControlSeparatorComponent "+getClass().getName()+" created.");
+            if (log.isTraceEnabled())
+                log.trace("ControlSeparatorComponent "+getClass().getName()+" created.");
         }
+        */
 
         @Override
         public String getFamily()

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinitions.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinitions.java?rev=1327528&r1=1327527&r2=1327528&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinitions.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinitions.java
Wed Apr 18 14:46:21 2012
@@ -18,8 +18,9 @@
  */
 package org.apache.empire.jsf2.pages;
 
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 
+import org.apache.empire.commons.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -31,7 +32,7 @@ public abstract class PageDefinitions
 {
     private static final Logger log = LoggerFactory.getLogger(PageDefinitions.class);
 
-    private static HashMap<String,PageDefinition> pageMap = new HashMap<String,PageDefinition>();
+    private static LinkedHashMap<String,PageDefinition> pageMap = new LinkedHashMap<String,PageDefinition>();
     
     private static PageDefinitions instance = null;
     
@@ -80,14 +81,29 @@ public abstract class PageDefinitions
             bm.register(mbi);
         }
     }
+
+    /**
+     * returns the default (welcome) page definition.
+     * By default this is the first page in the PageMap
+     * @return the default (welcome) page definition. 
+     */
+    public PageDefinition getDefaultPage()
+    {
+        // first page is default
+        return pageMap.values().iterator().next();
+    }
     
     /**
      * returns a page definition for a given viewId
      * @param viewId
      * @return the page definition
      */
-    public static PageDefinition getPageFromViewId(String viewId)
+    public PageDefinition getPageFromViewId(String viewId)
     {
+        // Empty-String == default page
+        if (StringUtils.isEmpty(viewId))
+            return getDefaultPage();
+        // find in map
         viewId = viewId.replace(".iface", ".xhtml");
         PageDefinition view = pageMap.get(viewId);
         if (view==null)

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PagePhaseListener.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PagePhaseListener.java?rev=1327528&r1=1327527&r2=1327528&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PagePhaseListener.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PagePhaseListener.java
Wed Apr 18 14:46:21 2012
@@ -25,19 +25,20 @@ import javax.faces.context.FacesContext;
 import javax.faces.event.PhaseEvent;
 import javax.faces.event.PhaseId;
 import javax.faces.event.PhaseListener;
-import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.beanutils.BeanUtils;
+import org.apache.empire.exceptions.ItemNotFoundException;
 import org.apache.empire.jsf2.app.FacesUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class PagePhaseListener implements PhaseListener
 {
-    private static final long   serialVersionUID    = 1L;
-    private static final Logger log                 = LoggerFactory.getLogger(PagePhaseListener.class);
+    private static final long   serialVersionUID       = 1L;
+    private static final Logger log                    = LoggerFactory.getLogger(PagePhaseListener.class);
 
-    public static final String  FORWARD_PAGE_PARAMS = "forwardPageParams";
+    public static final String  CURRENT_VIEW_ATTRIBUTE = "currentViewId";
+    public static final String  FORWARD_PAGE_PARAMS    = "forwardPageParams";
 
     public PagePhaseListener()
     {
@@ -55,10 +56,10 @@ public class PagePhaseListener implement
     {
         PhaseId phaseId = pe.getPhaseId();
         if (log.isTraceEnabled())
-            log.trace("Processing Phase {}.", phaseId);        
+            log.trace("Processing Phase {}.", phaseId);
 
         FacesContext fc = pe.getFacesContext();
-        UIViewRoot   vr = fc.getViewRoot();
+        UIViewRoot vr = fc.getViewRoot();
         if (vr == null)
         {
             /*
@@ -73,70 +74,97 @@ public class PagePhaseListener implement
             }
             */
             return;
-        }    
+        }
 
-        // Init Page
+        // Get the view Id
         String viewId = vr.getViewId();
-        PageDefinition pageDef = PageDefinitions.getPageFromViewId(viewId);
-        if (pageDef != null)
+        // boolean checkContextPath = false;
+
+        // Check view Change
+        Map<String, Object> sessionMap = fc.getExternalContext().getSessionMap();
+        Object lastViewId = sessionMap.get(CURRENT_VIEW_ATTRIBUTE);
+        if (lastViewId == null || !(((String) lastViewId).equalsIgnoreCase(viewId)))
+        { // view changes
+            FacesUtils.getFacesApplication().onChangeView(fc, (String) lastViewId, viewId);
+            if (fc.getResponseComplete())
+                return;
+            // set view Id
+            sessionMap.put(CURRENT_VIEW_ATTRIBUTE, viewId);
+        }
+
+        // Find Definition for view-id
+        PageDefinition pageDef = PageDefinitions.getInstance().getPageFromViewId(viewId);
+        if (pageDef == null)
         {
-            // Check Request context path 
-            if (phaseId==PhaseId.APPLY_REQUEST_VALUES)
+            pageDef = PageDefinitions.getInstance().getDefaultPage();
+            if (pageDef == null)
             {
-                HttpServletRequest req = FacesUtils.getHttpRequest(fc);
-                String reqURI = req.getRequestURI();
-                int vix = viewId.lastIndexOf('.');
-                int rix = reqURI.lastIndexOf('.');
-                if (rix<vix || !viewId.regionMatches(true, 0, reqURI, rix-vix, vix))
-                {   // redirect to view page
-                    String ctxPath = fc.getExternalContext().getRequestContextPath();
-                    String pageURI = ctxPath + viewId.substring(0,vix) + ".iface";
-                    log.warn("Invalid RequestURI '" + reqURI + "'. Redirecting to '"+pageURI+"'.");
-                    FacesUtils.redirectDirectly(fc, pageURI);
-                    return;
-                }
-            }
-            // Process page
-            String name = pageDef.getPageBeanName();
-            Map<String, Object> viewMap = vr.getViewMap();
-            Page pageBean = (Page) viewMap.get(name);
-            if (pageBean == null)
-            {
-                String pageBeanClassName = pageDef.getPageBeanClass().getName();
-                log.info("Creating page bean {} for {} in Phase {}.", new Object[] { pageBeanClassName,
viewId, pe.getPhaseId() });
-                try
-                {
-                    pageBean = pageDef.getPageBeanClass().newInstance();
-                    // List request parameters
-                    /*
-                     * FacesContext fc = pe.getFacesContext();
-                     * Map<String, String> map = fc.getExternalContext().getRequestParameterMap();
-                     * for (String key : map.keySet())
-                     * {
-                     * StringBuilder param = new StringBuilder();
-                     * param.append("Parameter: ");
-                     * param.append(key);
-                     * param.append(" = ");
-                     * param.append(map.get(key));
-                     * log.debug(param.toString());
-                     * }
-                     */
-                }
-                catch (Exception e)
-                {
-                    throw new RuntimeException("Error creating instance of page bean " +
pageBeanClassName, e);
-                }
-                viewMap.put(pageDef.getPageBeanName(), pageBean);
-                viewMap.put("page", pageBean);
+                log.error("No Page definition Error for path {}", viewId);
+                throw new ItemNotFoundException(viewId);
             }
-            pageBean.setPageDefinition(pageDef);
+            log.warn("No Page definition for {}. Redirecting to default page.", viewId);
+            FacesUtils.redirectDirectly(fc, pageDef);
+            return;
+        }
 
-            if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE)
-                initPageBean(pageBean, fc, viewMap);
+        // Check Request context path 
+        /*
+        if (checkContextPath)
+        {   HttpServletRequest req = FacesUtils.getHttpRequest(fc);
+            String reqURI = req.getRequestURI();
+            viewId = pageDef.getPath();
+            int vix = viewId.lastIndexOf('.');
+            int rix = reqURI.lastIndexOf('.');
+            if (rix<vix || !viewId.regionMatches(true, 0, reqURI, rix-vix, vix))
+            {   // redirect to view page
+                String ctxPath = fc.getExternalContext().getRequestContextPath();
+                String pageURI = ctxPath + viewId.substring(0,vix) + ".iface";
+                log.warn("Invalid RequestURI '" + reqURI + "'. Redirecting to '"+pageURI+"'.");
+                FacesUtils.redirectDirectly(fc, pageURI);
+                return;
+            }
+            // Save current viewId
+        }
+        */
 
+        // Init Page
+        String name = pageDef.getPageBeanName();
+        Map<String, Object> viewMap = vr.getViewMap();
+        Page pageBean = (Page) viewMap.get(name);
+        if (pageBean == null)
+        {
+            String pageBeanClassName = pageDef.getPageBeanClass().getName();
+            if (log.isDebugEnabled())
+                log.debug("Creating page bean {} for {} in Phase {}.", new Object[] { pageBeanClassName,
viewId, pe.getPhaseId() });
+            try
+            {
+                pageBean = pageDef.getPageBeanClass().newInstance();
+                // List request parameters
+                /*
+                 * FacesContext fc = pe.getFacesContext();
+                 * Map<String, String> map = fc.getExternalContext().getRequestParameterMap();
+                 * for (String key : map.keySet())
+                 * {
+                 * StringBuilder param = new StringBuilder();
+                 * param.append("Parameter: ");
+                 * param.append(key);
+                 * param.append(" = ");
+                 * param.append(map.get(key));
+                 * log.debug(param.toString());
+                 * }
+                 */
+            }
+            catch (Exception e)
+            {
+                throw new RuntimeException("Error creating instance of page bean " + pageBeanClassName,
e);
+            }
+            viewMap.put(pageDef.getPageBeanName(), pageBean);
+            viewMap.put("page", pageBean);
         }
-        else
-            log.warn("No Page Defintion Error for path {}", viewId);
+        pageBean.setPageDefinition(pageDef);
+
+        if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE)
+            initPageBean(pageBean, fc, viewMap);
 
         /*
          * Collection<UIViewParameter> params = ViewMetadata.getViewParameters(vr);
@@ -150,11 +178,11 @@ public class PagePhaseListener implement
     private void initPageBean(Page pageBean, FacesContext fc, Map<String, Object> viewMap)
     {
         if (!pageBean.isInitialized())
-        {   // Not yet initialized 
+        { // Not yet initialized 
             if (!PageNavigationHandler.isInitialized())
-            {   // Probably missing declaration in faces-config.xml 
+            { // Probably missing declaration in faces-config.xml 
                 log.error("PageNavigationHandler has not been initialized. Forward operations
will not work!");
-            }    
+            }
             // Check for forward page params
             if (viewMap.containsKey(FORWARD_PAGE_PARAMS))
             {
@@ -162,7 +190,7 @@ public class PagePhaseListener implement
                 Map<String, String> pageParams = (Map<String, String>) viewMap.remove(FORWARD_PAGE_PARAMS);
                 // TODO: Set view metadata
                 if (!setViewMetadata(pageParams))
-                {   // instead set properties directly
+                { // instead set properties directly
                     for (String name : pageParams.keySet())
                     {
                         String value = pageParams.get(name);
@@ -178,13 +206,14 @@ public class PagePhaseListener implement
                 }
             }
             // page prepared
-        }    
+        }
         // Init now
         pageBean.preRenderPage(fc);
     }
-    
+
     /**
      * TODO: Find a way to set the view Metadata. Don't know how to do it.
+     * 
      * @param pageParams
      * @return
      */
@@ -196,7 +225,7 @@ public class PagePhaseListener implement
         UIViewRoot   vr = fc.getViewRoot();
         UIComponent metadataFacet = vr.getFacet(UIViewRoot.METADATA_FACET_NAME);
         */
-        
+
         /*
         String viewId = vr.getViewId();        
         ViewDeclarationLanguage vdl = fc.getApplication().getViewHandler().getViewDeclarationLanguage(fc,
viewId);
@@ -236,10 +265,10 @@ public class PagePhaseListener implement
             }
         }
         */
-        
+
         return false;
     }
-    
+
     @Override
     public void afterPhase(PhaseEvent pe)
     {



Mime
View raw message