Class MarshallerFactory

  extended by org.drools.marshalling.MarshallerFactory

public class MarshallerFactory
extends Object

The MarshallerFactory is used to marshal and unmarshal StatefulKnowledgeSessions. At the simplest it can be used as follows:

 // ksession is the StatefulKnowledgeSession
 // kbase is the KnowledgeBase
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 Marshaller marshaller = MarshallerFactory.newMarshaller( kbase );
 marshaller.marshall( baos, ksession );

However with marshalling you need more flexibility when dealing with referenced user data. To achieve this we have the ObjectMarshallingStrategy interface. Two implementations are provided, but the user can implement their own. The two supplied are IdentityMarshallingStrategy and SerializeMarshallingStrategy. SerializeMarshallingStrategy is the default, as used in the example above and it just calls the Serializable or Externalizable methods on a user instance. IdentityMarshallingStrategy instead creates an int id for each user object and stores them in a Map the id is written to the stream. When unmarshalling it simply looks to the IdentityMarshallingStrategy map to retrieve the instance. This means that if you use the IdentityMarshallingStrategy it's stateful for the life of the Marshaller instance and will create ids and keep references to all objects that it attempts to marshal. Here is he code to use a IdentityMarshallingStrategy.

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 Marshaller marshaller = MarshallerFactory.newMarshaller( kbase, new ObjectMarshallingStrategy[] { MarshallerFactory.newIdentityMarshallingStrategy() } );
 marshaller.marshall( baos, ksession );

For added flexability we can't assume that a single strategy is suitable for this we have added the ObjectMarshallingStrategyAcceptor interface that each ObjectMarshallingStrategy has. The Marshaller has a chain of strategies and when it attempts to read or write a user object it iterates the strategies asking if they accept responsability for marshalling the user object. One one implementation is provided the ClassFilterAcceptor. This allows strings and wild cards to be used to match class names. The default is "*.*", so in the above the IdentityMarshallingStrategy is used which has a default "*.*" acceptor.

But lets say we want to serialise all classes except for one given package, where we will use identity lookup, we could do the following:

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 ObjectMarshallingStrategyAcceptor identityAceceptor = MarshallerFactory.newClassFilterAcceptor( new String[] { "org.domain.pkg1.*" } );
 ObjectMarshallingStrategy identityStratetgy = MarshallerFactory.newIdentityMarshallingStrategy( identityAceceptor );
 Marshaller marshaller = MarshallerFactory.newMarshaller( kbase, new ObjectMarshallingStrategy[] { identityStratetgy, MarshallerFactory.newSerializeMarshallingStrategy() } );
 marshaller.marshall( baos, ksession );

Note that the acceptance checking order is in the natural order of the supplied array.

Constructor Summary
Method Summary
static ObjectMarshallingStrategyAcceptor newClassFilterAcceptor(String[] patterns)
static ObjectMarshallingStrategy newIdentityMarshallingStrategy()
static ObjectMarshallingStrategy newIdentityMarshallingStrategy(ObjectMarshallingStrategyAcceptor acceptor)
static Marshaller newMarshaller(KnowledgeBase kbase)
          Default uses the serialise marshalling strategy.
static Marshaller newMarshaller(KnowledgeBase kbase, ObjectMarshallingStrategy[] strategies)
static ObjectMarshallingStrategy newSerializeMarshallingStrategy()
static ObjectMarshallingStrategy newSerializeMarshallingStrategy(ObjectMarshallingStrategyAcceptor acceptor)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public MarshallerFactory()
Method Detail


public static ObjectMarshallingStrategyAcceptor newClassFilterAcceptor(String[] patterns)


public static ObjectMarshallingStrategy newIdentityMarshallingStrategy()


public static ObjectMarshallingStrategy newIdentityMarshallingStrategy(ObjectMarshallingStrategyAcceptor acceptor)


public static ObjectMarshallingStrategy newSerializeMarshallingStrategy()


public static ObjectMarshallingStrategy newSerializeMarshallingStrategy(ObjectMarshallingStrategyAcceptor acceptor)


public static Marshaller newMarshaller(KnowledgeBase kbase)
Default uses the serialise marshalling strategy.



public static Marshaller newMarshaller(KnowledgeBase kbase,
                                       ObjectMarshallingStrategy[] strategies)

Copyright © 2001-2011 JBoss Inc.. All Rights Reserved.