Creator - examples

Printer-friendly versionPrinter-friendly version
How to use the Creator framework

 

Let's assume the default context got updated to this state:

  • environment: "PROD".
  • product type: "BAR1".
  • customer category: "GOLD".
  • current month: 6 .

 

Then the returned Foobar will be an instance of ConcreteFoobar1.

And with this state:

  • environment: "PROD".
  • product type: "BAR2".
  • customer category: "GOLD".
  • current month: 6 .

 

The returned Foobar will be an instance of ConcreteFoobar2.

But what about this state:

  • environment: "PROD".
  • product type: "BAR1".
  • customer category: "GOLD".
  • current month: 1 .

 

An exception will be thrown because no registered context matches with that state. It is not allowed to access a product of type "BAR1" in the second month of a year, as we specified before.

 

The created Foobar object will be created in the default scope (life cycle) "CREATION_REQUEST". Thus the lifetime of that object will depend solely on the client side and not be maintained by the framework and each creation call will create a new instance. But for certain purposes we need other scopes, e.g. a session scope, a singleton scope etc. Please have a look at the interface CreationScopeIdentifier to see the an enumeration of scopes in the Creator framework - currently 12 scopes. Of course we can also define custom scopes. Alternatively, scopes can be nested in each other for an easy custom scope definition.

Scopes can be applied to an object creation very simple within the responsible factory (typically in the default constructor):

addScope(Foobar.class, CREATION_CONTEXT);

With that, all objects of the type Foobar created in that factory will live in the "CREATION_CONTEXT" scope, that is one instance (of that type) per creation context.

 

To integrate the Creator framework (with a server specific Creator extension) with JEE web servers, in order to manage the life cycle of objects scoped to web requests and web sessions, we just add a single entry to the JEE deployment descriptor (usually ../WEB-INF/web.xml). We add the ServletContextListener:

<listener>
    <listener-class>net.swdes.creator.jee.ServletContextListener</listener-class>
</listener>

That's all. The Creator framework will do the rest automatically to manage the life cycle of objects scoped to web requests and web sessions.

 

Furthermore we might want to apply filters to the creation process in order to add a cross cutting behavior, proxies etc. to all creations or by criteria. Even more specific, we might want to preprocess or postprocess a certain creation with a convenient access to the object, method, arguments, context and scope. The Creator framework provides for that AOP-like capabilities, but in pure Java. Have a look at the interfaces CallPreprocessor and CallPostprocessor.

 

Note: the AOP wording is not used in that area - on purpose   wink

 

This was just a short overview of some simple stuff you can do with the Creator framework - so much more to discover...

 

 

Páginas