Example usage for javax.naming Context lookup

List of usage examples for javax.naming Context lookup

Introduction

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

Prototype

public Object lookup(String name) throws NamingException;

Source Link

Document

Retrieves the named object.

Usage

From source file:com.netspective.axiom.connection.JndiConnectionProvider.java

public ConnectionProviderEntries getDataSourceEntries(ValueContext vc) {
    ConnectionProviderEntries entries = new BasicConnectionProviderEntries();

    try {//  w w w.j a  v a 2  s .  c  o  m
        String envPath = getRootContextName();
        Context env = getRootContext();
        if (env != null) {
            for (NamingEnumeration e = env.list(""); e.hasMore();) {
                NameClassPair entry = (NameClassPair) e.nextElement();
                String dataSourceId = envPath != null ? (envPath + "/" + entry.getName()) : entry.getName();
                try {
                    DataSource source = (DataSource) env.lookup(entry.getName());
                    entries.add(getDataSourceEntry(dataSourceId, source));
                } catch (NamingException ex) {
                    log.debug(JndiConnectionProvider.class.getName() + ".getDataSourceEntries()", ex);
                } catch (SQLException ex) {
                    log.debug(JndiConnectionProvider.class.getName() + ".getDataSourceEntries()", ex);
                }
            }
        }
    } catch (NamingException e) {
        log.error("Errorw in getDataSourceEntries()", e);
        throw new NestableRuntimeException(e);
    }

    return entries;
}

From source file:com.redhat.lightblue.rest.crud.ITCaseCrudResourceRDBMSTest.java

@Test
public void testSelect()
        throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
        InstantiationException, IllegalAccessException, URISyntaxException, JSONException {
    try {/*w  w w. j  a v  a2 s.  c  o  m*/
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasource");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute(
                "CREATE TABLE Country ( name varchar(255), iso2code varchar(255), iso3code varchar(255) );");
        stmt.execute("INSERT INTO Country (name,iso2code,iso3code) VALUES ('a','CA','c');");
        stmt.close();
        conn.close();

        Assert.assertNotNull("CrudResource was not injected by the container", cutCrudResource);
        RestConfiguration.setDatasources(new DataSourcesConfiguration(
                JsonUtils.json(readConfigFile(RestConfiguration.DATASOURCE_FILENAME))));
        RestConfiguration.setFactory(new LightblueFactory(RestConfiguration.getDatasources()));

        String expectedCreated = readFile("expectedCreated.json");
        String metadata = readFile("metadata.json");
        EntityMetadata em = RestConfiguration.getFactory().getJSONParser()
                .parseEntityMetadata(JsonUtils.json(metadata));
        RestConfiguration.getFactory().getMetadata().createNewMetadata(em);
        EntityMetadata em2 = RestConfiguration.getFactory().getMetadata().getEntityMetadata("country", "1.0.0");
        String resultCreated = RestConfiguration.getFactory().getJSONParser().convert(em2).toString();
        JSONAssert.assertEquals(expectedCreated, resultCreated, false);

        String expectedFound = readFile("expectedFound.json");
        String resultFound = cutCrudResource.find("country", "1.0.0", readFile("resultFound.json")).getEntity()
                .toString();
        // TODO / NOTE we can change the result format if needed, now it return an array of arrays
        //System.err.println("!!!!!!!!!!!!!!!!!" + resultFound);
        JSONAssert.assertEquals(expectedFound, resultFound, false);
    } catch (NamingException | SQLException ex) {
        throw new IllegalStateException(ex);
    }
    mongo.dropDatabase(DB_NAME);
}

From source file:com.redhat.lightblue.rest.crud.ITCaseCrudResourceRDBMSTest.java

