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.firstopen.singularity.epm.StartupServlet.java

/**
 * Initialization of the servlet. <br>
 * //from w  ww .  j  a va2  s . c  o  m
 * @throws ServletException
 *             if an error occure
 */
public void init() throws ServletException {

    InitialContext jndiContext;
    try {
        jndiContext = JNDIUtil.getInitialContext();

        Object objref = jndiContext.lookup("ejb/ale/AleSLSB");
        AleSLSBHome aleSLSBHome = (AleSLSBHome) PortableRemoteObject.narrow(objref, AleSLSBHome.class);

        AleSLSBRemote aleSLSB = aleSLSBHome.create();
        aleSLSB.initialize();

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        log.error(e);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        log.error(e);
    } catch (CreateException e) {
        // TODO Auto-generated catch block
        log.error(e);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        log.error(e);
    }
}

From source file:uk.co.modularaudio.util.pooling.sql.JNDIConnectionFactory.java

/**
 * <P>/*from www  .j  a  v  a  2s.co  m*/
 * Initialise the factory by attempting to get the classes required for the
 * driver.
 * </P>
 *
 * @exception FactoryProductionException
 */
@Override
public void init() throws FactoryProductionException {
    try {
        final InitialContext ic = new InitialContext();
        ds = (DataSource) ic.lookup(icString);

        // Test creating a connection
        final Connection c = ds.getConnection();
        c.close();
    } catch (final NamingException ne) {
        throw new FactoryProductionException(ne.toString());
    } catch (final SQLException sqle) {
        throw new FactoryProductionException(sqle.toString());
    }
}

From source file:org.apache.cactus.spi.server.MessageDrivenBeanRedirector.java

/**
 * Called by the container to create an instance of this Message Driven
 * bean./*from w w w .  ja  v  a2  s. co  m*/
 *
 * @exception CreateException see the EJB specification
 */
public void ejbCreate() throws CreateException {
    LOGGER.debug("------------- MDB redirector service created");

    try {
        InitialContext initContext = new InitialContext();
        QueueConnectionFactory qcf = (QueueConnectionFactory) initContext.lookup("java:comp/env/jms/QCF1");
        connection = qcf.createQueueConnection();
        session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        connection.start();
    } catch (Exception e) {
        throw new EJBException("Failed to initialize MyMDB", e);
    }
}

From source file:org.rhq.jndi.test.JndiAccessTest.java

@Parameters("jnp.port")
public void testRemoteConnectionWorkingFromJava(int jnpPort) throws Exception {
    Properties env = new Properties();
    env.put("java.naming.factory.initial", "org.jboss.naming.NamingContextFactory");
    env.put("java.naming.provider.url", "jnp://localhost:" + jnpPort);
    InitialContext ctx = new InitialContext(env);
    Object kachny = ctx.lookup("kachny");

    assert kachny != null;
}

From source file:velo.ejb.seam.action.ConfActionsBean.java

