Example usage for javax.naming InitialContext close

List of usage examples for javax.naming InitialContext close

Introduction

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

Prototype

public void close() throws NamingException 

Source Link

Usage

From source file:org.settings4j.connector.JNDIConnector.java

/**
 * Calls {@link InitialContext#close()} with null-checks and Exception handling: log exception with log level info.
 * //from w w  w .  j  a va2 s. co  m
 * @param ctx
 *        the InitialContextto close ( )
 */
protected void closeQuietly(final InitialContext ctx) {
    if (ctx != null) {
        try {
            ctx.close();
        } catch (final NamingException e) {
            LOG.info("cannot close context: {}", ctx, e);
        }
    }
}

From source file:org.wso2.carbon.andes.event.core.internal.delivery.jms.JMSDeliveryManager.java

/**
 * Initialize and return topic connection
 *
 * @param userName username//from  w  ww. ja va 2  s .  co m
 * @return topic connection
 * @throws EventBrokerException
 */
public TopicConnection getTopicConnection(String userName) throws EventBrokerException {
    InitialContext initialContext = null;
    try {

        initialContext = new InitialContext(getInitialContextProperties(userName,
                EventBrokerHolder.getInstance().getQpidServerDetails().getAccessKey()));
        TopicConnectionFactory topicConnectionFactory = getTopicConnectionFactory(initialContext);

        TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
        topicConnection.start();
        return topicConnection;
    } catch (NamingException e) {
        throw new EventBrokerException("Can not create the initial context", e);
    } catch (EventBrokerException | JMSException e) {
        throw new EventBrokerException("Can not create topic connection", e);
    } finally {
        if (initialContext != null) {
            try {
                initialContext.close();
            } catch (NamingException e) {
                log.error("Can not close the inital context factory ", e);
            }
        }
    }
}

From source file:org.wso2.carbon.attachment.mgt.core.datasource.impl.BasicDataSourceManager.java

private DataSource lookupInJNDI(String remoteObjectName) throws NamingException {
    InitialContext ctx = null;
    try {/*  w w w  .  jav  a  2  s  . c om*/
        if (dataSourceConfig.getDataSourceJNDIRepoInitialContextFactory() != null
                && dataSourceConfig.getDataSourceJNDIRepoProviderURL() != null) {
            Properties jndiProps = new Properties();

            jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    dataSourceConfig.getDataSourceJNDIRepoInitialContextFactory());
            jndiProps.setProperty(Context.PROVIDER_URL, dataSourceConfig.getDataSourceJNDIRepoProviderURL());

            ctx = new InitialContext(jndiProps);
        } else {
            ctx = new InitialContext();
        }
        return (DataSource) ctx.lookup(remoteObjectName);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception ex1) {
                log.error("Error closing JNDI connection.", ex1);
            }
        }

    }
}

From source file:org.wso2.carbon.bpel.b4p.coordination.db.DatabaseUtil.java

@SuppressWarnings("unchecked")
private <T> T lookupInJndi(String objName) throws Exception {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    try {/*from  www  .  j  a  v  a  2 s. c  om*/
        InitialContext ctx = null;
        try {
            if (bpelServerConfiguration.getDataSourceJNDIRepoInitialContextFactory() != null
                    && bpelServerConfiguration.getDataSourceJNDIRepoProviderURL() != null) {
                Properties jndiProps = new Properties();

                jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                        bpelServerConfiguration.getDataSourceJNDIRepoInitialContextFactory());
                jndiProps.setProperty(Context.PROVIDER_URL,
                        bpelServerConfiguration.getDataSourceJNDIRepoProviderURL());
                ctx = new InitialContext(jndiProps);
            } else {
                ctx = new InitialContext();
            }
            return (T) ctx.lookup(objName);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception ex1) {
                    log.error("Error closing JNDI connection.", ex1);
                }
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}

From source file:org.wso2.carbon.bpmn.core.db.Database.java

/**
 * Performs lookup of JNDI datasource name
 *
 * @param objName       Lookup jndi data source name
 * @param <T>           Lookup object type
 * @return              DataSource object
 * @throws NamingException   Throws exception when lookup naming failed
 *///  w w  w.jav a 2s .c  o  m
