dev@javaserverfaces.java.net

[REVIEW] Enhance ManagedBean annotation lifecycle handling

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Tue, 25 Oct 2005 12:09:39 +0800

Use *AttributeListeners to handle ManagedBean preDestory lifecycle
instead of users
having to use ExternalContext maps.


SECTION: Modified Files
----------------------------

M src/com/sun/faces/config/ConfigureListener.java
  - Implement ServletRequestAttributeListener, HttpSessionAttributeListener,
    ServletContextAttributeListener
  - For the 'remove' case, call through to
ApplicationAssociate.handlePreDestroy()
  - In the 'replace' case, only call through to
ApplicationAssociate.handlePreDestroy()
    if the old/new values are different.
M src/com/sun/faces/context/ExternalContextImpl.java
  - Remove logic from Request, Session, Application clear() and remove()
methods
    that would call ApplicationAssociate.handlePreDestroy()

M
systest/src/com/sun/faces/systest/lifecycle/ManagedBeanLifecycleAnnotationTestCase.java
M systest/src/com/sun/faces/systest/model/TestBean.java
M systest/web/managed08.jsp
  - Updated test to verify replace cases
  (pardon the whitespace trimming)



SECTION: Diffs
----------------------------

===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/config/ConfigureListener.java,v
retrieving revision 1.53
diff -u -r1.53 ConfigureListener.java
--- src/com/sun/faces/config/ConfigureListener.java 30 Sep 2005
03:57:19 -0000 1.53
+++ src/com/sun/faces/config/ConfigureListener.java 25 Oct 2005
17:28:20 -0000
@@ -99,7 +99,6 @@
 
 import com.sun.faces.el.DummyPropertyResolverImpl;
 import com.sun.faces.el.DummyVariableResolverImpl;
-import com.sun.faces.spi.ManagedBeanFactory.Scope;
 import com.sun.faces.util.Util;
 
 import org.apache.commons.digester.Digester;
@@ -110,10 +109,19 @@
 
 import java.util.logging.Logger;
 import java.util.logging.Level;
+import java.lang.reflect.InvocationTargetException;
+
 import javax.servlet.ServletRequestEvent;
 import javax.servlet.ServletRequestListener;
+import javax.servlet.ServletContextAttributeListener;
+import javax.servlet.ServletRequestAttributeListener;
+import javax.servlet.ServletRequestAttributeEvent;
+import javax.servlet.ServletContextAttributeEvent;
 import javax.servlet.http.HttpSessionEvent;
 import javax.servlet.http.HttpSessionListener;
+import javax.servlet.http.HttpSessionAttributeListener;
+import javax.servlet.http.HttpSessionBindingEvent;
+import javax.servlet.http.HttpSession;
 
 
 /**
@@ -121,7 +129,12 @@
  * configure the Reference Implementation runtime environment.</p>
  * <p/>
  */
-public class ConfigureListener implements ServletRequestListener,
HttpSessionListener, ServletContextListener {
+public class ConfigureListener implements ServletRequestListener,
+ HttpSessionListener,
+ ServletContextListener,
+ ServletRequestAttributeListener,
+ HttpSessionAttributeListener,
+ ServletContextAttributeListener {
 
 
     // -------------------------------------------------------- Static
Variables
@@ -498,7 +511,6 @@
                 logger.info(e.getMessage());
             }
         }
-
 
         // Release any allocated application resources
     FactoryFinder.releaseFactories();
@@ -512,8 +524,115 @@
     }
 
 
