Here follows an JAVA code example showing how to access the DAC global property from a webservice application

Step 1: In the definitions section the WebServiceContext must be loaded:

@Resource
protected WebServiceContext wsContext;

Step 2: During initialization get the ServletContext and the id of the DAC resource.

/*\* This initialization only needs to occur once**/
  protected void init() {

      MessageContext mc = wsContext.getMessageContext();
      ServletContext context = ((javax.servlet.ServletContext) mc.get(MessageContext.SERVLET_CONTEXT));

      resourceId = context.getInitParameter("resourceId");
      if (resourceId == null){
           throw new RuntimeException("Initial parameter 'resourceId' not configured\!");
      }
      log.info("Using resource Id: " + resourceId);
   }

Step 3: Look up the global resource DAC. Keep in mind that the DAC is refreshed every time that new configuration is available. Therefore it is good practice not to hold onto an instance of the DAC when you are not using it.

DataAccessComponent dac;
try {
    InitialContext initialContext = new InitialContext();
    Context envCtx = (Context) initialContext.lookup("java:comp/env");
    dac = (DataAccessComponent) envCtx.lookup(resourceId);
} catch (NamingException e){
     throw new RuntimeException("Error looking up resource: " + e.getMessage());
}

if (dac == null) {
    throw new RuntimeException("Resource does not exist: " + resourceId);
}
if (log.isDebugEnabled()) log.debug("Data Access Component loaded with regionId: " + dac.getRegionId());
  • No labels