1

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?

Community
  • 1
  • 1
lathspell
  • 3,040
  • 1
  • 30
  • 49
  • "the value will be different on [various] servers"... I'm a little unclear. The JNDI name is always the same, right? It's just that what it points to that will vary, right? Also, why specifically the "comp/env" namespace? – dcsohl Jun 15 '15 at 17:30
  • 1
    I would suggest you read Chapter 19 of the [GlassFish Administration Guide](https://glassfish.java.net/docs/4.0/administration-guide.pdf). There's even a section titled "Administering Custom JNDI Resources"! – dcsohl Jun 15 '15 at 17:41
  • @dcsohl: Thanks for your answers! I meant: the key "java:comp/env/cas/serverName" stays the same, the value, which is a hostname, varies between devel and production AppServers. I'd read that chapter in the Admin Guide before but the only example asked for a restype which was not further explained and a factory class whereas I only wanted to use a simple String... but the solution was much simpler, see below :) – lathspell Jun 16 '15 at 09:10

1 Answers1

1

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>
lathspell
  • 3,040
  • 1
  • 30
  • 49