Using the same model for different applications

A more frequently occurring problem might be the need for using the same model in different applications. A simple solution is to make the model layer a module itself.

The source code of the example is available at examples/chapter5/module-model.

How to do it...

So, create an application and a module:

play new app01
play new-module my-module-model

Change the dependencies.yml to include the module:

require:
    - play
    - modelModules -> my-module-model

repositories:
    - playmodelModules:
        type:       local
        artifact:   "/path/to/my-module-model/"
        contains:
            - modelModules -> *

If you just created your application like the one that we just saw, do not forget to add a database connection in your application configuration. For testing purposes use the in memory database via db=mem.

Put a User model into your module at my-module-model/app/models/User.java:

@Entity
public class User extends Model {

   public String name;
   public String login;
}

Use it like any other model in your controller inside of your application. You can even use all the class reloading features after changing the model.

How it works...

When talking about the directory layout of a module, you have already seen that it also features an app/ directory. However, this directory is not included when a module is compiled and packaged into a JAR file via play build-module. This is intended and should never be changed. This means, a full module not only consists of a compiled jar library, but also of these sources, which are compiled dynamically, when the application is started.

As soon as a Java class is packaged into a JAR file, it cannot be changed anymore on the start of a Play application. The process of bytecode enhancement needs a compiled class file in the filesystem, as this file is modified during the enhancement. Every model class is enhanced, but there are many other classes as well, for example controllers and mailers.

This is also the reason why you cannot reference model classes in your plugin code inside the src/ directory, although this would have been the quickest solution in the registration plugin example. At the time of module compilation they are not enhanced and if they are included in the JAR file, they will never be.

There's more...

The next step about modules is one of the more complex parts: bytecode enhancement.

Learn more about bytecode enhancers

Bytecode enhancement is quite a tricky process and definitely nothing quick to understand. To learn about integration into Play, which makes writing your own enhancers pretty simple, you should check the play.classloading.enhancers package, where several enhancers are already defined.

Check the modules for even more enhancers

If you need more usage example of bytecode enhancers, there are several modules making use of it. It is used mostly in order to resemble the finder methods in the model classes of different persistence layers. Examples for these are the Morphia, Ebean, Siena, and Riak modules.

See also

The next recipe will exclusively deal with two examples of bytecode enhancement in Play and should give you a first impression how to make use of it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.59.198