+ // ----------------------------------- Methods from
RequestAttributeListener
+
+ public void attributeAdded(ServletRequestAttributeEvent event) {
+ // we're not interested in this event
+ }
+
+ public void attributeRemoved(ServletRequestAttributeEvent event) {
+
+ handleAttributeEvent(event.getName(),
+ event.getServletContext(),
+ Scope.REQUEST);
+
+ }
+
+ public void attributeReplaced(ServletRequestAttributeEvent event) {
+
+ String attrName = event.getName();
+ Object newValue = event.getServletRequest().getAttribute(attrName);
+
+ // perhaps a bit paranoid, but since the javadocs are a bit vague,
+ // only handle the event if oldValue and newValue are not the
+ // exact same object
+ if (event.getValue() != newValue) {
+ handleAttributeEvent(attrName,
+ event.getServletContext(),
+ Scope.REQUEST);
+ }
+ }
+
+ // ------------------------------- Methods from
HttpSessionAttributeListener
+
+ public void attributeAdded(HttpSessionBindingEvent event) {
+ // we're not interested in this event
+ }
+
+ public void attributeRemoved(HttpSessionBindingEvent event) {
+
+ handleAttributeEvent(event.getName(),
+ event.getSession().getServletContext(),
+ Scope.SESSION);
+
+ }
+
+ public void attributeReplaced(HttpSessionBindingEvent event) {
+
+ HttpSession session = event.getSession();
+ String attrName = event.getName();
+ Object newValue = session.getAttribute(attrName);
+
+ // perhaps a bit paranoid, but since the javadocs are a bit vague,
+ // only handle the event if oldValue and newValue are not the
+ // exact same object
+ if (event.getValue() != newValue) {
+ handleAttributeEvent(attrName,
+ session.getServletContext(),
+ Scope.SESSION);
+ }
+
+ }
+
+ // ---------------------------- Methods from
ServletContextAttributeListener
+
+ public void attributeAdded(ServletContextAttributeEvent event) {
+ //To change body of implemented methods use File | Settings |
File Templates.
+ }
+
+ public void attributeRemoved(ServletContextAttributeEvent event) {
+
+ handleAttributeEvent(event.getName(),
+ event.getServletContext(),
+ Scope.APPLICATION);
+
+ }
+
+ public void attributeReplaced(ServletContextAttributeEvent event) {
+
+ ServletContext context = event.getServletContext();
+ String attrName = event.getName();
+ Object newValue = context.getAttribute(attrName);
+
+ // perhaps a bit paranoid, but since the javadocs are a bit vague,
+ // only handle the event if oldValue and newValue are not the
+ // exact same object
+ if (event.getValue() != newValue) {
+ handleAttributeEvent(attrName,
+ context,
+ Scope.APPLICATION);
+ }
+
+ }
+
     // ---------------------------------------------------------
Private Methods
 
