下面列出了javax.naming.InitialContext#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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();
}
/**
* 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);
}
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();
}
}
/**
* 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);
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
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();
}
/**
* 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();
}
/**
* 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);
}
@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();
}
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();
}
}
/**
* 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);
}
/**
* 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();
}
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"));
}
/**
* 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);
}