users@glassfish.java.net

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

From: Sigal Shaharabani <Sigal.Shaharabani_at_Teledata-Networks.com>
Date: Tue, 22 Dec 2009 17:44:50 +0200

Do you get any error during deployment?

Regards
Sigal

P please consider the environment - do you really need to print this
email?
-----Original Message-----
From: glassfish_at_javadesktop.org [mailto:glassfish_at_javadesktop.org]
Sent: Tuesday, December 22, 2009 5:33 PM
To: users_at_glassfish.dev.java.net
Subject: Custom Resource work when looking it up, but not with injection

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.PrimitivesAndStringFact
ory
[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

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
For additional commands, e-mail: users-help_at_glassfish.dev.java.net