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);
}
}
}
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);
}
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());
}
}
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);
}
}
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));
}
}
}
Aggregations