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.smartfrog.avalanche.shared.jms.MessageListener.java

public void init() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.mom4j.jndi.InitialCtxFactory");

    p.put(Context.PROVIDER_URL, "xcp://" + serverName + ":" + port);

    Context ctx = null;
    try {//from w w  w  .  j  ava2s.co m
        ctx = new InitialContext(p);
        QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
        queue = (Queue) ctx.lookup(queueName);

        qc = qcf.createQueueConnection(userName, password);
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:org.sonar.server.database.JndiDatabaseConnector.java

private DataSource loadDatasourceFromJndi() {
    Context ctx;
    try {/* w w w .  j a v a  2s. co  m*/
        ctx = new InitialContext();
    } catch (NamingException e) {
        throw new JndiException("can not instantiate a JNDI context", e);
    }

    try {
        String jndiName = getJndiName();
        DataSource source = (DataSource) ctx.lookup(jndiName);
        Logs.INFO.info("Use JDBC datasource from JNDI, name=" + jndiName);
        return source;
    } catch (NamingException e) {
        // datasource not found
    } finally {
        try {
            ctx.close();
        } catch (NamingException e) {
        }
    }
    return null;
}

From source file:org.sonar.server.database.JndiDatabaseConnector.java

private void createAndBindDatasource() {
    Reference ref = createDatasourceReference();

    // bind the datasource to JNDI
    Context ctx = null;
    try {/*from   www  .  j av  a2  s .co  m*/
        ctx = new InitialContext();
        createJNDISubContexts(ctx, getJndiName());
        ctx.rebind(getJndiName(), ref);
        datasource = (DataSource) ctx.lookup(getJndiName());
        Logs.INFO.info("JDBC datasource bound to JNDI, name=" + getJndiName());

    } catch (NamingException e) {
        throw new JndiException("Can not bind JDBC datasource to JNDI", e);

    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            }
        }
    }
}

From source file:org.springframework.ejb.support.JndiEnvironmentBeanDefinitionReader.java

/**
 * Creates new JNDIBeanFactory//  w w w  . j a  v a2s.  c o  m
 * @param root likely to be "java:comp/env"
 */
public JndiEnvironmentBeanDefinitionReader(BeanDefinitionRegistry beanFactory, String root)
        throws BeansException {
    // We'll take everything from the NamingContext and dump it in a
    // Properties object, so that the superclass can efficiently manipulate it
    // after we've closed the context.
    HashMap m = new HashMap();

    Context initCtx = null;
    try {
        initCtx = new InitialContext();
        // Parameterize
        NamingEnumeration bindings = initCtx.listBindings(root);

        // Orion 1.5.2 doesn't seem to regard anything under a /
        // as a true subcontext, so we need to search all bindings
        // Not all that fast, but it doesn't matter            
        while (bindings.hasMore()) {
            Binding binding = (Binding) bindings.next();
            logger.debug("Name: " + binding.getName());
            logger.debug("Type: " + binding.getClassName());
            logger.debug("Value: " + binding.getObject());
            m.put(binding.getName(), binding.getObject());
        }
        bindings.close();

        PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(beanFactory);
        propReader.registerBeanDefinitions(m, BEANS_PREFIX);
    } catch (NamingException ex) {
        logger.debug("----- NO PROPERTIES FOUND " + ex);
    } finally {
        try {
            if (initCtx != null) {
                initCtx.close();
            }
        } catch (NamingException ex) {
            // IGNORE OR THROW RTE?
        }
    }
}

From source file:org.springframework.jndi.JndiTemplate.java

/**
 * Release a JNDI context as obtained from {@link #getContext()}.
 * @param ctx the JNDI context to release (may be {@code null})
 * @see #getContext/*from w ww.j  a  v  a2  s.c om*/
 */
public void releaseContext(@Nullable Context ctx) {
    if (ctx != null) {
        try {
            ctx.close();
        } catch (NamingException ex) {
            logger.debug("Could not close JNDI InitialContext", ex);
        }
    }
}

From source file:org.springframework.ldap.core.support.DefaultDirObjectFactory.java

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment, Attributes attrs)
        throws Exception {

    try {//  w  ww.  ja v  a  2  s  .co m
        String nameInNamespace = null;
        if (nameCtx != null) {
            nameInNamespace = nameCtx.getNameInNamespace();
        } else {
            nameInNamespace = "";
        }

        return constructAdapterFromName(attrs, name, nameInNamespace);
    } finally {
        // It seems that the object supplied to the obj parameter is a
        // DirContext instance with reference to the same Ldap connection as
        // the original context. Since it is not the same instance (that's
        // the nameCtx parameter) this one really needs to be closed in
        // order to correctly clean up and return the connection to the pool
        // when we're finished with the surrounding operation.
        if (obj instanceof Context) {

            Context ctx = (Context) obj;
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this
            }

        }
    }
}

From source file:org.springframework.security.ldap.LdapUtils.java

public static void closeContext(Context ctx) {
    if (ctx instanceof DirContextAdapter) {
        return;/*from   ww  w . j  a  v  a  2 s .co m*/
    }

    try {
        if (ctx != null) {
            ctx.close();
        }
    } catch (NamingException e) {
        logger.error("Failed to close context.", e);
    }
}

From source file:org.springframework.security.web.authentication.preauth.websphere.DefaultWASUsernameAndGroupsExtractor.java

/**
 * Get the WebSphere group names for the given security name.
 *
 * @param securityName The security name for which to retrieve the WebSphere group
 * names/*  w w  w . j  ava2s  . c  o  m*/
 * @return the WebSphere group names for the given security name
 */
@SuppressWarnings("unchecked")
private static List<String> getWebSphereGroups(final String securityName) {
    Context ic = null;
    try {
        // TODO: Cache UserRegistry object
        ic = new InitialContext();
        Object objRef = ic.lookup(USER_REGISTRY);
        Object userReg = invokeMethod(getNarrowMethod(), null, objRef,
                Class.forName("com.ibm.websphere.security.UserRegistry"));
        if (logger.isDebugEnabled()) {
            logger.debug("Determining WebSphere groups for user " + securityName
                    + " using WebSphere UserRegistry " + userReg);
        }
        final Collection groups = (Collection) invokeMethod(getGroupsForUserMethod(), userReg,
                new Object[] { securityName });
        if (logger.isDebugEnabled()) {
            logger.debug("Groups for user " + securityName + ": " + groups.toString());
        }

        return new ArrayList(groups);
    } catch (Exception e) {
        logger.error("Exception occured while looking up groups for user", e);
        throw new RuntimeException("Exception occured while looking up groups for user", e);
    } finally {
        try {
            if (ic != null) {
                ic.close();
            }
        } catch (NamingException e) {
            logger.debug("Exception occured while closing context", e);
        }
    }
}

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

public void close() throws NamingException {
    if (isSubTenant(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId())
            && !isBaseContextRequested()) {
        CarbonUtils.checkSecurity();/*from  ww w  .  java 2 s. c  om*/
    }

    Context ctx = this.getInitialContext();
    /* the below condition is there, because of a bug in Tomcat JNDI context close method,
     * see org.apache.naming.NamingContext#close() */
    if (!ctx.getClass().getName().equals("org.apache.naming.SelectorContext")) {
        ctx.close();
    }
}