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:31:00 UTC

svn commit: r1028178 - in /openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans: component/ResourceBean.java logger/WebBeansLogger.java proxy/ResourceProxyHandler.java

Author: yingwang
Date: Thu Oct 28 04:31:00 2010
New Revision: 1028178

URL: http://svn.apache.org/viewvc?rev=1028178&view=rev
Log:
[owb-448] resource bean serialization changes. Also make WebBeansLogger serializable. 

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/logger/WebBeansLogger.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ResourceProxyHandler.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java?rev=1028178&r1=1028177&r2=1028178&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java Thu Oct 28 04:31:00 2010
@@ -62,7 +62,7 @@ public class ResourceBean<X, T extends A
             this.actualResourceReference = resourceService.getResourceReference(this.resourceReference);
 
             instance = (X)(JavassistProxyFactory.getInstance().getProxyClass(proxyFactory).newInstance());
-            ((ProxyObject)instance).setHandler(new ResourceProxyHandler(this.actualResourceReference));
+            ((ProxyObject)instance).setHandler(new ResourceProxyHandler(this, this.actualResourceReference));
         }
         catch (Exception e)
         {
@@ -85,7 +85,19 @@ public class ResourceBean<X, T extends A
         this.actualResourceReference = null;
     }
 
-
+    /**
+     * Called after deserialization to get a new instance for some type of resource bean instance that are
+     * not serializable.
+     * 
+     * @return a new instance of this resource bean.
+     */
+    public X getActualInstance() 
+    {
+        ResourceInjectionService resourceService = ServiceLoader.getService(ResourceInjectionService.class);
+        this.actualResourceReference = resourceService.getResourceReference(this.resourceReference);
+        return actualResourceReference;
+    }
+    
     public boolean isPassivationCapable()
     {
         return true;

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/logger/WebBeansLogger.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/logger/WebBeansLogger.java?rev=1028178&r1=1028177&r2=1028178&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/logger/WebBeansLogger.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/logger/WebBeansLogger.java Thu Oct 28 04:31:00 2010
@@ -24,6 +24,11 @@ package org.apache.webbeans.logger;
 import java.util.logging.Logger;
 import java.util.logging.Level;
  
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.Serializable;
 import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.MissingResourceException;
@@ -38,7 +43,7 @@ import java.util.ResourceBundle;
  *
  * @version $Rev$ $Date$
  */
-public final class WebBeansLogger
+public final class WebBeansLogger implements Serializable, Externalizable
 {
     /** Log level mappings from OWB DEBUG, TRACE, INFO, WARN, ERROR, FATAL to whatever log
      *  levels the currently loaded logger supports (i.e. JUL provides FINEST, FINER, FINE,
@@ -53,12 +58,13 @@ public final class WebBeansLogger
     public final static Level WBL_FATAL = Level.SEVERE;
        
     /** Inner logger object to log actual log messages */
-    private Logger logger = null;
-    private ResourceBundle wbBundle = null;
+    private transient Logger logger = null;
+    private transient ResourceBundle wbBundle = null;
     private Class<?> caller = null;
+    private Locale locale = null;
     
     /** Private constructor */
-    private WebBeansLogger()
+    public WebBeansLogger()
     {
         wbBundle = ResourceBundle.getBundle("openwebbeans/Messages");
     }
@@ -90,6 +96,7 @@ public final class WebBeansLogger
     {
         WebBeansLogger wbLogger = new WebBeansLogger();
         wbLogger.caller = clazz;
+        wbLogger.locale = desiredLocale;
         Logger inLogger = Logger.getLogger(clazz.getName(), ResourceBundle.getBundle("openwebbeans/Messages", desiredLocale).toString());
         wbLogger.setLogger(inLogger);
 
@@ -303,4 +310,28 @@ public final class WebBeansLogger
         return (logger.isLoggable(WebBeansLogger.WBL_TRACE));
     }
 
+    @Override
+    public void writeExternal(ObjectOutput out) throws IOException 
+    {
+        out.writeObject(caller);
+        out.writeObject(locale);
+    }
+
+    @Override
+    public void readExternal(ObjectInput in) throws IOException,
+            ClassNotFoundException 
+    {
+        caller = (Class<?>)in.readObject();
+        locale = (Locale)in.readObject();
+        Logger inLogger = null;
+        if (locale == null) 
+        {
+            inLogger = Logger.getLogger(caller.getName(),"openwebbeans/Messages");
+        } 
+        else
+        {
+            inLogger = Logger.getLogger(caller.getName(), ResourceBundle.getBundle("openwebbeans/Messages", locale).toString());
+        }
+        this.setLogger(inLogger);
+    }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ResourceProxyHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ResourceProxyHandler.java?rev=1028178&r1=1028177&r2=1028178&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ResourceProxyHandler.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ResourceProxyHandler.java Thu Oct 28 04:31:00 2010
@@ -18,26 +18,45 @@
  */
 package org.apache.webbeans.proxy;
 
-import java.io.IOException;
-import java.io.Serializable;
 import java.io.Externalizable;
-import java.io.ObjectOutput;
+import java.io.IOException;
 import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.Serializable;
 import java.lang.reflect.Method;
 
+import org.apache.webbeans.component.ResourceBean;
+import org.apache.webbeans.container.BeanManagerImpl;
+import org.apache.webbeans.corespi.ServiceLoader;
+import org.apache.webbeans.spi.FailOverService;
+    
 import javassist.util.proxy.MethodHandler;
 
 public class ResourceProxyHandler implements MethodHandler, Serializable, Externalizable
 {
-    private Object actualResource = null;
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 4171212030439910547L;
+    
+    /**
+     * When ResourceProxyHandler deserialized, this will instruct owb to create a new actual instance, if
+     * the actual resource is not serializable.
+     */
+    private static final String DUMMY_STRING = "owb.actual.resource.dummy";
     
+    private Object actualResource = null;
+
+    private transient ResourceBean bean = null;
+
     //DO NOT REMOVE, used by failover and passivation.
-    public ResourceProxyHandler() 
+    public ResourceProxyHandler()
     {
     }
-
-    public ResourceProxyHandler(Object actualResource)
+    
+    public ResourceProxyHandler(ResourceBean bean, Object actualResource)
     {
+        this.bean = bean;
         this.actualResource = actualResource;
     }
     
@@ -47,34 +66,80 @@ public class ResourceProxyHandler implem
         return actualMethod.invoke(this.actualResource, args);
     }
 
+    /**
+     * When serialized, first try container provided failover service. If the failover service 
+     * does not handle the actual instance, the default behavior is:
+     * 1. If actual object is serializable, then serialize it directly.
+     * 2. If not, serialize the DUMMY_STRING.
+     */
     @Override
     public void writeExternal(ObjectOutput out) throws IOException 
-    {
-            if (actualResource instanceof javax.rmi.CORBA.Stub)
-            {
-                out.writeObject(actualResource);
-            }
-            else
+    {        
+        // write bean id first
+        out.writeObject(bean.getId());
+
+        // try fail over service to serialize the resource object
+        FailOverService failoverService = (FailOverService) ServiceLoader.getService(FailOverService.class);
+        if (failoverService != null)
+        {
+            Object ret = failoverService.handleResource(bean, actualResource, null, out);
+            if (ret != failoverService.NOT_HANDLED)
             {
-                //TODO: for other type of resources, I am planning to add some
-                // custom property to control whether owb should (de)serialize
-                // the object directly or grab a new instance from resource
-                // service (on the other jvm.).
-                out.writeObject(null);
+                return;
             }
+        }
+        
+        // default behavior
+        if (actualResource instanceof Serializable)
+        {
+            // for remote ejb stub and other serializable resources
+            out.writeObject(actualResource);
+        } 
+        else 
+        {
+            // otherwise, write a dummy string. 
+            out.writeObject(DUMMY_STRING);
+        }
     }
-    
+
+    /**
+     * When deserialized, first try container provided failover service. If the failover service does not 
+     * handle the actual instance, the default behavior is:
+     * 1. Read the object from the stream,
+     * 2. If the object is renote ejb stub, reconnect it.
+     * 3. if the object is DUMMY_STRING, invoke ResourceBean.getActualInstance to get a new instance of the resource.
+     */
     @Override
     public void readExternal(ObjectInput in) throws IOException,
             ClassNotFoundException 
     {
+        String id = (String)in.readObject();
+        bean = (ResourceBean)BeanManagerImpl.getManager().getPassivationCapableBean(id);
+        
+        // try fail over service to serialize the resource object
+        FailOverService failoverService = (FailOverService)
+        ServiceLoader.getService(FailOverService.class);
+        if (failoverService != null) 
+        {
+            actualResource = failoverService.handleResource(bean, actualResource, in, null);
+            if (actualResource != failoverService.NOT_HANDLED)
+            {
+                return;
+            }
+        }
+
+        // default behavior
         Object obj = in.readObject();
-        if (obj != null && obj instanceof javax.rmi.CORBA.Stub)
+        if (obj instanceof javax.rmi.CORBA.Stub) 
         {
+            // for remote ejb stub, reconnect after deserialization.
             actualResource = obj;
             org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(new String[0], null);
             ((javax.rmi.CORBA.Stub)actualResource).connect(orb);
+        } 
+        else if (obj.equals(DUMMY_STRING))
+        {
+            actualResource = bean.getActualInstance();
         }
-    }
-    
+    }    
 }