use of javax.naming.NameAlreadyBoundException in project tomee by apache.
the class JndiBuilder method bind.
private void bind(final String name, final Reference ref, final Bindings bindings, final EnterpriseBeanInfo beanInfo, final Class intrface) throws NamingException {
if (name.startsWith("openejb/local/") || name.startsWith("openejb/remote/") || name.startsWith("openejb/localbean/") || name.startsWith("openejb/global/")) {
final String externalName = name.replaceFirst("openejb/[^/]+/", "");
if (bindings.contains(name)) {
// We bind under two sections of jndi, only warn once.. the user doesn't need to be bothered with that detail
if (name.startsWith("openejb/local/")) {
logger.debug("Duplicate: Jndi(name=" + externalName + ")");
}
return;
}
try {
openejbContext.bind(name, ref);
bindings.add(name);
if (!beanInfo.jndiNames.contains(externalName)) {
beanInfo.jndiNames.add(externalName);
final JndiNameInfo nameInfo = new JndiNameInfo();
nameInfo.intrface = intrface == null ? null : intrface.getName();
nameInfo.name = externalName;
beanInfo.jndiNamess.add(nameInfo);
if (!embeddedEjbContainerApi && // filtering internal bean
!(beanInfo instanceof ManagedBeanInfo && ((ManagedBeanInfo) beanInfo).hidden)) {
logger.info("Jndi(name=" + externalName + ") --> Ejb(deployment-id=" + beanInfo.ejbDeploymentId + ")");
}
}
} catch (final NameAlreadyBoundException e) {
final BeanContext deployment = findNameOwner(name);
if (deployment != null) {
logger.error("Jndi(name=" + externalName + ") cannot be bound to Ejb(deployment-id=" + beanInfo.ejbDeploymentId + "). Name already taken by Ejb(deployment-id=" + deployment.getDeploymentID() + ")");
} else {
logger.error("Jndi(name=" + externalName + ") cannot be bound to Ejb(deployment-id=" + beanInfo.ejbDeploymentId + "). Name already taken by another object in the system.");
}
// the name in the exception that it throws
if (failOnCollision) {
throw new NameAlreadyBoundException(externalName);
}
}
} else {
try {
openejbContext.bind(name, ref);
logger.debug("bound ejb at name: " + name + ", ref: " + ref);
bindings.add(name);
} catch (final NameAlreadyBoundException e) {
logger.error("Jndi name could not be bound; it may be taken by another ejb. Jndi(name=" + name + ")");
// the name in the exception that it throws
throw new NameAlreadyBoundException(name);
}
}
}
use of javax.naming.NameAlreadyBoundException in project tomee by apache.
the class JndiBuilder method bindJava.
private void bindJava(final BeanContext cdi, final Class intrface, final Reference ref, final Bindings bindings, final EnterpriseBeanInfo beanInfo) throws NamingException {
final ModuleContext module = cdi.getModuleContext();
final AppContext application = module.getAppContext();
final Context moduleContext = module.getModuleJndiContext();
final Context appContext = application.getAppJndiContext();
final Context globalContext = application.getGlobalJndiContext();
final String appName = application.isStandaloneModule() ? "" : application.getId() + "/";
String moduleName = moduleName(cdi);
if (moduleName.startsWith("/")) {
moduleName = moduleName.substring(1);
}
String beanName = cdi.getEjbName();
if (intrface != null) {
beanName = beanName + "!" + intrface.getName();
}
final String globalName = "global/" + appName + moduleName + beanName;
try {
if (embeddedEjbContainerApi && !(beanInfo instanceof ManagedBeanInfo && ((ManagedBeanInfo) beanInfo).hidden)) {
logger.info(String.format("Jndi(name=\"java:%s\")", globalName));
}
globalContext.bind(globalName, ref);
application.getBindings().put(globalName, ref);
bind("openejb/global/" + globalName, ref, bindings, beanInfo, intrface);
} catch (final NameAlreadyBoundException e) {
// one interface in more than one role (e.g. both Local and Remote
return;
}
appContext.bind("app/" + moduleName + beanName, ref);
application.getBindings().put("app/" + moduleName + beanName, ref);
final String moduleJndi = "module/" + beanName;
moduleContext.bind(moduleJndi, ref);
// contextual if the same ejb (api) is deployed in 2 wars of an ear
ContextualEjbLookup contextual = ContextualEjbLookup.class.cast(application.getBindings().get(moduleJndi));
if (contextual == null) {
final Map<BeanContext, Object> potentials = new HashMap<>();
contextual = new ContextualEjbLookup(potentials, ref);
// TODO: we shouldn't do it but use web bindings
application.getBindings().put(moduleJndi, contextual);
}
contextual.potentials.put(cdi, ref);
}
use of javax.naming.NameAlreadyBoundException in project Payara by payara.
the class FileDirContext method bind.
/**
* Binds a name to an object, along with associated attributes. If attrs
* is null, the resulting binding will have the attributes associated
* with obj if obj is a DirContext, and no attributes otherwise. If attrs
* is non-null, the resulting binding will have attrs as its attributes;
* any attributes associated with obj are ignored.
*
* @param name the name to bind; may not be empty
* @param obj the object to bind; possibly null
* @param attrs the attributes to associate with the binding
* @exception NameAlreadyBoundException if name is already bound
* @exception javax.naming.directory.InvalidAttributesException if some
* "mandatory" attributes of the binding are not supplied
* @exception NamingException if a naming exception is encountered
*/
public void bind(String name, Object obj, Attributes attrs) throws NamingException {
// Note: No custom attributes allowed
File file = new File(base, name);
if (file.exists())
throw new NameAlreadyBoundException(MessageFormat.format(rb.getString(LogFacade.RESOURCES_ALREADY_BOUND), name));
rebind(file, obj, attrs);
}
use of javax.naming.NameAlreadyBoundException in project Payara by payara.
the class GlassfishNamingManagerImpl method createSubContexts.
/**
* Create any sub-contexts in name that don't already exist.
*
* @param name Name containing sub-contexts to create
* @param rootCtx in which sub-contexts should be created
* @throws Exception
*/
private void createSubContexts(Name name, Context rootCtx) throws NamingException {
int numSubContexts = name.size() - 1;
Context currentCtx = rootCtx;
for (int subCtxIndex = 0; subCtxIndex < numSubContexts; subCtxIndex++) {
String subCtxName = name.get(subCtxIndex);
try {
Object obj = currentCtx.lookup(subCtxName);
if (obj == null) {
// Doesn't exist so create it.
currentCtx = currentCtx.createSubcontext(subCtxName);
} else if (obj instanceof Context) {
// OK -- no need to create it.
currentCtx = (Context) obj;
} else {
// Context name clashes with existing object.
throw new NameAlreadyBoundException(subCtxName);
}
} catch (NameNotFoundException e) {
// Doesn't exist so create it.
currentCtx = currentCtx.createSubcontext(subCtxName);
}
}
// End for -- each sub-context
}
use of javax.naming.NameAlreadyBoundException in project jspwiki by apache.
the class JDBCGroupDatabaseTest method setUp.
/**
*/
@Before
public void setUp() throws Exception {
m_hu.setUp();
Properties props = TestEngine.getTestProperties();
WikiEngine engine = new TestEngine(props);
m_wiki = engine.getApplicationName();
// Set up the mock JNDI initial context
TestJNDIContext.initialize();
Context initCtx = new InitialContext();
try {
initCtx.bind("java:comp/env", new TestJNDIContext());
} catch (NameAlreadyBoundException e) {
// ignore
}
Context ctx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = new TestJDBCDataSource(new File("target/test-classes/jspwiki-custom.properties"));
ctx.bind(JDBCGroupDatabase.DEFAULT_GROUPDB_DATASOURCE, ds);
try {
m_conn = ds.getConnection();
} catch (SQLException e) {
Assert.fail("Looks like your database could not be connected to - " + "please make sure that you have started your database, exception: " + e.getMessage());
}
// Initialize the user database
m_db = new JDBCGroupDatabase();
m_db.initialize(engine, new Properties());
}
Aggregations