+ private static void handleAttributeEvent(String beanName,
+ ServletContext servletContext,
+ Scope scope) {
+
+ ApplicationAssociate associate =
+ ApplicationAssociate.getInstance(servletContext);
+ try {
+ associate.handlePreDestroy(beanName, scope);
+ } catch (InvocationTargetException ite) {
+ Throwable root = ite.getTargetException();
+ logger.info(root.getMessage());
+ } catch (Exception e) {
+ logger.info(e.getMessage());
+ }
+
+ } // END handleAttributeEvent
 
     /**
      * <p>Return the implementation-specific <code>Application</code>
Index: src/com/sun/faces/context/ExternalContextImpl.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/context/ExternalContextImpl.java,v
retrieving revision 1.38
diff -u -r1.38 ExternalContextImpl.java
--- src/com/sun/faces/context/ExternalContextImpl.java 30 Sep 2005
03:57:20 -0000 1.38
+++ src/com/sun/faces/context/ExternalContextImpl.java 25 Oct 2005
17:28:20 -0000
@@ -63,11 +63,6 @@
 import javax.servlet.http.HttpSession;
 
 import com.sun.faces.RIConstants;
-import com.sun.faces.application.ApplicationAssociate;
-import com.sun.faces.context.BaseContextMap.EntryIterator;
-import com.sun.faces.context.BaseContextMap.KeyIterator;
-import com.sun.faces.context.BaseContextMap.ValueIterator;
-import com.sun.faces.spi.ManagedBeanFactory.Scope;
 import com.sun.faces.util.Util;
 import java.util.logging.Logger;
 
@@ -760,13 +755,6 @@
         for (Enumeration e = servletContext.getAttributeNames();
              e.hasMoreElements(); ) {
             name = (String) e.nextElement();
- try {
- ApplicationAssociate.getInstance(servletContext).
- handlePreDestroy(name, Scope.APPLICATION);
- }
- catch (Throwable t) {
- ExternalContextImpl.logger.info(t.getMessage());
- }
             servletContext.removeAttribute(name);
         }
     }
@@ -805,13 +793,6 @@
         }
         String keyString = key.toString();
         Object result = servletContext.getAttribute(keyString);
- try {
- ApplicationAssociate.getInstance(servletContext).
- handlePreDestroy(keyString, Scope.APPLICATION);
- } catch (Throwable t) {
- ExternalContextImpl.logger.info(t.getMessage());
- }
-
         servletContext.removeAttribute(keyString);
         return (result);
     }
@@ -865,13 +846,6 @@
         for (Enumeration e = getSession().getAttributeNames();
              e.hasMoreElements(); ) {
             name = (String) e.nextElement();
- try {
-
ApplicationAssociate.getInstance(session.getServletContext()).
- handlePreDestroy(name, Scope.SESSION);
- }
- catch (Throwable t) {
- ExternalContextImpl.logger.info(t.getMessage());
- }
             session.removeAttribute(name);
         }
     }
@@ -913,12 +887,6 @@
         String keyString = key.toString();
         HttpSession session = getSession();
         Object result = session.getAttribute(keyString);
- try {
- ApplicationAssociate.getInstance(session.getServletContext()).
- handlePreDestroy(keyString, Scope.SESSION);
- } catch (Throwable t) {
- ExternalContextImpl.logger.info(t.getMessage());
- }
         session.removeAttribute(keyString);
         return (result);
     }
@@ -975,13 +943,6 @@
         for (Enumeration e = request.getAttributeNames();
              e.hasMoreElements(); ) {
             name = (String) e.nextElement();
- try {
- ApplicationAssociate.getInstance(extContext).
- handlePreDestroy(name, Scope.REQUEST);
- }
- catch (Throwable t) {
- ExternalContextImpl.logger.info(t.getMessage());
- }
             request.removeAttribute(name);
         }
     }
@@ -1020,12 +981,6 @@
         }
         String keyString = key.toString();
         Object result = request.getAttribute(keyString);
- try {
- ApplicationAssociate.getInstance(extContext).
- handlePreDestroy(keyString, Scope.REQUEST);
- } catch (Throwable t) {
- ExternalContextImpl.logger.info(t.getMessage());
- }
         request.removeAttribute(keyString);
         return (result);
     }

Index:
systest/src/com/sun/faces/systest/lifecycle/ManagedBeanLifecycleAnnotationTestCase.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/systest/src/com/sun/faces/systest/lifecycle/ManagedBeanLifecycleAnnotationTestCase.java,v
retrieving revision 1.2
diff -u -r1.2 ManagedBeanLifecycleAnnotationTestCase.java
---
systest/src/com/sun/faces/systest/lifecycle/ManagedBeanLifecycleAnnotationTestCase.java
 30 Sep 2005 03:57:22 -0000 1.2
+++
systest/src/com/sun/faces/systest/lifecycle/ManagedBeanLifecycleAnnotationTestCase.java
 25 Oct 2005 17:28:20 -0000
@@ -124,6 +124,16 @@
         text = page.asText();
         assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: sessionBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled:
true.*bean: sessionBean postConstructCalled: true.*",
                 text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:removeSessionBean2");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: sessionBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled:
true.*bean: sessionBean postConstructCalled: true.*",
+ text));
         
         button = (HtmlSubmitInput)
             page.getHtmlElementById("form:clearStatusMessage");
@@ -135,6 +145,16 @@
         assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: applicationBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled:
true.*bean: applicationBean postConstructCalled: true.*",
                 text));
 
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:removeApplicationBean2");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: applicationBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled:
true.*bean: applicationBean postConstructCalled: true.*",
+ text));
+
         button = (HtmlSubmitInput)
             page.getHtmlElementById("form:clearStatusMessage");
         page = (HtmlPage) button.click();
@@ -153,6 +173,79 @@
         page = (HtmlPage) button.click();
         text = page.asText();
         assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: sessionBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled:
true.*bean: sessionBean postConstructCalled: true.*",
+ text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:replaceRequestBean");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*-----------------.*",
+ text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:replaceRequestBean2");
+ button.click();
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*-----------------.*bean: requestBean
postConstructCalled: true.*bean: requestBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled: true.*",
+ text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:removeSessionBean");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:replaceSessionBean");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: sessionBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled: true.*",
+ text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:removeSessionBean");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:replaceSessionBean2");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*-----------------.*bean: requestBean
postConstructCalled: true.*",
+ text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:removeApplicationBean");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:replaceApplicationBean");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*bean: applicationBean preDestroyCalled:
true.*-----------------.*bean: requestBean postConstructCalled: true.*",
+ text));
+
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:removeApplicationBean");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:clearStatusMessage");
+ page = (HtmlPage) button.click();
+ button = (HtmlSubmitInput)
+ page.getHtmlElementById("form:replaceApplicationBean2");
+ page = (HtmlPage) button.click();
+ text = page.asText();
+ assertTrue(Pattern.matches("(?s).*-----------------.*bean:
requestBean postConstructCalled: true.*bean: requestBean
preDestroyCalled: true.*-----------------.*bean: requestBean
postConstructCalled: true.*",
                 text));
         
     }
Index: systest/src/com/sun/faces/systest/model/TestBean.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/systest/src/com/sun/faces/systest/model/TestBean.java,v
retrieving revision 1.21
diff -u -r1.21 TestBean.java
--- systest/src/com/sun/faces/systest/model/TestBean.java 30 Sep 2005
03:57:22 -0000 1.21
+++ systest/src/com/sun/faces/systest/model/TestBean.java 25 Oct 2005
17:28:20 -0000
@@ -53,6 +53,7 @@
 import javax.faces.context.ExternalContext;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpServletRequest;
 
 /**
  * <p>Test JavaBean for managed object creation facility.</p>
@@ -66,11 +67,11 @@
     ServletContext servletContext = null;
 
     public TestBean() {
- random = new Random(4143);
+ random = new Random(4143);
         FacesContext context = FacesContext.getCurrentInstance();
         ExternalContext extContext = (null != context) ?
context.getExternalContext() : null;
         servletContext = (null != extContext) ? (ServletContext)
extContext.getContext() : null;
- }
+ }
 
 
     private boolean booleanProperty = true;
@@ -87,11 +88,11 @@
 
     private boolean booleanProperty2 = false;
     public boolean getBooleanProperty2() {
- return booleanProperty2;
+ return booleanProperty2;
     }
 
     public void setBooleanProperty2(boolean newBooleanProperty2) {
- booleanProperty2 = newBooleanProperty2;
+ booleanProperty2 = newBooleanProperty2;
     }
 
 
@@ -207,7 +208,7 @@
     public void setUserName(UIInput userName) {
         this.userName = userName;
     }
-
+
     private String renderKitInfo = null;
     public String getRenderKitInfo() {
         renderKitInfo = FacesContext.getCurrentInstance().
@@ -242,83 +243,83 @@
     public List selectList = null;
 
     public List getSelectList() {
- if (null == selectList) {
- selectList = new ArrayList();
- selectList.add(new SelectItem("one", "one", "one"));
- selectList.add(new SelectItem("two", "two", "two"));
- selectList.add(new SelectItem("three", "three", "three"));
- }
- return selectList;
+ if (null == selectList) {
+ selectList = new ArrayList();
+ selectList.add(new SelectItem("one", "one", "one"));
+ selectList.add(new SelectItem("two", "two", "two"));
+ selectList.add(new SelectItem("three", "three", "three"));
+ }
+ return selectList;
     }
 
     public void setSelectList(List newSelectList) {
- selectList = newSelectList;
- }
+ selectList = newSelectList;
+ }
 
     protected String selection = null;
 
     public String getSelection() {
- return selection;
+ return selection;
     }
 
     public void setSelection(String newSelection) {
- selection = newSelection;
+ selection = newSelection;
     }
 
     protected String [] multiSelection;
     public String [] getMultiSelection() {
- return multiSelection;
+ return multiSelection;
     }
 
     public void setMultiSelection(String [] newMultiSelection) {
- multiSelection = newMultiSelection;
+ multiSelection = newMultiSelection;
     }
 
     public void valueChanged(ValueChangeEvent event)
         throws AbortProcessingException {
- String [] values = (String []) event.getNewValue();
- if (null == values) {
- valueChangeMessage = "";
- }
- else {
- valueChangeMessage = "value changed, new values: ";
- for (int i = 0; i < values.length; i++) {
- valueChangeMessage = valueChangeMessage + " " + values[i];
- }
- }
+ String [] values = (String []) event.getNewValue();
+ if (null == values) {
+ valueChangeMessage = "";
+ }
+ else {
+ valueChangeMessage = "value changed, new values: ";
+ for (int i = 0; i < values.length; i++) {
+ valueChangeMessage = valueChangeMessage + " " + values[i];
+ }
+ }
     }
 
     protected String valueChangeMessage;
     public String getValueChangeMessage() {
- return valueChangeMessage;
+ return valueChangeMessage;
     }
 
     public void setValueChangeMessage(String newValueChangeMessage) {
- valueChangeMessage = newValueChangeMessage;
+ valueChangeMessage = newValueChangeMessage;
     }
 
     public List getNondeterministicSelectList() {
- ArrayList list = new ArrayList(3);
- String str = new String((new Float(random.nextFloat())).toString());
- list.add(new SelectItem(str, str, str));
- str = new String((new Float(random.nextFloat())).toString());
- list.add(new SelectItem(str, str, str));
- str = new String((new Float(random.nextFloat())).toString());
- list.add(new SelectItem(str, str, str));
- return list;
+ ArrayList list = new ArrayList(3);
+ String str = new String((new Float(random.nextFloat())).toString());
+ list.add(new SelectItem(str, str, str));
+ str = new String((new Float(random.nextFloat())).toString());
+ list.add(new SelectItem(str, str, str));
+ str = new String((new Float(random.nextFloat())).toString());
+ list.add(new SelectItem(str, str, str));
+ return list;
     }
 
     public void setNondeterministicSelectList(List
newNondeterministicSelectList) {
     }
 
     public void addComponentToTree(ActionEvent action) {
- HtmlOutputText output = new HtmlOutputText();
- output.setValue("<p>==new output==</p>");
- output.setEscape(false);
-
- UIComponent group =
FacesContext.getCurrentInstance().getViewRoot().findComponent("form" +
NamingContainer.SEPARATOR_CHAR + "addHere");
- group.getChildren().add(output);
-
+ HtmlOutputText output = new HtmlOutputText();
+ output.setValue("<p>==new output==</p>");
+ output.setEscape(false);
+
+ UIComponent group =
FacesContext.getCurrentInstance().getViewRoot().findComponent("form" +
NamingContainer.SEPARATOR_CHAR + "addHere");
+ group.getChildren().add(output);
+
     }
 
     /**
@@ -327,107 +328,107 @@
      */
 
     public void replacePropertyResolver(ActionEvent action) {
- FacesContext context = FacesContext.getCurrentInstance();
- Application app = context.getApplication();
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application app = context.getApplication();
 
- // see if we need to take action-
- if (null ==
context.getExternalContext().getSessionMap().get("systest.replacePropertyResolver"))
{
- final PropertyResolver oldProp = app.getPropertyResolver();
- PropertyResolver
- newProp = new PropertyResolver() {
- public Object getValue(Object base, Object property)
- throws EvaluationException, PropertyNotFoundException {
- return oldProp.getValue(base, property);
- }
-
- public Object getValue(Object base, int index)
- throws EvaluationException, PropertyNotFoundException {
- return oldProp.getValue(base, index);
- }
-
- public void setValue(Object base, Object property, Object
value)
- throws EvaluationException, PropertyNotFoundException {
- TestBean.this.setValueChangeMessage("setValue() called");
- oldProp.setValue(base, property, value);
- }
-
- public void setValue(Object base, int index, Object value)
- throws EvaluationException, PropertyNotFoundException {
- TestBean.this.setValueChangeMessage("setValue() called");
- oldProp.setValue(base, index, value);
- }
-
- public boolean isReadOnly(Object base, Object property)
- throws EvaluationException, PropertyNotFoundException {
- return oldProp.isReadOnly(base, property);
- }
-
- public boolean isReadOnly(Object base, int index)
- throws EvaluationException, PropertyNotFoundException {
- return oldProp.isReadOnly(base, index);
- }
-
- public Class getType(Object base, Object property)
- throws EvaluationException, PropertyNotFoundException {
- return oldProp.getType(base, property);
- }
-
- public Class getType(Object base, int index)
- throws EvaluationException, PropertyNotFoundException {
- return oldProp.getType(base, index);
- }
-
- };
- app.setPropertyResolver(newProp);
-
context.getExternalContext().getSessionMap().put("systest.replacePropertyResolver",
oldProp);
- }
+ // see if we need to take action-
+ if (null ==
context.getExternalContext().getSessionMap().get("systest.replacePropertyResolver"))
{
+ final PropertyResolver oldProp = app.getPropertyResolver();
+ PropertyResolver
+ newProp = new PropertyResolver() {
+ public Object getValue(Object base, Object property)
+ throws EvaluationException, PropertyNotFoundException {
+ return oldProp.getValue(base, property);
+ }
+
+ public Object getValue(Object base, int index)
+ throws EvaluationException, PropertyNotFoundException {
+ return oldProp.getValue(base, index);
+ }
+
+ public void setValue(Object base, Object property, Object
value)
+ throws EvaluationException, PropertyNotFoundException {
+ TestBean.this.setValueChangeMessage("setValue() called");
+ oldProp.setValue(base, property, value);
+ }
+
+ public void setValue(Object base, int index, Object value)
+ throws EvaluationException, PropertyNotFoundException {
+ TestBean.this.setValueChangeMessage("setValue() called");
+ oldProp.setValue(base, index, value);
+ }
+
+ public boolean isReadOnly(Object base, Object property)
+ throws EvaluationException, PropertyNotFoundException {
+ return oldProp.isReadOnly(base, property);
+ }
+
+ public boolean isReadOnly(Object base, int index)
+ throws EvaluationException, PropertyNotFoundException {
+ return oldProp.isReadOnly(base, index);
+ }
+
+ public Class getType(Object base, Object property)
+ throws EvaluationException, PropertyNotFoundException {
+ return oldProp.getType(base, property);
+ }
+
+ public Class getType(Object base, int index)
+ throws EvaluationException, PropertyNotFoundException {
+ return oldProp.getType(base, index);
+ }
+
+ };
+ app.setPropertyResolver(newProp);
+
context.getExternalContext().getSessionMap().put("systest.replacePropertyResolver",
oldProp);
     }
+ }
+
 
 
-
     /**
      * restore the original PropertyResolver.
      */
 
     public void restorePropertyResolver(ActionEvent action) {
- FacesContext context = FacesContext.getCurrentInstance();
- Application app = context.getApplication();
- PropertyResolver oldProp = null;
-
- // see if we need to take action-
- if (null != (oldProp = (PropertyResolver)
context.getExternalContext().getSessionMap().get("systest.replacePropertyResolver")))
{
- app.setPropertyResolver(oldProp);
-
context.getExternalContext().getSessionMap().remove("systest.replacePropertyResolver");
- setValueChangeMessage(null);
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application app = context.getApplication();
+ PropertyResolver oldProp = null;
+
+ // see if we need to take action-
+ if (null != (oldProp = (PropertyResolver)
context.getExternalContext().getSessionMap().get("systest.replacePropertyResolver")))
{
+ app.setPropertyResolver(oldProp);
+
context.getExternalContext().getSessionMap().remove("systest.replacePropertyResolver");
+ setValueChangeMessage(null);
 
- }
+ }
     }
 
     protected HtmlCommandButton boundButton = new HtmlCommandButton();
     public HtmlCommandButton getBoundButton() {
- if (null != boundButton) {
- boundButton.setValue("button label");
- }
- return boundButton;
+ if (null != boundButton) {
+ boundButton.setValue("button label");
+ }
+ return boundButton;
     }
