You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2006/04/27 08:32:21 UTC

svn commit: r397436 - in /myfaces: core/trunk/impl/src/main/java/org/apache/myfaces/webapp/ shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/ shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/ shared/trunk/core/src/tes...

Author: dennisbyrne
Date: Wed Apr 26 23:32:20 2006
New Revision: 397436

URL: http://svn.apache.org/viewcvs?rev=397436&view=rev
Log:
MYFACES-1291 added pluggable serialization factory in order to avoid compile time dep on providers

this commit is cross project, so it might make Continuum mad

Added:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/DefaultSerialFactory.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/SerialFactory.java
Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/StateUtils.java
    myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/AbstractStateUtilsTest.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java
URL: http://svn.apache.org/viewcvs/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java?rev=397436&r1=397435&r2=397436&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/webapp/StartupServletContextListener.java Wed Apr 26 23:32:20 2006
@@ -27,7 +27,10 @@
 import org.apache.myfaces.config.FacesConfigValidator;
 import org.apache.myfaces.config.FacesConfigurator;
 import org.apache.myfaces.context.servlet.ServletExternalContextImpl;
+import org.apache.myfaces.shared_impl.util.ClassUtils;
 import org.apache.myfaces.shared_impl.util.StateUtils;
+import org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory;
+import org.apache.myfaces.shared_impl.util.serial.SerialFactory;
 import org.apache.myfaces.shared_impl.webapp.webxml.WebXml;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -98,9 +101,47 @@
         if(servletContext.getInitParameter(StateUtils.INIT_SECRET) != null)
             StateUtils.initSecret(servletContext);
         
+        handleSerialFactory(servletContext);
     }
 
