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.caricah.iotracah.datastore.IotDataSource.java

public void setupDatasource(String driver, String dbUrl, String username, String password)
        throws NamingException {

    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    System.setProperty(Context.PROVIDER_URL, "file:////tmp");

    Context ctx = new InitialContext();

    // Construct DriverAdapterCPDS reference
    Reference cpdsRef = new Reference("org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS",
            "org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS", null);
    cpdsRef.add(new StringRefAddr("driver", driver));
    cpdsRef.add(new StringRefAddr("url", dbUrl));
    cpdsRef.add(new StringRefAddr("user", username));
    cpdsRef.add(new StringRefAddr("password", password));
    ctx.rebind("jdbc_cpds", cpdsRef);

    Reference ref = new Reference("org.apache.commons.dbcp2.datasources.SharedPoolDataSource",
            "org.apache.commons.dbcp2.datasources.SharedPoolDataSourceFactory", null);
    ref.add(new StringRefAddr("dataSourceName", "jdbc_cpds"));
    ref.add(new StringRefAddr("defaultMaxTotal", "100"));
    ref.add(new StringRefAddr("defaultMaxIdle", "30"));
    ref.add(new StringRefAddr("defaultMaxWaitMillis", "10000"));

    ctx.rebind("jdbc_commonpool", ref);

}

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

public DefaultWebDbConnectionProvider2(String jdbcContextName) throws NamingException {
    //JNDI-Context
    Context ctx = new InitialContext();
    Context envCtx = (Context) ctx.lookup("java:comp/env");

    //JNDI-Lookup for jdbc connection
    this.datasource = (DataSource) envCtx.lookup(jdbcContextName);

    this.type = DbType.MYSQL; //FIXME implement some property handling for db type
}

From source file:com.liferay.util.mail.MailEngine.java

public static Session getSession() throws NamingException {
    return (Session) JNDIUtil.lookup(new InitialContext(), MAIL_SESSION);
}

From source file:com.wso2telco.mnc.resolver.mncrange.McnRangeDbUtil.java

/**
 * Initialize datasources./*from   w w  w  .  jav a2  s. com*/
 *
 * @throws MobileNtException the mobile nt exception
 */
public static void initializeDatasources() throws MobileNtException {
    if (axiataDatasource != null) {
        return;
    }

    try {
        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.appeligo.config.EnvironmentLookup.java

/**
 * /*from w w  w  .j  a  va 2 s  .  c o m*/
 * @return
 */
public String getEnvironmentName() {
    String envName = null;
    try {
        envName = (String) new InitialContext().lookup("java:comp/env/deploymentEnvironment");
    } catch (NamingException e) {
        log.warn("deploymentEnvironment JNDI lookup failed: " + e.getMessage(), e);
    }
    return envName;
}

From source file:com.versatus.jwebshield.DBHelper.java

public DBHelper(Configuration config) throws Exception {

    try {/*  w w  w.  j a  v  a2 s .co m*/
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");

        SubnodeConfiguration fields = ((XMLConfiguration) config).configurationAt("securityLock");

        if (envCtx != null) {
            ds = (DataSource) envCtx.lookup(fields.getString("dbJndiName"));
        }
    } catch (javax.naming.NoInitialContextException ne) {

    } catch (Exception e) {
        logger.error("constructor", e);
    }

}

From source file:com.mdmserver.managers.ControllerFacade.java

public void uninstallApp(int accountId, String appPackageName) {
    Account account = databaseManager.getAccountByAccountId(accountId);
    Context ctx;/*from  w  ww  . ja v  a 2  s.c o m*/
    try {
        ctx = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/mdmConnectionFactory");
        Queue queue = (Queue) ctx.lookup("jms/mdmQueue");
        MessageProducer messageProducer;
        System.out.println("Naming success");
        try {

            javax.jms.Connection connection = connectionFactory.createConnection();
            javax.jms.Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            messageProducer = session.createProducer(queue);
            TextMessage message = session.createTextMessage();
            message.setStringProperty(Account.FIELD_NAME_CLOUD_ID, account.getCloudId());
            ControlClient cClient = new ControlClient();
            cClient.setCommandType(ControlClient.UNINSTALL_APP_CONTROL);
            cClient.setJsonCommandDetails(appPackageName);
            Gson gson = new Gson();
            message.setStringProperty(ControlClient.CONTROL_CLIENT_KEY, gson.toJson(cClient));
            System.out.println("It come from Servlet:" + message.getText());
            messageProducer.send(message);
            System.out.println("JMS success");
        } catch (JMSException ex) {
            //                  Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("JMS exception");
        }

    } catch (NamingException ex) {
        //                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Naming exception");
    }
}

From source file:sample.tomcat.jndi.web.SampleController.java

@RequestMapping("/direct")
@ResponseBody/*from w w  w  . j  a  v  a  2 s.  c  o  m*/
public String direct() throws NamingException {
    return "DataSource retrieved directly from JNDI: "
            + new InitialContext().lookup("java:comp/env/jdbc/myDataSource");
}

From source file:com.alliander.osgp.signing.server.application.config.SigningServerInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {// w ww  . ja v a  2s  . c om
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext.lookup("java:comp/env/osp/signingServer/log-config");
        LogbackConfigurer.initLogging(logLocation);

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));

    } catch (final NamingException e) {
        throw new ServletException("Naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}

From source file:org.osgp.adapter.protocol.dlms.application.config.ProtocolAdapterDlmsInitializer.java

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    try {/*ww  w. j ava 2 s.c  om*/
        // Force the timezone of application to UTC (required for
        // Hibernate/JDBC)
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        final Context initialContext = new InitialContext();

        final String logLocation = (String) initialContext
                .lookup("java:comp/env/osp/osgpAdapterProtocolDlms/log-config");
        LogbackConfigurer.initLogging(logLocation);

        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(ApplicationContext.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));

    } catch (final NamingException e) {
        throw new ServletException("naming exception", e);
    } catch (final FileNotFoundException e) {
        throw new ServletException("Logging file not found", e);
    } catch (final JoranException e) {
        throw new ServletException("Logback exception", e);
    }
}