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

The following examples show how to use javax.naming.Context#unbind() . These examples are extracted from open source projects. 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 Project: lams   File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
/** 
 * Unbinds a name from ctx, and removes parents if they are empty
 * @param ctx the parent JNDI Context under which the name will be unbound
 * @param name The name to unbind
 * @throws NamingException for any error
 */
private static void unbind(Context ctx, Name name) throws NamingException
{
   ctx.unbind(name); //unbind the end node in the name
   int sz = name.size();
   // walk the tree backwards, stopping at the domain
   while (--sz > 0)
   {
      Name pname = name.getPrefix(sz);
      try
      {
         ctx.destroySubcontext(pname);
      }
      catch (NamingException e)
      {
         log.tracef("Unable to remove context: %s (%s)", pname, e);
         break;
      }
   }
}
 
Example 2
public static void removeInstance(String uid, String name, Properties properties) {
	//TODO: theoretically non-threadsafe...

	if (name!=null) {
		log.info("Unbinding factory from JNDI name: " + name);

		try {
			Context ctx = NamingHelper.getInitialContext(properties);
			ctx.unbind(name);
			log.info("Unbound factory from JNDI name: " + name);
		}
		catch (InvalidNameException ine) {
			log.error("Invalid JNDI name: " + name, ine);
		}
		catch (NamingException ne) {
			log.warn("Could not unbind factory from JNDI", ne);
		}

		NAMED_INSTANCES.remove(name);

	}

	INSTANCES.remove(uid);

}
 
Example 3
Source Project: ironjacamar   File: Util.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/** 
 * Unbinds a name from ctx, and removes parents if they are empty
 * @param ctx the parent JNDI Context under which the name will be unbound
 * @param name The name to unbind
 * @throws NamingException for any error
 */
private static void unbind(Context ctx, Name name) throws NamingException
{
   ctx.unbind(name); //unbind the end node in the name
   int sz = name.size();
   // walk the tree backwards, stopping at the domain
   while (--sz > 0)
   {
      Name pname = name.getPrefix(sz);
      try
      {
         ctx.destroySubcontext(pname);
      }
      catch (NamingException e)
      {
         log.tracef("Unable to remove context: %s (%s)", pname, e);
         break;
      }
   }
}
 
Example 4
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 5
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 6
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 7
Source Project: gemfirexd-oss   File: ContextTest.java    License: 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 8
public void stop() throws Exception
{
   if (m_isRunning)
   {
      PortableRemoteObject.unexportObject(this);
      Context ctx = new InitialContext();
      ctx.unbind(IIOP_JNDI_NAME);
      m_isRunning = false;
      System.out.println("My Service Servant stopped successfully");
   }
}
 
Example 9
Source Project: gemfirexd-oss   File: ContextTest.java    License: 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 10
Source Project: activemq-artemis   File: JNDIUtil.java    License: 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 11
Source Project: hibersap   File: JndiUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void unbindSessionManager(final String jndiName) {
    LOG.info("Unbinding Hibersap SessionManager from JNDI name '" + jndiName + "'");

    try {
        Context ctx = new InitialContext();
        ctx.unbind(jndiName);
    } catch (NamingException e) {
        LOG.warn("Failed to unbind Hibersap SessionManager binding for JNDI name [" + jndiName + "]", e);
    }
}
 
Example 12
@Test(expected = OperationNotSupportedException.class)
public void testContextPreventsUnbind() throws Exception {
    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    Context ctx = createInitialContext(env);

    ctx.unbind("lookupName");
}
 
Example 13
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 14
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 15
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 16
Source Project: oodt   File: ObjectContext.java    License: Apache License 2.0 5 votes vote down vote up
public void unbind(String name) throws NamingException {
	if (name == null) {
	  throw new IllegalArgumentException("Name required");
	}
	if (name.length() == 0) {
	  throw new InvalidNameException("Cannot unbind object named after context");
	}

	// See if it's an aliased name
	if (aliases.containsKey(name)) {
		aliases.remove(name);
		return;
	}

	boolean unbound = false;
  for (Object context : contexts) {
	Context c = (Context) context;
	try {
	  c.unbind(name);
	  unbound = true;
	} catch (NamingException ignore) {
	}
  }
	if (!unbound) {
	  throw new InvalidNameException("Name \"" + name + "\" not compatible with any managed subcontext");
	}
}
 
Example 17
Source Project: tomee   File: Assembler.java    License: Apache License 2.0 5 votes vote down vote up
private void unbind(final Context context, final String name) {
    try {
        context.unbind(name);
    } catch (final NamingException e) {
        // no-op
    }
}
 
Example 18
private void assertBeanBinding(WildFlyCamelContext camelctx) throws NamingException, Exception {

        InitialContext inicxt = new InitialContext();
        String bindingName = CamelConstants.CAMEL_CONTEXT_BINDING_NAME + "/" + camelctx.getName();

        Context jndictx = camelctx.getNamingContext();
        jndictx.bind("helloBean", new HelloBean());
        try {
            camelctx.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("direct:start").bean("helloBean");
                }
            });

            camelctx.start();
            try {

                // Assert that the context is bound into JNDI after start
                CamelContext lookup = (CamelContext) inicxt.lookup(bindingName);
                Assert.assertSame(camelctx, lookup);

                ProducerTemplate producer = camelctx.createProducerTemplate();
                String result = producer.requestBody("direct:start", "Kermit", String.class);
                Assert.assertEquals("Hello Kermit", result);
            } finally {
                camelctx.close();
            }

            // Assert that the context is unbound from JNDI after stop
            Assert.assertTrue("Expected " + bindingName + " to be unbound", awaitUnbinding(inicxt, bindingName));
        } finally {
            jndictx.unbind("helloBean");
            Assert.assertTrue("Expected java:/helloBean to be unbound", awaitUnbinding(jndictx, "helloBean"));
        }
    }
 
Example 19
Source Project: tomee   File: Assembler.java    License: 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);
    }