开发者

Commons configuration - JNDIConfiguration - How to?

I usually use Commons Configuration for manage my applications configs. I have used properties files configuration. Now I'm interested in using a JNDIConfiguration but I'm not able to understand how this works reading the documentation or googling it.

Contextualizing, I'm working in webapps runn开发者_开发问答ing in an JBoss AS.

Where will be the properties stored? In a file? some tables in a database?

I will be grateful for any guidance at this level even if it comes in shape of links where I can read some valuable information about it.

As a final note my goal is to free me of linking a file with a hardcoded path for my properties, but also don't force me to have my config in database tables. If you have any suggestions on how to do that in some other way be free to share.


JNDIConfiguration looks up the configuration data on a JNDI server (in your case, the JBoss JNDI server). However, you still need a way of getting that data into the JNDI server in the first place, and Commons-Configuration won't help you with that.

It sounds to me that JNDI isn't what you want, it's just pushing the problem around a bit. JBoss still needs to store the configuration data somewhere, so you'll still have the same basic problem.

If you don't want hard-coded file paths, and you don't want a database, then I suggest you pass in the location of the properties file via a system property, e.g.

java -Dmy.config.path=/my/config.properties com.MyClass

Then pass that location to Commons Configuration and let it load your config that way. No hardcoded-paths, no database.


I don't know much about Commons Configuration and JNDIConfiguration, but if what you want is a set of key/value pairs, the standard way of doing this as per the Java EE specs, is to use env-entry in the web.xml or ejb.xml.

<env-entry>
  <env-entry-name>maxExemptions</env-entry-name>
  <env-entry-value>10</env-entry-value>
  <env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>

(example taken from JBoss web conf. reference.)

These values are bound in the JNDI so they can be looked up or injected.


Where will be the properties stored? In a file? some tables in a database?

As @ewernli mentioned, the Java EE way to add entries in the JNDI tree is to use env-entry in your deployment descriptor(s).

Now, if you don't want to repeat the same env-entry in several deployment descriptors, then there is a service for specifying global JNDI bindings: JNDIBindingServiceMgr.

Below, the provided jboss-service.xml example:

<?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.tests:service=JNDIBindingServiceMgr">
      <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:1.0 resource:jndi-binding-service_1_0.xsd"
            >
            <jndi:binding name="urls/jboss-home">
               <jndi:value type="java.net.URL">http://www.jboss.org</jndi:value>
            </jndi:binding>

            <jndi:binding name="hosts/localhost">
               <jndi:value editor="org.jboss.util.propertyeditor.InetAddressEditor">
                  127.0.0.1
               </jndi:value>
            </jndi:binding>

            <jndi:binding name="maps/testProps">
               <java:properties xmlns:java="urn:jboss:java-properties"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                  xs:schemaLocation="urn:jboss:java-properties resource:java-properties_1_0.xsd">
                  <java:property>
                     <java:key>key1</java:key>
                     <java:value>value1</java:value>
                  </java:property>
                  <java:property>
                     <java:key>key2</java:key>
                     <java:value>value2</java:value>
                  </java:property>
               </java:properties>               
            </jndi:binding>
         </jndi:bindings>
      </attribute>
      <depends>jboss:service=Naming</depends>
   </mbean>

</server>

If this is not what you are looking for, then I don't understand what you're looking for :) In that case, you should maybe clarify it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