You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2007/10/05 14:42:23 UTC
svn commit: r582231 -
/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/jsf/JsfFrameworkAdapter.java
Author: skitching
Date: Fri Oct 5 05:42:22 2007
New Revision: 582231
URL: http://svn.apache.org/viewvc?rev=582231&view=rev
Log:
Implement "magic beans" in order to provide framework-specific default objects.
Modified:
myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/jsf/JsfFrameworkAdapter.java
Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/jsf/JsfFrameworkAdapter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/jsf/JsfFrameworkAdapter.java?rev=582231&r1=582230&r2=582231&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/jsf/JsfFrameworkAdapter.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/jsf/JsfFrameworkAdapter.java Fri Oct 5 05:42:22 2007
@@ -19,6 +19,8 @@
package org.apache.myfaces.orchestra.frameworkAdapter.jsf;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
import javax.faces.context.FacesContext;
import javax.servlet.ServletRequest;
@@ -28,8 +30,11 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.orchestra.conversation.ConversationMessager;
+import org.apache.myfaces.orchestra.conversation.jsf.JsfConversationMessager;
import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapterInterface;
+import org.apache.myfaces.orchestra.lib.OrchestraException;
/**
* An implementation of the FrameworkAdapter for JSF environments.
@@ -44,6 +49,16 @@
private final static ThreadLocal httpServletResponse = new ThreadLocal();
private final Log log = LogFactory.getLog(JsfFrameworkAdapter.class);
+ private final Map magicBeans = new HashMap();
+
+ public JsfFrameworkAdapter()
+ {
+ // Define magic bean names that cause an instance to be created even when it
+ // is not defined in the IOC configuration.
+ magicBeans.put(
+ ConversationMessager.class.getName(),
+ JsfConversationMessager.class);
+ }
protected FacesContext getFacesContext()
{
@@ -270,9 +285,62 @@
}
Object bean = context.getApplication()
- .getVariableResolver().resolveVariable(context, name);
+ .getVariableResolver().resolveVariable(context, name);
+
+ if (bean == null)
+ {
+ bean = createMagicBean(name);
+ }
return bean;
+ }
+
+ /**
+ * If the bean requested has one of the special "magic" names, then create an instance
+ * from the corresponding hard-wired defaults in this class.
+ * <p>
+ * This provides a kind of "hard wired IOC default". This method is called when the
+ * getBean method cannot locate an instance anywhere else.
+ * <p>
+ * Code throughout Orchestra generally locates resources by calling the getBean method
+ * passing a bean name which is the classname of the implementation required. The user
+ * can then configure an appropriate instance to be returned. If the user does not
+ * define a matching bean then the calling code uses "new" to create an instance of a
+ * suitable default type. Unfortunately that doesn't work when the default type depends
+ * upon what UI framework is being used. Intercepting calls here solves that issue by
+ * ensuring that a suitable default object is returned for this framework but only if
+ * the user has not mapped that name to their own definition.
+ */
+ protected Object createMagicBean(String name)
+ {
+ Class beanClass;
+ synchronized(magicBeans)
+ {
+ beanClass = (Class) magicBeans.get(name);
+ }
+
+ if (beanClass == null)
+ {
+ return null;
+ }
+
+ // Ok, this is indeed a magic bean. Create an instance.
+ // Note that magic beans are always "prototypes"; a new instance
+ // is created for each call to getBean().
+ try
+ {
+ return beanClass.newInstance();
+ }
+ catch(InstantiationException e)
+ {
+ throw new OrchestraException(
+ "Internal error: unable to create instance of special bean " + name, e);
+ }
+ catch(IllegalAccessException e)
+ {
+ throw new OrchestraException(
+ "Internal error: unable to create instance of special bean " + name, e);
+ }
}
public void invokeNavigation(String navigationName)