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(Hashtable<?, ?> environment) throws NamingException 

Source Link

Document

Constructs an initial context using the supplied environment.

Usage

From source file:com.stratelia.silverpeas.versioning.jcr.impl.AbstractJcrTestCase.java

@Resource
public void setDataSource(DataSource datasource) {
    this.datasource = datasource;
    try {//w  w w  . java2  s.c  o  m
        prepareJndi();
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        InitialContext ic = new InitialContext(env);
        Properties properties = new Properties();
        properties.load(PathTestUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
        // Construct BasicDataSource reference
        Reference ref = new Reference("javax.sql.DataSource", "org.apache.commons.dbcp.BasicDataSourceFactory",
                null);
        ref.add(new StringRefAddr("driverClassName",
                properties.getProperty("driverClassName", "org.postgresql.Driver")));
        ref.add(new StringRefAddr("url",
                properties.getProperty("url", "jdbc:postgresql://localhost:5432/postgres")));
        ref.add(new StringRefAddr("username", properties.getProperty("username", "postgres")));
        ref.add(new StringRefAddr("password", properties.getProperty("password", "postgres")));
        ref.add(new StringRefAddr("maxActive", "4"));
        ref.add(new StringRefAddr("maxWait", "5000"));
        ref.add(new StringRefAddr("removeAbandoned", "true"));
        ref.add(new StringRefAddr("removeAbandonedTimeout", "5000"));
        rebind(ic, JNDINames.DATABASE_DATASOURCE, ref);
        rebind(ic, JNDINames.ADMIN_DATASOURCE, ref);
    } catch (NamingException nex) {
        nex.printStackTrace();
    } catch (IOException nex) {
        nex.printStackTrace();
    }
}

From source file:com.reversemind.hypergate.server.PayloadProcessor.java

private void initJndiContext() {
    jndiPropertiesMap = this.getJndiProperties(jndiEnvironment);
    try {/*from  w  w w .j  ava2s  .  c  o m*/
        jndiContext = new InitialContext(jndiPropertiesMap);
        // buildingDAO = InitialContext.doLookup("java:global/ttk-house/ttk-house-ejb-2.0-SNAPSHOT/BuildingDAO!ru.ttk.baloo.house.data.service.building.IBuildingDAO");
    } catch (NamingException e) {
        LOG.error("NamingException ", e);
    }
}

From source file:it.cnr.isti.labse.glimpse.MainMonitoring.java

public static boolean init() {
    boolean successfullInit = false;

    try {//from  www .j a  v  a  2s . c om
        //the connection are initialized
        Properties environmentParameters = Manager.Read(ENVIRONMENTPARAMETERSFILE);
        initConn = new InitialContext(environmentParameters);

        DebugMessages.print(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "Setting up TopicConnectionFactory");
        connFact = (TopicConnectionFactory) initConn.lookup("TopicCF");
        DebugMessages.ok();
        DebugMessages.line();
        successfullInit = true;
    } catch (NamingException e) {
        e.printStackTrace();
        successfullInit = false;
    } catch (Exception e) {
        e.printStackTrace();
        successfullInit = false;
    }
    return successfullInit;
}

From source file:net.sf.ehcache.distribution.JNDIRMICacheManagerPeerListener.java

/**
 * Gets the initial context/*from w ww . j  a va  2s . c o  m*/
 *
 * @return an initial context
 * @throws NamingException if JNDI goes wrong
 */
private Context getInitialContext() throws NamingException {
    String initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);

    if (initialContextFactory != null && initialContextFactory.startsWith("net.sf.ehcache")) {
        // Put Context.PROVIDER_URL so unit tests can work
        Hashtable hashTable = new Hashtable();
        hashTable.put(Context.PROVIDER_URL, "//localhost:" + port);
        return new InitialContext(hashTable);
    }
    return new InitialContext();
}

From source file:it.doqui.index.ecmengine.client.backoffice.EcmEngineBackofficeDirectDelegateImpl.java

