Example usage for javax.naming Context INITIAL_CONTEXT_FACTORY

List of usage examples for javax.naming Context INITIAL_CONTEXT_FACTORY

Introduction

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

Prototype

String INITIAL_CONTEXT_FACTORY

To view the source code for javax.naming Context INITIAL_CONTEXT_FACTORY.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the initial context factory to use.

Usage

From source file:org.gbif.portal.registration.LDAPUtils.java

/**
 * Get LDAP context.//from w  w w .j  a va2s .c  o  m
 * @param url
 * @return
 * @throws NamingException
 */
public DirContext getContext(String url) throws NamingException {
    Hashtable env = new Hashtable();
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.SECURITY_AUTHENTICATION, authenticationType);
    env.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
    env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
    DirContext ctx = new InitialDirContext(env);
    return ctx;
}

From source file:org.apache.servicemix.jms.JmsSpringJcaTest.java

protected AbstractXmlApplicationContext createBeanFactory() {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, SpringInitialContextFactory.class.getName());
    System.setProperty(Context.PROVIDER_URL, "org/apache/servicemix/jms/jndi.xml");
    return new ClassPathXmlApplicationContext("org/apache/servicemix/jms/spring-jca.xml");
}

From source file:org.easy.ldap.LdapContextFactory.java

/**
 * @return//w w  w  . java  2s.c om
 */
private Hashtable<String, String> getEnviroment() {
    Hashtable<String, String> properties = new Hashtable<String, String>();

    properties.put(Context.PROVIDER_URL, createProviderUrl(environment.getProperty(PropertyNames.DOMAIN_DN)));

    properties.put(Context.INITIAL_CONTEXT_FACTORY,
            environment.getProperty(PropertyNames.INITIAL_CONTEXT_FACTORY_CLASS));
    properties.put("com.sun.jndi.ldap.connect.pool",
            environment.getProperty(PropertyNames.USE_LDAP_CONNECT_POOL));
    properties.put(Context.SECURITY_AUTHENTICATION,
            environment.getProperty(PropertyNames.ADMIN_AUTHENTICATION_METHOD));
    properties.put(Context.SECURITY_PRINCIPAL, environment.getProperty(PropertyNames.ADMIN_PRINCIPAL));
    properties.put(Context.SECURITY_CREDENTIALS, environment.getProperty(PropertyNames.ADMIN_CREDENTIALS));

    return properties;
}

From source file:org.grouter.core.readers.JmsReaderJob.java

private static InitialContext getInitialContext(String providerUrl, String initialContextFactory,
        String urlPkgPrefixes) throws NamingException {
    Hashtable<String, String> hashtable = new Hashtable<String, String>();
    hashtable.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    hashtable.put(Context.PROVIDER_URL, providerUrl);
    hashtable.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
    return new InitialContext(hashtable);
}

From source file:com.sterlingcommerce.xpedx.webchannel.services.XPEDXGetAllReportsAction.java

public void getConnection() throws SQLException {
    String XCOM_MST_CUST = getCustomerNo(getWCContext().getBuyerOrgCode());
    String DBUrl = YFSSystem.getProperty("datasource_url");
    String DBName = YFSSystem.getProperty("datasource_name");

    //String DBUrl= "t3://localhost:7002";
    //String DBName= "SeptJNDI";
    Connection connection = null;
    Statement stmt = null;// www  . j a va2  s  .  c  om
    ResultSet rs = null;
    XPEDXReportBean rpBean = null;
    try {
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        ht.put("java.naming.provider.url", DBUrl);
        Context env = new InitialContext(ht);

        //InitialContext context = new InitialContext(ht);
        DataSource dataSource = (DataSource) env.lookup(DBName);
        connection = dataSource.getConnection();
        if (log.isDebugEnabled()) {
            log.debug("Connection successful..");
        }
        //String schemaName=YFSSystem.getProperty("schemaname");
        //String Query="select distinct RPT_CUID, RPT_NAME,RPT_ID,RPT_KIND, RPT_DESC from " + schemaName + ".xpedx_custom_rpt_dtl where XCOM_MST_CUST=" + "'"+ XCOM_MST_CUST +"'"+"AND CUST_ROLE in (";
        String Query = "select distinct RPT_CUID, RPT_NAME,RPT_ID,RPT_KIND, RPT_DESC from DH.xpedx_custom_rpt_dtl where XCOM_MST_CUST="
                + "'" + XCOM_MST_CUST + "'" + "AND CUST_ROLE in (";
        Query = getUserRole(Query);
        stmt = connection.createStatement();
        boolean test = stmt.execute(Query);
        dataExchangeReportList = new ArrayList<Report>();
        if (test == true) {
            rs = stmt.getResultSet();
            while (rs.next()) {
                Report report = new Report();
                report.setCuid(rs.getString("RPT_CUID"));
                report.setName(rs.getString("RPT_NAME"));
                report.setKind(rs.getString("RPT_KIND"));
                report.setId(rs.getInt("RPT_ID"));
                report.setDescription(rs.getString("RPT_DESC"));

                dataExchangeReportList.add(report);
            }
        }
    } catch (Exception e) {
        LOG.debug("Not able to connect to DEV Datasource:->" + e.getMessage());
    } finally {
        stmt.close();
        connection.close();
    }
}