-
+
     public void setBoundButton(HtmlCommandButton newBoundButton) {
- boundButton = newBoundButton;
+ boundButton = newBoundButton;
     }
 
     public String getFactoryPrintout() {
- String result = "";
- String [] factoryNames = {
- FactoryFinder.APPLICATION_FACTORY,
- FactoryFinder.FACES_CONTEXT_FACTORY,
- FactoryFinder.LIFECYCLE_FACTORY,
- FactoryFinder.RENDER_KIT_FACTORY
- };
- for (int i = 0; i < factoryNames.length; i++) {
- result = result +
- FactoryFinder.getFactory(factoryNames[i]).toString() + " ";
- }
- return result;
+ String result = "";
+ String [] factoryNames = {
+ FactoryFinder.APPLICATION_FACTORY,
+ FactoryFinder.FACES_CONTEXT_FACTORY,
+ FactoryFinder.LIFECYCLE_FACTORY,
+ FactoryFinder.RENDER_KIT_FACTORY
+ };
+ for (int i = 0; i < factoryNames.length; i++) {
+ result = result +
+ FactoryFinder.getFactory(factoryNames[i]).toString() +
" ";
+ }
+ return result;
     }
 
     /**
@@ -495,15 +496,15 @@
 
         return this.converterMessage;
     }
-
+
     public ArrayList getNewList1() {
         return newList1;
     }
-
+
     public ArrayList getNewList2() {
         return newList2;
     }
-
+
    public void valueChange1(ValueChangeEvent vce) {
         String newValue = vce.getNewValue().toString();
         if (newList1.size() == 3){
@@ -511,7 +512,7 @@
         }
         newList1.add(newValue);
    }
-
+
    public void valueChange2(ValueChangeEvent vce) {
         String newValue = vce.getNewValue().toString();
         if (newList2.size() == 3){
@@ -521,15 +522,15 @@
    }
 
     private Integer selectedValue = new Integer(2);
-
 
+
     public Integer getSelectedValue() {
         return selectedValue;
     }
-
 
+
     public void setSelectedValue(Integer selectedValue) {
         this.selectedValue = selectedValue;
     }
-
 
+
     public SelectItem[] getMySelectItems(){
         return new SelectItem[]{
             new SelectItem(new Integer(1),"1"),
@@ -542,14 +543,14 @@
     public int getInt() {
         return intVal;
     }
-
 
-
 
+
+
     public void setInt(int newIntVal) {
         intVal = newIntVal;
     }
 
 
- @PostConstruct
+ @PostConstruct
     public void postConstruct() {
         setPostConstructCalled(true);
     }
@@ -580,15 +581,15 @@
     public void setPostConstructCalled(boolean postConstructCalled) {
 
         this.postConstructCalled = postConstructCalled;
- appendStatusMessage("bean: " + getStringProperty() +
- " postConstructCalled: " + postConstructCalled);
-
+ appendStatusMessage("bean: " + getStringProperty() +
+ " postConstructCalled: " +
postConstructCalled);
+
     }
 
     /**
      * Holds value of property preDestroyCalled.
      */
