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.atricore.idbus.idojos.dbidentitystore.DataSourceIdentityStore.java

/**
 * Lazy load the datasource instace used by this store.
 *
 *
 * @throws SSOIdentityException/*from  w ww  . j  a  va 2s  .  com*/
 */
protected DataSource getDataSource() throws SSOIdentityException {

    if (_datasource == null) {

        try {

            if (logger.isDebugEnabled())
                logger.debug("[getDatasource() : ]" + _dsJndiName);

            InitialContext ic = new InitialContext();
            _datasource = (DataSource) ic.lookup(_dsJndiName);

        } catch (NamingException ne) {
            logger.error("Error during DB connection lookup", ne);
            throw new SSOIdentityException("Error During Lookup\n" + ne.getMessage());
        }

    }

    return _datasource;
}

From source file:org.pepstock.jem.junit.test.springbatch.java.RestToBeExecuted.java

/**
 * //from   w w  w. j ava 2 s . com
 */
@ToBeExecuted
public void exec() {

    try {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.pepstock.jem.node.tasks.jndi.JemContextFactory");

        InitialContext context = new InitialContext(env);
        RestClient rest = (RestClient) context.lookup("JUNIT-REST-RESOURCE");
        System.err.println();
        System.err.println("*** REST XML");
        get(rest, MediaType.APPLICATION_XML);
        get(rest, MediaType.APPLICATION_JSON);
        get(rest, MediaType.APPLICATION_XML);
        get(rest, MediaType.APPLICATION_JSON);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        System.err.println();
        System.err.println("*** REST XML");
        get(restC, MediaType.APPLICATION_XML);
        get(restC, MediaType.APPLICATION_JSON);
        get(restC, MediaType.APPLICATION_XML);
        get(restC, MediaType.APPLICATION_JSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.jetbrains.webdemo.servlet.TwitterServlet.java

@Override
public void init() {
    try {/*w  w  w . jav a 2 s  . co  m*/
        InitialContext initialContext = new InitialContext();
        Context envContext = (Context) initialContext.lookup("java:comp/env");
        apiKey = (String) envContext.lookup("twitter_rw_key");
        apiSecret = (String) envContext.lookup("twitter_rw_secret");
    } catch (Exception e) {
        log.fatal("Can't initialize twitter servlet", e);
    }
}

From source file:org.jboss.test.classloader.scoping.override.web.comlog.Log4jServlet.java

private void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    log.info("processRequest, path=" + request.getPathInfo());
    try {//from   w w  w . j  a  v  a 2s .  co  m
        InitialContext ctx = new InitialContext();
        Context enc = (Context) ctx.lookup("java:comp/env");
        log.info("Was able to lookup ENC, " + enc);
    } catch (NamingException e) {
        throw new ServletException("Failed to lookup ENC", e);
    }

    // Validate that the cl-test.log
    String logDir = System.getProperty("jboss.server.log.dir");
    File logFile = new File(logDir, "cl-test.log");
    if (logFile.exists() == false)
        throw new ServletException(logFile + " does not exist");

    long length = logFile.length();
    log.info("Current length = " + length);
    for (int n = 0; n < 100; n++)
        log.info("Msg #" + n);
    long lastModified = logFile.lastModified();
    long length2 = logFile.length();
    if (!(length2 > length))
        throw new ServletException(logFile + " length is not increasing");
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<html><head><title>Commons logging test servlet</title></head>");
    pw.println("<body><h1>Commons logging test servlet</h1>");
    pw.println("Log length: " + length2);
    pw.println(", LastModified: " + new Date(lastModified));
    pw.println("</body></html>");
    pw.flush();
}

From source file:org.openbmp.db_rest.helpers.AuthenticationService.java

/**
 * Initialize the class Sets the data source
 *
 * @throws/*  w  ww . j av a2 s .  c  o  m*/
 */

public AuthenticationService() {
    InitialContext initctx = null;
    try {

        initctx = new InitialContext();
        mysql_ds = (DataSource) initctx.lookup("java:/comp/env/jdbc/MySQLDB");

    } catch (NamingException e) {
        System.err.println("ERROR: Cannot find resource configuration, check context.xml config");
        e.printStackTrace();
    }
}

From source file:nl.b3p.viewer.config.stripersist.DynamicStripersistInitializer.java

@Override
public List<String> getPersistenceUnitsToCreate() throws Exception {

    log.info("Trying to determine persistence unit from JNDI DataSource database");

    String persistenceUnit = null;
    DataSource ds = null;//from   www.  j  av a2 s. c om
    try {
        InitialContext ctx = new InitialContext();

        ds = (DataSource) ctx.lookup(DATA_SOURCE_NAME);
    } catch (Exception e) {
        log.info("No JNDI DataSource found under " + DATA_SOURCE_NAME + "");
    }

    if (ds != null) {
        try {
            Connection conn = ds.getConnection();
            try {
                databaseProductName = conn.getMetaData().getDatabaseProductName();
            } finally {
                conn.close();
            }

            if (databaseProductName == null) {
                throw new Exception("No database product name found!");
            } else {
                persistenceUnit = PU_PREFIX + databaseProductName.toLowerCase();
                if (!persistenceUnits.containsKey(persistenceUnit)) {
                    throw new Exception(
                            String.format("No persistence unit \"%s\" found for database product name \"%s\"",
                                    persistenceUnit, databaseProductName));
                }
            }
        } catch (Exception e) {
            log.error("Error looking up database product name", e);
            throw e;
        }
    }

    log.info("Found persistence unit: " + persistenceUnit);
    // If null, use HSQLDB with data directory in java.io.tmpdir or from 
    // environment variable? 

    return Arrays.asList(persistenceUnit);
}

From source file:mx.edu.ittepic.AEEcommerce.servlets.Login.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from www. ja  v  a2 s  .c  o  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json;charset=UTF-8");
    response.setHeader("Cache-Control", "no-store");
    PrintWriter out = response.getWriter();

    CartBean2Remoto ejb = (CartBean2Remoto) request.getSession().getAttribute("ejbsesion");
    String user = request.getParameter("usuario");
    String password = request.getParameter("password");
    System.out.println("password anterior:" + password);
    password = DigestUtils.md5Hex(password);
    System.out.println("password nueva:" + password);
    if (ejb == null) {
        try {
            InitialContext ic = new InitialContext();
            ejb = (CartBean2Remoto) ic.lookup("java:comp/env/ejbs/CartBean2");
            GsonBuilder builder = new GsonBuilder();
            Gson gson = builder.create();
            //password= DigestUtils.md5Hex(password);
            Message m = gson.fromJson(ejb.login(user, password), Message.class);
            if (m.getCode() == 200) {
                request.getSession().setAttribute("ejbsesion", ejb);

            }
            out.print(new GsonBuilder().create().toJson(m));
        } catch (NamingException e) {
            out.print(e.toString());
        }
    }
}

From source file:org.hibersap.execution.jca.JCAContext.java

private ConnectionFactory getConnectionFactory(final String jndiName) {
    try {//  ww  w  .  j a v  a 2s.c o m
        final InitialContext initialContext = new InitialContext();
        final Object object = initialContext.lookup(jndiName);

        if (object == null) {
            throw new HibersapException("Name not bound: " + jndiName);
        }

        if (!(object instanceof ConnectionFactory)) {
            throw new HibersapException("Object bound under " + jndiName + " is no ConnectionFactory");
        }

        return (ConnectionFactory) object;
    } catch (final NamingException e) {
        throw new HibersapException("JNDI lookup:", e);
    }
}

From source file:org.cauldron.execution.ContextUtils.java

public Task find(String name) {
    try {// w ww.j a va  2s .c om
        InitialContext ic = new InitialContext();
        return (Task) ic.lookup(prefix + name);
    } catch (NamingException e) {
        ContextImpl.log.error("JNDI exception while finding task named " + name + " : " + e.getMessage());
        return null;
    }
}

From source file:com.francetelecom.clara.cloud.commons.toggles.PaasJndiStateRepository.java

protected List<String> getFeatureUsers(Feature feature) {
    List<String> users = new ArrayList<String>();
    try {// ww w.ja va 2 s . c om
        InitialContext jndiContext = getInitialContext();
        String value = (String) jndiContext.lookup(feature.name() + ".users");
        if (value != null) {
            value = value.trim();
            if (!value.equals("")) {
                users = Arrays.asList(value.split("( )*,( )*"));
            }
        }
    } catch (NamingException e) {
        logger.debug("unable to fetch user lists for feature " + feature.name());
    }
    return users;
}