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

The following examples show how to use javax.naming.Context#unbind() . 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: Util.java    From lams with 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
Source File: Util.java    From ironjacamar with 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 3
Source File: SessionFactoryObjectFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 4
Source File: JndiUtil.java    From hibersap with 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 5
Source File: JNDIIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
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 6
Source File: Assembler.java    From tomee with 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 7
Source File: ObjectContext.java    From oodt with 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 8
Source File: UserTransactionImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 9
Source File: TransactionManagerImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 10
Source File: TransactionSynchronizationRegistryImpl.java    From ironjacamar with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 11
Source File: JmsInitialContextFactoryTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: TransactionSynchronizationRegistryImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 13
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 14
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 15
Source File: HelloImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 16
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 17
Source File: UserTransactionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 18
Source File: TransactionManagerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stop
 * @exception Throwable Thrown if an error occurs
 */
public void stop() throws Throwable
{
   Context context = new InitialContext();

   context.unbind(JNDI_NAME);

   context.close();
}
 
Example 19
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);
    }