Example usage for javax.naming InitialContext InitialContext

List of usage examples for javax.naming InitialContext InitialContext

Introduction

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

Prototype

public InitialContext() throws NamingException 

Source Link

Document

Constructs an initial context.

Usage

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JndiJdbcDataSourceService.java

protected Connection createConnection() {

    try {//from  w w w  . j  a  va  2s  .c  o  m
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/" + jndiName);
        Connection c = ds.getConnection();
        if (log.isDebugEnabled()) {
            log.debug(
                    "CreateConnection successful at com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JndiJdbcDataSourceService.createConnection");
        }
        return c;
    } catch (NamingException e) {
        try {
            //Added as short time solution due of http://bugzilla.jaspersoft.com/show_bug.cgi?id=26570.
            //The main problem - this code executes in separate tread (non http).
            //Jboss 7 support team recommend that you use the non-component environment namespace for such situations.
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(jndiName);
            Connection c = ds.getConnection();
            if (log.isDebugEnabled()) {
                log.debug(
                        "CreateConnection successful at com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.JndiJdbcDataSourceService.createConnection");
            }
            return c;

        } catch (NamingException ex) {
            if (log.isDebugEnabled())
                log.debug(e, e);
            throw new JSExceptionWrapper(e);
        } catch (SQLException ex) {
            if (log.isDebugEnabled())
                log.debug(e, e);
            throw new JSExceptionWrapper(e);
        }

    } catch (SQLException e) {
        if (log.isDebugEnabled())
            log.debug(e, e);
        throw new JSExceptionWrapper(e);
    }
}

From source file:com.iterranux.droolsjbpmAtomikosIntegration.TransactionManagerJNDIRegistrator.java

@PostConstruct
public void init() {
    log.debug("Registering TX to JNDI.");
    try {//from w w  w.  j a v a 2 s. c o m
        SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        Context context = new InitialContext();

        context.bind(userTransactionLookup, userTransaction);
        context.bind(transactionManagerLookup, transactionManager);

    } catch (NamingException e) {
        log.error("JNDI Registration of the Transasction Manager failed.", e);
    }

}

From source file:edu.byu.wso2.apim.extensions.BYUIdentifiersLookup.java

public void init(SynapseEnvironment synapseEnvironment) {
    if (log.isInfoEnabled()) {
        log.info("Initializing BYUIdentifiersLookup Mediator");
    }/*w w w .  j  a  v a  2  s .c  om*/
    if (log.isDebugEnabled())
        log.debug("BYUIdentifiersLookup: looking up datasource" + DsName);
    try {
        this.ds = (DataSource) new InitialContext().lookup(DsName);
    } catch (NamingException e) {
        e.printStackTrace();
    }
    if (log.isDebugEnabled())
        log.debug("BYUIdentifiersLookup: acquired datasource");

}

From source file:ar.com.zauber.commons.web.proxy.impl.dao.properties.provider.JndiPropertiesProvider.java

/**
 * Creates the JndiPropertiesProvider./*from  ww  w  .  j av a 2  s .  c o m*/
 *
 * @param jndiVariable
 * @param resourceLoader
 * @param fallback
 * @throws IOException .
 */
public JndiPropertiesProvider(final String jndiVariable, final ResourceLoader resourceLoader,
        final PropertiesProvider fallback) throws IOException {
    Validate.notEmpty(jndiVariable);
    Validate.notNull(resourceLoader);
    Validate.notNull(fallback);

    try {
        final InitialContext initCtx = new InitialContext();
        final Context envCtx = (Context) initCtx.lookup("java:comp/env");
        final Resource resource = resourceLoader.getResource((String) envCtx.lookup(jndiVariable));
        target = new ResourcePropertiesProvider(resource);
    } catch (final NamingException e) {
        target = fallback;
    }
}

From source file:com.jaspersoft.jasperserver.remote.dbservices.impl.JndiConnectionServiceImpl.java

/**
 *
 * @param resource//from ww  w.j  a  va 2 s  .  com
 * @return Connection object, guaranteed to be non-null (not found or not supported resource indicated by exception)
 * @throws ResourceNotFoundException if no resource found 
 * @throws RemoteException in case of unclassified error
 */
