下面列出了javax.naming.Context#unbind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
}
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);
}
/**
* 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;
}
}
}
/**
* Stop
* @exception Throwable Thrown if an error occurs
*/
public void stop() throws Throwable
{
Context context = new InitialContext();
context.unbind(JNDI_NAME);
context.close();
}
/**
* Stop
* @exception Throwable Thrown if an error occurs
*/
public void stop() throws Throwable
{
Context context = new InitialContext();
context.unbind(JNDI_NAME);
context.close();
}
/**
* Stop
* @exception Throwable Thrown if an error occurs
*/
public void stop() throws Throwable
{
Context context = new InitialContext();
context.unbind(JNDI_NAME);
context.close();
}
/**
* 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 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");
}
}
/**
* 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 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 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);
}
}
@Test(expected = OperationNotSupportedException.class)
public void testContextPreventsUnbind() throws Exception {
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
Context ctx = createInitialContext(env);
ctx.unbind("lookupName");
}
/**
* Stop
* @exception Throwable Thrown if an error occurs
*/
public void stop() throws Throwable
{
Context context = new InitialContext();
context.unbind(JNDI_NAME);
context.close();
}
/**
* Stop
* @exception Throwable Thrown if an error occurs
*/
public void stop() throws Throwable
{
Context context = new InitialContext();
context.unbind(JNDI_NAME);
context.close();
}
/**
* Stop
* @exception Throwable Thrown if an error occurs
*/
public void stop() throws Throwable
{
Context context = new InitialContext();
context.unbind(JNDI_NAME);
context.close();
}
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");
}
}
private void unbind(final Context context, final String name) {
try {
context.unbind(name);
} catch (final NamingException e) {
// no-op
}
}
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"));
}
}
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);
}