users@glassfish.java.net

Custom Resource work when looking it up, but not with injection

From: <glassfish_at_javadesktop.org>
Date: Tue, 22 Dec 2009 07:33:02 PST

Hi All.

I started playing with Glassfish v3, and have a question w.r.t custom resource injection.

[b][u]Custom Resource[/u][/b]
I created a custom resource in Glassfish as follow:
[b]JNDI Name:[/b]eq3.ws/serverName
[b]Resource Type:[/b]java.lang.String
[b]Factory Class:[/b]org.glassfish.resources.custom.factory.PrimitivesAndStringFactory
[b]Properties:[/b] value=Local Server

[u][b]Session Bean[/b][/u]
I then created a session bean and injected the resource as follows:
[i]
@Stateless
@Remote(HelloService.class)
public class HelloServiceBean implements HelloService {
        
        @Resource(mappedName="eq3.ws/serverName")
        private String serverName;
        
        public String greet(String name){
                try {
                        fixResource();
                } catch (NamingException e) {
                        e.printStackTrace();
                }
                
                return serverName + ": Hi " + name;
        }
        
        private void fixResource() throws NamingException{
                if(serverName==null){
                        System.out.println("Injection of sesameURL not successfull, finding it using JNDI lookup");
                        InitialContext ic = new InitialContext();
                        serverName = (String) ic.lookup("eq3.ws/serverName");
                }else{
                        System.out.println("Injection of sesameURL successfull");
                }
        }[/i]

If the fixResource method is not there rhe serverName (the injected resource) is null, however, using fixResource I can lookup via jndi...

If I test this with a test case the serverlog looks like this:
[b]INFO: Injection of sesameURL not successfull, finding it using JNDI lookup[/b]

Has anyone else experience this ? Am I doing something wrong?

Thanks
[Message sent by forum member 'phillipkruger' (phillip_at_thumbtribe.co.za)]

http://forums.java.net/jive/thread.jspa?messageID=372535