Search in sources :

Example 26 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project kernel by exoplatform.

the class InitialContextInitializer method recall.

/**
 * Patch for case when bound objects are not shared i.e. there are some parts
 * of app using different copy of Context, for example per web app
 * InitialContext in Tomcat
 */
@Deprecated
public void recall() {
    for (BindReferencePlugin plugin : bindReferencesPlugins) {
        try {
            InitialContext ic = getInitialContext();
            ic.bind(plugin.getBindName(), plugin.getReference());
            LOG.info("Reference bound (by recall()): " + plugin.getBindName());
        } catch (NameAlreadyBoundException e) {
            LOG.debug("Name already bound: " + plugin.getBindName());
        } catch (NamingException e) {
            LOG.error("Could not bind: " + plugin.getBindName(), e);
        }
    }
}
Also used : NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 27 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project kernel by exoplatform.

the class InitialContextTest method testGetContext.

public void testGetContext() throws Exception {
    assertNotNull(System.getProperty(Context.INITIAL_CONTEXT_FACTORY));
    InitialContext ctx = new InitialContext();
    assertNotNull(ctx);
    ctx.bind("test", "test");
    assertEquals("test", ctx.lookup("test"));
    try {
        ctx.bind("test", "test2");
        fail("A NameAlreadyBoundException is expected here");
    } catch (NameAlreadyBoundException e) {
    // expected exception
    }
    assertEquals("test", ctx.lookup("test"));
    ctx.rebind("test", "test2");
    assertEquals("test2", ctx.lookup("test"));
    initializer.getInitialContext().bind("test", "test3");
    assertEquals("test3", ctx.lookup("test"));
    ctx.rebind("test", "test4");
    assertEquals("test3", ctx.lookup("test"));
    initializer.getInitialContext().rebind("test", "test5");
    assertEquals("test5", ctx.lookup("test"));
    initializer.getInitialContext().unbind("test");
    try {
        initializer.getInitialContext().lookup("test");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
    // expected exception
    }
    assertEquals("test4", ctx.lookup("test"));
    ctx.unbind("test");
    try {
        ctx.lookup("test");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
    // expected exception
    }
    try {
        initializer.getInitialContext().unbind("test2");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
    // expected exception
    }
    initializer.getInitialContext().bind("foo", "foo");
    assertEquals("foo", ctx.lookup("foo"));
    initializer.getInitialContext().bind("foo2", "foo2");
    assertEquals("foo2", ctx.lookup("foo2"));
    try {
        initializer.getInitialContext().rename("foo", "foo2");
        fail("A NameAlreadyBoundException is expected here");
    } catch (NameAlreadyBoundException e) {
    // expected exception
    }
    assertEquals("foo", ctx.lookup("foo"));
    assertEquals("foo2", ctx.lookup("foo2"));
    try {
        initializer.getInitialContext().rename("foo3", "foo4");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
    // expected exception
    }
    initializer.getInitialContext().rename("foo", "foo3");
    assertEquals("foo", ctx.lookup("foo3"));
    assertEquals("foo2", ctx.lookup("foo2"));
    try {
        initializer.getInitialContext().lookup("foo");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
    // expected exception
    }
    // check same instance
    initializer.getInitialContext().bind("bla", "bla");
    Object obj1 = initializer.getInitialContext().lookup("bla");
    Object obj2 = initializer.getInitialContext().lookup("bla");
    assertTrue(obj1 == obj2);
}
Also used : NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext)

Example 28 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project tomee by apache.

the class Assembler method bindGlobals.

