empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1345284 - in /empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app: AppStartupListener.java FacesApplication.java
Date Fri, 01 Jun 2012 17:37:22 GMT
Author: doebele
Date: Fri Jun  1 17:37:22 2012
New Revision: 1345284

URL: http://svn.apache.org/viewvc?rev=1345284&view=rev
Log:
EMPIREDB-140
provide easy access to web context path

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

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=1345284&r1=1345283&r2=1345284&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
Fri Jun  1 17:37:22 2012
@@ -27,12 +27,12 @@ import javax.faces.event.SystemEvent;
 import javax.faces.event.SystemEventListener;
 import javax.servlet.ServletContext;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class AppStartupListener implements SystemEventListener
 {
-    final Log log = LogFactory.getLog(AppStartupListener.class);
+    private static final Logger log = LoggerFactory.getLogger(AppStartupListener.class);
 
     @Override
     public boolean isListenerForSource(Object source)
@@ -55,9 +55,13 @@ public class AppStartupListener implemen
             FacesApplication jsfApp = (FacesApplication)app;
             jsfApp.init(servletContext);
             // Set Servlet Attribute
-            servletContext.setAttribute(jsfApp.getApplicationBeanName(), jsfApp);
+            if (servletContext.getAttribute(FacesApplication.APPLICATION_ATTRIBUTE)!=null)
+            {
+                log.warn("WARNING: Ambiguous application definition. An object of name '{}'
already exists on application scope!", FacesApplication.APPLICATION_ATTRIBUTE);
+            }
+            servletContext.setAttribute(FacesApplication.APPLICATION_ATTRIBUTE, jsfApp);
             // done
-            jsfApp.initComplete();
+            jsfApp.initComplete(servletContext);
         }
         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=1345284&r1=1345283&r2=1345284&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
Fri Jun  1 17:37:22 2012
@@ -50,44 +50,45 @@ import com.sun.faces.application.Applica
 
 public abstract class FacesApplication extends ApplicationImpl
 {
-    private static final Logger    log    = LoggerFactory.getLogger(FacesApplication.class);
-    
-    private static final String APPLICATION_ATTRIBUTE  = "facesApp";
-    private static final String CONNECTION_ATTRIBUTE   = "dbConnections";
-    
-    protected TextResolver[] textResolvers = null; 
-    
+    private static final Logger log                   = LoggerFactory.getLogger(FacesApplication.class);
+
+    private static final String CONNECTION_ATTRIBUTE  = "dbConnections";
+
+    public static String        APPLICATION_ATTRIBUTE = "app";
+
+    protected TextResolver[]    textResolvers         = null;
+
+    private String              webRoot               = null;
+
     protected FacesApplication(AppStartupListener startupListener)
-    {   // subscribe
+    { // subscribe
         subscribeToEvent(javax.faces.event.PostConstructApplicationEvent.class, startupListener);
-    }   
-    
+    }
+
     protected FacesApplication()
-    {   // subscribe
+    { // 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()
+    protected abstract void init(ServletContext fc);
+
+    protected void initComplete(ServletContext servletContext)
     {
+        // Get Web Root
+        webRoot = servletContext.getContextPath();
+
         // Check Text resolvers
-        if (textResolvers==null)
-            initTextResolvers();        
+        if (textResolvers == null)
+            initTextResolvers();
 
         // done
         log.info("FacesApplication initialization complete");
     }
-    
+
     /* Context handling */
-    
+
     public void onChangeView(final FacesContext fc, String viewId)
     {
         // allow custom view change logic
@@ -97,7 +98,18 @@ public abstract class FacesApplication e
     {
         throw new NotSupportedException(this, "addJavascriptCall");
     }
-    
+
+    /**
+     * returns the web context path as returned from ServletContext.getContextPath()
+     */
+    public String getWebRoot()
+    {
+        return webRoot;
+    }
+
+    /**
+     * returns the active locale for a given FacesContext
+     */
     public Locale getContextLocale(final FacesContext ctx)
     {
         UIViewRoot root;
@@ -106,31 +118,34 @@ public abstract class FacesApplication e
         Locale defaultLocale = Locale.getDefault();
         locale = defaultLocale;
         // See if this FacesContext has a ViewRoot
-        if (null != (root = ctx.getViewRoot())) {
+        if (null != (root = ctx.getViewRoot()))
+        {
             // If so, ask it for its Locale
-            if (null == (locale = root.getLocale())) {
+            if (null == (locale = root.getLocale()))
+            {
                 // If the ViewRoot has no Locale, fall back to the default.
                 locale = defaultLocale;
             }
         }
         return locale;
     }
-    
+
     /**
      * checks if the current context contains an error
-     * @param fc the FacesContext
+     * 
+     * @param fc
+     *            the FacesContext
      * @return true if the context has an error set or false otherwise
      */
     public boolean hasError(final FacesContext fc)
     {
         Iterator<FacesMessage> msgIterator = fc.getMessages();
-        if (msgIterator!=null)
-        {   // Check Messages
+        if (msgIterator != null)
+        { // Check Messages
             while (msgIterator.hasNext())
-            {   // Check Severity
+            { // Check Severity
                 Severity fms = msgIterator.next().getSeverity();
-                if (fms==FacesMessage.SEVERITY_ERROR ||
-                    fms==FacesMessage.SEVERITY_FATAL)
+                if (fms == FacesMessage.SEVERITY_ERROR || fms == FacesMessage.SEVERITY_FATAL)
                     return true;
             }
         }
@@ -139,53 +154,56 @@ public abstract class FacesApplication e
 
     /**
      * finds a component from with a given id from a given start component
-     * @param fc the FacesContext
+     * 
+     * @param fc
+     *            the FacesContext
      * @param componentId
-     * @param nearComponent a component within the same naming container
+     * @param nearComponent
+     *            a component within the same naming container
      * @return
      */
     public UIComponent findComponent(FacesContext fc, String componentId, UIComponent nearComponent)
     {
         if (StringUtils.isEmpty(componentId))
-        	throw new InvalidArgumentException("forComponentId", componentId);
+            throw new InvalidArgumentException("forComponentId", componentId);
         // Search for compoent
         UIComponent forComponent = null;
-        if (nearComponent!=null)
+        if (nearComponent != null)
         {
-	        // Look for the 'for' component in the nearest parental naming container 
-	        // of the UIComponent (there's actually a bit more to this search - see
-	        // the docs for the findComponent method
-	        forComponent = nearComponent.findComponent(componentId);
-	        // Since the nearest naming container may be nested, search the 
-	        // next-to-nearest parental naming container in a recursive fashion, 
-	        // until we get to the view root
-	        if (forComponent == null)
-	        {
-	            UIComponent nextParent = nearComponent;
-	            while (true)
-	            {
-	                nextParent = nextParent.getParent();
-	                // avoid extra searching by going up to the next NamingContainer
-	                // (see the docs for findComponent for an information that will
-	                // justify this approach)
-	                while (nextParent != null && !(nextParent instanceof NamingContainer))
-	                {
-	                    nextParent = nextParent.getParent();
-	                }
-	                if (nextParent == null)
-	                {
-	                    break;
-	                }
-	                else
-	                {
-	                    forComponent = nextParent.findComponent(componentId);
-	                }
-	                if (forComponent != null)
-	                {
-	                    break;
-	                }
-	            }
-	        }
+            // Look for the 'for' component in the nearest parental naming container 
+            // of the UIComponent (there's actually a bit more to this search - see
+            // the docs for the findComponent method
+            forComponent = nearComponent.findComponent(componentId);
+            // Since the nearest naming container may be nested, search the 
+            // next-to-nearest parental naming container in a recursive fashion, 
+            // until we get to the view root
+            if (forComponent == null)
+            {
+                UIComponent nextParent = nearComponent;
+                while (true)
+                {
+                    nextParent = nextParent.getParent();
+                    // avoid extra searching by going up to the next NamingContainer
+                    // (see the docs for findComponent for an information that will
+                    // justify this approach)
+                    while (nextParent != null && !(nextParent instanceof NamingContainer))
+                    {
+                        nextParent = nextParent.getParent();
+                    }
+                    if (nextParent == null)
+                    {
+                        break;
+                    }
+                    else
+                    {
+                        forComponent = nextParent.findComponent(componentId);
+                    }
+                    if (forComponent != null)
+                    {
+                        break;
+                    }
+                }
+            }
         }
         // There is one other situation to cover: if the 'for' component 
         // is not situated inside a NamingContainer then the algorithm above
@@ -225,6 +243,7 @@ public abstract class FacesApplication e
 
     /**
      * returns the default control type for a given data Type
+     * 
      * @param dataType
      * @return
      */
@@ -235,54 +254,58 @@ public abstract class FacesApplication e
             case CLOB:
                 return TextAreaInputControl.NAME;
             default:
-            	return TextInputControl.NAME;
+                return TextInputControl.NAME;
         }
     }
-    
+
     /* Message handling */
-    
+
     protected void initTextResolvers()
-    {        
+    {
         int count = 0;
         Iterator<Locale> locales = getSupportedLocales();
-        for (count=0; locales.hasNext(); count++) { locales.next(); }
-        
+        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++)
+        for (int i = 0; locales.hasNext(); i++)
         {
             Locale locale = locales.next();
             textResolvers[i] = new TextResolver(ResourceBundle.getBundle(messageBundle, locale));
         }
     }
-    
+
     public TextResolver getTextResolver(Locale locale)
     {
         // No text Resolvers provided
-        if (textResolvers==null || textResolvers.length==0)
+        if (textResolvers == null || textResolvers.length == 0)
         {
             throw new NotSupportedException(this, "getTextResolver");
         }
         // Lookup resolver for locale
-        for (int i=0; i<textResolvers.length; i++)
+        for (int i = 0; i < textResolvers.length; i++)
             if (locale.equals(textResolvers[i].getLocale()))
                 return textResolvers[i];
         // locale not found: return default
         return textResolvers[0];
     }
-    
+
     public TextResolver getTextResolver(FacesContext ctx)
     {
         return getTextResolver(getContextLocale(ctx));
     }
-    
+
     /**
-     * @see javax.faces.application.Application#getResourceBundle(javax.faces.context.FacesContext,
String)
+     * @see javax.faces.application.Application#getResourceBundle(javax.faces.context.FacesContext,
+     *      String)
      */
     @Override
-    public ResourceBundle getResourceBundle(FacesContext fc, String var) 
+    public ResourceBundle getResourceBundle(FacesContext fc, String var)
     {
         if (var.equals("msg"))
         {
@@ -291,9 +314,10 @@ public abstract class FacesApplication e
         }
         return null;
     }
-    
+
     /**
      * returns a connection from the connection pool
+     * 
      * @return
      */
     protected Connection getConnection(DBDatabase db)
@@ -353,39 +377,41 @@ public abstract class FacesApplication e
      */
     public Connection getConnectionForRequest(FacesContext fc, DBDatabase db)
     {
-        if (fc==null)
+        if (fc == null)
             throw new InvalidArgumentException("FacesContext", fc);
-        if (db==null)
+        if (db == null)
             throw new InvalidArgumentException("DBDatabase", db);
         // Get Conneciton map
         @SuppressWarnings("unchecked")
-        Map<DBDatabase, Connection> connMap = (Map<DBDatabase, Connection>)FacesUtils.getRequestAttribute(fc,
CONNECTION_ATTRIBUTE);
-        if (connMap!=null && connMap.containsKey(db))
+        Map<DBDatabase, Connection> connMap = (Map<DBDatabase, Connection>) FacesUtils.getRequestAttribute(fc,
CONNECTION_ATTRIBUTE);
+        if (connMap != null && connMap.containsKey(db))
             return connMap.get(db);
         // Pooled Connection
         Connection conn = getConnection(db);
-        if (conn==null)
+        if (conn == null)
             return null;
         // Add to map
-        if (connMap==null)
-        {   connMap= new HashMap<DBDatabase, Connection>();
+        if (connMap == null)
+        {
+            connMap = new HashMap<DBDatabase, Connection>();
             FacesUtils.setRequestAttribute(fc, CONNECTION_ATTRIBUTE, connMap);
-        }    
-        connMap.put(db, conn);    
+        }
+        connMap.put(db, conn);
         return conn;
     }
-    
+
     /**
      * Releases the current request connection
+     * 
      * @param fc
      * @param commit
      */
     public void releaseAllConnections(final FacesContext fc, boolean commit)
     {
         @SuppressWarnings("unchecked")
-        Map<DBDatabase, Connection> connMap = (Map<DBDatabase, Connection>)FacesUtils.getRequestAttribute(fc,
CONNECTION_ATTRIBUTE);
+        Map<DBDatabase, Connection> connMap = (Map<DBDatabase, Connection>) FacesUtils.getRequestAttribute(fc,
CONNECTION_ATTRIBUTE);
         if (connMap != null)
-        {   // Walk the connection map
+        { // Walk the connection map
             for (Map.Entry<DBDatabase, Connection> e : connMap.entrySet())
             {
                 releaseConnection(e.getKey(), e.getValue(), commit);
@@ -403,12 +429,12 @@ public abstract class FacesApplication e
     public void releaseConnection(final FacesContext fc, DBDatabase db, boolean commit)
     {
         @SuppressWarnings("unchecked")
-        Map<DBDatabase, Connection> connMap = (Map<DBDatabase, Connection>)FacesUtils.getRequestAttribute(fc,
CONNECTION_ATTRIBUTE);
+        Map<DBDatabase, Connection> connMap = (Map<DBDatabase, Connection>) FacesUtils.getRequestAttribute(fc,
CONNECTION_ATTRIBUTE);
         if (connMap != null && connMap.containsKey(db))
-        {   // Walk the connection map
+        { // Walk the connection map
             releaseConnection(db, connMap.get(db), commit);
             connMap.remove(db);
-            if (connMap.size()==0)
+            if (connMap.size() == 0)
                 FacesUtils.setRequestAttribute(fc, CONNECTION_ATTRIBUTE, null);
         }
     }
@@ -417,5 +443,5 @@ public abstract class FacesApplication e
     {
         releaseConnection(fc, db, !hasError(fc));
     }
-    
+
 }



Mime
View raw message