Skip to content

Setting Configuration Defaults Using JNDI

Bill Davidson edited this page Sep 8, 2016 · 11 revisions

JSONUtil has a number of configuration options available by passing a JSONConfig object to the toJSON() methods but setting these every time when you want the same behavior every time can be annoying and tedious. JSONConfig objects are not thread safe so they should not be shared among threads. If you always want the same defaults that are not the same as the original defaults, you can change most of the defaults using JNDI provided you have a JNDI server. All JEE Web Tier containers that I am aware of provide a JNDI server for the webapps that they are running. This example is for Tomcat.

Tomcat lets you set JNDI properties within its configuration directory $CATALINA_BASE/conf/Catalina. Under that directory you should create a directory with the host name from your server.xml file ("myHost" in this example) and in that directory you should create an XML file with the same name as you webapp (MyApp in this example):

$CATALINA_BASE/conf/Catalina/myHost/MyApp.xml

In that file you should put a Context tag for your app. Within that tag you should create Environment tags which contain the properties that you want to change and the values that you want to change them to. This example sets the appName to "MyApp", disables property name validation and enables full JSON identifier code points. It also sets the default locale to Spanish and disables logging and MBean registration.

<Context path="/MyApp">
   <Environment name="org/kopitubruk/util/json/appName" type="java.lang.String" value="MyApp" override="false" />
   <Environment name="org/kopitubruk/util/json/validatePropertyNames" type="java.lang.Boolean" value="false" override="false" />
   <Environment name="org/kopitubruk/util/json/fullJSONIdentifierCodePoints" type="java.lang.Boolean" value="true" override="false" />
   <Environment name="org/kopitubruk/util/json/locale" type="java.lang.String" value="es" override="false" />
   <Environment name="org/kopitubruk/util/json/logging" type="java.lang.Boolean" value="false" override="false" />
   <Environment name="org/kopitubruk/util/json/registerMBean" type="java.lang.Boolean" value="false" override="false" />
</Context>

Because the appName tends to remain consistent across all deployments, it might be preferable to put it into your webapp's web.xml. This should work with all JEE web tier containers. There is a different syntax for adding environment variables in web.xml:

<env-entry>
  <env-entry-name>org/kopitubruk/util/json/appName</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>MyApp</env-entry-value>
</env-entry>

You can use the same mechanism for other defaults provided that you want them the same across all deployments of your app.

You may want different defaults for development and testing than you do for production. For example, property name validation is probably a good idea for development and testing but you might want to improve performance in production by disabling validation on your production servers only.

It is possible to put those Environment tags inside the Context tag in your $CATALINA_BASE/conf/context.xml file. That is probably not a good idea.

If you're using Glassfish, JBoss or some other web tier container, see its documentation for how to set variables in JNDI.

There are some defaults which can only be changed programmatically. If you want to change those, then you should probably do so in a static block for something that loads early in your app.

Clone this wiki locally