public void bindGlobals(final Map<String, Object> bindings) throws NamingException {
    final Context containerSystemContext = containerSystem.getJNDIContext();
    for (final Entry<String, Object> value : bindings.entrySet()) {
        final String path = value.getKey();
        // keep only global bindings
        if (path.startsWith("module/") || path.startsWith("app/") || path.startsWith("comp/") || path.equalsIgnoreCase("global/dummy")) {
            continue;
        }
        // a bit weird but just to be consistent if user doesn't lookup directly the resource
        final Context lastContext = Contexts.createSubcontexts(containerSystemContext, path);
        try {
            lastContext.rebind(path.substring(path.lastIndexOf("/") + 1, path.length()), value.getValue());
        } catch (final NameAlreadyBoundException nabe) {
            nabe.printStackTrace();
        }
        containerSystemContext.rebind(path, value.getValue());
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) SimpleBootstrapContext(org.apache.openejb.core.transaction.SimpleBootstrapContext) Context(javax.naming.Context) ServletContext(javax.servlet.ServletContext) MethodContext(org.apache.openejb.MethodContext) IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) AppContext(org.apache.openejb.AppContext) InitialContext(javax.naming.InitialContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) CreationalContext(javax.enterprise.context.spi.CreationalContext) DeploymentContext(org.apache.openejb.DeploymentContext) GeronimoBootstrapContext(org.apache.geronimo.connector.GeronimoBootstrapContext) BootstrapContext(javax.resource.spi.BootstrapContext) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException)

Example 29 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project tomee by apache.

the class Assembler method bindResource.

