You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by im...@apache.org on 2007/09/05 11:03:13 UTC

svn commit: r572904 - /myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/lib/jsf/OrchestraApplicationFactory.java

Author: imario
Date: Wed Sep  5 02:03:12 2007
New Revision: 572904

URL: http://svn.apache.org/viewvc?rev=572904&view=rev
Log:
use a proxy instead of inheritance to deal with JSF api changes

Modified:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/lib/jsf/OrchestraApplicationFactory.java

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/lib/jsf/OrchestraApplicationFactory.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/lib/jsf/OrchestraApplicationFactory.java?rev=572904&r1=572903&r2=572904&view=diff
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/lib/jsf/OrchestraApplicationFactory.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/lib/jsf/OrchestraApplicationFactory.java Wed Sep  5 02:03:12 2007
@@ -18,11 +18,27 @@
  */
 package org.apache.myfaces.orchestra.lib.jsf;
 
+import org.aopalliance.intercept.MethodInterceptor;
+import org.aopalliance.intercept.MethodInvocation;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
+import org.springframework.aop.framework.ProxyFactory;
+
+import javax.faces.FacesException;
 import javax.faces.application.Application;
 import javax.faces.application.ApplicationFactory;
+import javax.faces.convert.Converter;
 
 /**
- * @see OrchestraApplication
+ * Proxy the orignial application and provide the following enhancements:
+ * <ul>
+ *     <li>Create a converter based on a spring configuration.
+ *         <br />
+ *         This allows you to use the spring dependency mechanism and thus get in touch to e.g. the
+ *         persistence context.
+ *     </li>
+ * </ul>
+ *
+ * Notice: we use a proxy here to be compatible with JSF 1.1 and 1.2.
  */
 public class OrchestraApplicationFactory extends ApplicationFactory
 {
@@ -31,7 +47,32 @@
 	public OrchestraApplicationFactory(ApplicationFactory original)
 	{
 		this.original = original;
-		original.setApplication(new OrchestraApplication(original.getApplication()));
+
+		Application application = original.getApplication();
+
+		ProxyFactory factory = new ProxyFactory(application);
+		// factory.setProxyTargetClass(true);
+		factory.addAdvice(new MethodInterceptor()
+		{
+			public Object invoke(MethodInvocation methodInvocation) throws Throwable
+			{
+				Object[] arguments = methodInvocation.getArguments();
+				String methodName = methodInvocation.getMethod().getName();
+				if ("createConverter".equals(methodName) &&
+					arguments != null && arguments.length == 1 &&
+					arguments[0] instanceof String)
+				{
+					String converterId = (String) arguments[0];
+
+					return createConverter(converterId, methodInvocation);
+				}
+
+				return methodInvocation.proceed();
+			}
+		});
+		Application orchestraApplication = (Application) factory.getProxy();
+
+		original.setApplication(orchestraApplication);
 	}
 
 	public Application getApplication()
@@ -42,5 +83,25 @@
 	public void setApplication(Application application)
 	{
 		original.setApplication(application);
+	}
+
+	private Converter createConverter(String converterId, MethodInvocation methodInvocation) throws Throwable
+	{
+		try
+		{
+			return (Converter) methodInvocation.proceed();
+		}
+		catch (FacesException e)
+		{
+			// looks like there is no converter configured, try to find a spring-configured one
+			Converter converter = (Converter) FrameworkAdapter.getInstance().getBean(converterId);
+			if (converter == null)
+			{
+				// no spring converter ... now re-throw the exception
+				throw e;
+			}
+
+			return new SerializableConverter(converterId, converter);
+		}
 	}
 }