Question

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.

No correct solution

OTHER TIPS

Try using @Resource(lookup = "configurationUri")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top