Example usage for javax.naming InitialContext lookup

List of usage examples for javax.naming InitialContext lookup

Introduction

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

Prototype

public Object lookup(Name name) throws NamingException 

Source Link

Usage

From source file:org.exoplatform.services.naming.InitialContextTest.java

public void testCompositeNameUsing() throws Exception {
    Name name = new CompositeName("java:comp/env/jdbc/jcr");
    Enumeration en = name.getAll();
    while (en.hasMoreElements()) {
        en.nextElement();// w w  w .ja  v a2  s  .c  o m
    }
    InitialContext ctx = new InitialContext();
    ctx.bind(name, "foo");
    assertEquals("foo", ctx.lookup(name));
    try {
        ctx.bind(name, "foo2");
        fail("A NameAlreadyBoundException is expected here");
    } catch (NameAlreadyBoundException e) {
        // expected exception
    }
    assertEquals("foo", ctx.lookup(name));
    assertEquals("foo", ctx.lookup("java:comp/env/jdbc/jcr"));
    ctx.unbind(name);
    try {
        ctx.lookup(name);
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }
}

From source file:org.jbpm.msg.jms.AsyncProcessingTest.java

protected void setUp() throws Exception {
    InitialContext initialContext = new InitialContext();
    LocalCommandServiceHome localCommandServiceHome = (LocalCommandServiceHome) initialContext
            .lookup("java:comp/env/ejb/LocalCommandServiceBean");
    commandService = localCommandServiceHome.create();
}

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
 *///from  w  w  w . j  a  v  a2  s. 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:com.openkm.util.MailUtils.java

/**
 * /*w w  w.j  ava 2  s .  co  m*/
 */
private static Session getMailSession() {
    Session mailSession = null;

    try {
        InitialContext initialContext = new InitialContext();
        Object obj = initialContext.lookup(Config.JNDI_BASE + "mail/OpenKM");
        mailSession = (Session) PortableRemoteObject.narrow(obj, Session.class);
    } catch (javax.naming.NamingException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return mailSession;
}

From source file:org.josso.jbportal27.agent.JOSSOIdentityServiceImpl.java

/**
 * //  ww  w . jav  a2s.c o  m
 * @param username
 * @return
 */
public boolean exists(String username) {
    Session session = null;
    Transaction tx = null;
    try {
        boolean exists = false;

        InitialContext initialContext = new InitialContext();
        SessionFactory sessionFactory = (SessionFactory) initialContext
                .lookup("java:/portal/IdentitySessionFactory");
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        User user = this.userModule.findUserByUserName(username);
        if (user != null && user.getUserName().trim().equals(username.trim())) {
            exists = true;
        }

        return exists;
    } catch (Exception e) {
        logger.error(this, e);
        throw new RuntimeException(e);
    } finally {
        tx.commit();
        session.close();
    }
}

From source file:org.wso2.carbon.andes.ui.client.QueueBrowserClient.java

/**
 * Return a lazy-loading iterator (java.util.Enumeration) of the messages of the given queue.
 * @return java.util.Enumeration//from w  ww.j a v a  2  s  .  c  o m
 */
public Enumeration browseQueue() {

    Enumeration queueContentsEnu = null;
    try {
        InitialContext ctx = new InitialContext(properties);
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        queueConnection = connFactory.createQueueConnection();
        Queue queue = (Queue) ctx.lookup(nameOfQueue);
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueBrowser = queueSession.createBrowser(queue);
        queueConnection.start();

        queueContentsEnu = queueBrowser.getEnumeration();

    } catch (NamingException e) {
        log.error("Error browsing queue.", e);
    } catch (JMSException e) {
        log.error("Error browsing queue.", e);
    }
    return queueContentsEnu;
}

From source file:org.josso.jbportal27.agent.JOSSOIdentityServiceImpl.java

/**
 * /*from   w  w  w  .j  a  v a2s.co m*/
 */
public boolean authenticate(String username, String password) {
    Session session = null;
    Transaction tx = null;
    try {
        boolean status = false;

        InitialContext initialContext = new InitialContext();
        SessionFactory sessionFactory = (SessionFactory) initialContext
                .lookup("java:/portal/IdentitySessionFactory");
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        User user = this.userModule.findUserByUserName(username);
        if (user != null) {
            //Check and make sure the user account is enabled
            Boolean enabled = (Boolean) this.profileModule.getProperty(user, User.INFO_USER_ENABLED);
            if (enabled != null || enabled.booleanValue()) {
                //Now perform validation
                status = user.validatePassword(password);
            }
        }

        return status;
    } catch (Exception e) {
        logger.error(this, e);
        return false;
    } finally {
        tx.commit();
        session.close();
    }
}

From source file:org.wso2.carbon.oc.agent.publisher.mb.MBPublisher.java

/**
 * @param queueName   - String mb queue name
 * @param jsonMessage - String mb queue message json string
 *///ww  w.  ja  v a  2s.c o m
public void sendMessages(String queueName, String jsonMessage) {

    try {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
        properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(username, password));
        properties.put(QUEUE_NAME_PREFIX + queueName, queueName);
        InitialContext ctx = new InitialContext(properties);
        // lookup connection factory
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME);
        QueueConnection queueConnection = connFactory.createQueueConnection();
        queueConnection.start();
        QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        // send message
        Queue queue = (Queue) ctx.lookup(queueName);
        // create the message to send

        TextMessage textMessage = queueSession.createTextMessage(jsonMessage);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.send(textMessage);
        queueSender.close();
        queueSession.close();
        queueConnection.close();
    } catch (JMSException e) {
        logger.error("MBPublisher connection down", e);
    } catch (NamingException e) {
        logger.error("Naming error", e);
    }

}

From source file:org.josso.jbportal27.agent.JOSSOIdentityServiceImpl.java

/**
 * // w w  w. j a  va2s  . c o m
 * @param username
 * @return
 */
public String[] getUserRoles(String username) {
    Session session = null;
    Transaction tx = null;
    try {
        String[] userRoles = null;

        InitialContext initialContext = new InitialContext();
        SessionFactory sessionFactory = (SessionFactory) initialContext
                .lookup("java:/portal/IdentitySessionFactory");
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        User user = this.userModule.findUserByUserName(username);
        if (user != null && user.getUserName().trim().equals(username.trim())) {
            Set roles = this.membershipModule.getRoles(user);
            userRoles = new String[roles.size() + 1];
            userRoles[0] = "Authenticated";
            int index = 1;
            for (Iterator itr = roles.iterator(); itr.hasNext();) {
                Role role = (Role) itr.next();
                userRoles[index++] = role.getName();
            }
        }

        return userRoles;
    } catch (Exception e) {
        logger.error(this, e);
        throw new RuntimeException(e);
    } finally {
        tx.commit();
        session.close();
    }
}

From source file:com.dattack.naming.StandaloneJndiTest.java

@Test
public void testLookupValidDataSource() {

    try {/*w w  w  .j  a  va 2  s  . c  o  m*/
        final InitialContext context = new InitialContext();
        final String name = getCompositeName(VALID_CONTEXT, VALID_OBJECT_NAME);
        final DataSource dataSource = (DataSource) context.lookup(name);
        assertNotNull(dataSource);
    } catch (final NamingException e) {
        fail(e.getMessage());
    }
}