You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by yi...@apache.org on 2010/10/28 06:29:43 UTC

svn commit: r1028177 - in /openwebbeans/trunk: webbeans-spi/src/main/java/org/apache/webbeans/spi/ webbeans-web/src/main/java/org/apache/webbeans/web/failover/

Author: yingwang
Date: Thu Oct 28 04:29:43 2010
New Revision: 1028177

URL: http://svn.apache.org/viewvc?rev=1028177&view=rev
Log:
[owb-448] resource bean failover service changes. Also move object stream to failover service, so vendor could customize the object streams.

Added:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/OwbProxyObjectInputStream.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/SerializationHandlerV10.java
Modified:
    openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/FailOverService.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/DefaultOwbFailOverService.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/FailOverBagWrapper.java

Modified: openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/FailOverService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/FailOverService.java?rev=1028177&r1=1028176&r2=1028177&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/FailOverService.java (original)
+++ openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/FailOverService.java Thu Oct 28 04:29:43 2010
@@ -18,8 +18,20 @@
  */
 package org.apache.webbeans.spi;
 
+import javax.enterprise.inject.spi.Bean;
 import javax.servlet.http.HttpSession;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.OutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 
+/**
+ * Container provided failover and passivation service.
+ * 
+ */
 public interface FailOverService 
 {
     /**
@@ -36,12 +48,30 @@ public interface FailOverService 
      */
     public String getFailOverAttributeName();
     
+    /**
+     * Whether or not to support failover.
+     * @return
+     */
     public boolean isSupportFailOver();
-    
+
+    /**
+     * Whether or not to support passivation.
+     * @return
+     */
     public boolean isSupportPassivation();
     
+    /**
+     * Enable failover support.
+     * 
+     * @param flag
+     */
     public void enableFailOverSupport(boolean flag);
 
+    /**
+     * Enable passivation support.
+     * 
+     * @param flag
+     */
     public void enablePassivationSupport(boolean flag);
 
     /**
@@ -74,4 +104,52 @@ public interface FailOverService 
      * @param session
      */
     public void sessionWillPassivate(HttpSession session);
+    
+    /**
+     * Container provided object input stream.
+     *  
+     * Note, the stream should support deserializing javassist objects.
+     * 
+     * @return custom object input stream.
+     */
+    public ObjectInputStream getObjectInputStream(InputStream in) throws IOException;
+    
+    /**
+     * Container provided object output stream. 
+     * 
+     * Note, the stream should support serializing javassist objects.
+     * 
+     * @return custom object output stream.
+     */
+    public ObjectOutputStream getObjectOutputStream(OutputStream out) throws IOException;
+
+
+    /**
+     * Container provided custom handler for serialize / deserialize a resource bean. 
+     * Add clean up code in this method will allow OWB to override default resource 
+     * bean passivation behavior. 
+     * 
+     * Note, in the method, a container may first invoke the application provided 
+     * handler(@See SerializationHandler) if it is configured. 
+     * 
+     * @param bean                The resource bean.
+     * @param resourceObject    The resource bean instance
+     * @param in                The input object stream
+     * @param out                The output object stream
+     * 
+     * @return NOT_HANDLED if not handled by handler.
+     */
+    public Object handleResource(
+            Bean<?> bean,
+            Object resourceObject,
+            ObjectInput in,
+            ObjectOutput out
+    );
+
+    /**
+     * Returned, if container or application does not handle the resource object
+     * in the handleResource() method.
+     */
+    public final static Object NOT_HANDLED = new Object();
+
 }

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/DefaultOwbFailOverService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/DefaultOwbFailOverService.java?rev=1028177&r1=1028176&r2=1028177&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/DefaultOwbFailOverService.java (original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/DefaultOwbFailOverService.java Thu Oct 28 04:29:43 2010
@@ -21,8 +21,17 @@ package org.apache.webbeans.web.failover
 import java.util.UUID;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInput;
 import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
 import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+
+import javassist.util.proxy.ProxyObjectOutputStream;
+
+import javax.enterprise.inject.spi.Bean;
 import javax.servlet.http.HttpSession;
 
 import org.apache.webbeans.config.OpenWebBeansConfiguration;
@@ -48,10 +57,17 @@ public class DefaultOwbFailOverService i
     private static final String OWB_FAILOVER_IS_SUPPORT_PASSIVATE = 
         "org.apache.webbeans.web.failover.issupportpassivation";
 
+    private static final String OWB_FAILOVER_RESOURCSES_SERIALIZATION_HANDLER =
+        "org.apache.webbeans.web.failover.resources.serialization.handler.v10";
+    
     boolean isSupportFailOver;
     
     boolean isSupportPassivation;
 
+    SerializationHandlerV10 handler;
+    
+    ThreadLocal<Boolean> isForPassivation = new ThreadLocal<Boolean>();
+    
     public DefaultOwbFailOverService () 
     {
         String value;
@@ -71,6 +87,18 @@ public class DefaultOwbFailOverService i
         if (isSupportFailOver || isSupportPassivation)
         {
             WebBeansUtil.initProxyFactoryClassLoaderProvider();
+            value = OpenWebBeansConfiguration.getInstance().getProperty(OWB_FAILOVER_RESOURCSES_SERIALIZATION_HANDLER);
+            try 
+            {
+                if (value != null) 
+                {
+                    handler = (SerializationHandlerV10) Class.forName(value).newInstance();
+                }
+            } 
+            catch (Exception e) 
+            {
+                logger.debug("DefaultOwbFailOverService could not instanciate: [{0}]", value);
+            }
         }
         
         if (logger.wblWillLogDebug())
@@ -109,6 +137,8 @@ public class DefaultOwbFailOverService i
             // could also be serialized.
             session.setAttribute(getFailOverAttributeName(), bagWrapper);
         }
+        isForPassivation.remove();
+        isForPassivation.set(null);
     }
     
     public void sessionIsInUse(HttpSession session)
