How to Customize and Extend opentaps 2 with your own OSGi Bundle

From Opentaps Wiki
Revision as of 01:47, 7 March 2012 by Sichen (talk | contribs)
Jump to navigationJump to search

Ok, you've been patient and reading through all the other tutorials. But at some point, you've probably wondered -- what's the big idea here?

This tutorial will show you.

The most important thing for successful enterprise software is modularity: Separate and maintainable modules for each part of the application. OSGi lets you extend and customize your application with your own bundle. Even better, it lets you manage those bundles in real time, so you can manage your application by loading and unloading bundles from the Geronimo console.

In this tutorial, we will add security permissions to our opentaps 2 Notes application and then show you how to implement an OSGi bundle which overrides the base permission with your own. You can then see how you can deploy and undeploy the bundle in real time to change the security permissions of your system without restarting the server.

The first step is to create a new package, org.opentaps.notes.security, and a new interface NoteSecurity. All it does is define some operations and a security method:

public interface NoteSecurity {

    public static enum Operation {CREATE, UPDATE, DELETE};
    
    public boolean hasPermission(Note note, Operation permissionId);

    public String getErrorMessage();    
}

Then, I implement my security feature with a new module in notes/impl/security. I follow the steps from [[]] to create a new module. The code itself is trivial:

public class NoteSecurityImpl implements NoteSecurity {

    @Override
    public boolean hasPermission(Note note, String permissionId) {
        Log.logInfo("This test implementation will always return true, so I will let you create your note.");
        return true;
    }

}

Next, I modify my CreateNoteServiceImpl to check security before creating the Note:

        if (!security.hasPermission(note, NoteSecurity.Operation.CREATE)) {
            throw new ServiceException(security.getErrorMessage());
        }

TODO:

  • How to wire security into CreateNoteServiceImpl
  • How to use blueprint.xml to define the service