How to set a java:comp/env/foo JNDI variable in Glassfish 4.1?

2.6k views Asked by At

I'd like to use a ServletFilter that reads it configuration from the java:comp/env/ JNDI namespace. As the value will be different on devel and production servers, it should not be hardcoded via in web.xml but instead be configured in the application server.

I already tried the following places but none seemed to be right:

  • System Properties
  • Web Container / General Properties
  • JVM Settings / Additional Properties (suggested in How to set an env variable in Glassfish 2.1)
  • Resource Connectors (didn't work)
  • asenv.conf (only in Glassfish 2?)

How can I set a custom JNDI variable in Glassfish 4.1?

1

There are 1 answers

2
lathspell On

I installed only the Glassfish 4.1 Web Edition. While it has JNDI support, the web GUI comlpetely lacked the Resources -> JNDI menu to configure own variables!

In the Web Profile Glassfish, custom JNDI variables can be written in the domain.xml, they seem to survive other changes via the Admin GUI:

<resources>
  ...
  <custom-resource res-type="java.lang.String" jndi-name="cas/serverName" factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory">
    <property name="value" value="https://sso.example.com/"></property>
  </custom-resource>
</resources>
...
<servers>
  <server name="server" config-ref="server-config">
    ...
    <resource-ref ref="cas/serverName"></resource-ref>
  </server>
</servers>

After adding a JNDI variable to Glassfish, it's only availble with InitialContext.doLookup("cas/serverName") though, not within the java:comp/env namespace. To get that, I had to add the following to my web.xml (it did not work in glassfish-web.xml!):

 <resource-ref>
   <res-ref-name>cas/serverName</res-ref-name> 
   <res-type>java.lang.String</res-type>
   <lookup-name>cas/serverName</lookup-name> 
 </resource-ref>