Example usage for javax.naming Context close

List of usage examples for javax.naming Context close

Introduction

In this page you can find the example usage for javax.naming Context close.

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this context.

Usage

From source file:org.apache.openjpa.lib.conf.Configurations.java

/**
 * Looks up the given name in JNDI. If the name is null, null is returned.
 *//*  www .jav  a  2 s.c om*/
public static Object lookup(String name, String userKey, Log log) {
    if (StringUtils.isEmpty(name))
        return null;

    Context ctx = null;
    try {
        ctx = new InitialContext();
        Object result = ctx.lookup(name);
        if (result == null && log != null && log.isWarnEnabled())
            log.warn(_loc.get("jndi-lookup-failed", userKey, name));
        return result;
    } catch (NamingException ne) {
        throw new NestableRuntimeException(_loc.get("naming-err", name).getMessage(), ne);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException ne) {
                // ignore
            }
        }
    }
}

From source file:org.brucalipto.sqlutil.SQLManager.java

protected static DataSource setupDataSource(final String dsJNDIName) {
    Context env = null;
    final String contextURI = dsJNDIName.startsWith(REF_PREFIX) ? dsJNDIName : REF_PREFIX + dsJNDIName;
    log.debug("Looking for '" + contextURI + "' in Context");
    try {/* w w  w .ja v  a2  s.c om*/
        env = new InitialContext();
        return (DataSource) env.lookup(contextURI);
    } catch (NamingException e) {
        log.error("Error getting datasource '' from Context", e);
        return null;
    } finally {
        try {
            if (env != null)
                env.close();
        } catch (NamingException e) {
            log.error("Error closing context", e);
        }
    }
}

From source file:org.dhatim.db.JndiDataSource.java

private Object lookup(String jndi) {
    Context context = null;
    try {//from  ww  w. j a v  a2 s.c o  m
        context = new InitialContext();
        return context.lookup(jndi);
    } catch (NamingException e) {
        throw new SmooksConfigurationException("JNDI Context lookup failed for '" + jndi + "'.", e);
    } finally {
        if (context != null) {
            try {
                context.close();
            } catch (NamingException e) {
                throw new SmooksConfigurationException(
                        "Error closing Naming Context after looking up DataSource JNDI '" + datasourceJndi
                                + "'.",
                        e);
            }
        }
    }
}

From source file:org.dhatim.routing.jms.JMSRouter.java

@Initialize
public void initialize() throws SmooksConfigurationException, JMSException {
    Context context = null;
    boolean initialized = false;

    if (beanId == null) {
        throw new SmooksConfigurationException("Mandatory 'beanId' property not defined.");
    }/*from   w  ww. j ava  2  s  .c  o m*/
    if (jmsProperties.getDestinationName() == null) {
        throw new SmooksConfigurationException("Mandatory 'destinationName' property not defined.");
    }

    try {
        if (correlationIdPattern != null) {
            correlationIdTemplate = new FreeMarkerTemplate(correlationIdPattern);
        }

        Properties jndiContextProperties = jndiProperties.toProperties();

        if (jndiContextProperties.isEmpty()) {
            context = new InitialContext();
        } else {
            context = new InitialContext(jndiContextProperties);
        }
        destination = (Destination) context.lookup(jmsProperties.getDestinationName());
        msgProducer = createMessageProducer(destination, context);
        setMessageProducerProperties();

        initialized = true;
    } catch (NamingException e) {
        final String errorMsg = "NamingException while trying to lookup [" + jmsProperties.getDestinationName()
                + "]";
        logger.error(errorMsg, e);
        throw new SmooksConfigurationException(errorMsg, e);
    } finally {
        if (context != null) {
            try {
                context.close();
            } catch (NamingException e) {
                logger.debug("NamingException while trying to close initial Context");
            }
        }

        if (!initialized) {
            releaseJMSResources();
        }
    }
}

From source file:org.dhatim.util.JNDIUtil.java

