gif animé ordinateur

dimanche 30 septembre 2012

Define and add a new JNDI object to JBoss context



Lors de mes derniers développements Java/j2ee, j’ai été amené à configurer et à ajouter des objets JNDI dans le serveur d’application JBOSS.
Cela m’a pris tout de même, une demi-journée de recherche, de configuration puis de teste, c’est pourquoi j’ai décidé de partager ces informations avec tous ceux que cela peut intéresser (des développeurs J2ee en l’occurrence).

Alors, voici ce que dit la documentation JBOSS :

JNDI Binding Manager

The JNDI binding manager service allows you to quickly bind objects into JNDI for use by application code. The MBean class for the binding service is org.jboss.naming.JNDIBindingServiceMgr. It has a single attribute, BindingsConfig, which accepts an XML document that conforms to the jndi-binding-service_1_0.xsd schema. The content of the BindingsConfig attribute is unmarshalled using the JBossXB framework.

Exemple :
Monsieur Carlo Bertoldi a mis en ligne un super tutorial  pour mieux illustrer ça, en voici le contenu :
In the last week I’ve had to deal with configuration management for the J2EE application I’m working at the moment. As you may have guessed, I chose the JNDI path. Since I’ve spent an insane amount of time, i.e. more than 5 minutes ;), googling for a solution, I’m gonna share what I discovered. Actually, it’s quite simple, we just need to instantiate a managed bean. Let’s create a file to define one java.lang.String. Create a file named pizza-service.xml in your server deploy directory. For the default configuration it should be $JBOSS_HOME/default/deploy. Filename must end with the suffix “-service”, otherwise JBoss will ignore it.
Insert the following content :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server PUBLIC "-//JBoss//DTD MBean    Service    4.0//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-service_4_0.dtd">
<server>
    <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="jboss.apps:name=pizzeria">
        <attribute name="BindingsConfig" serialDataType="jbxb">
            <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jndi="urn:jboss:jndi-binding-service:1.0"
xs:schemaLocation="urn:jboss:jndi-binding-service resource:jndi-binding-service_1_0.xsd">
                <jndi:binding name="java:pizzeria/capricciosa">
                    <jndi:value trim="true" type="java.lang.String">
                        Hello, JNDI
                    </jndi:value>
                </jndi:binding>
                <jndi:binding name="java:pizzeria/margherita">
                    <jndi:value trim="true" type="java.net.URL">
                        http://localhost:8080/capricciosa
                    </jndi:value>
                </jndi:binding>
            </jndi:bindings>
        </attribute>
    </mbean>
</server>

This binds the text string "Hello, JNDI!" under the JNDI name java:pizzeria/capricciosa. An application would look up the value just as it would for any other JNDI value. The trim attribute specifies that leading and trailing whitespace should be ignored. The use of the attribute here is purely for illustrative purposes as the default value is true.
With this configuration, you can access the the string value “Hello, JNDI”  from within the JBoss VM using the following code fragment :
 
InitialContext ctx  = new InitialContext();
String text = (String) ctx.lookup("java:pizzeria/capricciosa");
 
Once you saved the file, JBoss should reload it automatically.
This is all it takes. If you want to separate the JNDI definitions in multiple files, just change the name attribute of the mbean definition. With this solution you can easily handle multiple configurations for your environments (test, staging etc.)

Bind a local file system directory into the JBoss JNDI namespace.

This configuration describes binding a local file system directory E:/DEV_TOOLS/TEMP/local.properties into the JBoss JNDI namespace under the name external/fs. To do so, we need to add in same xml file pizza-service.xml above, a new <mbean></mbean> XML tag like below:

<mbean code="org.jboss.naming.ExternalContext" name="jboss.jndi:service=ExternalContext,jndiName=external/fs">
<attribute name="JndiName">external/fs</attribute>
<attribute name="Properties">
java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
java.naming.provider.url=file:///E:/DEV_TOOLS/TEMP
</attribute>
<attribute name="InitialContext">
javax.naming.directory.InitialDirContext</attribute>
<attribute name="RemoteAccess">false</attribute>
</mbean>

Note that the use the Sun JNDI service providers, which must be downloaded from internet.  The provider JARs com.sun.jndi.fscontext.jar and java.naming.provider (providerutil.jar) should be placed in the server configuration lib directory.
Those two jars are available to download manually at http://www.docjar.com
This how to access programmatically this file in your code:

Context externalFsContext =(Context)initialContext.lookup("external/fs");
java.io.File  f = (File) externalFsContext.lookup(local.properties);
Properties properties = new java.util.Properties();
properties.load(new FileInputStream(f));
String urlEnv = properties.getProperty("my.env.url");

Assume that your local file local.properties countains property key my.env.url with the value you want, example: “Hello, JNDI”

For more questions, this internet link will help:
http://docs.jboss.org/jbossas/jboss4guide/r4/html/ch3.chapter.html

Hope you'll enjoy.
Mohammad, the blog owner.

Aucun commentaire:

Enregistrer un commentaire