Java Code Examples for javax.naming.Context#listBindings()

The following examples show how to use javax.naming.Context#listBindings() . 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: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: TestNamingContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 3
Source File: TestNamingContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: ContextTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
  }
  
}
 
Example 5
Source File: ContextTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
  }
  
}
 
Example 6
Source File: JndiBinding.java    From javamelody with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: JNDITestSupport.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
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);
   }
}
 
Example 8
Source File: JNDIUtil.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
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);
   }
}
 
Example 9
Source File: Debug.java    From tomee with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 10
Source File: ContextTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
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();
  }
}
 
Example 11
Source File: ContextTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
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();
  }
}
 
Example 12
Source File: Assembler.java    From tomee with Apache License 2.0 4 votes vote down vote up
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);
    }