/**
 * Lookup an object through the JNDI context.
 *
 * @param objectName     The name of the object to be looked up.
 * @param jndiProperties JNDI properties.
 * @return The object./*from  ww w. j  a va 2  s  .c o  m*/
 * @throws NamingException Error getting object.
 */
public static Object lookup(final String objectName, final Properties jndiProperties) throws NamingException {
    Object object = null;
    Context context;

    context = JNDIUtil.getNamingContext(jndiProperties);
    try {
        object = context.lookup(objectName);
    } finally {
        try {
            context.close();
        } catch (NamingException ne) {
            logger.debug("Failed to close Naming Context.", ne);
        }
    }

    return object;
}

From source file:org.easy.ldap.LdapContextFactory.java

/**
 * @param ctx//w ww  .  j  a v  a2s  .  c  o  m
 */
public void closeContext(Context ctx) {
    if (ctx != null) {
        try {
            ctx.close();
        } catch (NamingException e) {
            log.warn(e);
        }
    }
}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param tenantId//w  w w . ja va  2 s  . c  o  m
 * @param userId
 * @param password
 * @return
 * @throws NamingException 
 */
public boolean isValidUser(String tenantId, String userId, String password) {
    boolean out = false;
    Context ctx = null;

    try {
        LdapName rootDn = contextFactory.getNamingFactory().createUserDn(tenantId, userId);
        ctx = contextFactory.createSecureContext(rootDn, rootDn, password,
                LdapEnvironment.USER_AUTHENTICATION_METHOD);

        if (ctx != null)
            out = true;

        ctx.close();
    } catch (NamingException e) {
        log.debug("FYI", e);
    } finally {
        if (ctx != null)
            try {
                ctx.close();
            } catch (NamingException e) {
                log.debug("FYI", e);
            }
    }

    return out;
}

From source file:org.exoplatform.services.organization.DummyLDAPServiceImpl.java

private void closeContext(Context ctx) {
    try {//  ww w . j av a2  s  .  co m
        if (ctx != null) {
            ctx.close();
        }
    } catch (NamingException e) {
        LOG.warn("Exception occurred when tried to close context", e);
    }
}

From source file:org.jboss.test.NamingUtil.java

/**
 * Create test data. It needs to use org.jboss.naming.HttpNamingContextFactory since JNDI through RMI
 * is already secured and disallow bind/unbind/rebind operations.
 * //from  w w w  . j  av  a2 s .  c  o  m
 *   
 *   
 * @param jndiName JNDI path where to bind data.
 * @param dataKey key under which data is bound
 * @param data Data object to bind. In case data is null last path element is considered subContext. 
 * @param serverHost host on which to create binding 
 * @throws Exception
 */
public static void createTestJNDIBinding(String jndiName, String dataKey, Object data, String serverHost,
        boolean useHAJNDI) throws Exception {

    log.debug("XXX");

    Context ctx = null;
    if (useHAJNDI) {
        ctx = NamingUtil.getFullHAInitialContext(serverHost);
    } else {
        ctx = NamingUtil.getFullInitialContext(serverHost);
    }

    String[] path = jndiName.split("/");
    String subPath = "";
    for (int i = 0; i < path.length; i++) {

        if (path[i].equals("")) {
            continue;
        }

        subPath = subPath + "/" + path[i];
        log.debug("creating subcontext=" + subPath);
        try {
            ctx.createSubcontext(subPath);
            log.debug("subcontext=" + subPath + " created.");
        } catch (NameAlreadyBoundException e) {
            // ignore
        }
    }

    if (data != null) {
        log.debug("bind s=" + subPath + ", dataKey=" + dataKey + ", data=" + data);
        ctx.bind(subPath + "/" + dataKey, data);
        log.debug(data + " bound.");
    }

    ctx.close();

}

From source file:org.jbpm.bpel.integration.jms.JmsIntegrationServiceFactory.java

private static Object lookup(String name) throws NamingException {
    Context initialContext = new InitialContext();
    try {/*from w ww  .ja v  a2s. c  o  m*/
        return initialContext.lookup(name);
    } finally {
        initialContext.close();
    }
}