blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Configure and lookup beans using resource-env-ref (websphere)

It is explained in detail here: ibm techarticle


But the example here follows on how to do it for a bean like this,
public class MyBean {
   //default values 
   private String foo = "Default Foo";
   private int bar = 0;
   private int qux = 123;
}
Like we did for tomcat, we modify the bean to implement Referenceable.

Create a utility project in eclipse. And create the following classes.

MyBean.java

package com.resource;

import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;

    public class MyBean implements Referenceable {

    private String foo = "Default Foo";
    private int bar = 0;
    private int qux = 123;


    public Reference getReference() throws NamingException {
        String classname = MyBean.class.getName();
        StringRefAddr barref = new StringRefAddr("bar", Integer.toString(bar));
        StringRefAddr fooref = new StringRefAddr("foo", foo);
        StringRefAddr quxref = new StringRefAddr("qux", Integer.toString(qux));
        String classfactoryname = MyBeanFactory.class.getName();
        Reference ref = new Reference(classname, classfactoryname, null);
        ref.add(barref);
        ref.add(fooref);
        return ref;
    }

    //getters and setters

    @Override
        public String toString() {
        return "MyBean [foo=" + foo + ", bar=" + bar + ", qux=" + qux + "]";
        }
    }

MyBeanFactory.java
package com.resource;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.spi.ObjectFactory;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;

public class MyBeanFactory implements ObjectFactory {
 private static MyBean bean = null;

 public Object getObjectInstance(Object object, Name name, Context nameCtx,
   Hashtable environment) throws NamingException {
  if (bean == null) {
   bean = new MyBean();
   Reference ref = (Reference) object;
   Enumeration addrs = ref.getAll();
   while (addrs.hasMoreElements()) {
    RefAddr addr = (RefAddr) addrs.nextElement();
    String attrName = addr.getType();
    String value = (String) addr.getContent();
    if (attrName.equals("foo")) {
     bean.setFoo(value);
    } else if (attrName.equals("bar")) {
     try {
      bean.setBar(Integer.parseInt(value) + 100); // changed
     } catch (NumberFormatException e) {
      throw new NamingException("Invalid 'bar' value "
        + value);
     }
    }else if (attrName.equals("qux")) {
     try {
      bean.setBar(Integer.parseInt(value)); 
     } catch (NumberFormatException e) {
      throw new NamingException("Invalid 'qux' value "
        + value);
     }
    }
   }
  }
  return bean;
 }
}

Build and export the project.

Lets follow steps highlighted in the above article.

1. Create a shared library


Note that I had renamed the jar and placed it in E:\resourceFiles.

2. Create a resource environment provider

3. Create a referenceable object

4. Create resource environment entries

5. Create custom properties

Restart your server.

If something went wrong in the configuration you will be notified in console. Watch out for Error code CWNEN004*E.
Eg:
com.ibm.wsspi.injectionengine.InjectionException: CWNEN0044E: A resource reference binding could not be found for the following resource references [bean/myBeanOne], defined for the JNDITest component.

If it is successful you will get
ResourceMgrIm I WSVR0049I: Binding myBeanOne as bean/myBeanOne

6. Create resource environment reference within the Web module

In your project's web.xml add the below resource-env-entry,

<resource-env-ref>
    <description>Object factory for MyBean instances</description>
    <resource-env-ref-name>ref/bean/myBeanOne</resource-env-ref-name>
    <resource-env-ref-type>com.resource.MyBean</resource-env-ref-type>
</resource-env-ref>

In ibm-web-bnd.xml specify the binding to the real jndi resource.

<resource-env-ref name="ref/bean/myBeanOne" binding-name="bean/myBeanOne" />

7. Refer or retrive the resource in your code
Context initCxt = new InitialContext();
// one could use relative names into the sub-context
Context envContext = (Context) initCxt.lookup("java:comp/env");
MyBean myBean1 = (MyBean) envContext.lookup("ref/bean/myBeanOne");
System.out.println("bean value:");
System.out.println(myBean1.toString());

If the configuration is wrong or missed then while the application is being deployed you will probably get an error like the one below.
ResourceProce W CWNEN0046W: The com.resource.MyBean type specified on the resource-ref, resource-env-ref, or message-destination-ref with the ref/bean/myBeanOne name in the JNDITest.war module could not be loaded. Compatibility type checking will not be performed for this resource reference.

8. Add runtime reference to the resource jar.
This could be done either from the admin console while the application is being deployed or even after that.

Since I mostly run the application directly from Eclipse/RAD. I prefer adding any dependent modules to the EAR's deployment assembly. RightClick EAR or WAR project -> Select Deployment assembly.

If all is well, deploy and start the application.

Output:
MyBean [foo=This is good, bar=12, qux=123]

Continue reading: Using JNDI lookup for DataSource using resource-ref (websphere)

No comments:

Post a Comment