Revision: 1478
          http://stripes.svn.sourceforge.net/stripes/?rev=1478&view=rev
Author:   bengunter
Date:     2012-03-05 16:16:15 +0000 (Mon, 05 Mar 2012)
Log Message:
-----------
Somewhat related to STS-871: Cleaned things up a little by removing the 
getContext() methods (one of which had side-effects) and replacing them with 
initialization methods that set most of the fields before tag execution begins.

Modified Paths:
--------------
    
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentTag.java
    
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutDefinitionTag.java
    
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutRenderTag.java

Modified: 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentTag.java
===================================================================
--- 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentTag.java
       2012-03-05 15:17:22 UTC (rev 1477)
+++ 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutComponentTag.java
       2012-03-05 16:16:15 UTC (rev 1478)
@@ -18,8 +18,10 @@
 import java.util.regex.Pattern;
 
 import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.PageContext;
 
 import net.sourceforge.stripes.exception.StripesJspException;
+import net.sourceforge.stripes.exception.StripesRuntimeException;
 import net.sourceforge.stripes.util.Log;
 
 /**
@@ -47,25 +49,23 @@
     /** Sets the name of the component. */
     public void setName(String name) { this.name = name; }
 
-    /**
-     * Get the current layout context.
-     * 
-     * @throws StripesJspException If a {@link LayoutContext} is not found.
-     */
-    public LayoutContext getContext() throws StripesJspException {
-        if (context == null) {
-            context = LayoutContext.lookup(pageContext);
+    @Override
+    public void setPageContext(PageContext pageContext) {
+        // Call super method
+        super.setPageContext(pageContext);
 
-            if (context == null) {
-                throw new StripesJspException("A component tag named \"" + 
getName() + "\" in "
-                        + getCurrentPagePath() + " was unable to find a layout 
context.");
-            }
+        // Initialize the layout context and related fields
+        context = LayoutContext.lookup(pageContext);
 
-            log.trace("Component ", getName() + " has context ", 
context.getRenderPage(), " -> ",
-                    context.getDefinitionPage());
+        if (context == null) {
+            throw new StripesRuntimeException("A component tag named \"" + 
getName() + "\" in "
+                    + getCurrentPagePath() + " was unable to find a layout 
context.");
         }
 
-        return context;
+        log.trace("Component ", getName() + " has context ", 
context.getRenderPage(), " -> ",
+                context.getDefinitionPage());
+
+        silent = context.getOut().isSilent();
     }
 
     /**
@@ -75,7 +75,6 @@
      * @throws StripesJspException If a {@link LayoutContext} is not found.
      */
     public boolean isCurrentComponent() throws StripesJspException {
-        final LayoutContext context = getContext();
         String name = context.getComponent();
         if (name == null || !name.equals(getName()))
             return false;
@@ -109,9 +108,6 @@
     @Override
     public int doStartTag() throws JspException {
         try {
-            LayoutContext context = getContext();
-            silent = context.getOut().isSilent();
-
             if (context.isComponentRenderPhase()) {
                 if (isChildOfRender()) {
                     if (isCurrentComponent()) {
@@ -235,7 +231,6 @@
             // Set current component name back to null as a signal to the 
component tag within the
             // definition tag that the component did, indeed, render and it 
should not output the
             // default contents.
-            LayoutContext context = getContext();
             if (isCurrentComponent())
                 context.setComponent(null);
 

Modified: 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutDefinitionTag.java
===================================================================
--- 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutDefinitionTag.java
      2012-03-05 15:17:22 UTC (rev 1477)
+++ 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutDefinitionTag.java
      2012-03-05 16:16:15 UTC (rev 1478)
@@ -20,7 +20,7 @@
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.PageContext;
 
-import net.sourceforge.stripes.exception.StripesJspException;
+import net.sourceforge.stripes.exception.StripesRuntimeException;
 
 /**
  * On the surface, allows a developer to define a layout using a custom tag - 
but is actually
@@ -34,25 +34,23 @@
     private LayoutContext context;
     private boolean renderPhase, silent;
 
-    /**
-     * Get the current layout context.
-     * 
-     * @throws StripesJspException If there is no {@link LayoutContext} for 
this layout in the
-     *             current {@link PageContext}.
-     */
-    public LayoutContext getContext() throws StripesJspException {
-        if (context == null) {
-            context = LayoutContext.lookup(pageContext);
+    @Override
+    public void setPageContext(PageContext pageContext) {
+        // Call super method
+        super.setPageContext(pageContext);
+        
+        // Initialize layout context and related fields
+        context = LayoutContext.lookup(pageContext);
 
-            if (context == null || getLayoutParent() != null) {
-                throw new StripesJspException("The JSP page " + 
getCurrentPagePath()
-                        + " contains a layout-definition tag and was invoked 
directly. "
-                        + "A layout-definition can only be invoked by a page 
that contains "
-                        + "a layout-render tag.");
-            }
+        if (context == null || getLayoutParent() != null) {
+            throw new StripesRuntimeException("The JSP page " + 
getCurrentPagePath()
+                    + " contains a layout-definition tag and was invoked 
directly. "
+                    + "A layout-definition can only be invoked by a page that 
contains "
+                    + "a layout-render tag.");
         }
 
-        return context;
+        renderPhase = context.isComponentRenderPhase();
+        silent = context.getOut().isSilent();
     }
 
     /**
@@ -65,10 +63,6 @@
     @Override
     public int doStartTag() throws JspException {
         try {
-            LayoutContext context = getContext(); // Initialize context
-            renderPhase = context.isComponentRenderPhase(); // Initialize 
phase flag
-            silent = context.getOut().isSilent();
-
             // Flag this definition has rendered, even though it's not really 
done yet.
             context.setRendered(true);
 
@@ -100,7 +94,7 @@
     public int doEndTag() throws JspException {
         try {
             cleanUpComponentRenderers();
-            getContext().getOut().setSilent(silent, pageContext);
+            context.getOut().setSilent(silent, pageContext);
             return SKIP_PAGE;
         }
         catch (IOException e) {

Modified: 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutRenderTag.java
===================================================================
--- 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutRenderTag.java
  2012-03-05 15:17:22 UTC (rev 1477)
+++ 
branches/1.5.x/stripes/src/net/sourceforge/stripes/tag/layout/LayoutRenderTag.java
  2012-03-05 16:16:15 UTC (rev 1478)
@@ -46,35 +46,41 @@
     /** Gets the name of the layout to be used. */
     public String getName() { return name; }
 
-    /** Sets the name of the layout to be used. */
-    public void setName(String name) { this.name = name; }
+    /** Sets the name of the layout to be used and then calls {@link 
#initialize()}. */
+    public void setName(String name) {
+        this.name = name;
+        initialize();
+    }
 
     /** Get the {@link LayoutRenderTagPath} that identifies this tag within 
the current page. */
     public LayoutRenderTagPath getPath( ) { return path; }
 
-    /** Look up an existing layout context or create a new one if none is 
found. */
-    public LayoutContext getContext() {
-        if (context == null) {
-            LayoutContext context = LayoutContext.lookup(pageContext);
+    /**
+     * Initialize fields before execution begins. Typically, this would be 
done by overriding
+     * {@link #setPageContext(javax.servlet.jsp.PageContext)}, but that isn't 
possible in this case
+     * because some of the logic depends on {@link #setName(String)} having 
been called, which does
+     * not happen until after {@link 
#setPageContext(javax.servlet.jsp.PageContext)} has been
+     * called.
+     */
+    protected void initialize() {
+        LayoutContext context = LayoutContext.lookup(pageContext);
 
-            boolean create = context == null || 
!context.isComponentRenderPhase()
-                    || isChildOfCurrentComponent();
+        boolean create = context == null || !context.isComponentRenderPhase()
+                || isChildOfCurrentComponent();
 
-            LayoutRenderTagPath path;
-            if (create) {
-                context = LayoutContext.push(this);
-                path = context.getComponentPath();
-            }
-            else {
-                path = new LayoutRenderTagPath(this);
-            }
-
-            this.context = context;
-            this.contextIsNew = create;
-            this.path = path;
+        LayoutRenderTagPath path;
+        if (create) {
+            context = LayoutContext.push(this);
+            path = context.getComponentPath();
         }
+        else {
+            path = new LayoutRenderTagPath(this);
+        }
 
-        return context;
+        this.context = context;
+        this.contextIsNew = create;
+        this.path = path;
+        this.silent = context.getOut().isSilent();
     }
 
     /** Returns true if this tag is a child of the current component tag. */
@@ -92,7 +98,7 @@
 
     /** Used by the JSP container to provide the tag with dynamic attributes. 
*/
     public void setDynamicAttribute(String uri, String localName, Object 
value) throws JspException {
-        getContext().getParameters().put(localName, value);
+        context.getParameters().put(localName, value);
     }
 
     /**
@@ -109,9 +115,6 @@
     @Override
     public int doStartTag() throws JspException {
         try {
-            LayoutContext context = getContext();
-            silent = context.getOut().isSilent();
-
             if (contextIsNew) {
                 log.debug("Start layout init in ", context.getRenderPage());
                 pushPageContextAttributes(context.getParameters());
@@ -163,7 +166,6 @@
     @Override
        public int doEndTag() throws JspException {
         try {
-            LayoutContext context = getContext();
             if (contextIsNew) {
                 log.debug("End layout init in ", context.getRenderPage());
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to