@SuppressWarnings("unused")
public Connection getConnection(Resource resource) {
    Connection result = null;
    String DATASOURCE_CONTEXT = null;
    long startTime = System.currentTimeMillis();
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Enter getConnection .. Start Time" + System.currentTimeMillis());
        }
        if (resource instanceof JndiJdbcReportDataSource) {
            JndiJdbcReportDataSource jndiDataSource = (JndiJdbcReportDataSource) resource;
            DATASOURCE_CONTEXT = "java:comp/env/" + jndiDataSource.getJndiName();
            Context initialContext = new InitialContext();
            if (initialContext != null) {
                DataSource datasource = (DataSource) initialContext.lookup(DATASOURCE_CONTEXT);
                if (datasource != null) {
                    result = datasource.getConnection();
                } else {
                    throw new RemoteException("Cannot get database connection: Please check datasource url");
                }
            } else {
                throw new RemoteException("Cannot get jndi context: Please check datasource url");
            }

        } else {
            throw new RemoteException("Invalid Resource: Please check datasource url");
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        throw new RemoteException("Cannot get jndi connection:" + ex.getMessage());
    } finally {
        if (logger.isDebugEnabled()) {
            long elapsedTime = System.currentTimeMillis() - startTime;
            logger.debug("Exit getConnection .. Total Time Spent: " + elapsedTime);
        }
    }
    return result;

}

From source file:com.wso2telco.dbutils.DbUtils.java

/**
 * Initialize datasources./* w w  w. ja  v  a2s  .  c o m*/
 *
 * @throws SQLException
 *             the SQL exception
 * @throws AxataDBUtilException
 *             the axata db util exception
 */
public static void initializeDatasources() throws SQLException, AxataDBUtilException {
    if (axiataDatasource != null) {
        return;
    }

    try {
        log.info("Before DB Initialize");
        Context ctx = new InitialContext();
        axiataDatasource = (DataSource) ctx.lookup(AXIATA_DATA_SOURCE);
    } catch (NamingException e) {
        handleException("Error while looking up the data source: " + AXIATA_DATA_SOURCE, e);
    }
}

From source file:com.glaf.core.jdbc.DBConnectionFactory.java

public static boolean checkConnection(java.util.Properties props) {
    Connection connection = null;
    try {/*from   ww  w .  j  a  v a  2 s  .  co m*/
        if (StringUtils.isNotEmpty(props.getProperty(DBConfiguration.JDBC_DATASOURCE))) {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup(props.getProperty(DBConfiguration.JDBC_DATASOURCE));
            connection = ds.getConnection();
        } else {
            ConnectionProvider provider = ConnectionProviderFactory.createProvider(props);
            if (provider != null) {
                connection = provider.getConnection();
            }
        }
        if (connection != null) {
            return true;
        }
    } catch (Exception ex) {
        logger.error(ex);
        ex.printStackTrace();
    } finally {
        JdbcUtils.close(connection);
    }
    return false;
}

From source file:com.comcast.cats.service.settop.command.SettopServiceBaseCommand.java

public void lookupSettopCatalog() {
    try {//from   w ww  . j a  v a  2 s .  com
        InitialContext ctx = new InitialContext();
        catalog = (SettopCatalog) ctx.lookup("cats/services/SettopCatalog");
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.wabacus.config.database.datasource.JNDIDataSource.java

public DataSource getDataSource() {
    Context context = null;// w w  w  . j  a  v  a 2  s .  co m
    try {
        if (ds != null) {
            return ds;
        }
        context = new InitialContext();
        ds = (DataSource) context.lookup(jndi);
        return ds;
    } catch (Exception e) {
        log.error("????", e);
        return null;
    } finally {
        try {
            if (context != null) {
                context.close();
            }
        } catch (Exception ex) {
            log.error("????", ex);
        }
    }

}

From source file:com.jaspersoft.jasperserver.remote.connection.JndiConnectionStrategy.java

@Override
public JndiConnection createConnection(JndiConnection connectionDescription, Map<String, Object> data)
        throws IllegalParameterValueException {
    Connection conn = null;//from   ww  w .  j a  v a2 s.co  m
    boolean passed = false;
    Throwable exception = null;
    try {
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + connectionDescription.getJndiName());
        conn = dataSource.getConnection();
        if (conn != null) {
            passed = true;
        }
    } catch (SQLException vex) {
        if (vex.getMessage().indexOf("[JI_CONNECTION_VALID]") >= 0)
            passed = true;
        exception = vex;
    } catch (Throwable e) {
        exception = e;
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("Couldn't disconnect JNDI connection", e);
            }
    }
    if (!passed) {
        throw new ConnectionFailedException(connectionDescription.getJndiName(), "jndiName",
                "Invalid JNDI name: " + connectionDescription.getJndiName(), exception);
    }
    return connectionDescription;
}