Creating Resource Environmental Variables and Accessing REP Variables

Creating Resource Environmental Variables and Accessing REPVariables
Before Doing this
                Create two classes Config.java and ConfigFactory.java given in point number 6 and export as jar
Creating Resource Environmental Variables:-
1.       Open WAS Admin Console
2.       Navigate to Resources ->Resource Environment ->Resource Environmental Providers
3.       Select Scope
4.       Click on New give Name and click Apply save
5.       Click on Referenceables under Additional Properties
6.       Click on New and enter Factory class name and Class name
In my Case Factory class name is com.rep.lib.ConfigFactory    where class name is com.rep.lib.Config   (Complete Qualified Name of Class)
These two class look like this
a.       ConfigFactory.java
package com.rep.lib;
importjava.util.Enumeration;
import java.util.Hashtable;
importjavax.naming.Context;
import javax.naming.Name;
importjavax.naming.NamingException;
importjavax.naming.RefAddr;
importjavax.naming.Reference;
importjavax.naming.spi.ObjectFactory;
public class ConfigFactory implements ObjectFactory {
     private static Config config = null;
     public Object getObjectInstance(Object object, Name name, Context nameCtx,
                Hashtableenvironment) throws NamingException {
           if (config == null) {
                config = new Config();
                Reference ref = (Reference) object;
                Enumerationaddrs = ref.getAll();
                RefAddr addr = null;
                String entryName = null;
                String value = null;
                while(addrs.hasMoreElements()) {
                     addr = (RefAddr) addrs.nextElement();
                     entryName = addr.getType();
                     value = (String) addr.getContent();
                     config.setAttribute(entryName, value);
                }
           }
           return config;
     }
}
b.  Config.java
package com.rep.lib;
import java.util.HashMap;
import java.util.Map;
/**
 * This class will hold rep values
 *
 * @author 
 *
 */
public class Config {
     private Map attributes = null;
     public Config() {
           attributes = new HashMap(10);
     }
     protected void setAttribute(String attributeName, String attributeValue) {
           attributes.put(attributeName, attributeValue);
     }
     public Object getAttribute(String attributeName) {
           return attributes.get(attributeName);
     }
}
7.       Click on Resource Environment Entries under Additional Properties
8.       Click New and enter Name and JNDIname
In My Case Name is TestREPJNDIname JNDI name is rep/test/testjndi

9.       Click on Custom Properties under Additional Properties and click on new to add REP Variables what you want
In my Case Name: webURL   Value : http://localhost:8080
10.   Export Two Java files (ConfigFactory and Config) as jar into any of temp folder
11.   And add this jar to shared libraries which is under Environment ->Shared libraries
12.   Select Scope and click on new and give Name and Classpath if want Provide Description and Native Library Path
In my Case Name : replib
           Classpath : C:temprepreplib.jar (this is exported jar path)
13.   Click Apply and Save
14.   Restart Server
Accessing REPVariables From your Project:-

1.       Create resource env ref in Web.xml
                <resource-env-ref>
            <description/>
            <resource-env-ref-name>REPENVNAME</resource-env-ref-name>
<resource-env-ref-type>com.rep.lib.Config</resource-env-ref-type>
      </resource-env-ref>
2.       Create resource env ref in ibm-web-bnd.xml
   <resource-env-ref name=“REPENVNAME”binding-name=“rep/test/testjndi”/>
3.       From your Code
        Create Context and lookup for jndi name
        It look like this
        REP JNDI NAME denoted as  java:comp/env/REPENVNAME


                // private static final String REP_JNDI_NAME = “rep/test/testjndi“;
      private static final String REP_JNDI_NAME = “java:comp/env/REPENVNAME”;
      private static Config config;
      /**
       * @see HttpServlet#HttpServlet()
      */
       public MainServlet() {
      
      }
      @Override
      public void init() throwsServletException {
            try {
                        Context ctx  = newInitialContext();
                        Object object = ctx.lookup(REP_JNDI_NAME);
                        config  = (Config) object;
                  } catch(NamingException e) {
                        // TODOAuto-generated catch block
                        e.printStackTrace();
                  }
    }
4.       To retrieve Environmental Variables
From Code with config Object  we can get Rep Variable value
       if (config != null) {

         config.getAttribute(“webURL”);
}
Best Practices:-
1.       Lookup operation is more costly try to minimize lookup operation. Here are some possible ways
a.       Write lookup Operation Code in init method and store lookup object either in session or
Instance variable of class (it should always be private static)
2.       Try to use Resource env references in web.xml and ibm-web-bnd.xml so we can reduce runtime errors (here Resource env references to jndi name). If resource is not present means we can identify in compile time only
3.       We need to check lookup object present or not before accessing rep variable using lookup object to reduce runtime errors         
if (config != null) {
                     value = config.getAttribute(propertyName);
              }         

4.       Mostly What we store in Resource Environmental Variables  are URLS or Mail ids   
These are some of Best Practices if you know more or any doubts please comment it
 Reference links:-

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks