Java Code Examples for javax.naming.InitialContext#close()

The following examples show how to use javax.naming.InitialContext#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QpidJmsClientProvider.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private Destination getDestination(String type, String name) throws NamingException
{
    final String jndiName = "test";
    final Properties initialContextProperties = new Properties();
    initialContextProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
    initialContextProperties.put(type + "." + jndiName, name);

    InitialContext initialContext = new InitialContext(initialContextProperties);
    try
    {
        return (Destination) initialContext.lookup(jndiName);
    }
    finally
    {
        initialContext.close();
    }
}
 
Example 2
Source File: JdbcWrapperHelper.java    From javamelody with Apache License 2.0 6 votes vote down vote up
static Map<String, DataSource> getJndiDataSources() throws NamingException {
	final Map<String, DataSource> dataSources = new LinkedHashMap<String, DataSource>(2);
	final String datasourcesParameter = Parameter.DATASOURCES.getValue();
	if (datasourcesParameter == null) {
		dataSources.putAll(getJndiDataSourcesAt("java:comp/env/jdbc"));
		// pour jboss sans jboss-env.xml ou sans resource-ref dans web.xml :
		dataSources.putAll(getJndiDataSourcesAt("java:/jdbc"));
		// pour JavaEE 6 :
		// (voir par exemple http://smokeandice.blogspot.com/2009/12/datasourcedefinition-hidden-gem-from.html)
		dataSources.putAll(getJndiDataSourcesAt("java:global/jdbc"));
		// pour WebLogic 10 et WebSphere 7, cf issue 68
		dataSources.putAll(getJndiDataSourcesAt("jdbc"));
	} else if (!datasourcesParameter.trim().isEmpty()) {
		final InitialContext initialContext = new InitialContext();
		for (final String datasource : datasourcesParameter.split(",")) {
			final String jndiName = datasource.trim();
			// ici, on n'ajoute pas java:/comp/env
			// et on suppose qu'il n'en faut pas ou que cela a été ajouté dans le paramétrage
			final DataSource dataSource = (DataSource) initialContext.lookup(jndiName);
			dataSources.put(jndiName, dataSource);
		}
		initialContext.close();
	}
	return Collections.unmodifiableMap(dataSources);
}
 
Example 3
Source File: RMIConnectorServer.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 4
Source File: RMIConnector.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li>
 *       <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li>
 *       <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @param isIiop true if the stub is expected to be an IIOP stub.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env,
        boolean isIiop)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    if (isIiop)
        return narrowIIOPServer(objref);
    else
        return narrowJRMPServer(objref);
}
 
Example 5
Source File: RMIConnector.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li>
 *       <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li>
 *       <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @param isIiop true if the stub is expected to be an IIOP stub.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env,
        boolean isIiop)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    if (isIiop)
        return narrowIIOPServer(objref);
    else
        return narrowJRMPServer(objref);
}
 
Example 6
Source File: Mailer.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private Object lookupFromJndiName() throws NamingException {
	final InitialContext ctx = new InitialContext();
	try {
		return ctx.lookup("java:comp/env/" + jndiName);
	} catch (final NameNotFoundException e) {
		try {
			return ctx.lookup("java:/" + jndiName);
		} catch (final NameNotFoundException e2) {
			return ctx.lookup(jndiName);
		}
	} finally {
		ctx.close();
	}
}
 
Example 7
Source File: AsynchInRoleTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassScopeAsynch() throws Exception {
    //Build the application
    final AppModule app = new AppModule(this.getClass().getClassLoader(), "testclassasynch");
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new SingletonBean(TestBeanA.class));
    app.getEjbModules().add(new EjbModule(ejbJar));

    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);

    final Properties env = new Properties();
    env.put(javax.naming.Context.SECURITY_PRINCIPAL, "jonathan");
    env.put(javax.naming.Context.SECURITY_CREDENTIALS, "secret");

    final InitialContext context = new InitialContext(env);
    final TestBean test = (TestBean) context.lookup("TestBeanALocal");

    test.testA(Thread.currentThread().getId());
    Thread.sleep(1000L);
    Assert.assertEquals("testA was never executed", "testA", test.getLastInvokeMethod());

    final Future<String> future = test.testB(Thread.currentThread().getId());
    Thread.sleep(1000L);
    Assert.assertTrue("The task should be done", future.isDone());
    Assert.assertEquals("testB was never executed", "testB", test.getLastInvokeMethod());

    test.testC(Thread.currentThread().getId());
    Assert.assertEquals("testC was never executed", "testC", test.getLastInvokeMethod());

    test.testD(Thread.currentThread().getId());
    Assert.assertEquals("testD was never executed", "testD", test.getLastInvokeMethod());

    context.close();
}
 