@Test
public void testSelectAll()
        throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
        InstantiationException, IllegalAccessException, URISyntaxException, JSONException {
    try {/*w w  w.ja  v a  2s . c o  m*/
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasource");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute(
                "CREATE TABLE Country ( name varchar(255), iso2code varchar(255), iso3code varchar(255) );");
        stmt.execute("INSERT INTO Country (name,iso2code,iso3code) VALUES ('a','CA','c');");
        stmt.close();
        conn.close();

        Assert.assertNotNull("CrudResource was not injected by the container", cutCrudResource);
        RestConfiguration.setDatasources(new DataSourcesConfiguration(
                JsonUtils.json(readConfigFile(RestConfiguration.DATASOURCE_FILENAME))));
        RestConfiguration.setFactory(new LightblueFactory(RestConfiguration.getDatasources()));

        String expectedCreated = readFile("expectedCreated.json");
        String metadata = readFile("metadata.json");
        EntityMetadata em = RestConfiguration.getFactory().getJSONParser()
                .parseEntityMetadata(JsonUtils.json(metadata));
        RestConfiguration.getFactory().getMetadata().createNewMetadata(em);
        EntityMetadata em2 = RestConfiguration.getFactory().getMetadata().getEntityMetadata("country", "1.0.0");
        String resultCreated = RestConfiguration.getFactory().getJSONParser().convert(em2).toString();
        JSONAssert.assertEquals(expectedCreated, resultCreated, false);

        String expectedFound = readFile("expectedFoundAll.json");
        String resultFound = cutCrudResource.find("country", "1.0.0", readFile("resultFoundAll.json"))
                .getEntity().toString();
        // TODO / NOTE we can change the result format if needed, now it return an array of arrays
        //System.err.println("!!!!!!!!!!!!!!!!!" + resultFound);
        JSONAssert.assertEquals(expectedFound, resultFound, false);
    } catch (NamingException | SQLException ex) {
        throw new IllegalStateException(ex);
    }
    mongo.dropDatabase(DB_NAME);
}

From source file:com.sap.cloudlabs.connectivity.proxy.ProxyServlet.java

/**
 * Returns an initialized HttpClient which points to the specified destination.     
 *///w w  w.j a v  a2  s  .c o m
private HttpDestination getDestination(String destinationName) throws ServletException {
    try {
        /*
         * In case the an annotation @Resource is used, the following if block is obsolete
         */
        if (destinationFactory == null) {
            Context ctx = new InitialContext();
            destinationFactory = (DestinationFactory) ctx.lookup(DestinationFactory.JNDI_NAME);
        }
        HttpDestination dest = (HttpDestination) destinationFactory.getDestination(destinationName);
        return dest;
    } catch (Exception e) {
        throw new ServletException(writeMessage("Unable to resolve destination " + destinationName), e);
    }
}

From source file:com.stratelia.silverpeas.versioning.jcr.impl.AbstractJcrTestCase.java

/**
 * Workaround to be able to use Sun's JNDI file system provider on Unix
 * @param ic : the JNDI initial context//from   w  w  w  .  j a va2  s  .  c o m
 * @param jndiName : the binding name
 * @param ref : the reference to be bound
 * @throws NamingException
 */
protected void rebind(InitialContext ic, String jndiName, Reference ref) throws NamingException {
    Context currentContext = ic;
    StringTokenizer tokenizer = new StringTokenizer(jndiName, "/", false);
    while (tokenizer.hasMoreTokens()) {
        String name = tokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
            try {
                currentContext = (Context) currentContext.lookup(name);
            } catch (javax.naming.NameNotFoundException nnfex) {
                currentContext = currentContext.createSubcontext(name);
            }
        } else {
            currentContext.rebind(name, ref);
        }
    }
}

From source file:com.redhat.lightblue.rest.crud.ITCaseCrudResourceRDBMSTest.java

