Developing Juzu portlets – Step 5: Saving Secrets in the JCR


Previous steps:
Learn how to develop great Juzu portlets for eXo Platform!
Step 2: viewing and posting secrets
Step 3: Building a sexy secret wall
Step 4: adding Likes and Comments for Secret

Thanks to the previous steps, we have a JuZcret application with some nice features. However, we’re saving our secret in memory, which is good for testing but not for production. It’s time to learn how to save our secret in eXo JCR. During this step, we will implement a new secret service to save all secrets in JCR instead of memory.

eXo services and eXo JCR storage

JuZcret JCR NodeType declaration

First, we will create a JCR node type definition.

Note: We will not focus on the eXo JCR API in this tutorial but on how to leverage JCR support from eXo Platform to develop Juzu Portlet. We will not explain in detail the JCR node type definition below. If you need more information to understand the code below, please take a look at the eXo JCR website.

We will define the exo:secret and exo:secretComment node types. Their properties reflect our JuZcret domain classes: Secret and Comment.

Create a new file secret-nodetypes.xml in src/main/webapp/WEB-INF/conf:

When secret-nodetypes.xml file is ready, we need to register it to the eXo JCR service. Add this new eXo container configuration.xml file in /src/main/webapp/WEB-INF/conf/:

This configuration registers a node type plugin with eXo RepositoryService, which will parse our node type at war: /conf/secret-nodetypes.xml path.

The only thing missing now is to make sure that eXo Platform will scan and process our configuration.xml file in tutorial-juzcret webapp when it initializes the eXo container. This task is not specific to Juzu; it’s about configuring a webapp as an eXo Platform extension (for more details about extensions, please look at the eXo documentation).

First, we need to modify the web.xml file:

eXo container will need to know which webapp container contains its configuration files. By adding thePortalContainerConfigOwner, a servlet context listener, we’ve registered the JuZcret webapp context to the eXo container to scan and process the xml configuration file. Notice that we also need to declare the <display-name> tag. The eXo container uses that information to map the registered webapp.

Finally, we configure the JuZcret as a dependency of the eXo container. Even though we’ve registered the webapp context, we still need to tell the eXo container that the JuZcret webapp is a portal container definition dependency. There are two places to add the configuration:

  • TOMCAT/gatein/conf/configuration.xml
  • Create a jar file that contains /conf/configuration.xml and put it into tomcat/lib.

Note: We use the first solution in this tutorial, as it’s quicker for the purpose of the tutorial, which is not about eXo Platform extension. For your next application, follow the official documentation and create a specific jar containing this configuration.xml file.

Let’s modify the TOMCAT/gatein/conf/configuration.xml file:

Note: It’s important to declare our application before the import of jar:/conf/platform/configuration.xml.

We finish declaring all necessary JCR node types for JuZcret, and we add the JuZcret webapp context as a portal container definition dependency.

Now we can configure the eXo JCR service.

Binding the eXo JCR service

First, we need to declare dependency on the eXo kernel. Add exo.jcr.component.ext to the project pom.xml:

It’s time for us to implement the new secret service with the JCR API. Let’s create a new SecretServiceJCRImpl.javaclass in the package:

sessionService and nodeCreator are service components created by the eXo container. There must be a bridge between eXo Platform’s container (the eXo container) and JuZcret’s IOC container (Guice). This means that, before using it, we need to bind the necessary services in

The secret service implementation is now the JCR version (we update the implementation of SecretService.class to SecretServiceJCRImpl.class instead of SecretServiceMemImpl.class).

Notice that there is no declaration for the implementation class of SessionProviderService and NodeHierarchyCreator. We only declare the interfaces because JuZcret’s IOC container will not instantiate those services itself but retrieve them from eXo’s container by delegating the call to KernelProviderFactory (we declared this in step 4 using the service loader).

JCR service implementation

All necessary services are now managed by JuZcret’s container, and they are ready to be injected and used.

The NodeHierarchyCreator service will help us to initialize our JuZcret application’s JCR data structure.

We create the root node of the JuZcret application by adding this method to our secret service:

By calling the nodeCreator#getPublicApplicationNode method, we get the typical place to put application data. If it’s the first time running JuZcret, we create a new SECRET_APP nt:unstructured node type. This node will then contain children with the node type exo:secret. This node type should reflect our Secret domain class, and then we must declare this to the eXo JCR service (we’ll come back to this part later).

Now we get the root application node; let’s implement the function to add a new secret:

It’s a pretty simple JCR API, and actually, there is no Juzu stuff here. The most important thing here is getting the root node using the getSecretHome method.

There are some other similar methods that we need to implement: addComment, addLike, and getSecrets:

That’s all. The secret JCR service is now ready to use.

Now re-compile and deploy JuZcret eXo Platform as explained in a previous step of this tutorial:

Copy/paste the war (replace the old one) in the webapp folder of eXo Platform, start the server, and open the JuZcret page created in step 1.

All features – sharing a secret, adding a comment, etc. – should run similarly to the previous memory service implementation, except that we don’t lose a shared secret or comment after restarting the server. The data is now really persistent!

Next week, we will continue to improve our JuZcret application by adding internationalization.

The final source of step 5 is available for downloading on Github.

Previous and next steps:
Learn how to develop great Juzu portlets for eXo Platform!
Step 2: viewing and posting secrets
Step 3: Building a sexy secret wall
Step 4: adding Likes and Comments for Secret
Step 5: Saving Secrets in the JCR
Step 6: Let the whole world create new secrets
Step 7: Finishing the job properly with unit test

Join the eXo tribe by registering for the community and get updates, tutorials, support, and access to the Platform and add-on downloads!

Make the most out of eXo Platform 4

Register to the next webinar and get a complete overview of what you can do with eXo Platform 4. Reserve your seat now!

Be part of the discussion, share your comments


Keep in touch with the author

Tags: , , , , , , ,