Author: chris.laprun(a)jboss.com
Date: 2008-09-30 18:23:28 -0400 (Tue, 30 Sep 2008)
New Revision: 12005
Modified:
branches/JBoss_Portal_Branch_2_7/faces/src/main/org/jboss/portal/faces/gui/BeanContext.java
Log:
- Added getFromSession method.
Modified:
branches/JBoss_Portal_Branch_2_7/faces/src/main/org/jboss/portal/faces/gui/BeanContext.java
===================================================================
---
branches/JBoss_Portal_Branch_2_7/faces/src/main/org/jboss/portal/faces/gui/BeanContext.java 2008-09-30
17:03:17 UTC (rev 12004)
+++
branches/JBoss_Portal_Branch_2_7/faces/src/main/org/jboss/portal/faces/gui/BeanContext.java 2008-09-30
22:23:28 UTC (rev 12005)
@@ -41,6 +41,7 @@
private static final String RESOURCE_NAME = "Resource";
private static final String UNEXPECTED_ERROR =
"bean_support_unexpected_error";
private static final String CAUSE = "bean_support_cause";
+ private static final String CURRENT_PLACEHOLDER = "###";
/**
@@ -153,10 +154,12 @@
* name, then only an object of the same type (as defined by {@link
Class#isAssignableFrom(Class)}) can be assigned
* to this name.
*
- * @param name the name identifying the object to be replaced
+ * @param name the name identifying the object to be replaced
* @param newValue the new value for the object to be replaced or
<code>null</code> if the object is to be removed
- * @param <T> the type of the object to be replaced
+ * @param <T> the type of the object to be replaced
* @return the new value for the object or <code>null</code> if the remove
semantics is used
+ * @throws IllegalArgumentException if the new value for the identified object is not
compatible with the currently
+ * stored value
*/
public <T> T replaceInSession(String name, T newValue)
{
@@ -170,14 +173,48 @@
return null;
}
- Object current = sessionMap.get(name);
- // check that the new bean is compatible with the previously assigned
- if (current != null &&
!newValue.getClass().isAssignableFrom(current.getClass()))
- {
- throw new IllegalArgumentException("Provided object: " + newValue +
" is not compatible with previously assigned '"
- + name + "' object: " + current);
- }
+ getFromSession(name, newValue.getClass(), sessionMap, "Provided object: "
+ newValue
+ + " is not compatible with previously assigned '" + name +
"' object: " + CURRENT_PLACEHOLDER);
sessionMap.put(name, newValue);
return newValue;
}
+
+ /**
+ * Retrieves the session object associated with the specified name and the expected
type.
+ *
+ * @param name name of the session object to be retrieved
+ * @param expectedClass expected class of the object
+ * @param <T> type of the object to be retrieved
+ * @return the session object associated with the specified name
+ * @throws IllegalArgumentException if the value associated with the specified name is
not <code>null</code> and does
+ * not match the specified expected class
+ */
+ public <T> T getFromSession(String name, Class<T> expectedClass)
+ {
+ return getFromSession(name, expectedClass, getSessionMap(), "Current
object:" + CURRENT_PLACEHOLDER
+ + " is not compatible with expected class " + expectedClass +
" for '" + name + "'");
+ }
+
+ /**
+ * @param name name of the session attribute to retrieve
+ * @param expectedClass expected class of the attribute
+ * @param sessionMap the session map to retrieve the attribute from
+ * @param errorMessage the error message that will be used if the attribute value is
not of the expected class, in
+ * which {@link #CURRENT_PLACEHOLDER} will be substituted by the
current value of the attribute
+ * at runtime
+ * @param <T> the type of the object to be retrieved
+ * @return the value associated with the specified name
+ * @throws IllegalArgumentException if the value associated with the specified name is
not <code>null</code> and does
+ * not match the specified expected class
+ */
+ private <T> T getFromSession(String name, Class<T> expectedClass,
Map<String, Object> sessionMap, String errorMessage)
+ {
+ Object result = sessionMap.get(name);
+ if (result != null && !expectedClass.isAssignableFrom(result.getClass()))
+ {
+ throw new IllegalArgumentException(errorMessage.replace(CURRENT_PLACEHOLDER,
result.toString()));
+ }
+
+ return expectedClass.cast(result);
+ }
}
Show replies by date