private void bindResource(final String id, final Object service, final boolean canReplace) throws OpenEJBException {
    final String name = OPENEJB_RESOURCE_JNDI_PREFIX + id;
    final Context jndiContext = containerSystem.getJNDIContext();
    Object existing = null;
    try {
        ContextualJndiReference.followReference.set(false);
        existing = jndiContext.lookup(name);
    } catch (final Exception ignored) {
    // no-op
    } finally {
        // if the lookup fails the remove is not done
        ContextualJndiReference.followReference.remove();
    }
    boolean rebind = false;
    if (existing != null) {
        final boolean existingIsContextual = ContextualJndiReference.class.isInstance(existing);
        final boolean serviceIsExisting = ContextualJndiReference.class.isInstance(service);
        if (!existingIsContextual && serviceIsExisting) {
            ContextualJndiReference.class.cast(service).setDefaultValue(existing);
            rebind = true;
        } else if (existingIsContextual && !serviceIsExisting) {
            ContextualJndiReference.class.cast(existing).setDefaultValue(service);
        } else if (existingIsContextual) {
            // && serviceIsExisting is always true here
            final ContextualJndiReference contextual = ContextualJndiReference.class.cast(existing);
            if (canReplace && contextual.prefixesSize() == 1) {
                // replace!
                contextual.removePrefix(contextual.lastPrefix());
                contextual.setDefaultValue(service);
            } else {
                contextual.addPrefix(ContextualJndiReference.class.cast(service).lastPrefix());
            }
            return;
        }
    }
    try {
        if (canReplace && existing != null) {
            jndiContext.unbind(name);
        }
        if (rebind) {
            jndiContext.rebind(name, service);
        } else {
            jndiContext.bind(name, service);
        }
    } catch (final NameAlreadyBoundException nabe) {
        logger.warning("unbounding resource " + name + " can happen because of a redeployment or because of a duplicated id");
        try {
            jndiContext.unbind(name);
            jndiContext.bind(name, service);
        } catch (final NamingException e) {
            throw new OpenEJBException("Cannot bind resource adapter with id " + id, e);
        }
    } catch (final NamingException e) {
        throw new OpenEJBException("Cannot bind resource adapter with id " + id, e);
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) SimpleBootstrapContext(org.apache.openejb.core.transaction.SimpleBootstrapContext) Context(javax.naming.Context) ServletContext(javax.servlet.ServletContext) MethodContext(org.apache.openejb.MethodContext) IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) AppContext(org.apache.openejb.AppContext) InitialContext(javax.naming.InitialContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) CreationalContext(javax.enterprise.context.spi.CreationalContext) DeploymentContext(org.apache.openejb.DeploymentContext) GeronimoBootstrapContext(org.apache.geronimo.connector.GeronimoBootstrapContext) BootstrapContext(javax.resource.spi.BootstrapContext) OpenEJBException(org.apache.openejb.OpenEJBException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NamingException(javax.naming.NamingException) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ContextualJndiReference(org.apache.openejb.core.ivm.naming.ContextualJndiReference)

Example 30 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project tomcat70 by apache.

the class NamingContextListener method createNamingContext.

/**
 * Create and initialize the JNDI naming context.
 */
private void createNamingContext() throws NamingException {
    // Creating the comp subcontext
    if (container instanceof Server) {
        compCtx = namingContext;
        envCtx = namingContext;
    } else {
        compCtx = namingContext.createSubcontext("comp");
        envCtx = compCtx.createSubcontext("env");
    }
    int i;
    if (log.isDebugEnabled())
        log.debug("Creating JNDI naming context");
    if (namingResources == null) {
        namingResources = new NamingResources();
        namingResources.setContainer(container);
    }
    // Resource links
    ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
    for (i = 0; i < resourceLinks.length; i++) {
        addResourceLink(resourceLinks[i]);
    }
    // Resources
    ContextResource[] resources = namingResources.findResources();
    for (i = 0; i < resources.length; i++) {
        addResource(resources[i]);
    }
    // Resources Env
    ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
    for (i = 0; i < resourceEnvRefs.length; i++) {
        addResourceEnvRef(resourceEnvRefs[i]);
    }
    // Environment entries
    ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
    for (i = 0; i < contextEnvironments.length; i++) {
        addEnvironment(contextEnvironments[i]);
    }
    // EJB references
    ContextEjb[] ejbs = namingResources.findEjbs();
    for (i = 0; i < ejbs.length; i++) {
        addEjb(ejbs[i]);
    }
    // WebServices references
    ContextService[] services = namingResources.findServices();
    for (i = 0; i < services.length; i++) {
        addService(services[i]);
    }
    // Binding a User Transaction reference
    if (container instanceof Context) {
        try {
            Reference ref = new TransactionRef();
            compCtx.bind("UserTransaction", ref);
            ContextTransaction transaction = namingResources.getTransaction();
            if (transaction != null) {
                Iterator<String> params = transaction.listProperties();
                while (params.hasNext()) {
                    String paramName = params.next();
                    String paramValue = (String) transaction.getProperty(paramName);
                    StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
                    ref.add(refAddr);
                }
            }
        } catch (NameAlreadyBoundException e) {
        // Ignore because UserTransaction was obviously
        // added via ResourceLink
        } catch (NamingException e) {
            logger.error(sm.getString("naming.bindFailed", e));
        }
    }
    // Binding the resources directory context
    if (container instanceof Context) {
        try {
            compCtx.bind("Resources", ((Container) container).getResources());
        } catch (NamingException e) {
            logger.error(sm.getString("naming.bindFailed", e));
        }
    }
}
Also used : ContextEnvironment(org.apache.catalina.deploy.ContextEnvironment) NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) ContextService(org.apache.catalina.deploy.ContextService) Server(org.apache.catalina.Server) ContextResourceLink(org.apache.catalina.deploy.ContextResourceLink) Reference(javax.naming.Reference) NamingResources(org.apache.catalina.deploy.NamingResources) ContextTransaction(org.apache.catalina.deploy.ContextTransaction) ContextEjb(org.apache.catalina.deploy.ContextEjb) ContextResource(org.apache.catalina.deploy.ContextResource) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) StringRefAddr(javax.naming.StringRefAddr) TransactionRef(org.apache.naming.TransactionRef) NamingException(javax.naming.NamingException) ContextResourceEnvRef(org.apache.catalina.deploy.ContextResourceEnvRef)

Aggregations

NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)33 InitialContext (javax.naming.InitialContext)17 Context (javax.naming.Context)16 NamingException (javax.naming.NamingException)15 NameNotFoundException (javax.naming.NameNotFoundException)12 Reference (javax.naming.Reference)8 File (java.io.File)6 OperationNotSupportedException (javax.naming.OperationNotSupportedException)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 BeanContext (org.apache.openejb.BeanContext)5 InvalidNameException (javax.naming.InvalidNameException)4 LinkRef (javax.naming.LinkRef)4 Name (javax.naming.Name)4 AppContext (org.apache.openejb.AppContext)4 Properties (java.util.Properties)3 CreationalContext (javax.enterprise.context.spi.CreationalContext)3 Referenceable (javax.naming.Referenceable)3 DataSource (javax.sql.DataSource)3 InvalidObjectException (java.io.InvalidObjectException)2