Example usage for javax.naming InitialContext lookupLink

List of usage examples for javax.naming InitialContext lookupLink

Introduction

In this page you can find the example usage for javax.naming InitialContext lookupLink.

Prototype

public Object lookupLink(Name name) throws NamingException 

Source Link

Usage

From source file:edu.mayo.cts2.framework.core.config.ConfigInitializer.java

/**
 * Gets the variable.//from  ww  w .  j a v  a2  s .c o m
 *
 * @param jndiName the jndi name
 * @param systemVariableName the system variable name
 * @param defaultValue the default value
 * @return the variable
 */
private String getVariable(String jndiName, String systemVariableName, String defaultValue) {
    String sysVarValue = System.getProperty(systemVariableName);

    if (StringUtils.isNotBlank(sysVarValue)) {
        log.info("Using value: " + sysVarValue + " for Environment Variable: " + systemVariableName);
        return sysVarValue;
    }

    String jndiValue = null;
    try {
        InitialContext ctx = new InitialContext();
        jndiValue = (String) ctx.lookupLink("java:/comp/env/" + jndiName);
    } catch (NoInitialContextException e) {
        log.warn("No JNDI Context found.");
    } catch (NameNotFoundException e) {
        // this is ok, it means there is no JNDI name registered
    } catch (NamingException e) {
        // this is also ok, it means there is no JNDI name registered
    }

    if (StringUtils.isNotBlank(jndiValue)) {
        log.info("Using value: " + jndiValue + " for JNDI Variable: " + jndiName);
        return jndiValue;
    }

    log.info("Using default value: " + defaultValue);
    return defaultValue;
}