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:de.micromata.genome.util.runtime.jndi.JndiDumper.java

public static void dumpJndi(StringBuilder sb, String indent) throws NamingException {
    InitialContext initialContext = new InitialContext();

    NamingEnumeration<Binding> bindings = initialContext.listBindings("");

    while (bindings.hasMore()) {
        Binding binding = bindings.next();
        dumpJndiBinding(initialContext, "", binding, sb, indent);
    }/*from  w  ww. j  a  va2s  .  c om*/
    bindings = initialContext.listBindings("java:comp");

    while (bindings.hasMore()) {
        Binding binding = bindings.next();
        dumpJndiBinding(initialContext, "java:comp", binding, sb, indent);
    }

}

From source file:be.fedict.eid.dss.sp.StartupServletContextListener.java

public static void bindComponent(String jndiName, Object component) throws NamingException {

    LOG.debug("bind component: " + jndiName);
    InitialContext initialContext = new InitialContext();
    String[] names = jndiName.split("/");
    Context context = initialContext;
    for (int idx = 0; idx < names.length - 1; idx++) {
        String name = names[idx];
        LOG.debug("name: " + name);
        NamingEnumeration<NameClassPair> listContent = context.list("");
        boolean subContextPresent = false;
        while (listContent.hasMore()) {
            NameClassPair nameClassPair = listContent.next();
            if (!name.equals(nameClassPair.getName())) {
                continue;
            }/*from  ww w.j a v  a  2  s .  c o  m*/
            subContextPresent = true;
        }
        if (!subContextPresent) {
            context = context.createSubcontext(name);
        } else {
            context = (Context) context.lookup(name);
        }
    }
    String name = names[names.length - 1];
    context.rebind(name, component);
}

From source file:com.tdclighthouse.prototype.utils.EmailUtils.java

public static Session getMailSession(final String sessionName) {
    Session result = null;//from www  .  ja va  2 s.  c o  m
    InitialContext initialContext = null;
    try {
        initialContext = new InitialContext();
        Context context = (Context) initialContext.lookup("java:comp/env");
        result = (Session) context.lookup(sessionName);
    } catch (NamingException e) {
        throw new HstComponentException(e);
    } finally {
        try {
            if (initialContext != null) {
                initialContext.close();
            }
        } catch (NamingException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    return result;
}

From source file:com.peadargrant.filecheck.web.support.ServerEnvironment.java

public String getPropertyAsString(String propertyName) throws Exception {
    Context env = (Context) new InitialContext().lookup("java:comp/env");

    String property = (String) env.lookup(propertyName);

    return property;
}

From source file:edu.harvard.i2b2.crc.util.HibernateUtil.java

/**
 * Function to fetch session via jboss hibernate mbean
 * Enables filter condition for delete_flag in query
 * tables/*from   w w w.j av  a  2 s  . c om*/
 * @return Session
 */
public static Session getSession() {
    Session session = null;

    InitialContext ctx;
    SessionFactory factory;

    try {
        ctx = new InitialContext();
        factory = (SessionFactory) ctx.lookup(DATASOURCE_JNDI_NAME);
        // session = factory.openSession();
        session = factory.getCurrentSession();
        session.enableFilter("deleteInstanceFlagFilter").setParameter("deleteFlagFilterParam", "N");
        session.enableFilter("deleteMasterFlagFilter").setParameter("deleteFlagFilterParam", "N");
        session.enableFilter("deleteResultInstanceFlagFilter").setParameter("deleteFlagFilterParam", "N");
    } catch (NamingException e) {
        log.error("DB Session jndi lookup[" + DATASOURCE_JNDI_NAME + "] failed", e);
        throw new ExceptionInInitializerError(e);
    }

    return session;
}

From source file:com.wso2telco.historylog.DbTracelog.java

/**
 * Initialize datasources.//from  www  .jav a  2  s.  co  m
 *
 * @throws LogHistoryException the log history exception
 */
public static void initializeDatasources() throws LogHistoryException {
    if (connectDatasource != null) {
        return;
    }

    try {
        Context ctx = new InitialContext();
        connectDatasource = (DataSource) ctx.lookup(CONNECT_DATA_SOURCE);
    } catch (NamingException e) {
        handleException("Error while looking up the data source: " + CONNECT_DATA_SOURCE, e);
    }
}

From source file:org.akhikhl.examples.gretty.hellogretty.MainConfig.java

@Bean
public DataSource dataSource() {
    /* final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);/*  w  w  w.ja  v  a  2 s  .  c  om*/
    return dsLookup.getDataSource("jdbc/devcore"); */

    DataSource dataSource = null;
    try {
        InitialContext init = new InitialContext();
        Context env = (Context) init.lookup("java:comp/env");
        dataSource = (DataSource) env.lookup("jdbc/devcore");
        Connection con = dataSource.getConnection();
    } catch (Exception e) {
        logger.error("Exception in dataSource", e);
        throw new RuntimeException(e);
    }
    return dataSource;
}

From source file:SeeAccount.java

public void doGet(HttpServletRequest inRequest, HttpServletResponse outResponse)
        throws ServletException, IOException {

    PrintWriter out = null;// w ww  .  j a  v a 2  s .  co  m
    Connection connection = null;
    Statement statement = null;

    ResultSet rs;

    try {
        outResponse.setContentType("text/html");
        out = outResponse.getWriter();

        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
        connection = ds.getConnection();

        statement = connection.createStatement();
        rs = statement.executeQuery("SELECT * FROM acc_acc");
        ResultSetMetaData md = rs.getMetaData();

        out.println("<HTML><HEAD><TITLE>        Thumbnail Identification Record</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("Account Information:<BR>");
        out.println("<table>");
        out.println("<tr><td>");
        for (int i = 1; i <= md.getColumnCount(); i++) {
            out.println("Column #" + i + "<BR>");
            out.println("getColumnName : " + md.getColumnName(i) + "<BR>");
            out.println("getColumnClassName : " + md.getColumnClassName(i) + "<BR>");
            out.println("getColumnDisplaySize : " + md.getColumnDisplaySize(i) + "<BR>");
            out.println("getColumnType : " + md.getColumnType(i) + "<BR>");
            out.println("getTableName : " + md.getTableName(i) + "<BR>");
            out.println("<HR>");
        }
        out.println("</BODY></HTML>");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.bank.config.code.JndiDataConfig.java

@Bean
public DataSource dataSource() {
    try {//from w ww . j a  v a2  s.  c  o  m
        Context ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
    } catch (NamingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.highbyte_le.weberknecht.db.DefaultWebDbConnectionProvider.java

public Connection getConnection() throws DBConnectionException {
    try {//from   w w w  . jav a2  s.  c o  m

        //JNDI-Context
        Context ctx = new InitialContext();
        Context envCtx = (Context) ctx.lookup("java:comp/env");

        //JNDI-Lookup for jdbc connection
        DataSource ds = (DataSource) envCtx.lookup("jdbc/mydb");
        Connection con = ds.getConnection();

        return con;

    } catch (NamingException e) {
        logger.error("NamingException: " + e.getMessage(), e);
        throw new DBConnectionException("database connection is not yet configured (naming exception)", e);
    } catch (SQLException e) {
        logger.error("SQLException: " + e.getMessage(), e);
        throw new DBConnectionException("cannot connect to database (sql exception)", e);
    }

}