Example 8
Source File: RMIConnector.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li>
 *       <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li>
 *       <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @param isIiop true if the stub is expected to be an IIOP stub.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env,
        boolean isIiop)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    if (isIiop)
        return narrowIIOPServer(objref);
    else
        return narrowJRMPServer(objref);
}
 
Example 9
Source File: RMIConnectorServer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 10
Source File: SecurityTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void testDefaultUser() throws Exception {
    final Assembler assembler = configureAssembler("public");

    // no credentials provided, the default user should be "guest"
    final Properties props = new Properties();

    final InitialContext ctx = new InitialContext(props);

    final Project foo = (Project) ctx.lookup("FooBeanLocal");

    foo.svnCheckout("");
    try {
        foo.svnCommit("");
        fail("Should not be allowed");
    } catch (final Exception e) {
        // good.
    }

    assertFalse("in role committer", foo.isCallerInRole("committer"));
    assertFalse("in role community", foo.isCallerInRole("community"));
    assertFalse("in role contributor", foo.isCallerInRole("contributor"));
    assertFalse("in role guest", foo.isCallerInRole("guest"));
    assertTrue("Caller is not public", foo.isCaller("public"));

    ctx.close();
    assembler.destroy();
}
 
Example 11
Source File: RMIConnectorServer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 12
Source File: RMIConnectorServer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 13
Source File: RMIConnectorServer.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 14
Source File: RMIConnector.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li>
 *       <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li>
 *       <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @param isIiop true if the stub is expected to be an IIOP stub.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env,
        boolean isIiop)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    if (isIiop)
        return narrowIIOPServer(objref);
    else
        return narrowJRMPServer(objref);
}
 
Example 15
Source File: FinderTestBean.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static Object lookup(final String s) throws NamingException {
    final Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
    final InitialContext ctx = new InitialContext(p);
    try {
        return ctx.lookup("java:comp/env/ejb/" + s);
    } finally {
        ctx.close();
    }
}
 
Example 16
Source File: RMIConnector.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li>
 *       <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li>
 *       <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @param isIiop true if the stub is expected to be an IIOP stub.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env,
        boolean isIiop)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    if (isIiop)
        return narrowIIOPServer(objref);
    else
        return narrowJRMPServer(objref);
}
 
Example 17
Source File: RMIConnectorServer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 18
Source File: RMIConnector.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li>
 *       <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li>
 *       <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @param isIiop true if the stub is expected to be an IIOP stub.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env,
        boolean isIiop)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    if (isIiop)
        return narrowIIOPServer(objref);
    else
        return narrowJRMPServer(objref);
}
 
Example 19
Source File: SecurityTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void test() throws Exception {
    final Assembler assembler = configureAssembler(null);

    final Properties props = new Properties();
    props.setProperty(Context.SECURITY_PRINCIPAL, "jonathan");
    props.setProperty(Context.SECURITY_CREDENTIALS, "secret");

    final InitialContext ctx = new InitialContext(props);

    final Project foo = (Project) ctx.lookup("FooBeanLocal");

    foo.svnCheckout("");
    foo.svnCommit("");

    try {
        foo.deleteProject("");
        fail("Should not be allowed");
    } catch (final Exception e) {
        // good.
    }

    assertTrue("not in role committer", foo.isCallerInRole("committer"));
    assertTrue("not in role community", foo.isCallerInRole("community"));
    assertFalse("in role contributor", foo.isCallerInRole("contributor"));
    assertTrue("Caller is not jonathan", foo.isCaller("jonathan"));

    ctx.close();
    assembler.destroy();

    //        Project bar = (Project) ctx.lookup("BarBeanLocal");
    //
    //        bar.svnCheckout("");
    //
    //        try {
    //            bar.svnCommit("");
    //            fail("Should not be allowed");
    //        } catch (Exception e) {
    //            // good
    //        }
    //
    //        try {
    //            bar.deleteProject("");
    //            fail("Should not be allowed");
    //        } catch (Exception e) {
    //            // good.
    //        }
    //
    //        assertFalse("in role committer", bar.isCallerInRole("committer"));
    //        assertFalse("in role community", bar.isCallerInRole("community"));
    //        assertTrue("not in role contributor", bar.isCallerInRole("contributor"));
}
 
Example 20
Source File: RMIConnector.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Lookup the RMIServer stub in a directory.
 * @param jndiURL A JNDI URL indicating the location of the Stub
 *                (see {@link javax.management.remote.rmi}), e.g.:
 *   <ul><li>{@code rmi://registry-host:port/rmi-stub-name}</li>
 *       <li>or {@code ldap://ldap-host:port/java-container-dn}</li>
 *   </ul>
 * @param env the environment Map passed to the connector.
 * @return The retrieved RMIServer stub.
 * @exception NamingException if the stub couldn't be found.
 **/
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env)
        throws NamingException {

    InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env));

    Object objref = ctx.lookup(jndiURL);
    ctx.close();

    return narrowJRMPServer(objref);
}