Example usage for javax.naming Context unbind

List of usage examples for javax.naming Context unbind

Introduction

In this page you can find the example usage for javax.naming Context unbind.

Prototype

public void unbind(String name) throws NamingException;

Source Link

Document

Unbinds the named object.

Usage

From source file:com.dattack.naming.AbstractContext.java

@Override
public void unbind(final Name name) throws NamingException {

    ensureContextNotClosed();/*ww w.  ja v a2  s  . co  m*/

    if (name.isEmpty()) {
        throw new InvalidNameException("Cannot unbind to empty name");
    }

    if (name.size() == 1) {
        if (objectTable.containsKey(name)) {
            objectTable.remove(name);
        }
        return;
    }

    final Context parentContext = getParentContext(name);

    parentContext.unbind(name.getSuffix(name.size() - 1));
}

From source file:org.picketlink.idm.performance.TestBase.java

public void removeContext(Context mainCtx, String name) throws Exception {
    Context deleteCtx = (Context) mainCtx.lookup(name);
    NamingEnumeration subDirs = mainCtx.listBindings(name);

    while (subDirs.hasMoreElements()) {
        Binding binding = (Binding) subDirs.nextElement();
        String subName = binding.getName();

        removeContext(deleteCtx, subName);
    }//w w  w.ja  v  a  2  s.c  o m

    mainCtx.unbind(name);
}

From source file:jdao.JDAO.java

public static Context unbind(Context ctx, String nameStr) throws NamingException {
    log("unbinding " + nameStr);

    Name name = ctx.getNameParser("").parse(nameStr);

    //no name, nothing to do
    if (name.size() == 0)
        return null;

    Context subCtx = ctx;

    for (int i = 0; i < name.size() - 1; i++) {
        try {/*from www .  ja v a  2 s. co m*/
            subCtx = (Context) subCtx.lookup(name.get(i));
        } catch (NameNotFoundException e) {
            log("Subcontext " + name.get(i) + " undefined", e);
            return null;
        }
    }

    subCtx.unbind(name.get(name.size() - 1));
    log("unbound object " + nameStr);
    return subCtx;
}

From source file:org.apache.openejb.assembler.classic.Assembler.java