- private boolean preDestroyCalled = false;;
+ private boolean preDestroyCalled = false;
 
     /**
      * Getter for property preDestroyCalled.
@@ -605,35 +606,101 @@
      */
     public void setPreDestroyCalled(boolean preDestroyCalled) {
         this.preDestroyCalled = preDestroyCalled;
- appendStatusMessage("bean: " + getStringProperty() +
- " preDestroyCalled: " + preDestroyCalled);
+ appendStatusMessage("bean: " + getStringProperty() +
+ " preDestroyCalled: " + preDestroyCalled);
     }
-
+
     public String invalidateSession() {
         
((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).invalidate();
         return null;
     }
-
+
     public String removeRequestBean() {
         
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("requestBean");
         return null;
     }
-
+
+ public String removeRequestBean2() {
+ HttpServletRequest request = (HttpServletRequest)
+
FacesContext.getCurrentInstance().getExternalContext().getRequest();
+ request.removeAttribute("requestBean");
+ return null;
+ }
+
+ public String replaceRequestBean() {
+ HttpServletRequest request = (HttpServletRequest)
+
FacesContext.getCurrentInstance().getExternalContext().getRequest();
+ request.setAttribute("requestBean", new TestBean());
+ return null;
+ }
+
+ public String replaceRequestBean2() {
+ HttpServletRequest request = (HttpServletRequest)
+
FacesContext.getCurrentInstance().getExternalContext().getRequest();
+ Object oldValue = request.getAttribute("requestBean");
+ request.setAttribute("requestBean", oldValue);
+ return null;
+ }
+
     public String removeSessionBean() {
         
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("sessionBean");
         return null;
     }
-
+
+ public String removeSessionBean2() {
+ HttpSession request = (HttpSession)
+
FacesContext.getCurrentInstance().getExternalContext().getSession(true);
+ request.removeAttribute("sessionBean");
+ return null;
+ }
+
+ public String replaceSessionBean() {
+ HttpSession session = (HttpSession)
+
FacesContext.getCurrentInstance().getExternalContext().getSession(true);
+ session.setAttribute("sessionBean", new TestBean());
+ return null;
+ }
+
+ public String replaceSessionBean2() {
+ HttpSession session = (HttpSession)
+
FacesContext.getCurrentInstance().getExternalContext().getSession(true);
+ Object oldValue = session.getAttribute("sessionBean");
+ session.setAttribute("sessionBean", oldValue);
+ return null;
+ }
+
     public String removeApplicationBean() {
         
FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().remove("applicationBean");
         return null;
     }
-
+
+ public String removeApplicationBean2() {
+ ServletContext request = (ServletContext)
+
FacesContext.getCurrentInstance().getExternalContext().getContext();
+ request.removeAttribute("applicationBean");
+ return null;
+ }
+
+ public String replaceApplicationBean() {
+ ServletContext application = (ServletContext)
+
FacesContext.getCurrentInstance().getExternalContext().getContext();
+ application.setAttribute("applicationBean", new TestBean());
+ return null;
+ }
+
+ public String replaceApplicationBean2() {
+ ServletContext application = (ServletContext)
+
FacesContext.getCurrentInstance().getExternalContext().getContext();
+ Object oldValue = application.getAttribute("applicationBean");
+ application.setAttribute("applicationBean", oldValue);
+ return null;
+ }
+
     public String clearRequestMap() {
         
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().clear();
         return null;
     }
-
+
     public String clearRequestMapTwice() {
         clearRequestMap();
         clearRequestMap();
@@ -644,7 +711,7 @@
         
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
         return null;
     }
-
+
     public String clearSessionMapTwice() {
         clearSessionMap();
         clearSessionMap();
@@ -660,19 +727,19 @@
         message = (null != message) ? message : "";
         oldMessage = oldMessage + message;
         servletContext.setAttribute("previousRequestStatus", oldMessage);
-
+
     }
-
+
     public String getAppendRequestMarker() {
         appendStatusMessage("-----------------");
         return "";
     }
-
+
     public String clearStatusMessage() {
         if (null != servletContext) {
             servletContext.removeAttribute("previousRequestStatus");
         }
         return null;
     }
-
+
 }