@Test
public void testDelete()
        throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
        InstantiationException, IllegalAccessException, URISyntaxException, JSONException {
    try {//from   w  ww . j av a 2 s .co  m
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasource");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute(
                "CREATE TABLE Country ( name varchar(255), iso2code varchar(255), iso3code varchar(255) );");
        stmt.execute("INSERT INTO Country (name,iso2code,iso3code) VALUES ('a','CA','c');");
        stmt.close();
        conn.close();

        Assert.assertNotNull("CrudResource was not injected by the container", cutCrudResource);
        RestConfiguration.setDatasources(new DataSourcesConfiguration(
                JsonUtils.json(readConfigFile(RestConfiguration.DATASOURCE_FILENAME))));
        RestConfiguration.setFactory(new LightblueFactory(RestConfiguration.getDatasources()));

        String expectedCreated = readFile("expectedCreated.json");
        String metadata = readFile("metadata.json").replaceAll("YYZ",
                " DELETE FROM Country WHERE ISO2CODE=:ISO2CODE;");
        EntityMetadata em = RestConfiguration.getFactory().getJSONParser()
                .parseEntityMetadata(JsonUtils.json(metadata));
        RestConfiguration.getFactory().getMetadata().createNewMetadata(em);
        EntityMetadata em2 = RestConfiguration.getFactory().getMetadata().getEntityMetadata("country", "1.0.0");
        String resultCreated = RestConfiguration.getFactory().getJSONParser().convert(em2).toString();
        JSONAssert.assertEquals(expectedCreated, resultCreated, false);

        String expectedDeleted = readFile("expectedDeleted.json");
        String resultDeleted = cutCrudResource.delete("country", "1.0.0", readFile("resultDeleted.json"))
                .getEntity().toString();
        //System.err.println("!!!!!!!!!!!!!!!!!" + resultDeleted);

        ds = (DataSource) initCtx.lookup("java:/mydatasource");
        conn = ds.getConnection();
        stmt = conn.createStatement();
        stmt.execute("SELECT * FROM Country;");
        ResultSet resultSet = stmt.getResultSet();

        Assert.assertEquals(false, resultSet.next());

        JSONAssert.assertEquals(expectedDeleted, resultDeleted, false);
    } catch (NamingException | SQLException ex) {
        throw new IllegalStateException(ex);
    }
    mongo.dropDatabase(DB_NAME);
}

From source file:com.redhat.lightblue.rest.crud.ITCaseCrudResourceRDBMSTest.java