From source file:org.wso2.carbon.apimgt.impl.dao.test.CertificateMgtDaoTest.java

private static void initializeDatabase(String configFilePath)
        throws IOException, XMLStreamException, NamingException {

    InputStream in;/*from  ww  w .  ja  va 2  s.c  o m*/
    in = FileUtils.openInputStream(new File(configFilePath));
    StAXOMBuilder builder = new StAXOMBuilder(in);
    String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
            .getText();
    OMElement databaseElement = builder.getDocumentElement().getFirstChildWithName(new QName("Database"));
    String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
    String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
    String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
    String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(databaseDriver);
    basicDataSource.setUrl(databaseURL);
    basicDataSource.setUsername(databaseUser);
    basicDataSource.setPassword(databasePass);

    // Create initial context
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    try {
        InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
    } catch (NamingException e) {
        InitialContext ic = new InitialContext();
        ic.createSubcontext("java:");
        ic.createSubcontext("java:/comp");
        ic.createSubcontext("java:/comp/env");
        ic.createSubcontext("java:/comp/env/jdbc");

        ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
    }
}

From source file:org.apache.synapse.util.DataSourceRegistrar.java

/**
 * Register data sources in the JNDI context
 * Given properties should contains all the properties need for construct JNDI naming references
 *
 * @param dsProperties The source properties
 *///from  www.  ja  v a 2s.c o m
public static void registerDataSources(Properties dsProperties) {

    if (dsProperties == null) {
        if (log.isDebugEnabled()) {
            log.debug("DataSource properties cannot be found..");
        }
        return;
    }

    String dataSources = getProperty(dsProperties, SynapseConstants.SYNAPSE_DATASOURCES, null);

    if (dataSources == null || "".equals(dataSources)) {
        if (log.isDebugEnabled()) {
            log.debug("No DataSources defined for initialization..");
        }
        return;
    }

    String[] dataSourcesNames = dataSources.split(",");
    if (dataSourcesNames == null || dataSourcesNames.length == 0) {
        if (log.isDebugEnabled()) {
            log.debug("No DataSource definitions found for initialization..");
        }
        return;
    }

    StringBuffer buffer = new StringBuffer();
    buffer.append(SynapseConstants.SYNAPSE_DATASOURCES);
    buffer.append(DOT_STRING);
    // The prefix for root level properties
    String rootPrefix = buffer.toString();

    // setting naming provider
    Hashtable props = new Hashtable();
    Properties jndiEvn = new Properties(); //This is needed for PerUserPoolDatasource

    String namingFactory = getProperty(dsProperties, rootPrefix + PROP_ICFACTORY,
            "com.sun.jndi.rmi.registry.RegistryContextFactory");

    props.put(Context.INITIAL_CONTEXT_FACTORY, namingFactory);
    jndiEvn.put(Context.INITIAL_CONTEXT_FACTORY, namingFactory);

    String providerHost = "localhost";
    try {
        InetAddress addr = InetAddress.getLocalHost();
        if (addr != null) {
            String hostname = addr.getHostName();
            if (hostname == null) {
                String ipAddr = addr.getHostAddress();
                if (ipAddr != null) {
                    providerHost = ipAddr;
                }
            } else {
                providerHost = hostname;
            }
        }
    } catch (UnknownHostException e) {
        log.warn("Unable to determine hostname or IP address.. Using localhost", e);
    }

    // default port for RMI registry
    int port = 2199;
    String providerPort = getProperty(dsProperties, rootPrefix + PROP_PROVIDER_PORT, String.valueOf(port));
    try {
        port = Integer.parseInt(providerPort);
    } catch (NumberFormatException ignored) {
    }

    // Create a RMI local registry
    RMIRegistryController.getInstance().createLocalRegistry(port);

    String providerUrl = getProperty(dsProperties, rootPrefix + PROP_PROVIDER_URL,
            "rmi://" + providerHost + ":" + providerPort);

    props.put(Context.PROVIDER_URL, providerUrl);
    jndiEvn.put(Context.PROVIDER_URL, providerUrl);

    log.info("DataSources will be registered in the JNDI context with provider PROP_URL : " + providerUrl);

    try {
        InitialContext initialContext = new InitialContext(props);
        //Registering data sources with the initial context
        for (int i = 0; i < dataSourcesNames.length; i++) {
            registerDataSource(dataSourcesNames[i], dsProperties, initialContext, jndiEvn);
        }

    } catch (NamingException e) {
        String msg = "Error constructing an InitialContext to register DataSources";
        handleException(msg, e);
    }
}

