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:jdao.JDAO.java

public static Context retrieveContext(String jndi_path) {
    InitialContext jndiContext = null;
    Context env = null;
    try {/*w  w  w.  j  a v a2s.c o  m*/
        log("INFO: resolving " + jndi_path);

        env = jndiContext = new InitialContext();
        env = (Context) jndiContext.lookup(jndi_path);
    } catch (Exception xe) {
        try {
            Name jname = jndiContext.getNameParser(jndi_path).parse(jndi_path);
            Enumeration<String> en = jname.getAll();
            while (en.hasMoreElements()) {
                String name = en.nextElement();
                Context tmp = null;
                try {
                    tmp = (Context) env.lookup(name);
                    env = (Context) env.lookup(name);
                } catch (NameNotFoundException nnf) {
                    log("INFO: creating " + name);
                    env = env.createSubcontext(name);
                }
            }
        } catch (Exception xe2) {
            log("ERROR: resolving " + jndi_path, xe2);
        }
    }
    return env;
}

From source file:csiro.pidsvc.mappingstore.Manager.java

/**************************************************************************
 *  Construction/destruction.//from  w w  w. j av a  2  s .co m
 */

public Manager() throws NamingException, SQLException, IOException {
    InitialContext initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup(Settings.getInstance().getProperty("jndiReferenceName"));
    _connection = ds.getConnection();
    refreshCaseSensitivity();
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstPgSql.CFAstPgSqlSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }//w ww  . j a  va2s.c o m

    if (configuration != null) {
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:postgresql://" + dbServer + ":" + Integer.toString(dbPort) + "/" + dbDatabase;
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
            setDbSchemaName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from PooledConnection for ConnectionPoolDataSource \""
                                + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstDb2LUW.CFAstDb2LUWSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }/*from  w w  w . ja va2  s .com*/

    if (configuration != null) {
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
        } catch (ClassNotFoundException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), "connect",
                    "Could not load IBM DB/2 LUW 10.5 driver", e);
        }
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:db2://" + dbServer + ":" + Integer.toString(dbPort) + "/" + dbDatabase;
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
            setDbSchemaName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from PooledConnection for ConnectionPoolDataSource \""
                                + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstOracle.CFAstOracleSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }/*  w  w  w . j av  a2s  .c o m*/

    if (configuration != null) {
        try {
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:oracle:thin:@" + dbServer;
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.rollback();
            setDbSchemaName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from PooledConnection for ConnectionPoolDataSource \""
                                + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstSybase.CFAstSybaseSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }/* w  w w  .ja  va  2  s  .c o m*/

    if (configuration != null) {
        try {
            Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
        } catch (ClassNotFoundException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), "connect",
                    "Could not load Sybase ASE 15.7 driver", e);
        }
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:sybase:Tds:" + dbServer + ":" + Integer.toString(dbPort);
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
            setDbSchemaName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName + "<<connect>>", e);
        }
        Statement stmtUseDatabase = null;
        try {
            stmtUseDatabase = cnx.createStatement();
            stmtUseDatabase.executeUpdate("use " + dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName + "<<useDatabase>>",
                    e);
        } finally {
            if (stmtUseDatabase != null) {
                try {
                    stmtUseDatabase.close();
                } catch (SQLException e) {
                }
                stmtUseDatabase = null;
            }
        }
        Statement stmtSetChainedOff = null;
        try {
            stmtSetChainedOff = cnx.createStatement();
            stmtSetChainedOff.executeUpdate("set chained off");
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(),
                    S_ProcName + "<<setChainedOff>>", e);
        } finally {
            if (stmtSetChainedOff != null) {
                try {
                    stmtSetChainedOff.close();
                } catch (SQLException e) {
                }
                stmtSetChainedOff = null;
            }
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from PooledConnection for ConnectionPoolDataSource \""
                                + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(),
                    S_ProcName + "<<jndiGetConnection>>", "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(),
                    S_ProcName + "<<jndiGetConnection>>", e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstMySql.CFAstMySqlSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }/*ww w  .j ava  2  s .  c om*/

    if (configuration != null) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), "connect",
                    "Could not load MySql 5.5 JDBC driver", e);
        }
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:mysql://" + dbServer + ":" + Integer.toString(dbPort) + "/" + dbDatabase;
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
            setDbSchemaName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from PooledConnection for ConnectionPoolDataSource \""
                                + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}

From source file:net.sourceforge.msscodefactory.cfacc.v2_0.CFAccPgSql.CFAccPgSqlSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }/*  ww  w.ja  v a2  s.c o m*/

    if (configuration != null) {
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:postgresql://" + dbServer + ":" + Integer.toString(dbPort) + "/" + dbDatabase;
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
            setSchemaDbName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from DataSource \"" + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstMSSql.CFAstMSSqlSchema.java

public boolean connect() {
    final String S_ProcName = "connect";
    if (cnx != null) {
        return (false);
    }/* w  ww.ja  v a2 s  .  c  o  m*/

    if (configuration != null) {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), "connect",
                    "Could not load MS SQL Server 2012 Express Advanced Edition driver", e);
        }
        String dbServer = configuration.getDbServer();
        int dbPort = configuration.getDbPort();
        String dbDatabase = configuration.getDbDatabase();
        String dbUserName = configuration.getDbUserName();
        String dbPassword = configuration.getDbPassword();
        String url = "jdbc:sqlserver://" + dbServer + ":" + Integer.toString(dbPort) + ";";
        Properties props = new Properties();
        props.setProperty("user", dbUserName);
        props.setProperty("password", dbPassword);
        try {
            cnx = DriverManager.getConnection(url, props);
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
            setDbSchemaName(dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName + "<<connect>>", e);
        }
        Statement stmtUseDatabase = null;
        try {
            stmtUseDatabase = cnx.createStatement();
            stmtUseDatabase.executeUpdate("use " + dbDatabase);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName + "<<useDatabase>>",
                    e);
        } finally {
            if (stmtUseDatabase != null) {
                try {
                    stmtUseDatabase.close();
                } catch (SQLException e) {
                }
                stmtUseDatabase = null;
            }
        }
        return (true);
    }
    if (jndiName != null) {
        try {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            if (ds == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get resolve DataSource \"" + jndiName + "\"");
            }
            cnx = ds.getConnection();
            if (cnx == null) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Could not get Connection from PooledConnection for ConnectionPoolDataSource \""
                                + jndiName + "\"");
            }
            cnx.setAutoCommit(false);
            cnx.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            cnx.rollback();
        } catch (NamingException e) {
            cnx = null;
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(),
                    S_ProcName + "<<jndiGetConnection>>", "NamingException " + e.getMessage(), e);
        } catch (SQLException e) {
            cnx = null;
            inTransaction = false;
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(),
                    S_ProcName + "<<jndiGetConnection>>", e);
        }
        return (true);
    }
    throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
            "Neither configurationFile nor jndiName found, do not know how to connect to database");
}