@@ -129,6 +159,7 @@ public class DefaultOwbFailOverService i
     {
         FailOverBagWrapper bagWrapper = new FailOverBagWrapper(session, this);
         session.setAttribute(getFailOverAttributeName(), bagWrapper);
+        isForPassivation.set(Boolean.TRUE);
     }
     
     public void restoreBeans(HttpSession session)
@@ -167,6 +198,50 @@ public class DefaultOwbFailOverService i
         isSupportPassivation = flag;
     }
     
+    /**
+     * Get object input stream. Note, the stream should support deserialize javassist objects.
+     * 
+     * @return custom object input stream.
+     */
+    @Override
+    public ObjectInputStream getObjectInputStream(InputStream in) throws IOException 
+    {
+        return new OwbProxyObjectInputStream(in);
+    }
+    
+    /**
+     * Get object output stream. Note, the stream should support deserialize javassist objects.
+     * 
+     * @return custom object output stream.
+     */
+    @Override
+    public ObjectOutputStream getObjectOutputStream(OutputStream out) throws IOException 
+    {
+        return new ProxyObjectOutputStream(out);
+    }
+    
+    /**
+     * Except the EJB remote stub, it is hard to handle other types of resources.
+     * Here we delegate serialization/deserialization to the application provided
+     * SerializationHandler.
+     * 
+     */
+    @Override
+    public Object handleResource(
+            Bean<?> bean,
+            Object resourceObject,
+            ObjectInput in,
+            ObjectOutput out)
+    {
+        if (handler != null) 
+        {
+            return handler.handleResource(bean, resourceObject, in, out, 
+                (Boolean.TRUE == isForPassivation.get()) ? 
+                SerializationHandlerV10.TYPE_PASSIVATION : SerializationHandlerV10.TYPE_FAILOVER);
+        }
+        return NOT_HANDLED;
+    }
+    
     private static void verifyTest(FailOverBagWrapper bagWrapper) 
     {        //test code
         byte[] bytes = getBytes(bagWrapper);

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/FailOverBagWrapper.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/FailOverBagWrapper.java?rev=1028177&r1=1028176&r2=1028177&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/FailOverBagWrapper.java (original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/FailOverBagWrapper.java Thu Oct 28 04:29:43 2010
@@ -21,8 +21,6 @@ package org.apache.webbeans.web.failover
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Externalizable;
-import java.io.InputStream;
-import java.io.ObjectStreamClass;
 import java.io.Serializable;
 import java.io.IOException;
 import java.io.ObjectInput;
@@ -36,9 +34,6 @@ import org.apache.webbeans.corespi.Servi
 import org.apache.webbeans.logger.WebBeansLogger;
 import org.apache.webbeans.spi.FailOverService;
 
-import javassist.util.proxy.ProxyObjectInputStream;
-import javassist.util.proxy.ProxyObjectOutputStream;
-
 /**
  * Use javassist Proxy streams to serialize and restore failover bean bag.
  * 
@@ -46,26 +41,26 @@ import javassist.util.proxy.ProxyObjectO
 public class FailOverBagWrapper implements Serializable, Externalizable 
 {
     /**Logger instance*/
-    private  final WebBeansLogger logger = 
+    protected  final WebBeansLogger logger = 
             WebBeansLogger.getLogger(FailOverBagWrapper.class);
 
     private transient FailOverService failoverService;
 
-    FailOverBag bag;
+    protected FailOverBag bag;
 
-    String sessionId;
+    protected String sessionId;
 
-    boolean isSessionInUse;
+    protected boolean isSessionInUse;
  
     //do not remove, used by serialization. 
     public FailOverBagWrapper()
     {
         failoverService = (FailOverService)ServiceLoader.getService(FailOverService.class);
-
     }
     
     public FailOverBagWrapper(HttpSession session, FailOverService service) 
     {
+        failoverService = service;
         isSessionInUse = false;
         sessionId = session.getId();
         bag = new FailOverBag(session, service);
@@ -112,7 +107,7 @@ public class FailOverBagWrapper implemen
         {
             byte[] buf = (byte[])in.readObject();
             ByteArrayInputStream bais = new ByteArrayInputStream(buf);
-            ObjectInputStream ois = new OwbProxyObjectInputStream(bais);
+            ObjectInputStream ois = failoverService.getObjectInputStream(bais);
             bag = (FailOverBag) ois.readObject();
             ois.close();
         }
@@ -139,47 +134,13 @@ public class FailOverBagWrapper implemen
         ByteArrayOutputStream baos = null;
         ObjectOutputStream oos = null;
         byte[] buf = null;
-        try 
-        {
-            baos = new ByteArrayOutputStream();
-            oos = new ProxyObjectOutputStream(baos);
-            oos.writeObject(bag);
-            oos.flush();
-            buf = baos.toByteArray();
-            oos.close();
-            baos.close();
-            out.writeObject(buf);
-        } 
-        catch (Throwable e) 
-        {
-            e.printStackTrace();
-        }        
-    }
-    
-    /**
-     * A little wrapper class to correct the class loader.
-     */
-    public static class OwbProxyObjectInputStream extends ProxyObjectInputStream 
-    {
-        public OwbProxyObjectInputStream(InputStream in) throws IOException 
-        {
-            super(in);
-        }
-        
-        protected Class<?> resolveClass(ObjectStreamClass desc)
-        throws IOException, ClassNotFoundException
-        {
-            String name = desc.getName();
-            try 
-            {
-                return Class.forName(name, false, 
-                    Thread.currentThread().getContextClassLoader());
-            } 
-            catch (ClassNotFoundException ex) 
-            {
-                return super.resolveClass(desc);
-            }
-        }
+        baos = new ByteArrayOutputStream();
+        oos = failoverService.getObjectOutputStream(baos);
+        oos.writeObject(bag);
+        oos.flush();
+        buf = baos.toByteArray();
+        oos.close();
+        baos.close();
+        out.writeObject(buf);
     }
-
 }

Added: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/OwbProxyObjectInputStream.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/OwbProxyObjectInputStream.java?rev=1028177&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/OwbProxyObjectInputStream.java (added)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/OwbProxyObjectInputStream.java Thu Oct 28 04:29:43 2010
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.web.failover;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectStreamClass;
+
+import javassist.util.proxy.ProxyObjectInputStream;
+
+public class OwbProxyObjectInputStream extends ProxyObjectInputStream 
+{
+    public OwbProxyObjectInputStream(InputStream in) throws IOException 
+    {
+        super(in);
+    }
+    
+    protected Class<?> resolveClass(ObjectStreamClass desc)
+    throws IOException, ClassNotFoundException
+    {
+        String name = desc.getName();
+        try 
+        {
+            return Class.forName(name, false, 
+                Thread.currentThread().getContextClassLoader());
+        } 
+        catch (ClassNotFoundException ex) 
+        {
+            return super.resolveClass(desc);
+        }
+    }
+}

Added: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/SerializationHandlerV10.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/SerializationHandlerV10.java?rev=1028177&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/SerializationHandlerV10.java (added)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/failover/SerializationHandlerV10.java Thu Oct 28 04:29:43 2010
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.web.failover;
+
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+import javax.enterprise.inject.spi.Bean;
+
+/**
+ * Application could implement this interface and register with failover service to handle 
+ * serialization/deserialization of resource beans. Failover serivce will invoke this
+ * handleResource Method then.
+ * 
+ */
+public interface SerializationHandlerV10 
+{
+    /**
+     * failover case.
+     */
+    public final static int TYPE_FAILOVER = 0;
+
+    /**
+     * passivation case.
+     */
+    public final static int TYPE_PASSIVATION = 1;
+        
+    /**
+     * Application provided custom handler for serialize 
+     * and deserialize resource beans.
+     *  
+     * @param bean                The resource bean.
+     * @param resourceObject    The resource bean instance
+     * @param in                The input object stream
+     * @param out                The output object stream
+     * @param type                TYPE_FAILOVER or TYPE_PASSIVATION
+     * 
+     * @return NOT_HANDLED if not handled by handler.
+     */
+    public Object handleResource(
+            Bean<?> bean,
+            Object resourceObject,
+            ObjectInput in,
+            ObjectOutput out,
+            int type
+            );
+
+}