-
+    private static void handleSerialFactory(ServletContext servletContext){
+        
+        String serialProvider = servletContext.getInitParameter(StateUtils.SERIAL_FACTORY);
+        SerialFactory serialFactory = null;
+        
+        if(serialProvider == null)
+        {
+            serialFactory = new DefaultSerialFactory();
+        }
+        else
+        {
+            try
+            {
+                serialFactory = (SerialFactory) ClassUtils.newInstance(serialProvider);
+                
+            }catch(ClassCastException e){
+                log.error("Make sure '" + serialProvider + 
+                        "' implements the correct interface", e);
+            }
+            catch(Exception e){
+                log.error(e);
+            }
+            finally
+            {
+                if(serialFactory == null)
+                {
+                    serialFactory = new DefaultSerialFactory();
+                    log.error("Using default serialization provider");
+                }
+            }
+            
+        }
+        
+        log.info("Serialization provider : " + serialFactory.getClass());
+        servletContext.setAttribute(StateUtils.SERIAL_FACTORY, serialFactory);
+        
+    }
+    
     public void contextDestroyed(ServletContextEvent e)
     {
         FactoryFinder.releaseFactories();

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/StateUtils.java
URL: http://svn.apache.org/viewcvs/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/StateUtils.java?rev=397436&r1=397435&r2=397436&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/StateUtils.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/StateUtils.java Wed Apr 26 23:32:20 2006
@@ -15,6 +15,8 @@
  */
 package org.apache.myfaces.shared.util;
 
+import org.apache.myfaces.shared.util.serial.SerialFactory;
+
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -100,6 +102,8 @@
     public static final String INIT_ALGORITHM_IV = INIT_PREFIX + "algorithm.iv";
     public static final String INIT_ALGORITHM_PARAM = INIT_PREFIX + "algorithm.parameters";
     
+    public static final String SERIAL_FACTORY = INIT_PREFIX + "SERIAL_FACTORY";
+    
     /** Utility class, do not instatiate */
     private StateUtils()
     {
@@ -133,7 +137,7 @@
      */
 
     public static final String construct(Object object, ExternalContext ctx){
-        byte[] bytes = getAsByteArray(object);
+        byte[] bytes = getAsByteArray(object, ctx);
         if(isSecure(ctx))
                 bytes = encrypt(bytes, ctx);
         bytes = compress(bytes);
@@ -148,12 +152,20 @@
         }
     }
 
-    public static final byte[] getAsByteArray(Object object)
+    public static final byte[] getAsByteArray(Object object, ExternalContext ctx)
     {
     	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        
+        // get the Factory that was instantiated @ startup
+        SerialFactory serialFactory = (SerialFactory) ctx.getApplicationMap().get(SERIAL_FACTORY);
+        
+        if(serialFactory == null)
+            throw new NullPointerException("serialFactory");
+        
         try
         {
-            ObjectOutputStream writer = new ObjectOutputStream(outputStream);
+            ObjectOutputStream writer = serialFactory.getObjectOutputStream(outputStream);
+            //new ObjectOutputStream(outputStream);
             writer.writeObject(object);
             byte[] bytes = outputStream.toByteArray();
             writer.close();
@@ -214,7 +226,7 @@
             bytes = decompress(bytes);
             if(isSecure(ctx))
                 bytes = decrypt(bytes, ctx);
-            return getAsObject(bytes);
+            return getAsObject(bytes, ctx);
         }
         catch (UnsupportedEncodingException e)
         {
@@ -264,13 +276,20 @@
 
     }
 
-    public static final Object getAsObject(byte[] bytes)
+    public static final Object getAsObject(byte[] bytes, ExternalContext ctx)
     {
     	ByteArrayInputStream input = new ByteArrayInputStream(bytes);
 
+        // get the Factory that was instantiated @ startup
+        SerialFactory serialFactory = (SerialFactory) ctx.getApplicationMap().get(SERIAL_FACTORY);
+        
+        if(serialFactory == null)
+            throw new NullPointerException("serialFactory");
+        
     	try
         {
-            ObjectInputStream s = new MyFacesObjectInputStream(input);
+            ObjectInputStream s = serialFactory.getObjectInputStream(input); 
+            //new MyFacesObjectInputStream(input);
             Object object = s.readObject();
             s.close();
             input.close();

Added: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/DefaultSerialFactory.java
URL: http://svn.apache.org/viewcvs/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/DefaultSerialFactory.java?rev=397436&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/DefaultSerialFactory.java (added)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/DefaultSerialFactory.java Wed Apr 26 23:32:20 2006
@@ -0,0 +1,28 @@
+package org.apache.myfaces.shared.util.serial;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+
+import org.apache.myfaces.shared.util.MyFacesObjectInputStream;
+
+/**
+ * @author Dennis C. Byrne
+ */
+
+public class DefaultSerialFactory implements SerialFactory
+{
+
+    public ObjectOutputStream getObjectOutputStream(OutputStream outputStream) throws IOException
+    {
+        return new ObjectOutputStream(outputStream);
+    }
+
+    public ObjectInputStream getObjectInputStream(InputStream inputStream) throws IOException
+    {
+        return new MyFacesObjectInputStream(inputStream);
+    }
+    
+}

Added: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/SerialFactory.java
URL: http://svn.apache.org/viewcvs/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/SerialFactory.java?rev=397436&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/SerialFactory.java (added)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/serial/SerialFactory.java Wed Apr 26 23:32:20 2006
@@ -0,0 +1,17 @@
+package org.apache.myfaces.shared.util.serial;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+
+/**
+ * @author Dennis C. Byrne
+ */
+
+public interface SerialFactory
+{
+    public ObjectOutputStream getObjectOutputStream(OutputStream outputStream) throws IOException;
+    public ObjectInputStream getObjectInputStream(InputStream inputStream) throws IOException;
+}

Modified: myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/AbstractStateUtilsTest.java
URL: http://svn.apache.org/viewcvs/myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/AbstractStateUtilsTest.java?rev=397436&r1=397435&r2=397436&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/AbstractStateUtilsTest.java (original)
+++ myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/util/AbstractStateUtilsTest.java Wed Apr 26 23:32:20 2006
@@ -24,6 +24,7 @@
 
 import org.apache.shale.test.base.AbstractJsfTestCase;
 import org.apache.myfaces.shared.util.StateUtils;
+import org.apache.myfaces.shared.util.serial.DefaultSerialFactory;
 
 import java.io.Serializable;
 import java.util.Arrays;
@@ -49,6 +50,7 @@
     {
     	super.setUp();
         sensitiveString = "this is my secret";
+        externalContext.getApplicationMap().put(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
     }
 
     public void tearDown()
@@ -84,8 +86,8 @@
 
     public void testSerialization()
     {
-        byte[] bytes = StateUtils.getAsByteArray(TEST_DATA);
-        Object object = StateUtils.getAsObject(bytes);
+        byte[] bytes = StateUtils.getAsByteArray(TEST_DATA, externalContext);
+        Object object = StateUtils.getAsObject(bytes, externalContext);
         assertTrue(TEST_DATA.equals(object));
     }
 
@@ -143,11 +145,11 @@
 
     public void testSerializationNegative()
     {
-        byte[] bytes = StateUtils.getAsByteArray(TEST_DATA);
+        byte[] bytes = StateUtils.getAsByteArray(TEST_DATA, externalContext);
         bytes[1] = (byte) 3;
         try
         {
-            Object object = StateUtils.getAsObject(bytes);
+            Object object = StateUtils.getAsObject(bytes, externalContext);
             assertFalse(TEST_DATA.equals(object));
         }
         catch (Exception e)