public synchronized void destroyApplication(AppInfo appInfo) throws UndeployException {
    deployedApplications.remove(appInfo.path);
    logger.info("destroyApplication.start", appInfo.path);

    fireBeforeApplicationDestroyed(appInfo);

    final AppContext appContext = containerSystem.getAppContext(appInfo.appId);

    for (Map.Entry<String, Object> value : appContext.getBindings().entrySet()) {
        String path = value.getKey();
        if (path.startsWith("global")) {
            path = "java:" + path;
        }/*ww  w. ja  v  a  2  s  . co  m*/
        if (!path.startsWith("java:global")) {
            continue;
        }

        try {
            containerSystem.getJNDIContext().unbind(path);
        } catch (NamingException ignored) {
            // no-op
        }
    }
    try {
        containerSystem.getJNDIContext().unbind("java:global");
    } catch (NamingException ignored) {
        // no-op
    }

    EjbResolver globalResolver = new EjbResolver(null, EjbResolver.Scope.GLOBAL);
    for (AppInfo info : deployedApplications.values()) {
        globalResolver.addAll(info.ejbJars);
    }
    SystemInstance.get().setComponent(EjbResolver.class, globalResolver);

    Context globalContext = containerSystem.getJNDIContext();
    UndeployException undeployException = new UndeployException(
            messages.format("destroyApplication.failed", appInfo.path));

    WebAppBuilder webAppBuilder = SystemInstance.get().getComponent(WebAppBuilder.class);
    if (webAppBuilder != null) {
        try {
            webAppBuilder.undeployWebApps(appInfo);
        } catch (Exception e) {
            undeployException.getCauses().add(new Exception("App: " + appInfo.path + ": " + e.getMessage(), e));
        }
    }

    // get all of the ejb deployments
    List<BeanContext> deployments = new ArrayList<BeanContext>();
    for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
        for (EnterpriseBeanInfo beanInfo : ejbJarInfo.enterpriseBeans) {
            String deploymentId = beanInfo.ejbDeploymentId;
            BeanContext beanContext = containerSystem.getBeanContext(deploymentId);
            if (beanContext == null) {
                undeployException.getCauses().add(new Exception("deployment not found: " + deploymentId));
            } else {
                deployments.add(beanContext);
            }
        }
    }

    // Just as with startup we need to get things in an
    // order that respects the singleton @DependsOn information
    // Theoreticlly if a Singleton depends on something in its
    // @PostConstruct, it can depend on it in its @PreDestroy.
    // Therefore we want to make sure that if A dependsOn B,
    // that we destroy A first then B so that B will still be
    // usable in the @PreDestroy method of A.

    // Sort them into the original starting order
    deployments = sort(deployments);
    // reverse that to get the stopping order
    Collections.reverse(deployments);

    // stop
    for (BeanContext deployment : deployments) {
        String deploymentID = deployment.getDeploymentID() + "";
        try {
            Container container = deployment.getContainer();
            container.stop(deployment);
        } catch (Throwable t) {
            undeployException.getCauses()
                    .add(new Exception("bean: " + deploymentID + ": " + t.getMessage(), t));
        }
    }

    // undeploy
    for (BeanContext bean : deployments) {
        String deploymentID = bean.getDeploymentID() + "";
        try {
            Container container = bean.getContainer();
            container.undeploy(bean);
            bean.setContainer(null);
        } catch (Throwable t) {
            undeployException.getCauses()
                    .add(new Exception("bean: " + deploymentID + ": " + t.getMessage(), t));
        } finally {
            bean.setDestroyed(true);
        }
    }

    // get the client ids
    List<String> clientIds = new ArrayList<String>();
    for (ClientInfo clientInfo : appInfo.clients) {
        clientIds.add(clientInfo.moduleId);
        for (String className : clientInfo.localClients) {
            clientIds.add(className);
        }
        for (String className : clientInfo.remoteClients) {
            clientIds.add(className);
        }
    }

    if (appContext != null)
        for (WebContext webContext : appContext.getWebContexts()) {
            containerSystem.removeWebContext(webContext);
        }

    // Clear out naming for all components first
    for (BeanContext deployment : deployments) {
        String deploymentID = deployment.getDeploymentID() + "";
        try {
            containerSystem.removeBeanContext(deployment);
        } catch (Throwable t) {
            undeployException.getCauses().add(new Exception(deploymentID, t));
        }

        JndiBuilder.Bindings bindings = deployment.get(JndiBuilder.Bindings.class);
        if (bindings != null)
            for (String name : bindings.getBindings()) {
                try {
                    globalContext.unbind(name);
                } catch (Throwable t) {
                    undeployException.getCauses()
                            .add(new Exception("bean: " + deploymentID + ": " + t.getMessage(), t));
                }
            }
    }

    for (PersistenceUnitInfo unitInfo : appInfo.persistenceUnits) {
        try {
            Object object = globalContext.lookup(PERSISTENCE_UNIT_NAMING_CONTEXT + unitInfo.id);
            globalContext.unbind(PERSISTENCE_UNIT_NAMING_CONTEXT + unitInfo.id);

            // close EMF so all resources are released
            ReloadableEntityManagerFactory remf = ((ReloadableEntityManagerFactory) object);
            remf.close();
            persistenceClassLoaderHandler.destroy(unitInfo.id);
            remf.unregister();
        } catch (Throwable t) {
            undeployException.getCauses()
                    .add(new Exception("persistence-unit: " + unitInfo.id + ": " + t.getMessage(), t));
        }
    }

    for (String sId : moduleIds) {
        try {
            globalContext.unbind(VALIDATOR_FACTORY_NAMING_CONTEXT + sId);
            globalContext.unbind(VALIDATOR_NAMING_CONTEXT + sId);
        } catch (NamingException e) {
            undeployException.getCauses().add(new Exception("validator: " + sId + ": " + e.getMessage(), e));
        }
    }
    moduleIds.clear();

    try {
        if (globalContext instanceof IvmContext) {
            IvmContext ivmContext = (IvmContext) globalContext;
            ivmContext.prune("openejb/Deployment");
            ivmContext.prune("openejb/local");
            ivmContext.prune("openejb/remote");
            ivmContext.prune("openejb/global");
        }
    } catch (NamingException e) {
        undeployException.getCauses().add(new Exception(
                "Unable to prune openejb/Deployments and openejb/local namespaces, this could cause future deployments to fail.",
                e));
    }

    deployments.clear();

    for (String clientId : clientIds) {
        try {
            globalContext.unbind("/openejb/client/" + clientId);
        } catch (Throwable t) {
            undeployException.getCauses().add(new Exception("client: " + clientId + ": " + t.getMessage(), t));
        }
    }

    // mbeans
    MBeanServer server = LocalMBeanServer.get();
    for (String objectName : appInfo.jmx) {
        try {
            ObjectName on = new ObjectName(objectName);
            if (server.isRegistered(on)) {
                server.unregisterMBean(on);
            }
        } catch (InstanceNotFoundException e) {
            logger.warning("can't unregister " + objectName + " because the mbean was not found", e);
        } catch (MBeanRegistrationException e) {
            logger.warning("can't unregister " + objectName, e);
        } catch (MalformedObjectNameException mone) {
            logger.warning("can't unregister because the ObjectName is malformed: " + objectName, mone);
        }
    }

    containerSystem.removeAppContext(appInfo.appId);

    ClassLoaderUtil.destroyClassLoader(appInfo.path);

    if (undeployException.getCauses().size() > 0) {
        throw undeployException;
    }

    logger.debug("destroyApplication.success", appInfo.path);
}

