[jboss-svn-commits] JBL Code SVN: r18728 - labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Mar 6 09:00:10 EST 2008


Author: mingjin
Date: 2008-03-06 09:00:10 -0500 (Thu, 06 Mar 2008)
New Revision: 18728

Modified:
   labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
Log:
Implementation of custom serialization.

Modified: labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
===================================================================
--- labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java	2008-03-06 13:54:51 UTC (rev 18727)
+++ labs/jbossrules/branches/ming-serialization/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java	2008-03-06 14:00:10 UTC (rev 18728)
@@ -16,14 +16,11 @@
  * limitations under the License.
  */
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.Externalizable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.util.Collections;
 import java.util.HashMap;
@@ -136,7 +133,7 @@
     /**
      * Construct.
      *
-     * @param rete
+     * @param id
      *            The rete network.
      */
     public AbstractRuleBase(final String id,
@@ -174,31 +171,21 @@
      * The Package uses PackageCompilationData to hold a reference to the generated bytecode. The generated bytecode must be restored before any Rules.
      *
      */
-    public void doWriteExternal(final ObjectOutput stream,
-                                final Object[] objects) throws IOException {        
-        stream.writeObject( this.pkgs );
+    public void doWriteExternal(final ObjectOutput stream) throws IOException {
         stream.writeObject( this.classLoader.getStore() );
+        stream.writeObject( this.pkgs );
 
         // Rules must be restored by an ObjectInputStream that can resolve using a given ClassLoader to handle seaprately by storing as
         // a byte[]
-        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final ObjectOutput out = new ObjectOutputStream( bos );
-        out.writeObject( this.id );
-        out.writeObject( this.processes );
-        out.writeObject( this.agendaGroupRuleTotals );
-        out.writeObject( this.factHandleFactory );
-        out.writeObject( this.globals );
-        out.writeObject( this.config );
-        
+        stream.writeObject( this.id );
+        stream.writeObject( this.processes );
+        stream.writeObject( this.agendaGroupRuleTotals );
+        stream.writeObject( this.factHandleFactory );
+        stream.writeObject( this.globals );
+        stream.writeObject( this.config );
+
         this.eventSupport.removeEventListener( RuleBaseEventListener.class );
-        out.writeObject( this.eventSupport );
-        
-        for ( int i = 0, length = objects.length; i < length; i++ ) {
-            out.writeObject( objects[i] );
-        }        
-        
-        out.close();
-        stream.writeObject( bos.toByteArray() );
+        stream.writeObject( this.eventSupport );
     }
 
     /**
@@ -207,59 +194,45 @@
      * A custom ObjectInputStream, able to resolve classes against the bytecode in the PackageCompilationData, is used to restore the Rules.
      *
      */
-    public void doReadExternal(final ObjectInput stream,
-                               final Object[] objects) throws IOException,
+    public void doReadExternal(final ObjectInput stream) throws IOException,
                                                       ClassNotFoundException {
-        // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules        
-        this.pkgs = (Map) stream.readObject();
-        Map store = (Map) stream.readObject();
-        
-        if ( stream instanceof DroolsObjectInputStream ) {
-            final DroolsObjectInputStream parentStream = (DroolsObjectInputStream) stream;
-            parentStream.setRuleBase( this );
-            this.packageClassLoader = new CompositePackageClassLoader( parentStream.getClassLoader() );
-            this.classLoader = new MapBackedClassLoader( parentStream.getClassLoader(), store );
-        } else {
-            this.packageClassLoader = new CompositePackageClassLoader( Thread.currentThread().getContextClassLoader() );
-            this.classLoader = new MapBackedClassLoader( Thread.currentThread().getContextClassLoader(), store );
-        }
+        // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
+        DroolsObjectInput droolsStream = stream instanceof DroolsObjectInput
+                          ? (DroolsObjectInput)stream
+                          : new DroolsObjectInputStream(stream);
 
+        droolsStream.setRuleBase( this );
+        Map store = (Map) stream.readObject();
+        this.packageClassLoader = new CompositePackageClassLoader( droolsStream.getClassLoader() );
+        this.classLoader = new MapBackedClassLoader( droolsStream.getClassLoader(), store );
         this.packageClassLoader.addClassLoader( this.classLoader );
         this.objenesis = createObjenesis();
 
-        for ( final Iterator it = this.pkgs.values().iterator(); it.hasNext(); ) {
-            this.packageClassLoader.addClassLoader( ((Package) it.next()).getDialectDatas().getClassLoader() );
-        }
+        droolsStream.setClassLoader(this.packageClassLoader);
 
-        // Return the rules stored as a byte[]
-        final byte[] bytes = (byte[]) stream.readObject();
+        this.pkgs = (Map) stream.readObject();
 
-        //  Use a custom ObjectInputStream that can resolve against a given classLoader
-        final DroolsObjectInputStream childStream = new DroolsObjectInputStream( new ByteArrayInputStream( bytes ),
-                                                                                 this.packageClassLoader );
-        childStream.setRuleBase( this );
+        for ( final Object object : this.pkgs.values()) {
+            this.packageClassLoader.addClassLoader( ((Package) object).getDialectDatas().getClassLoader() );
+        }
+        // PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
+        this.id = (String) stream.readObject();
+        this.processes = (Map) stream.readObject();
+        this.agendaGroupRuleTotals = (Map) stream.readObject();
+        this.factHandleFactory = (FactHandleFactory) stream.readObject();
+        this.globals = (Map) stream.readObject();
+        this.config = (RuleBaseConfiguration) stream.readObject();
 
-        this.id = (String) childStream.readObject();
-        this.processes = (Map) childStream.readObject();
-        this.agendaGroupRuleTotals = (Map) childStream.readObject();
-        this.factHandleFactory = (FactHandleFactory) childStream.readObject();
-        this.globals = (Map) childStream.readObject();
+        this.config.setClassLoader( droolsStream.getClassLoader() );
 
-        this.config = (RuleBaseConfiguration) childStream.readObject();
-        this.config.setClassLoader( childStream.getClassLoader() );
-        this.eventSupport = (RuleBaseEventSupport) childStream.readObject();
-        this.eventSupport.setRuleBase( this );
+        this.eventSupport = (RuleBaseEventSupport) stream.readObject();
 
+        this.eventSupport.setRuleBase( this );
         this.statefulSessions = new ObjectHashSet();
-
-        for ( int i = 0, length = objects.length; i < length; i++ ) {
-            objects[i] = childStream.readObject();
-        }
-        childStream.close();
     }
 
     /**
-     * Creates Objenesis instance for the RuleBase. 
+     * Creates Objenesis instance for the RuleBase.
      * @return a standart Objenesis instanse with caching turned on.
      */
     protected Objenesis createObjenesis() {
@@ -376,7 +349,7 @@
      * network. Before update network each referenced <code>WorkingMemory</code>
      * is locked.
      *
-     * @param pkg
+     * @param newPkg
      *            The package to add.
      */
     public synchronized void addPackage(final Package newPkg) {
@@ -725,7 +698,7 @@
             ExecutorService executor = ExecutorServiceFactory.createExecutorService( this.config.getExecutorService() );
 
             executor.setCommandExecutor( new CommandExecutor( session ) );
-            
+
             ((InternalWorkingMemory) session).setExecutorService( executor );
 
             if ( keepReference ) {




More information about the jboss-svn-commits mailing list