@SuppressWarnings("unchecked")
private <T> T lookupInJndi(String objName) throws NamingException {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    InitialContext ctx = null;

    try {

        try {
            ctx = new InitialContext();
            return (T) ctx.lookup(objName);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (NamingException ex1) {
                    log.error("Error closing JNDI connection.", ex1);
                }
            }

        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}

From source file:org.wso2.carbon.event.core.internal.delivery.jms.JMSDeliveryManager.java

public TopicConnection getTopicConnection(String userName) throws EventBrokerException {
    InitialContext initialContext = null;
    try {/*from  www. ja v a 2 s  .  co  m*/

        initialContext = new InitialContext(getInitialContextProperties(userName,
                EventBrokerHolder.getInstance().getQpidServerDetails().getAccessKey()));
        TopicConnectionFactory topicConnectionFactory = getTopicConnectionFactory(initialContext);

        TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
        topicConnection.start();
        return topicConnection;
    } catch (NamingException e) {
        throw new EventBrokerException("Can not create the initial context", e);
    } catch (JMSException e) {
        throw new EventBrokerException("Can not create topic connection", e);
    } catch (Exception e) {
        throw new EventBrokerException("Can not create topic connection", e);
    } finally {
        if (initialContext != null) {
            try {
                initialContext.close();
            } catch (NamingException e) {
                log.error("Can not close the inital context factory ", e);
            }
        }
    }
}

From source file:org.wso2.carbon.humantask.core.db.Database.java

@SuppressWarnings("unchecked")
private <T> T lookupInJndi(String objName) throws Exception {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    try {/*from www.jav a  2s .  c  o  m*/
        InitialContext ctx = null;
        try {
            if (serverConfiguration.getDataSourceJNDIRepoInitialContextFactory() != null
                    && serverConfiguration.getDataSourceJNDIRepoProviderURL() != null) {
                Properties jndiProps = new Properties();

                jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                        serverConfiguration.getDataSourceJNDIRepoInitialContextFactory());
                jndiProps.setProperty(Context.PROVIDER_URL,
                        serverConfiguration.getDataSourceJNDIRepoProviderURL());

                ctx = new InitialContext(jndiProps);
            } else {
                ctx = new InitialContext();
            }
            return (T) ctx.lookup(objName);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception ex1) {
                    log.error("Error closing JNDI connection.", ex1);
                }
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}

From source file:velo.ejb.impl.EmailBean.java

public void sendEmailsToApproversGroup(String agUniqueName, String emailTemplateName,
        Map<String, Object> varsMap) throws EmailNotificationException {
    ApproversGroup ag = approversGroupManager.findApproversGroup(agUniqueName);

    if (ag == null) {
        throw new EmailNotificationException("Could not find Approvers Group with name '" + agUniqueName + "'");
    }//from  w w  w. j  a va  2 s  .co m

    EmailTemplate et = findEmailTemplate(emailTemplateName);
    if (et == null) {
        throw new EmailNotificationException(
                "Could not find Email Template with name '" + emailTemplateName + "'");
    }

    InitialContext ctx = null;
    TransactionManager tm = null;
    try {
        ctx = new InitialContext();
        tm = (TransactionManager) ctx.lookup("java:/TransactionManager");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            }
        }
    }

    /*
    try {
      System.out.println("!!!!!!!!!!!!!!!zzzzzzzzzzZ2: " + tm.getStatus());
      System.out.println("!!!!!!!!!!!!!!!Zzzzzzzzzz2: " + tm.getTransaction().getStatus());
    }catch(SystemException e) {
      e.printStackTrace();
    }
    */

    //throw new EmailNotificationException("ERROR ERROR ERROR!");

    et.setContentVars(varsMap);

    EdmEmailSender es = new EdmEmailSender();

    Email email = null;
    try {
        email = es.factoryEmail(et.getSubject(), et.getParsedContent());
        //email.addTo("asaf@mydomain.com");
        //email.send();
    } catch (EmailException e) {
        log.error("Could not factory email objecT: " + e.getMessage());
        throw new EmailNotificationException(e);
    } catch (ExpressionCreationException e) {
        log.error("Could parsing email content: " + e.getMessage());
        throw new EmailNotificationException(e);
    }

    for (User currUser : ag.getApprovers()) {
        try {
            if (Generic.isEmailValid(currUser.getEmail())) {
                email.addTo(currUser.getEmail());
            } else {
                log.warn("The specified email address is not valid, skipping email: " + currUser.getEmail());
            }
        } catch (EmailException e) {
            log.error("Could not send email to user: " + currUser.getName() + " (email address: '"
                    + currUser.getEmail() + "'), part of ApproversGroup '" + ag.getDescription() + "' due to: "
                    + e.getMessage());
        }
    }

    try {
        email.send();
    } catch (EmailException e) {
        throw new EmailNotificationException(e);
    }
}