quarta-feira, 8 de abril de 2015

Liferay - Create endpoints in jBoss 7.1.1

Just like a properties file, the advantage of using JNDI in an application server is evident. You can define your application to read certain bindings only by name. If you ever have to change the application server, you'll only have to define the same bindings again - no application changes will be needed.

In jBoss 7.1.1 it's pretty straight forward to create bindings.

Acess your standalone configuration file (standalone.xml) and look for the

        <subsystem xmlns="urn:jboss:domain:naming:1.1">


tag.

There you'll define two differente tags:

- a Simple tag, where you define you property as key/value
- a Lookup tag, that will be used to map your property to a more user-friendly name. That name will be exposed in the JNDI context and accessible in your java classes.

Here is an example:
        
<subsystem xmlns="urn:jboss:domain:naming:1.1">
 <bindings>
  <simple name="java:jboss/applicationmode" type="java.lang.String" value="DEMO"></simple>
  <lookup name="property/applicationmode" lookup="java:jboss/applicationmode"/> 
 </bindings>
</subsystem>

And then, in your java class, instatiate InitialContext and get the property by its exposed name:

InitialContext ctx = new InitialContext();

String applicationEnv = (String) ctx.lookup("property/applicationenvironment");