public void test() {
    try {/*www. ja v a  2s  .co m*/
        InitialContext ctx = new InitialContext();
        Scheduler sched = (Scheduler) ctx.lookup("Quartz");

        System.out.println("SCHEDULER IS: " + sched);
        System.out.println("SCHEDULER NAME IS: " + sched.getSchedulerName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.hippoecm.repository.BootstrapHippoRepository.java

public BootstrapHippoRepository(String location) throws RepositoryException {
    super();/*from ww w. ja v  a 2s.  c o m*/
    this.location = location;

    backingRepository = RepositoryImpl.create(RepositoryConfig
            .create(getClass().getResourceAsStream("BootstrapHippoRepository-repository.xml"), "/dev/null"));
    repository = backingRepository;

    Properties properties = new Properties();
    try {
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup(location);
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        try {
            ResultSet rs = stmt.executeQuery("SELECT hippokey, hippoval FROM hippotbl");
            while (rs.next()) {
                String key = rs.getString(1);
                String value = rs.getString(2);
                properties.put(key, value);
            }
            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            log.warn("bootstrap database not found or not accessible, trying to create: " + ex.getMessage());
            stmt.close();
            stmt = conn.createStatement();
            stmt.executeQuery("CREATE TABLE hippotbl ( hippokey VARCHAR(64), hippoval VARCHAR(63) )");
            stmt.close();
            log.info("bootstrap database created, loading default content");
            InputStream is = null;
            try {
                is = getClass().getResourceAsStream("BootstrapHippoRepository-template.properties");
                properties.load(is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        conn.close();
    } catch (IOException ex) {
        log.error("bootstrap database communication problem: " + ex.getMessage());
    } catch (SQLException ex) {
        log.error("bootstrap database access problem: " + ex.getMessage());
    } catch (NamingException ex) {
        log.error("bootstrap database name lookup failure: " + ex.getMessage());
    }
}

From source file:org.wso2.carbon.appfactory.resource.mgt.listeners.TenantCreationDurableSubscriber.java

/**
 * Subscribe as a durable subscriber to the topic.
 *
 * @throws AppFactoryEventException// ww w .  j av  a 2  s. co  m
 */
public void subscribe() throws AppFactoryEventException {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, AppFactoryConstants.ANDES_ICF);
    properties.put(AppFactoryConstants.CF_NAME_PREFIX + AppFactoryConstants.CF_NAME,
            Util.getTCPConnectionURL());
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");
    properties.put(AppFactoryConstants.TOPIC, topicName);
    TopicConnectionFactory connFactory;
    TopicConnection topicConnection;
    TopicSession topicSession;
    TopicSubscriber topicSubscriber;
    InitialContext ctx;
    try {
        ctx = new InitialContext(properties);
        connFactory = (TopicConnectionFactory) ctx.lookup(AppFactoryConstants.CF_NAME);
        topicConnection = connFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);
        Topic topic;
        try {
            topic = (Topic) ctx.lookup(topicName);
        } catch (NamingException e) {
            topic = topicSession.createTopic(topicName);
        }
        topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);
        topicSubscriber.setMessageListener(
                new TenantCreationMessageListener(topicConnection, topicSession, topicSubscriber));
        topicConnection.start();
        if (log.isDebugEnabled()) {
            log.debug("Durable Subscriber created for topic " + topicName + " with subscription id : "
                    + subscriptionId);
        }
    } catch (NamingException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    } catch (JMSException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    }
}

From source file:org.wso2.carbon.appfactory.stratos.listeners.StratosSubscriptionDurableSubscriber.java

/**
 * Subscribe as a durable subscriber to the topic.
 *
 * @throws AppFactoryEventException//from w  w  w. j av a2s  .c  o  m
 */
public void subscribe() throws AppFactoryEventException {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, AppFactoryConstants.ANDES_ICF);
    properties.put(AppFactoryConstants.CF_NAME_PREFIX + AppFactoryConstants.CF_NAME,
            Util.getTCPConnectionURL());
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, true);
    properties.put(AppFactoryConstants.TOPIC, topicName);
    TopicConnectionFactory connFactory;
    TopicConnection topicConnection;
    TopicSession topicSession;
    TopicSubscriber topicSubscriber;
    InitialContext ctx;
    try {
        ctx = new InitialContext(properties);
        connFactory = (TopicConnectionFactory) ctx.lookup(AppFactoryConstants.CF_NAME);
        topicConnection = connFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);
        Topic topic;
        try {
            topic = (Topic) ctx.lookup(topicName);
        } catch (NamingException e) {
            topic = topicSession.createTopic(topicName);
        }
        topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);
        topicSubscriber.setMessageListener(
                new StratosSubscriptionMessageListener(topicConnection, topicSession, topicSubscriber));
        topicConnection.start();
        if (log.isDebugEnabled()) {
            log.debug("Durable Subscriber created for topic " + topicName + " with subscription id : "
                    + subscriptionId);
        }
    } catch (NamingException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    } catch (JMSException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    }
}

From source file:org.hibersap.ejb.interceptor.HibersapSessionInterceptor.java

private SessionManager lookupSessionManager(final String jndiName) {
    InitialContext context = null;
    try {//from  w ww  .  j a  v a 2 s .  c om
        context = new InitialContext();
        Object object = context.lookup(jndiName);

        if (object == null) {
            throw new HibersapException(
                    format("Lookup for JNDI name '%s' returned null. Expected to find an instance of %s",
                            jndiName, SessionManager.class.getName()));
        }
        if (!SessionManager.class.isAssignableFrom(object.getClass())) {
            throw new HibersapException(
                    format("Object bound under JNDI name '%s' is not a %s but an instance of %s", jndiName,
                            SessionManager.class.getName(), object.getClass().getName()));
        }
        return (SessionManager) object;
    } catch (NamingException e) {
        throw new HibersapException("Error creating InitialContext", e);
    } finally {
        if (context != null) {
            try {
                context.close();
            } catch (NamingException e) {
                LOGGER.warn("Error closing InitialContext", e);
            }
        }
    }
}

From source file:org.opendds.esb.actions.routing.JmsRouter.java

public JmsRouter(ConfigTree config) throws ConfigurationException {
    super(config);
    try {/*  w  w  w. jav  a 2  s.co m*/
        InitialContext ctx = new InitialContext();

        ConnectionFactory cf = (ConnectionFactory) ctx
                .lookup(ConfigTreeHelper.requireAttribute(config, "connection-factory"));

        connection = cf.createConnection();

        destination = (Destination) ctx.lookup(ConfigTreeHelper.requireAttribute(config, "dest-name"));

        messages = new ThreadedQueue<Message>(new MessageListener(),
                ConfigTreeHelper.getAttribute(config, "maxThreads"));

    } catch (Exception e) {
        throw new ConfigurationException(e);
    }
}