users@glassfish.java.net

@Resource not working for custom-resource

From: <forums_at_java.net>
Date: Wed, 4 Jan 2012 21:43:51 -0600 (CST)

I have defined a custom resource in <resources> segment of domain.xml:

[code]<custom-resource res-type="java.lang.String" description=""
jndi-name="fileloc"
factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory">
      <property name="filelocpath" value="c:/dev/java/"></property>
    </custom-resource>

[/code]

and expect this to work:

<code>@Resource(name="fileloc")
private String fileloc;
@PostConstruct
public void init() {
    System.out.println("fileloc=" + this.fileloc);
....</code>

but it print out null. Then I tried:

<code>        Context c = new InitialContext(); 
        String ss = (String) c.lookup("fileloc");
        System.out.println("fileloc="+ss);
</code>

but it returns "javax.naming.CommunicationException" because "Root exception
is java.lang.IllegalAccessException: value cannot be null". Finally I tried:

<code>        NamingEnumeration<Binding> neb = c.listBindings("");
        while (neb.hasMore()) {
            Binding b = neb.next();
            if (b.getName().equals("fileloc")) {
                javax.naming.Reference obj =
(javax.naming.Reference) b.getObject();
                Enumeration<RefAddr> en = obj.getAll();
                while (en.hasMoreElements()) {
                    RefAddr ra = en.nextElement();
                   
System.out.println(ra.getType()+"="+ra.getContent());
                }
            }
        }
</code>

and I got "filelocpath=c:/.....".

With this info, I hope somebody can tell me what's wrong with me setup so as
to make the @Resource method works for me.

 


--
[Message sent by forum member 'senderj']
View Post: http://forums.java.net/node/880055