下面列出了javax.naming.Context#listBindings ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain;UTF-8");
PrintWriter out = resp.getWriter();
try {
Context ctx = new InitialContext();
NamingEnumeration<Binding> enm =
ctx.listBindings("java:comp/env/list");
while (enm.hasMore()) {
Binding b = enm.next();
out.print(b.getObject().getClass().getName());
}
} catch (NamingException ne) {
ne.printStackTrace(out);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain;UTF-8");
PrintWriter out = resp.getWriter();
try {
Context ctx = new InitialContext();
NamingEnumeration<Binding> enm =
ctx.listBindings("java:comp/env/list");
while (enm.hasMore()) {
Binding b = enm.next();
out.print(b.getObject().getClass().getName());
}
} catch (NamingException ne) {
ne.printStackTrace(out);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain;UTF-8");
PrintWriter out = resp.getWriter();
try {
Context ctx = new InitialContext();
NamingEnumeration<Binding> enm =
ctx.listBindings("java:comp/env/list");
while (enm.hasMore()) {
Binding b = enm.next();
out.print(b.getObject().getClass().getName());
}
} catch (NamingException ne) {
ne.printStackTrace(out);
}
}
/**
* Removes all entries from the specified context, including subcontexts.
*
* @param context context ot clear
*/
private void clearContext(Context context)
throws NamingException {
for (NamingEnumeration e = context.listBindings(""); e
.hasMoreElements();) {
Binding binding = (Binding) e.nextElement();
if (binding.getObject() instanceof Context) {
clearContext((Context) binding.getObject());
}
context.unbind(binding.getName());
}
}
/**
* Removes all entries from the specified context, including subcontexts.
*
* @param context context ot clear
*/
private void clearContext(Context context)
throws NamingException {
for (NamingEnumeration e = context.listBindings(""); e
.hasMoreElements();) {
Binding binding = (Binding) e.nextElement();
if (binding.getObject() instanceof Context) {
clearContext((Context) binding.getObject());
}
context.unbind(binding.getName());
}
}
public static List<JndiBinding> listBindings(Context context, String path)
throws NamingException {
final String normalizedPath = normalizePath(path);
final String jndiName;
if (Parameters.getServletContext().getServerInfo().contains("WebLogic")) {
// path + '/' nécessaire pour WebLogic 10.3.1.0 mais pas supporté dans JBoss
jndiName = JNDI_PREFIX + normalizedPath + '/';
} else {
jndiName = JNDI_PREFIX + normalizedPath;
}
final List<JndiBinding> result = new ArrayList<JndiBinding>();
final NamingEnumeration<Binding> enumeration = context.listBindings(jndiName);
try {
while (enumeration.hasMore()) {
try {
final Binding binding = enumeration.next();
final JndiBinding jndiBinding = createJndiBinding(normalizedPath, binding);
// si on veux corriger http://java.net/jira/browse/GLASSFISH-12831
// sous glassfish 3.0.1 et non 3.1, les bindings d'un contexte contienne le contexte lui-même
// if (jndiBinding.getName().isEmpty()) {
// return;
// }
result.add(jndiBinding);
} catch (final Exception e) {
// catch Exception et non catch NamingException car glassfish 3.1 par exemple
// lance parfois des RuntimeException encapsulant des NamingException lors du next()
continue;
}
}
} finally {
// Comme indiqué dans la javadoc enumeration.close() n'est pas nécessaire après que hasMore()
// a retourné false. De plus, cela provoquerait une exception dans glassfish 3.0.1
// "javax.naming.OperationNotSupportedException: close() not implemented"
// enumeration.close();
context.close();
}
return result;
}
protected void assertBinding(Binding binding) throws NamingException {
Object object = binding.getObject();
assertTrue("Should have got a child context but got: " + object, object instanceof Context);
Context childContext = (Context) object;
NamingEnumeration<Binding> iter = childContext.listBindings("");
while (iter.hasMore()) {
Binding destinationBinding = iter.next();
LOG.info("Found destination: " + destinationBinding.getName());
Object destination = destinationBinding.getObject();
assertTrue("Should have a Destination but got: " + destination, destination instanceof Destination);
}
}
public static void tearDownRecursively(final Context c) throws Exception {
for (NamingEnumeration<Binding> ne = c.listBindings(""); ne.hasMore(); ) {
Binding b = ne.next();
String name = b.getName();
Object object = b.getObject();
if (object instanceof Context) {
JNDIUtil.tearDownRecursively((Context) object);
}
c.unbind(name);
}
}
public static void contextToMap(final Context context, final String baseName, final Map<String, Object> results) throws NamingException {
final NamingEnumeration<Binding> namingEnumeration = context.listBindings("");
while (namingEnumeration.hasMoreElements()) {
final Binding binding = namingEnumeration.nextElement();
final String name = binding.getName();
final String fullName = baseName + name;
final Object object = binding.getObject();
results.put(fullName, object);
if (object instanceof Context) {
contextToMap((Context) object, fullName + "/", results);
}
}
}
private void verifyListBindings(Context c, String name,
Object obj1, Object obj2) throws NamingException {
boolean datasourceFoundFlg = false;
boolean o2FoundFlg = false;
boolean datasourceO1FoundFlg = false;
boolean datasourceNullFoundFlg = false;
// List bindings for the specified context
for (NamingEnumeration en = c.listBindings(name); en
.hasMore();) {
Binding b = (Binding) en.next();
if (b.getName().equals("datasource")) {
assertEquals(b.getObject(), datasourceCtx);
datasourceFoundFlg = true;
Context nextCon = (Context) b.getObject();
for (NamingEnumeration en1 = nextCon
.listBindings(""); en1.hasMore();) {
Binding b1 = (Binding) en1.next();
if (b1.getName().equals("sub41")) {
assertEquals(b1.getObject(), obj1);
datasourceO1FoundFlg = true;
}
else if (b1.getName().equals("sub43")) {
// check for null object
assertNull(b1.getObject());
datasourceNullFoundFlg = true;
}
}
}
else if (b.getName().equals("sub42")) {
assertEquals(b.getObject(), obj2);
o2FoundFlg = true;
}
}
if (!(datasourceFoundFlg && o2FoundFlg
&& datasourceO1FoundFlg && datasourceNullFoundFlg)) {
fail();
}
}
private void verifyListBindings(Context c, String name,
Object obj1, Object obj2) throws NamingException {
boolean datasourceFoundFlg = false;
boolean o2FoundFlg = false;
boolean datasourceO1FoundFlg = false;
boolean datasourceNullFoundFlg = false;
// List bindings for the specified context
for (NamingEnumeration en = c.listBindings(name); en
.hasMore();) {
Binding b = (Binding) en.next();
if (b.getName().equals("datasource")) {
assertEquals(b.getObject(), datasourceCtx);
datasourceFoundFlg = true;
Context nextCon = (Context) b.getObject();
for (NamingEnumeration en1 = nextCon
.listBindings(""); en1.hasMore();) {
Binding b1 = (Binding) en1.next();
if (b1.getName().equals("sub41")) {
assertEquals(b1.getObject(), obj1);
datasourceO1FoundFlg = true;
}
else if (b1.getName().equals("sub43")) {
// check for null object
assertNull(b1.getObject());
datasourceNullFoundFlg = true;
}
}
}
else if (b.getName().equals("sub42")) {
assertEquals(b.getObject(), obj2);
o2FoundFlg = true;
}
}
if (!(datasourceFoundFlg && o2FoundFlg
&& datasourceO1FoundFlg && datasourceNullFoundFlg)) {
fail();
}
}
private void destroyLookedUpResource(final Context globalContext, final String id, final String name) throws NamingException {
final Object object;
try {
object = globalContext.lookup(name);
} catch (final NamingException e) {
// if we catch a NamingException, check to see if the resource is a LaztObjectReference that has not been initialized correctly
final String ctx = name.substring(0, name.lastIndexOf('/'));
final String objName = name.substring(ctx.length() + 1);
final NamingEnumeration<Binding> bindings = globalContext.listBindings(ctx);
while (bindings.hasMoreElements()) {
final Binding binding = bindings.nextElement();
if (!binding.getName().equals(objName)) {
continue;
}
if (!LazyObjectReference.class.isInstance(binding.getObject())) {
continue;
}
final LazyObjectReference<?> ref = LazyObjectReference.class.cast(binding.getObject());
if (! ref.isInitialized()) {
globalContext.unbind(name);
removeResourceInfo(name);
return;
}
}
throw e;
}
final String clazz;
if (object == null) { // should it be possible?
clazz = "?";
} else {
clazz = object.getClass().getName();
}
destroyResource(id, clazz, object);
globalContext.unbind(name);
}