public void bind(EjbJarInfo ejbJarInfo, BeanContext bean, EnterpriseBeanInfo beanInfo, JndiNameStrategy strategy) {
Bindings bindings = new Bindings();
bean.set(Bindings.class, bindings);
Reference simpleNameRef = null;
Object id = bean.getDeploymentID();
// Our openejb.jndiname.format concept works such that there doesn't need to be one explicit jndi name
// for each view that the bean may offer. If the user configured a name that results in few possible
// jndi names than views, this is ok. The 'optionalBind' method will do its best and log the results.
// This openejb.jndiname.format affects only the OpenEJB-specific global jndi tree.
//
// Should there be a so described "deficit" of names, we give precedence to the most universal and local first
// Essentially this:
// 1. Local Bean view as it implements all business interfaces of the bean, local or remote
// 2. The business local view -- "the" is applicable as create proxies with all possible local interfaces
// 3. The business remote view -- same note on "the" as above
// 4. The EJBLocalHome
// 5. The EJBHome
//
// This ordering also has an affect on which view wins the "java:global/{app}/{module}/{ejbName}" jndi name.
// In the case that the bean has just one view, the name refers to that view. Otherwise, the name is unspecified
try {
if (bean.isLocalbean()) {
Class beanClass = bean.getBeanClass();
BeanContext.BusinessLocalBeanHome home = bean.getBusinessLocalBeanHome();
BusinessLocalBeanReference ref = new BusinessLocalBeanReference(home);
optionalBind(bindings, ref, "openejb/Deployment/" + format(bean.getDeploymentID(), beanClass.getName(), InterfaceType.LOCALBEAN));
// if the user inject the EJB using a parent class
if (!bean.getBeanClass().isInterface()) {
for(Class<?> clazz = bean.getBeanClass().getSuperclass(); !clazz.equals(Object.class); clazz = clazz.getSuperclass()) {
optionalBind(bindings, ref, "openejb/Deployment/" + format(bean.getDeploymentID(), clazz.getName(), InterfaceType.LOCALBEAN));
}
}
String internalName = "openejb/Deployment/" + format(bean.getDeploymentID(), beanClass.getName(), InterfaceType.BUSINESS_LOCALBEAN_HOME);
bind(internalName, ref, bindings, beanInfo, beanClass);
String name = strategy.getName(beanClass, DEFAULT_NAME_KEY, JndiNameStrategy.Interface.LOCALBEAN);
bind("openejb/local/" + name, ref, bindings, beanInfo, beanClass);
bindJava(bean, beanClass, ref, bindings, beanInfo);
simpleNameRef = ref;
}
} catch (NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind business remote deployment in jndi.", e);
}
try {
for (Class interfce : bean.getBusinessLocalInterfaces()) {
BeanContext.BusinessLocalHome home = bean.getBusinessLocalHome(interfce);
BusinessLocalReference ref = new BusinessLocalReference(home);
optionalBind(bindings, ref, "openejb/Deployment/" + format(bean.getDeploymentID(), interfce.getName()));
String internalName = "openejb/Deployment/" + format(bean.getDeploymentID(), interfce.getName(), InterfaceType.BUSINESS_LOCAL);
bind(internalName, ref, bindings, beanInfo, interfce);
String externalName = "openejb/local/" + strategy.getName(interfce, DEFAULT_NAME_KEY, JndiNameStrategy.Interface.BUSINESS_LOCAL);
bind(externalName, ref, bindings, beanInfo, interfce);
bindJava(bean, interfce, ref, bindings, beanInfo);
if (simpleNameRef == null) simpleNameRef = ref;
}
} catch (NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind business local interface for deployment " + id, e);
}
try {
for (Class interfce : bean.getBusinessRemoteInterfaces()) {
BeanContext.BusinessRemoteHome home = bean.getBusinessRemoteHome(interfce);
BusinessRemoteReference ref = new BusinessRemoteReference(home);
optionalBind(bindings, ref, "openejb/Deployment/" + format(bean.getDeploymentID(), interfce.getName(), null));
String internalName = "openejb/Deployment/" + format(bean.getDeploymentID(), interfce.getName(), InterfaceType.BUSINESS_REMOTE);
bind(internalName, ref, bindings, beanInfo, interfce);
String name = strategy.getName(interfce, DEFAULT_NAME_KEY, JndiNameStrategy.Interface.BUSINESS_REMOTE);
bind("openejb/local/" + name, ref, bindings, beanInfo, interfce);
bind("openejb/remote/" + name, ref, bindings, beanInfo, interfce);
bindJava(bean, interfce, ref, bindings, beanInfo);
if (simpleNameRef == null) simpleNameRef = ref;
}
} catch (NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind business remote deployment in jndi.", e);
}
try {
Class localHomeInterface = bean.getLocalHomeInterface();
if (localHomeInterface != null) {
ObjectReference ref = new ObjectReference(bean.getEJBLocalHome());
String name = strategy.getName(bean.getLocalHomeInterface(), DEFAULT_NAME_KEY, JndiNameStrategy.Interface.LOCAL_HOME);
bind("openejb/local/" + name, ref, bindings, beanInfo, localHomeInterface);
optionalBind(bindings, ref, "openejb/Deployment/" + format(bean.getDeploymentID(), localHomeInterface.getName(), InterfaceType.EJB_LOCAL_HOME));
name = "openejb/Deployment/" + format(bean.getDeploymentID(), bean.getLocalInterface().getName());
bind(name, ref, bindings, beanInfo, localHomeInterface);
name = "openejb/Deployment/" + format(bean.getDeploymentID(), bean.getLocalInterface().getName(), InterfaceType.EJB_LOCAL);
bind(name, ref, bindings, beanInfo, localHomeInterface);
bindJava(bean, localHomeInterface, ref, bindings, beanInfo);
if (simpleNameRef == null) simpleNameRef = ref;
}
} catch (NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind local home interface for deployment " + id, e);
}
try {
Class homeInterface = bean.getHomeInterface();
if (homeInterface != null) {
ObjectReference ref = new ObjectReference(bean.getEJBHome());
String name = strategy.getName(homeInterface, DEFAULT_NAME_KEY, JndiNameStrategy.Interface.REMOTE_HOME);
bind("openejb/local/" + name, ref, bindings, beanInfo, homeInterface);
bind("openejb/remote/" + name, ref, bindings, beanInfo, homeInterface);
optionalBind(bindings, ref, "openejb/Deployment/" + format(bean.getDeploymentID(), homeInterface.getName(), InterfaceType.EJB_HOME));
name = "openejb/Deployment/" + format(bean.getDeploymentID(), bean.getRemoteInterface().getName());
bind(name, ref, bindings, beanInfo, homeInterface);
name = "openejb/Deployment/" + format(bean.getDeploymentID(), bean.getRemoteInterface().getName(), InterfaceType.EJB_OBJECT);
bind(name, ref, bindings, beanInfo, homeInterface);
bindJava(bean, homeInterface, ref, bindings, beanInfo);
if (simpleNameRef == null) simpleNameRef = ref;
}
} catch (NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind remote home interface for deployment " + id, e);
}
try {
if (simpleNameRef != null) {
bindJava(bean, null, simpleNameRef, bindings, beanInfo);
}
} catch (NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind simple java:global name in jndi", e);
}
try {
if (MessageListener.class.equals(bean.getMdbInterface())) {
String destinationId = bean.getDestinationId();
String jndiName = "openejb/Resource/" + destinationId;
Reference reference = new IntraVmJndiReference(jndiName);
String deploymentId = bean.getDeploymentID().toString();
bind("openejb/local/" + deploymentId, reference, bindings, beanInfo, MessageListener.class);
bind("openejb/remote/" + deploymentId, reference, bindings, beanInfo, MessageListener.class);
}