0

I am desperately trying to inject a simple String into a singleton EJB on Glassfish.

import java.net.URI;
import java.net.URISyntaxException;
import javax.annotation.Resource;
import javax.ejb.Singleton;

@Singleton
public class MavenArtifactResolverProviderBean {

    private URI configurationUri() throws URISyntaxException {
        return new URI(configurationString);
    }

    @Resource(name = "configurationUri")
    private String configurationString;

    ...
}

The bean class is packaged as an EJB. Here is my META-INF/ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
         version = "3.1"
         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
    <enterprise-beans>
        <session>
            <ejb-name>MavenArtifactResolverProviderBean</ejb-name>
            <ejb-class>net.java.trueupdate.server.impl.maven.MavenArtifactResolverProviderBean</ejb-class>
            <env-entry>
                <env-entry-name>configurationUri</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>net/java/trueupdate/artifact/impl/maven/main-repositories.xml</env-entry-value>
                <injection-target>
                    <injection-target-class>net.java.trueupdate.server.impl.maven.MavenArtifactResolverProviderBean</injection-target-class>
                    <injection-target-name>configurationString</injection-target-name>
                </injection-target>
            </env-entry>
        </session>
    </enterprise-beans>
</ejb-jar>

I'm not sure if I even need this mess.

Next, I am running a JerseyTest on Embedded GlassFish 3.1.1 via the Jersey Test Framework 1.17.1. However, nothing is injected and all I get is a NullPointerException from the configurationUri() method.

I've also tried to ignored the failed test, package the bean JAR in a WAR and deploy it to a standalone GlassFish 4.0 with the same result. Next I have copy-pasted the env-entry to the web.xml of the WAR and redeploy it with the same result.

What am I doing wrong? From searching the Internet I got that maybe I need to tweak a domain.xml, but there seems to be no XML schema for this and the documentation of the asadmin command is... overwhelming.

Any hint is greatly appreciated!

EDIT: One my ask why I don't simply set a default value to the string field. This is because I want to document this configuration item via the ejb-jar.xml. Otherwise it would be just hidden in the code.

Christian Schlichtherle
  • 3,125
  • 1
  • 23
  • 47
  • You don't need the mess :) – rdcrng Aug 15 '13 at 14:16
  • Well, I hate to be of unhelpful again, but I just tested the env-entry in web.xml on GF 3.1.2.2 and it works as expected. So I have to ask - your `configurationURI()` method, you're not calling it from a constructor, are you? – rdcrng Aug 15 '13 at 14:57
  • No, I'm not. Mind you that my bean is in a separate EJB jar which is put inside the WAR. Then the env-entry doesn't seem to work, neither in the EJB jar not in the WAR. – Christian Schlichtherle Aug 15 '13 at 15:01

1 Answers1

0

Try using @Resource(lookup = "configurationUri")

nickrak
  • 1,635
  • 15
  • 18
  • It still doesn't work. The reason seems to be that I packaged the bean as a separate EJB JAR which gets placed into the WAR instead of into the WAR directly. However, I don't want to change this structure because it makes the EJB JAR reusable with other apps. – Christian Schlichtherle Aug 17 '13 at 08:12