Index: systest/web/managed08.jsp
===================================================================
RCS file: /cvs/javaserverfaces-sources/jsf-ri/systest/web/managed08.jsp,v
retrieving revision 1.2
diff -u -r1.2 managed08.jsp
--- systest/web/managed08.jsp 30 Sep 2005 03:57:23 -0000 1.2
+++ systest/web/managed08.jsp 25 Oct 2005 17:28:20 -0000
@@ -26,11 +26,29 @@
   
   <p>previous request status: <pre><h:outputText
value="#{applicationScope.previousRequestStatus}" /></pre></p>
   
- <p><h:commandButton id="removenoneBean" value="remove request bean"
action="#{noneBean.removeRequestBean}" /></p>
+ <p><h:commandButton id="removeRequestBean" value="remove request
bean" action="#{noneBean.removeRequestBean}" /></p>
+
+ <p><h:commandButton id="removeRequestBean2" value="remove request
bean2" action="#{noneBean.removeRequestBean2}" /></p>
+
+ <p><h:commandButton id="replaceRequestBean" value="replace request
bean" action="#{noneBean.replaceRequestBean}" /></p>
+
+ <p><h:commandButton id="replaceRequestBean2" value="replace request
bean2" action="#{noneBean.replaceRequestBean2}" /></p>
        
   <p><h:commandButton id="removeSessionBean" value="remove session
bean" action="#{noneBean.removeSessionBean}" /></p>
 
+ <p><h:commandButton id="removeSessionBean2" value="remove session
bean2" action="#{noneBean.removeSessionBean2}" /></p>
+
+ <p><h:commandButton id="replaceSessionBean" value="replace session
bean" action="#{noneBean.replaceSessionBean}" /></p>
+
+ <p><h:commandButton id="replaceSessionBean2" value="replace session
bean2" action="#{noneBean.replaceSessionBean2}" /></p>
+
   <p><h:commandButton id="removeApplicationBean" value="remove
application bean" action="#{noneBean.removeApplicationBean}" /></p>
+
+ <p><h:commandButton id="removeApplicationBean2" value="remove
application bean2" action="#{noneBean.removeApplicationBean2}" /></p>
+
+ <p><h:commandButton id="replaceApplicationBean" value="replace
application bean" action="#{noneBean.replaceApplicationBean}" /></p>
+
+ <p><h:commandButton id="replaceApplicationBean2" value="replace
application bean2" action="#{noneBean.replaceApplicationBean2}" /></p>
        
   <p><h:commandButton id="invalidateSession" value="invalidate session"
action="#{noneBean.invalidateSession}" /></p>