From source file:org.apache.torque.JndiConfigurationTest.java

/**
 * unbinds and closes the BasicDataSource in jndi.
 * @throws Exception if creation or binfding fails.
 *///  w  w  w . j  a v a  2s  .c  o  m
protected void unbindDataSource() throws Exception {
    Context context = getInitialContext();
    BasicDataSource dataSource = (BasicDataSource) context.lookup(JNDI_PATH);

    try {
        if (dataSource != null) {
            dataSource.close();
        }
    } finally {
        context.unbind(JNDI_PATH);
    }
}

From source file:org.compass.core.jndi.CompassObjectFactory.java

public static void removeInstance(String uid, String name, CompassSettings settings) {
    // TODO: theoretically non-threadsafe...

    if (name != null) {
        if (log.isInfoEnabled()) {
            log.info("Unbinding compass from JNDI name [" + name + "]");
        }/*from  ww  w .j ava 2  s  . co  m*/

        try {
            Context ctx = NamingHelper.getInitialContext(settings);
            ctx.unbind(name);
        } catch (InvalidNameException ine) {
            log.error("Invalid JNDI name [" + name + "]", ine);
        } catch (NamingException ne) {
            log.warn("Could not unbind compass from JNDI", ne);
        }

        NAMED_INSTANCES.remove(name);

    }

    INSTANCES.remove(uid);

}

From source file:org.hibersap.ejb.util.JndiUtil.java

@PreDestroy
public static void unbindSessionManager(final String jndiName) {
    LOG.info("Unbinding Hibersap SessionManager from JNDI name '" + jndiName + "'");

    try {/* w w  w .j  av  a2s .  c  o m*/
        Context ctx = new InitialContext();
        ctx.unbind(jndiName);
    } catch (NamingException e) {
        LOG.warn("Failed to unbind Hibersap SessionManager binding for JNDI name [" + jndiName + "]", e);
    }
}

From source file:org.jsecurity.jndi.JndiTemplate.java

/**
 * Remove the binding for the given name from the current JNDI context.
 *
 * @param name the JNDI name of the object
 * @throws NamingException thrown by JNDI, mostly name not found
 *//*from   w  ww .j  a v  a 2  s  . c  o m*/
public void unbind(final String name) throws NamingException {
    if (log.isDebugEnabled()) {
        log.debug("Unbinding JNDI object with name [" + name + "]");
    }
    execute(new JndiCallback() {
        public Object doInContext(Context ctx) throws NamingException {
            ctx.unbind(name);
            return null;
        }
    });
}

From source file:org.nuxeo.runtime.datasource.DataSourceDescriptor.java

public void unbindSelf(Context naming) throws NamingException {
    try {/*  ww w  .  j  a va 2s  . c  om*/
        final PooledDataSourceRegistry registry = Framework.getLocalService(PooledDataSourceRegistry.class);
        if (registry != null) {
            registry.clearPool(getName());
        }
    } finally {
        try {
            if (xaReference != null) {
                naming.unbind(DataSourceHelper.getDataSourceJNDIName(getName() + "-xa"));
            }
        } finally {
            naming.unbind(DataSourceHelper.getDataSourceJNDIName(getName()));
        }
    }
}

From source file:org.nuxeo.runtime.test.runner.JndiHelper.java

/**
 * 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//  www  . ja v a  2s.co  m
 * @throws NamingException for any error
 */
public 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.error(e, e);
            break;
        }
    }
}