Hitachi Vantara Pentaho Community Wiki
Child pages
  • RuntimeObjectFactory Usage
Skip to end of metadata
Go to start of metadata

5.1 introduces new features for registering Objects and Object References at runtime within the Platform. Below are some usage examples.

Object Registration
import static org.pentaho.platform.api.engine.IPentahoRegistrableObjectFactory.Types;
...
// "Testing" will be registered under all Classes in the inheritance chain (String, Object).
PentahoSystem.registerObject( "Testing", Types.CLASSES );

// "Testing" will be registered under all implementing Interfaces ( CharSequence an Serializable )
PentahoSystem.registerObject( "Testing", Types.INTERFACES );

// "Testing" will be registered under all implemented Interfaces and all Classes in the inheritance chain (String, Object, CharSequence, Serializable).
PentahoSystem.registerObject( "Testing", Types.ALL );

// "Testing" will be registered under just the String Class.
PentahoSystem.registerObject( "Testing", String.class);

// "Testing" will be registered under the String Class and CharSequence Interface
PentahoSystem.registerObject( "Testing", String.class, CharSequence.class);

IPentahoObjectReference registration is much the same as Object registration. Below are some examples of some of the built-in ObjectReference types available out-of-the-box.

SingletonPentahoObjectReference
import static org.pentaho.platform.api.engine.IPentahoRegistrableObjectFactory.Types;
...

// Singleton will always return the same Object it was constructed with
SingletonPentahoObjectReference reference = new SingletonPentahoObjectReference.Builder<String>( String.class ).object( "Hello" ).attributes(
    Collections.<String, Object>singletonMap( "attr1", "value1" ) ).build()
PentahoSystem.registerReference( reference, Types.CLASSES );

PrototypePentahoObjectReference
import static org.pentaho.platform.api.engine.IPentahoRegistrableObjectFactory.Types;
...

// Prototype requires an IObjectCreator which is called for each call to reference.getObject()
PrototypePentahoObjectReference protoReference = new PrototypePentahoObjectReference.Builder<UUID>( UUID.class ).creator(
  new IObjectCreator<UUID>() {
    @Override public UUID create( IPentahoSession session ) {
      return UUID.randomUUID();
    }
  }).build();
PentahoSystem.registerReference( protoReference, UUID.class );

SessionBoundPentahoObjectReference
import static org.pentaho.platform.api.engine.IPentahoRegistrableObjectFactory.Types;
...

// Session-Based will call the IObjectCreator for each unique IPentahoSession it's called with. Subsequent calls with the same session will return the same Object
SessionBoundPentahoObjectReference sessionReference = new SessionBoundPentahoObjectReference.Builder<UUID>( UUID.class ).creator(
  new IObjectCreator<UUID>() {
    @Override public UUID create( IPentahoSession session ) {
      return UUID.randomUUID();
    }
  }).build();
PentahoSystem.registerReference( sessionReference, UUID.class );

IPentahoObjectRegistration

Both PentahoSystem.registerObject and PentahoSystem.registerReference return a IPentahoObjectRegistration handle. This can be used later to de-register the Object or Reference

Registration Handles
IPentahoObjectRegistration handle = PentahoSystem.registerObject( "Testing", Types.CLASSES );

// later when you want to remove this registration
handle.remove();
  • No labels