users@glassfish.java.net

[gf-users] Handling env-entry application configuration with Glassfish

From: Lachezar Dobrev <l.dobrev_at_gmail.com>
Date: Mon, 21 Jul 2014 18:53:33 +0300

  Hello all.
  I'm having trouble using environment entries to configure an
application on deployment in Glassfish.

  web.xml:

  <web-app ...>
    <env-entry>
      <env-entry-name>config/user</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>
  </web-app>


  ServiceProducer.java:

  @ApplicationScoped
  public class ServiceProducer {
    @Produces
    @Default
    public Service get() {
      return new ServiceImpl(user);
    }

    private String user;
    @Resource(name = "java:comp/env/config/user")
    public void setUser(String user) {
      this.user = user;
    }
  }

  With "GlassFish Server Open Source Edition 4.0 (build 89)" the
ServiceProducer.get() is called before setUser() is called.

  I'm at a loss here: How can I configure the application?

  I have added a config/user JNDI Custom Resource:
   Resource Type: java.lang.String
   Factory Class:
org.glassfish.resources.custom.factory.PrimitivesAndStringFactory
   Status : Enabled (checked)
   Properties:
    name = "value", value = "ConfiguredUserName"

  Please advise! I'm getting seriously frustrated with this.