@Override
protected EcmEngineBackofficeBusinessInterface createBackofficeService() throws Throwable {
    Properties properties = new Properties();
    this.log.debug("[" + getClass().getSimpleName() + "::createBackofficeService] BEGIN ");

    rb = ResourceBundle.getBundle(ECMENGINE_BKO_PROPERTIES_FILE);
    /*// ww  w . j  a  v  a2s .  com
     * Caricamento della porta delegata del backoffice.
     */
    try {
        this.log.debug(
                "[" + getClass().getSimpleName() + "::createBackofficeService] P-Delegata di backoffice.");

        this.log.debug("[" + getClass().getSimpleName() + "::createBackofficeService] context factory vale : "
                + rb.getString(ECMENGINE_BKO_CONTEXT_FACTORY));
        properties.put(Context.INITIAL_CONTEXT_FACTORY, rb.getString(ECMENGINE_BKO_CONTEXT_FACTORY));
        this.log.debug("[" + getClass().getSimpleName() + "::createBackofficeService] url to connect vale : "
                + rb.getString(ECMENGINE_BKO_URL_TO_CONNECT));
        properties.put(Context.PROVIDER_URL, rb.getString(ECMENGINE_BKO_URL_TO_CONNECT));

        /* Controllo che la property cluster partition sia valorizzata per capire se 
         * sto lavorando in una configurazione in cluster oppure no */
        String clusterPartition = rb.getString(ECMENGINE_BKO_CLUSTER_PARTITION);
        this.log.debug("[" + getClass().getSimpleName() + "::createBackofficeService] clusterPartition vale : "
                + clusterPartition);
        if (clusterPartition != null && clusterPartition.length() > 0) {
            properties.put("jnp.partitionName", clusterPartition);
            this.log.debug(
                    "[" + getClass().getSimpleName() + "::createBackofficeService] disable discovery vale : "
                            + rb.getString(ECMENGINE_BKO_DISABLE_DISCOVERY));
            properties.put("jnp.disableDiscovery", rb.getString(ECMENGINE_BKO_DISABLE_DISCOVERY));
        }

        // Get an initial context
        InitialContext jndiContext = new InitialContext(properties);
        log.debug("[" + getClass().getSimpleName() + "::createBackofficeService] context istanziato");

        // Get a reference to the Bean
        Object ref = jndiContext.lookup(ECMENGINE_BKO_JNDI_NAME);

        // Get a reference from this to the Bean's Home interface
        EcmEngineBackofficeHome home = (EcmEngineBackofficeHome) PortableRemoteObject.narrow(ref,
                EcmEngineBackofficeHome.class);

        // Create an Adder object from the Home interface
        return home.create();

    } catch (Throwable e) {
        this.log.error("[" + getClass().getSimpleName() + "::createBackofficeService] "
                + "Impossibile istanziare la P-Delegata di backoffice: " + e.getMessage());
        throw e;
    } finally {
        this.log.debug("[" + getClass().getSimpleName() + "::createBackofficeService] END ");
    }
}

From source file:com.mirth.connect.connectors.jms.JmsDispatcherTests.java

private static ConnectionFactory lookupConnectionFactoryWithJndi(JmsConnectorProperties connectorProperties)
        throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.PROVIDER_URL, connectorProperties.getJndiProviderUrl());
    env.put(Context.INITIAL_CONTEXT_FACTORY, connectorProperties.getJndiInitialContextFactory());
    env.put(Context.SECURITY_PRINCIPAL, connectorProperties.getUsername());
    env.put(Context.SECURITY_CREDENTIALS, connectorProperties.getPassword());

    initialContext = new InitialContext(env);
    String connectionFactoryName = connectorProperties.getJndiConnectionFactoryName();
    return (ConnectionFactory) initialContext.lookup(connectionFactoryName);
}

From source file:com.clican.pluto.common.util.JndiUtils.java

/**
 * Unbind the rsource manager instance from the JNDI directory.
 * <p>/* w  w  w. ja va 2s .  c o  m*/
 * After this method is called, it is no longer possible to obtain the
 * resource manager instance from the JNDI directory.
 * <p>
 * Note that this method will not remove the JNDI contexts corresponding to
 * the individual path segments of the full resource manager JNDI path.
 * 
 * @param jndiName
 *            The full JNDI path at which the resource manager instance was
 *            previously bound.
 * @return <b>true</b> if the resource manager was successfully unbound
 *         from JNDI; otherwise <b>false</b>.
 * 
 * @see #bind(String, Object)
 */