@Test
public void testInsert()
        throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
        InstantiationException, IllegalAccessException, URISyntaxException, JSONException {
    try {//from   w  w w .  jav a  2s .  co m
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasource");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute(
                "CREATE TABLE Country ( name varchar(255), iso2code varchar(255), iso3code varchar(255) );");
        stmt.close();
        conn.close();

        Assert.assertNotNull("CrudResource was not injected by the container", cutCrudResource);
        RestConfiguration.setDatasources(new DataSourcesConfiguration(
                JsonUtils.json(readConfigFile(RestConfiguration.DATASOURCE_FILENAME))));
        RestConfiguration.setFactory(new LightblueFactory(RestConfiguration.getDatasources()));

        String expectedCreated = readFile("expectedCreated.json");
        String metadata = readFile("metadata.json").replaceAll("XXY",
                "INSERT INTO Country (NAME,ISO2CODE,ISO3CODE) VALUES (:name,:iso2code,:iso3code);");
        EntityMetadata em = RestConfiguration.getFactory().getJSONParser()
                .parseEntityMetadata(JsonUtils.json(metadata));
        RestConfiguration.getFactory().getMetadata().createNewMetadata(em);
        EntityMetadata em2 = RestConfiguration.getFactory().getMetadata().getEntityMetadata("country", "1.0.0");
        String resultCreated = RestConfiguration.getFactory().getJSONParser().convert(em2).toString();
        JSONAssert.assertEquals(expectedCreated, resultCreated, false);

        String expectedInserted = readFile("expectedInserted.json");
        String resultInserted = cutCrudResource.insert("country", "1.0.0", readFile("resultInserted.json"))
                .getEntity().toString();
        System.err.println("!!!!!!!!!!!!!!!!!" + resultInserted);

        ds = (DataSource) initCtx.lookup("java:/mydatasource");
        conn = ds.getConnection();
        stmt = conn.createStatement();
        stmt.execute("SELECT * FROM Country;");
        ResultSet resultSet = stmt.getResultSet();
        resultSet.next();
        Assert.assertEquals("Canad", resultSet.getString("name"));
        Assert.assertEquals("CA", resultSet.getString("iso2code"));
        Assert.assertEquals("CAN", resultSet.getString("iso3code"));

        JSONAssert.assertEquals(expectedInserted, resultInserted, false);
    } catch (NamingException ex) {
        throw new IllegalStateException(ex);
    } catch (SQLException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:com.redhat.lightblue.rest.crud.ITCaseCrudResourceRDBMSTest.java

@Test
public void testUpdate()
        throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
        InstantiationException, IllegalAccessException, URISyntaxException, JSONException {
    try {/*from  w w  w. j a va2  s . c o m*/
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource) initCtx.lookup("java:/mydatasource");
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute(
                "CREATE TABLE Country ( name varchar(255), iso2code varchar(255), iso3code varchar(255) );");
        stmt.execute("INSERT INTO Country (name,iso2code,iso3code) VALUES ('a','CA','c');");
        stmt.close();
        conn.close();

        Assert.assertNotNull("CrudResource was not injected by the container", cutCrudResource);
        RestConfiguration.setDatasources(new DataSourcesConfiguration(
                JsonUtils.json(readConfigFile(RestConfiguration.DATASOURCE_FILENAME))));
        RestConfiguration.setFactory(new LightblueFactory(RestConfiguration.getDatasources()));

        String expectedCreated = readFile("expectedCreated.json");
        String metadata = readFile("metadata.json").replaceAll("ZZY",
                " UPDATE Country SET NAME=:name  WHERE ISO2CODE=:ISO2CODE;");
        EntityMetadata em = RestConfiguration.getFactory().getJSONParser()
                .parseEntityMetadata(JsonUtils.json(metadata));
        RestConfiguration.getFactory().getMetadata().createNewMetadata(em);
        EntityMetadata em2 = RestConfiguration.getFactory().getMetadata().getEntityMetadata("country", "1.0.0");
        String resultCreated = RestConfiguration.getFactory().getJSONParser().convert(em2).toString();
        JSONAssert.assertEquals(expectedCreated, resultCreated, false);

        String expectedUpdated = readFile("expectedUpdated.json");
        String resultUpdated = cutCrudResource.update("country", "1.0.0", readFile("resultUpdated.json"))
                .getEntity().toString();
        System.err.println("!!!!!!!!!!!!!!!!!" + resultUpdated);

        ds = (DataSource) initCtx.lookup("java:/mydatasource");
        conn = ds.getConnection();
        stmt = conn.createStatement();
        stmt.execute("SELECT * FROM Country;");
        ResultSet resultSet = stmt.getResultSet();
        resultSet.next();
        Assert.assertEquals("Canada", resultSet.getString("name"));
        Assert.assertEquals("CA", resultSet.getString("iso2code"));
        Assert.assertEquals("c", resultSet.getString("iso3code"));

        JSONAssert.assertEquals(expectedUpdated, resultUpdated, false);
    } catch (NamingException | SQLException ex) {
        throw new IllegalStateException(ex);
    }
    mongo.dropDatabase(DB_NAME);
}

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

private ConnectionFactory getConnectionFactory(InitialContext initialContext) throws NamingException {
    Context jmsContext = getJmsContext(initialContext);
    ConnectionFactory jmsConnectionFactory;
    try {//w  ww  .j a  v a  2s  .c o  m
        jmsConnectionFactory = (ConnectionFactory) jmsContext.lookup(CONNECTION_FACTORY_NAME);
        log.debug("retrieved jms connection factory: " + CONNECTION_FACTORY_NAME);
    } catch (NamingException e) {
        log.debug("jms connection factory not found: " + CONNECTION_FACTORY_NAME);
        jmsConnectionFactory = integrationServiceFactory.getConnectionFactory();
        if (jmsConnectionFactory == null)
            throw e;
        log.debug("fell back on default connection factory");
    }
    return jmsConnectionFactory;
}

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

public boolean isRdnExists(LdapName rootDn, LdapName rdnName) {
    Object result = null;//from w ww.ja  v a2 s .c om
    Context ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());
        result = ctx.lookup(rdnName);

    } catch (NamingException e) {
        throw new RuntimeException(rdnName.toString() + "," + rootDn.toString(), e);
    } finally {
        contextFactory.closeContext(ctx);
    }

    if (result != null)
        return true;
    else
        return false;
}