From source file:org.wso2.carbon.humantask.core.db.Database.java

@SuppressWarnings("unchecked")
private <T> T lookupInJndi(String objName) throws Exception {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    try {/*from   w w w.j a  va  2s.c o m*/
        InitialContext ctx = null;
        try {
            if (serverConfiguration.getDataSourceJNDIRepoInitialContextFactory() != null
                    && serverConfiguration.getDataSourceJNDIRepoProviderURL() != null) {
                Properties jndiProps = new Properties();

                jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                        serverConfiguration.getDataSourceJNDIRepoInitialContextFactory());
                jndiProps.setProperty(Context.PROVIDER_URL,
                        serverConfiguration.getDataSourceJNDIRepoProviderURL());

                ctx = new InitialContext(jndiProps);
            } else {
                ctx = new InitialContext();
            }
            return (T) ctx.lookup(objName);
        } finally {
            if (ctx != null) {
                try {
                    ctx.close();
                } catch (Exception ex1) {
                    log.error("Error closing JNDI connection.", ex1);
                }
            }
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}

From source file:org.andromda.timetracker.test.EJB3Container.java

private static Hashtable<String, String> getInitialContextProperties(String principal, String credential) {
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
    props.put(Context.SECURITY_PRINCIPAL, principal);
    props.put(Context.SECURITY_CREDENTIALS, credential);
    return props;
}

From source file:org.apache.synapse.config.xml.AbstractDBMediatorFactory.java

/**
 * Lookup the DataSource on JNDI using the specified properties
 * @param pool the toplevel 'pool' element that holds DataSource information
 * @param mediator the mediator to store properties for serialization
 * @return a DataSource looked up using specified properties
 *//*from   w ww  .  j a  v  a  2  s.  c o m*/
private DataSource lookupDataSource(OMElement pool, AbstractDBMediator mediator) {

    Hashtable props = new Hashtable();
    // load the minimum required properties
    props.put(Context.INITIAL_CONTEXT_FACTORY, (getValue(pool, ICCLASS_Q)));
    props.put(Context.SECURITY_PRINCIPAL, getValue(pool, USER_Q));
    props.put(Context.SECURITY_CREDENTIALS, getValue(pool, PASS_Q));
    props.put(Context.PROVIDER_URL, getValue(pool, URL_Q));
    String dsName = getValue(pool, DSNAME_Q);

    //save loaded properties for later
    mediator.addDataSourceProperty(ICCLASS_Q, getValue(pool, ICCLASS_Q));
    mediator.addDataSourceProperty(DSNAME_Q, getValue(pool, DSNAME_Q));
    mediator.addDataSourceProperty(URL_Q, getValue(pool, URL_Q));
    mediator.addDataSourceProperty(USER_Q, getValue(pool, USER_Q));
    mediator.addDataSourceProperty(PASS_Q, getValue(pool, PASS_Q));

    try {
        Context ctx = new InitialContext(props);
        if (ctx != null) {
            Object ds = ctx.lookup(dsName);
            if (ds != null && ds instanceof DataSource) {
                return (DataSource) ds;
            } else {
                handleException("DataSource : " + dsName + " not found when looking up"
                        + " using JNDI properties : " + props);
            }
        } else {
            handleException("Error getting InitialContext using JNDI properties : " + props);
        }
    } catch (NamingException e) {
        handleException("Error looking up DataSource : " + dsName + " using JNDI properties : " + props, e);
    }
    return null;
}