public static boolean unbind(String jndiName) {
    if (log.isDebugEnabled()) {
        log.debug("Unbinding object at path [" + jndiName + "] from the JNDI repository.");
    }
    Context ctx = null;
    try {
        Hashtable<String, String> ht = new Hashtable<String, String>();
        // If a special JNDI initial context factory was specified in the
        // constructor, then use it.
        if (jndiInitialContextFactory != null) {
            ht.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitialContextFactory);
        }
        ctx = new InitialContext(ht);
        Object obj = lookupObject(jndiName);
        if (obj instanceof Serializable || jndiInitialContextFactory != null) {
            ctx.unbind(jndiName);
        } else {
            NonSerializableFactory.unbind(jndiName);
        }
    } catch (NamingException ex) {
        log.error("An error occured while unbinding [" + jndiName + "] from JNDI:", ex);
        return false;
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException ne) {
                log.error("Close context error:", ne);
            }
        }
    }
    return true;
}

From source file:OCCIConnectionServlet.java

public static ConnectionPoolDataSource getConnectionPoolDataSource(String baseName) {
    Context context = null;//from   w w w.j  a v a 2  s. c om
    ConnectionPoolDataSource cpds = null;
    try {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        properties.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC");
        context = new InitialContext(properties);
        cpds = (ConnectionPoolDataSource) context.lookup(baseName);
    } catch (NamingException e) {
        System.err.println(e.getMessage() + " creating JNDI context for " + baseName);
    }
    return cpds;
}

From source file:eu.learnpad.simulator.mon.MainMonitoring.java

public static boolean init() {
    boolean successfullInit = false;

    try {/*from  www. ja va2  s . c  om*/
        //the connection are initialized
        environmentParameters = Manager.Read(ENVIRONMENTPARAMETERSFILE);
        initConn = new InitialContext(environmentParameters);

        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "Connection Parameters");
        DebugMessages.line();
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "java.naming.factory.initial "
                        + environmentParameters.getProperty("java.naming.factory.initial"));
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "java.naming.provider.url " + environmentParameters.getProperty("java.naming.provider.url"));
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "java.naming.security.principal "
                        + environmentParameters.getProperty("java.naming.security.principal"));
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "java.naming.security.credentials "
                        + environmentParameters.getProperty("java.naming.security.credentials"));
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "connectionFactoryNames " + environmentParameters.getProperty("connectionFactoryNames"));
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "topic.serviceTopic " + environmentParameters.getProperty("topic.serviceTopic"));
        DebugMessages.println(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "topic.probeTopic " + environmentParameters.getProperty("topic.probeTopic"));
        DebugMessages.line();
        DebugMessages.print(TimeStamp.getCurrentTime(), MainMonitoring.class.getSimpleName(),
                "Setting up TopicConnectionFactory");
        connFact = (TopicConnectionFactory) initConn.lookup("TopicCF");
        DebugMessages.ok();
        DebugMessages.line();
        successfullInit = true;
    } catch (NamingException e) {
        e.printStackTrace();
        successfullInit = false;
    } catch (Exception e) {
        e.printStackTrace();
        successfullInit = false;
    }
    return successfullInit;
}

From source file:com.legstar.mq.client.AbstractCicsMQ.java

/**
 * Given the endpoint parameters, setup a JNDI context to lookup JMS
 * resources.//w w w .  j a  v  a  2s .co m
 * 
 * @param cicsMQEndpoint the endpoint paramers
 * @return the JNDI context
 * @throws CicsMQConnectionException if JNDI context cannot be created
 */
protected Context createJndiContext(final CicsMQEndpoint cicsMQEndpoint) throws CicsMQConnectionException {
    try {
        Properties env = new Properties();
        if (cicsMQEndpoint.getInitialContextFactory() != null
                && cicsMQEndpoint.getInitialContextFactory().length() > 0) {
            env.put(Context.INITIAL_CONTEXT_FACTORY, cicsMQEndpoint.getInitialContextFactory());
        }
        if (cicsMQEndpoint.getJndiProviderURL() != null && cicsMQEndpoint.getJndiProviderURL().length() > 0) {
            env.put(Context.PROVIDER_URL, cicsMQEndpoint.getJndiProviderURL());
        }
        if (cicsMQEndpoint.getJndiUrlPkgPrefixes() != null
                && cicsMQEndpoint.getJndiUrlPkgPrefixes().length() > 0) {
            env.put(Context.URL_PKG_PREFIXES, cicsMQEndpoint.getJndiUrlPkgPrefixes());
        }
        if (cicsMQEndpoint.getJndiProperties() != null) {
            env.putAll(getProperties(cicsMQEndpoint.getJndiProperties()));
        }
        if (env.size() > 0) {
            return new InitialContext(env);
        } else {
            return new InitialContext();
        }
    } catch (NamingException e) {
        throw new CicsMQConnectionException(e);
    }
}