Skip to main content

@Resource not working for custom-resource

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
5 replies [Last post]
senderj
Offline
Joined: 2008-09-24

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

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

and expect this to work:
@Resource(name="fileloc")
private String fileloc;
@PostConstruct
public void init() {
System.out.println("fileloc=" + this.fileloc);
....
but it print out null. Then I tried:
Context c = new InitialContext();
String ss = (String) c.lookup("fileloc");
System.out.println("fileloc="+ss);

but it returns "javax.naming.CommunicationException" because "Root exception is java.lang.IllegalAccessException: value cannot be null". Finally I tried:
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());
}
}
}
and I got what I wanted "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.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cf126330
Offline
Joined: 2005-03-29

Can you try @Resource(lookup="fileloc"), or @Resource(mappedName="fileloc")?

lookup("fileloc") should work. Can you post the stacktrace? Could be caused by some special characters in your value like : \

-Cheng

senderj
Offline
Joined: 2008-09-24

I've further tested my case and found something even more puzzling to me. If I start glassfish from fresh, the jndi "fileloc" is bound to "org.glassfish.javaee.services.ResourceProxy", so the c.getBindings method failed when casting the Reference (since the return is not a Reference). I then put a c.lookup("fileloc") before the c.getBindings. It will still failed with "IllegalAccessException: value cannot be null". Then I change the coding again to remove the c.lookup and only leave c.getBindings, and run it the third time, in which the jndi "fileloc" will be bound to "javax.naming.Reference" and so the c.getBindings works.

I repeated the above 3-4 times and it behaves like this consistently. Any idea why it behaves like this?

senderj
Offline
Joined: 2008-09-24

Cheng, thanks for the reply.

For @Resource, I've tried name only, mappedName only and both together, all returns null.

For lookup("fileloc"), it seems it can found fileloc but finally ended with the IllegalAccessException. I do not have any \ , only /. Here is the stacktrace (testUtil is my singleton bean):

</p>
<p>javax.naming.CommunicationException: Communication exception for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is java.lang.IllegalAccessException: value cannot be null]<br />
 at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:542)<br />
 at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)<br />
 at javax.naming.InitialContext.lookup(InitialContext.java:392)<br />
 at javax.naming.InitialContext.lookup(InitialContext.java:392)<br />
 at ejb.testUtil.init(testUtil.java:72)<br />
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br />
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)</p>
<p>

cf126330
Offline
Joined: 2005-03-29

Change the prop name to "value". @Resource and lookup() should work. If you want arbitary property name, you need to use JavaBeanFactory or your own object factory class. If the problem persists, please attach a test.

http://docs.oracle.com/cd/E18930_01/html/821-2418/beanx.html

-Cheng

senderj
Offline
Joined: 2008-09-24

Cheng,

Thank you. This solved my problem