You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sl...@apache.org on 2009/07/30 00:42:40 UTC

svn commit: r799109 [2/2] - in /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view: ./ facelets/ facelets/impl/ jsp/

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFaceletFactory.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFaceletFactory.java?rev=799109&r1=799108&r2=799109&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFaceletFactory.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFaceletFactory.java Wed Jul 29 22:42:40 2009
@@ -25,14 +25,14 @@
 import java.net.URLConnection;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import javax.el.ELException;
 import javax.faces.FacesException;
 import javax.faces.view.facelets.FaceletException;
 import javax.faces.view.facelets.FaceletHandler;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.view.facelets.Facelet;
 import org.apache.myfaces.view.facelets.FaceletFactory;
 import org.apache.myfaces.view.facelets.compiler.Compiler;
@@ -46,19 +46,22 @@
  */
 public final class DefaultFaceletFactory extends FaceletFactory
 {
-    protected final static Logger log = Logger.getLogger("facelets.factory");
+    private static final long INFINITE_DELAY = -1;
+    private static final long NO_CACHE_DELAY = 0;
+    
+    protected final Log log = LogFactory.getLog(DefaultFaceletFactory.class);
 
-    private final Compiler _compiler;
+    private URL _baseUrl;
 
-    private Map<String, DefaultFacelet> _facelets;
+    private Compiler _compiler;
 
-    private Map<String, URL> _relativeLocations;
+    private Map<String, DefaultFacelet> _facelets;
 
-    private final ResourceResolver _resolver;
+    private long _refreshPeriod;
 
-    private final URL _baseUrl;
+    private Map<String, URL> _relativeLocations;
 
-    private final long _refreshPeriod;
+    private ResourceResolver _resolver;
 
     public DefaultFaceletFactory(Compiler compiler, ResourceResolver resolver) throws IOException
     {
@@ -69,15 +72,34 @@
     {
         ParameterCheck.notNull("compiler", compiler);
         ParameterCheck.notNull("resolver", resolver);
+
         _compiler = compiler;
+
         _facelets = new HashMap<String, DefaultFacelet>();
+
         _relativeLocations = new HashMap<String, URL>();
+
         _resolver = resolver;
+
         _baseUrl = resolver.resolveUrl("/");
-        // this.location = url;
-        log.fine("Using ResourceResolver: " + resolver);
-        _refreshPeriod = (refreshPeriod >= 0) ? refreshPeriod * 1000 : -1;
-        log.fine("Using Refresh Period: " + _refreshPeriod);
+
+        _refreshPeriod = refreshPeriod < 0 ? INFINITE_DELAY : refreshPeriod * 1000;
+
+        if (log.isDebugEnabled())
+        {
+            log.debug("Using ResourceResolver: " + _resolver);
+            log.debug("Using Refresh Period: " + _refreshPeriod);
+        }
+    }
+
+    /**
+     * Compiler this factory uses
+     * 
+     * @return final Compiler instance
+     */
+    public Compiler getCompiler()
+    {
+        return _compiler;
     }
 
     /*
@@ -90,7 +112,7 @@
         URL url = (URL) _relativeLocations.get(uri);
         if (url == null)
         {
-            url = this.resolveURL(_baseUrl, uri);
+            url = resolveURL(_baseUrl, uri);
             if (url != null)
             {
                 Map<String, URL> newLoc = new HashMap<String, URL>(_relativeLocations);
@@ -106,6 +128,45 @@
     }
 
     /**
+     * Create a Facelet from the passed URL. This method checks if the cached Facelet needs to be refreshed before
+     * returning. If so, uses the passed URL to build a new instance;
+     * 
+     * @param url
+     *            source url
+     * @return Facelet instance
+     * @throws IOException
+     * @throws FaceletException
+     * @throws FacesException
+     * @throws ELException
+     */
+    public Facelet getFacelet(URL url) throws IOException, FaceletException, FacesException, ELException
+    {
+        ParameterCheck.notNull("url", url);
+        
+        String key = url.toString();
+        
+        DefaultFacelet f = _facelets.get(key);
+        
+        if (f == null || this.needsToBeRefreshed(f))
+        {
+            f = this._createFacelet(url);
+            if (_refreshPeriod != NO_CACHE_DELAY)
+            {
+                Map<String, DefaultFacelet> newLoc = new HashMap<String, DefaultFacelet>(_facelets);
+                newLoc.put(key, f);
+                _facelets = newLoc;
+            }
+        }
+        
+        return f;
+    }
+
+    public long getRefreshPeriod()
+    {
+        return _refreshPeriod;
+    }
+
+    /**
      * Resolves a path based on the passed URL. If the path starts with '/', then resolve the path against
      * {@link javax.faces.context.ExternalContext#getResource(java.lang.String)
      * javax.faces.context.ExternalContext#getResource(java.lang.String)}. Otherwise create a new URL via
@@ -136,36 +197,6 @@
     }
 
     /**
-     * Create a Facelet from the passed URL. This method checks if the cached Facelet needs to be refreshed before
-     * returning. If so, uses the passed URL to build a new instance;
-     * 
-     * @param url
-     *            source url
-     * @return Facelet instance
-     * @throws IOException
-     * @throws FaceletException
-     * @throws FacesException
-     * @throws ELException
-     */
-    public Facelet getFacelet(URL url) throws IOException, FaceletException, FacesException, ELException
-    {
-        ParameterCheck.notNull("url", url);
-        String key = url.toString();
-        DefaultFacelet f = _facelets.get(key);
-        if (f == null || this.needsToBeRefreshed(f))
-        {
-            f = this.createFacelet(url);
-            if (_refreshPeriod != 0)
-            {
-                Map<String, DefaultFacelet> newLoc = new HashMap<String, DefaultFacelet>(_facelets);
-                newLoc.put(key, f);
-                _facelets = newLoc;
-            }
-        }
-        return f;
-    }
-
-    /**
      * Template method for determining if the Facelet needs to be refreshed.
      * 
      * @param facelet
@@ -175,42 +206,43 @@
     protected boolean needsToBeRefreshed(DefaultFacelet facelet)
     {
         // if set to 0, constantly reload-- nocache
-        if (_refreshPeriod == 0)
+        if (_refreshPeriod == NO_CACHE_DELAY)
+        {
             return true;
+        }
+
         // if set to -1, never reload
-        if (_refreshPeriod == -1)
+        if (_refreshPeriod == INFINITE_DELAY)
+        {
             return false;
-        long ttl = facelet.getCreateTime() + _refreshPeriod;
-        URL url = facelet.getSource();
-        InputStream is = null;
-        if (System.currentTimeMillis() > ttl)
+        }
+
+        long target = facelet.getCreateTime() + _refreshPeriod;
+        if (System.currentTimeMillis() > target)
         {
+            // Should check for file modification
+
             try
             {
-                URLConnection conn = url.openConnection();
-                is = conn.getInputStream();
-                long atl = conn.getLastModified();
-                return atl == 0 || atl > ttl;
+                URLConnection conn = facelet.getSource().openConnection();
+                InputStream is = conn.getInputStream();
+                try
+                {
+                    long lastModified = conn.getLastModified();
+
+                    return lastModified == 0 || lastModified > target;
+                }
+                finally
+                {
+                    is.close();
+                }
             }
-            catch (Exception e)
+            catch (IOException e)
             {
                 throw new FaceletException("Error Checking Last Modified for " + facelet.getAlias(), e);
             }
-            finally
-            {
-                if (is != null)
-                {
-                    try
-                    {
-                        is.close();
-                    }
-                    catch (Exception e)
-                    {
-                        // do nothing
-                    }
-                }
-            }
         }
+
         return false;
     }
 
@@ -225,12 +257,13 @@
      * @throws FacesException
      * @throws ELException
      */
-    private DefaultFacelet createFacelet(URL url) throws IOException, FaceletException, FacesException, ELException
+    private DefaultFacelet _createFacelet(URL url) throws IOException, FaceletException, FacesException, ELException
     {
-        if (log.isLoggable(Level.FINE))
+        if (log.isDebugEnabled())
         {
-            log.fine("Creating Facelet for: " + url);
+            log.debug("Creating Facelet for: " + url);
         }
+
         String alias = "/" + url.getFile().replaceFirst(_baseUrl.getFile(), "");
         try
         {
@@ -243,19 +276,4 @@
             throw new FileNotFoundException("Facelet " + alias + " not found at: " + url.toExternalForm());
         }
     }
-
-    /**
-     * Compiler this factory uses
-     * 
-     * @return final Compiler instance
-     */
-    public Compiler getCompiler()
-    {
-        return _compiler;
-    }
-
-    public long getRefreshPeriod()
-    {
-        return _refreshPeriod;
-    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java?rev=799109&r1=799108&r2=799109&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/jsp/JspViewDeclarationLanguage.java Wed Jul 29 22:42:40 2009
@@ -26,7 +26,6 @@
 
 import javax.faces.FacesException;
 import javax.faces.FactoryFinder;
-import javax.faces.application.Application;
 import javax.faces.application.Resource;
 import javax.faces.application.StateManager;
 import javax.faces.application.ViewHandler;
@@ -57,7 +56,7 @@
 /**
  * @author Simon Lessard (latest modification by $Author: slessard $)
  * @version $Revision: 696523 $ $Date: 2009-03-22 13:55:12 -0400 (mer., 17 sept. 2008) $
- *
+ * 
  * @since 2.0
  */
 public class JspViewDeclarationLanguage extends ViewDeclarationLanguageBase
@@ -66,18 +65,19 @@
     public static final String FORM_STATE_MARKER = "<!-...@-->";
     public static final int FORM_STATE_MARKER_LEN = FORM_STATE_MARKER.length();
 
-    private static final String AFTER_VIEW_TAG_CONTENT_PARAM = JspViewDeclarationLanguage.class + ".AFTER_VIEW_TAG_CONTENT";
-    
+    private static final String AFTER_VIEW_TAG_CONTENT_PARAM = JspViewDeclarationLanguage.class
+            + ".AFTER_VIEW_TAG_CONTENT";
+
     private ViewHandlerSupport _cachedViewHandlerSupport;
-    
+
     /**
      * 
      */
     public JspViewDeclarationLanguage()
     {
         if (log.isTraceEnabled())
-            log.trace("New JspViewDeclarationLanguage instance created");    
-        }
+            log.trace("New JspViewDeclarationLanguage instance created");
+    }
 
     /**
      * {@inheritDoc}
@@ -86,7 +86,7 @@
     public void buildView(FacesContext context, UIViewRoot view) throws IOException
     {
         ExternalContext externalContext = context.getExternalContext();
-        ServletResponse response = (ServletResponse)externalContext.getResponse(); 
+        ServletResponse response = (ServletResponse) externalContext.getResponse();
         String viewId = view.getViewId();
         ViewResponseWrapper wrappedResponse = new ViewResponseWrapper((HttpServletResponse) response);
 
@@ -137,12 +137,12 @@
     {
         // Not necessary given that this method always returns null, but staying true to
         // the spec.
-        
+
         checkNull(context, "context");
         checkNull(viewId, "viewId");
-        
+
         // JSP impl must return null.
-        
+
         return null;
     }
 
@@ -199,7 +199,8 @@
         if (responseWriter == null)
         {
             responseWriter = renderKit.createResponseWriter(response.getWriter(), null,
-                    ((HttpServletRequest) externalContext.getRequest()).getCharacterEncoding());
+                                                            ((HttpServletRequest) externalContext.getRequest())
+                                                                    .getCharacterEncoding());
             context.setResponseWriter(responseWriter);
         }
 
@@ -222,9 +223,9 @@
 
         context.setResponseWriter(oldResponseWriter);
 
-        //We're done with the document - now we can write all content
-        //to the response, properly replacing the state-markers on the way out
-        //by using the stateAwareWriter
+        // We're done with the document - now we can write all content
+        // to the response, properly replacing the state-markers on the way out
+        // by using the stateAwareWriter
         if (stateManager.isSavingStateInClient(context))
         {
             stateAwareWriter.flushToWriter(response.getWriter());
@@ -236,34 +237,17 @@
 
         // Final step - we output any content in the wrappedResponse response from above to the response,
         // removing the wrappedResponse response from the request, we don't need it anymore
-        ViewResponseWrapper afterViewTagResponse = (ViewResponseWrapper) externalContext.getRequestMap().get(
-                AFTER_VIEW_TAG_CONTENT_PARAM);
+        ViewResponseWrapper afterViewTagResponse = (ViewResponseWrapper) externalContext.getRequestMap()
+                .get(AFTER_VIEW_TAG_CONTENT_PARAM);
         externalContext.getRequestMap().remove(AFTER_VIEW_TAG_CONTENT_PARAM);
 
         if (afterViewTagResponse != null)
         {
-            afterViewTagResponse.flushToWriter(response.getWriter(),
-                    context.getExternalContext().getResponseCharacterEncoding());
+            afterViewTagResponse.flushToWriter(response.getWriter(), context.getExternalContext()
+                    .getResponseCharacterEncoding());
         }
 
         response.flushBuffer();
-        }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public UIViewRoot restoreView(FacesContext context, String viewId)
-    {
-        checkNull(context, "context");
-        checkNull(viewId, "viewId");
-        
-        Application application = context.getApplication();
-        ViewHandler applicationViewHandler = application.getViewHandler();
-        String renderKitId = applicationViewHandler.calculateRenderKitId(context);
-        
-        UIViewRoot viewRoot = application.getStateManager().restoreView(context, viewId, renderKitId);
-        return viewRoot;
     }
 
     @Override
@@ -273,11 +257,10 @@
         {
             _cachedViewHandlerSupport = new DefaultViewHandlerSupport();
         }
-        
+
         return _cachedViewHandlerSupport.calculateViewId(context, viewId);
     }
 
-
     @Override
     protected void sendSourceNotFound(FacesContext context, String message)
     {
@@ -294,21 +277,20 @@
     }
 
     @Override
-    public StateManagementStrategy getStateManagementStrategy(
-            FacesContext context, String viewId)
+    public StateManagementStrategy getStateManagementStrategy(FacesContext context, String viewId)
     {
         return null;
     }
-    
+
     /**
      * Render the view now - properly setting and resetting the response writer
      */
-    private void actuallyRenderView(FacesContext facesContext,
-                                    UIViewRoot viewToRender) throws IOException {
+    private void actuallyRenderView(FacesContext facesContext, UIViewRoot viewToRender) throws IOException
+    {
         // Set the new ResponseWriter into the FacesContext, saving the old one aside.
         ResponseWriter responseWriter = facesContext.getResponseWriter();
 
-        //Now we actually render the document
+        // Now we actually render the document
         // Call startDocument() on the ResponseWriter.
         responseWriter.startDocument();
 
@@ -320,7 +302,7 @@
 
         responseWriter.flush();
     }
-    
+
     /**
      * Writes the response and replaces the state marker tags with the state information for the current context
      */
@@ -346,10 +328,12 @@
         @Override
         public void write(char[] cbuf, int off, int len) throws IOException
         {
-            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
-                    ((off + len) > cbuf.length) || ((off + len) < 0)) {
+            if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0))
+            {
                 throw new IndexOutOfBoundsException();
-            } else if (len == 0) {
+            }
+            else if (len == 0)
+            {
                 return;
             }
             buf.append(cbuf, off, len);
@@ -378,11 +362,15 @@
             String state = stateWriter.getBuffer().toString();
 
             ExternalContext extContext = facesContext.getExternalContext();
-            if (JavascriptUtils.isJavascriptAllowed(extContext) && MyfacesConfig.getCurrentInstance(extContext).isViewStateJavascript()) {
+            if (JavascriptUtils.isJavascriptAllowed(extContext)
+                    && MyfacesConfig.getCurrentInstance(extContext).isViewStateJavascript())
+            {
                 // If javascript viewstate is enabled no state markers were written
                 write(contentBuffer, 0, contentBuffer.length(), writer);
                 writer.write(state);
-            } else {
+            }
+            else
+            {
                 // If javascript viewstate is disabled state markers must be replaced
                 int lastFormMarkerPos = 0;
                 int formMarkerPos = 0;
@@ -397,7 +385,8 @@
                     lastFormMarkerPos = formMarkerPos;
                 }
                 // Write content after last state marker
-                if (lastFormMarkerPos < contentBuffer.length()) {
+                if (lastFormMarkerPos < contentBuffer.length())
+                {
                     write(contentBuffer, lastFormMarkerPos, contentBuffer.length(), writer);
                 }
             }
@@ -405,16 +394,22 @@
         }
 
         /**
-         * Writes the content of the specified StringBuffer from index
-         * <code>beginIndex</code> to index <code>endIndex - 1</code>.
-         *
-         * @param contentBuffer  the <code>StringBuffer</code> to copy content from
-         * @param beginIndex  the beginning index, inclusive.
-         * @param endIndex  the ending index, exclusive
-         * @param writer  the <code>Writer</code> to write to
-         * @throws IOException  if an error occurs writing to specified <code>Writer</code>
+         * Writes the content of the specified StringBuffer from index <code>beginIndex</code> to index
+         * <code>endIndex - 1</code>.
+         * 
+         * @param contentBuffer
+         *            the <code>StringBuffer</code> to copy content from
+         * @param beginIndex
+         *            the beginning index, inclusive.
+         * @param endIndex
+         *            the ending index, exclusive
+         * @param writer
+         *            the <code>Writer</code> to write to
+         * @throws IOException
+         *             if an error occurs writing to specified <code>Writer</code>
          */
-        private void write(StringBuilder contentBuffer, int beginIndex, int endIndex, Writer writer) throws IOException {
+        private void write(StringBuilder contentBuffer, int beginIndex, int endIndex, Writer writer) throws IOException
+        {
             int index = beginIndex;
             int bufferSize = 2048;
             char[] bufToWrite = new char[bufferSize];