use of org.apache.openejb.core.ivm.naming.ObjectReference in project tomee by apache.
the class JndiBuilder method bind.
public void bind(final EjbJarInfo ejbJarInfo, final BeanContext bean, final EnterpriseBeanInfo beanInfo, final JndiNameStrategy strategy) {
// and we shouldn't need it
if (BeanContext.Comp.class.equals(bean.getBeanClass())) {
return;
}
final Bindings bindings = new Bindings();
bean.set(Bindings.class, bindings);
Reference simpleNameRef = null;
final Object id = bean.getDeploymentID();
try {
if (bean.isLocalbean()) {
final Class beanClass = bean.getBeanClass();
final BeanContext.BusinessLocalBeanHome home = bean.getBusinessLocalBeanHome();
final BusinessLocalBeanReference ref = new BusinessLocalBeanReference(home);
optionalBind(bindings, ref, "openejb/Deployment/" + format(id, 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(id, clazz.getName(), InterfaceType.LOCALBEAN));
}
}
final String internalName = "openejb/Deployment/" + format(id, beanClass.getName(), InterfaceType.BUSINESS_LOCALBEAN_HOME);
bind(internalName, ref, bindings, beanInfo, beanClass);
final 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);
if (USE_OLD_JNDI_NAMES) {
bean.getModuleContext().getAppContext().getBindings().put(name, ref);
}
simpleNameRef = ref;
}
} catch (final NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind business remote deployment in jndi.", e);
}
try {
for (final Class interfce : bean.getBusinessLocalInterfaces()) {
final BeanContext.BusinessLocalHome home = bean.getBusinessLocalHome(interfce);
final BusinessLocalReference ref = new BusinessLocalReference(home);
optionalBind(bindings, ref, "openejb/Deployment/" + format(id, interfce.getName()));
final String internalName = "openejb/Deployment/" + format(id, interfce.getName(), InterfaceType.BUSINESS_LOCAL);
bind(internalName, ref, bindings, beanInfo, interfce);
final String name = strategy.getName(interfce, DEFAULT_NAME_KEY, JndiNameStrategy.Interface.BUSINESS_LOCAL);
final String externalName = "openejb/local/" + name;
bind(externalName, ref, bindings, beanInfo, interfce);
bindJava(bean, interfce, ref, bindings, beanInfo);
if (USE_OLD_JNDI_NAMES) {
bean.getModuleContext().getAppContext().getBindings().put(name, ref);
}
if (simpleNameRef == null) {
simpleNameRef = ref;
}
}
} catch (final NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind business local interface for deployment " + id, e);
}
try {
for (final Class interfce : bean.getBusinessRemoteInterfaces()) {
final BeanContext.BusinessRemoteHome home = bean.getBusinessRemoteHome(interfce);
final BusinessRemoteReference ref = new BusinessRemoteReference(home);
optionalBind(bindings, ref, "openejb/Deployment/" + format(id, interfce.getName(), null));
final String internalName = "openejb/Deployment/" + format(id, interfce.getName(), InterfaceType.BUSINESS_REMOTE);
bind(internalName, ref, bindings, beanInfo, interfce);
final 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);
bind("openejb/remote/" + computeGlobalName(bean, interfce), ref, bindings, beanInfo, interfce);
bindJava(bean, interfce, ref, bindings, beanInfo);
if (USE_OLD_JNDI_NAMES) {
bean.getModuleContext().getAppContext().getBindings().put(name, ref);
}
if (simpleNameRef == null) {
simpleNameRef = ref;
}
}
} catch (final NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind business remote deployment in jndi.", e);
}
try {
final Class localHomeInterface = bean.getLocalHomeInterface();
if (localHomeInterface != null) {
final 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(id, localHomeInterface.getName(), InterfaceType.EJB_LOCAL_HOME));
name = "openejb/Deployment/" + format(id, bean.getLocalInterface().getName());
bind(name, ref, bindings, beanInfo, localHomeInterface);
name = "openejb/Deployment/" + format(id, bean.getLocalInterface().getName(), InterfaceType.EJB_LOCAL);
bind(name, ref, bindings, beanInfo, localHomeInterface);
bindJava(bean, localHomeInterface, ref, bindings, beanInfo);
if (simpleNameRef == null) {
simpleNameRef = ref;
}
}
} catch (final NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind local home interface for deployment " + id, e);
}
try {
final Class homeInterface = bean.getHomeInterface();
if (homeInterface != null) {
final 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(id, homeInterface.getName(), InterfaceType.EJB_HOME));
name = "openejb/Deployment/" + format(id, bean.getRemoteInterface().getName());
bind(name, ref, bindings, beanInfo, homeInterface);
name = "openejb/Deployment/" + format(id, bean.getRemoteInterface().getName(), InterfaceType.EJB_OBJECT);
bind(name, ref, bindings, beanInfo, homeInterface);
bindJava(bean, homeInterface, ref, bindings, beanInfo);
if (simpleNameRef == null) {
simpleNameRef = ref;
}
}
} catch (final 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 (final NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind simple java:global name in jndi", e);
}
try {
if (MessageListener.class.equals(bean.getMdbInterface())) {
final String destinationId = bean.getDestinationId();
final String jndiName = "openejb/Resource/" + destinationId;
final Reference reference = new IntraVmJndiReference(jndiName);
final String deploymentId = id.toString();
bind("openejb/local/" + deploymentId, reference, bindings, beanInfo, MessageListener.class);
bind("openejb/remote/" + deploymentId, reference, bindings, beanInfo, MessageListener.class);
}
} catch (final NamingException e) {
throw new OpenEJBRuntimeException("Unable to bind mdb destination in jndi.", e);
} catch (final NoClassDefFoundError ncdfe) {
// no-op: no jms API
}
}
use of org.apache.openejb.core.ivm.naming.ObjectReference in project tomee by apache.
the class JndiEncBuilder method buildMap.
public Map<String, Object> buildMap(final JndiScope scope) throws OpenEJBException {
// let it be sorted for real binding
final Map<String, Object> bindings = new TreeMap<>();
// get JtaEntityManagerRegistry
final JtaEntityManagerRegistry jtaEntityManagerRegistry = SystemInstance.get().getComponent(JtaEntityManagerRegistry.class);
for (final EjbReferenceInfo referenceInfo : jndiEnc.ejbReferences) {
final Reference reference;
if (referenceInfo.location != null) {
reference = buildReferenceLocation(referenceInfo.location);
} else if (referenceInfo.ejbDeploymentId == null) {
reference = new LazyEjbReference(new Ref(referenceInfo), moduleUri, useCrossClassLoaderRef);
} else {
final String jndiName = "openejb/Deployment/" + JndiBuilder.format(referenceInfo.ejbDeploymentId, referenceInfo.interfaceClassName, referenceInfo.localbean ? InterfaceType.LOCALBEAN : InterfaceType.BUSINESS_REMOTE);
if (useCrossClassLoaderRef && referenceInfo.externalReference) {
reference = new CrossClassLoaderJndiReference(jndiName);
} else {
reference = new IntraVmJndiReference(jndiName);
}
}
bindings.put(normalize(referenceInfo.referenceName), reference);
}
for (final EjbReferenceInfo referenceInfo : jndiEnc.ejbLocalReferences) {
final Reference reference;
if (referenceInfo.location != null) {
reference = buildReferenceLocation(referenceInfo.location);
} else if (referenceInfo.ejbDeploymentId == null) {
reference = new LazyEjbReference(new Ref(referenceInfo), moduleUri, false);
} else {
final String jndiName = "openejb/Deployment/" + JndiBuilder.format(referenceInfo.ejbDeploymentId, referenceInfo.interfaceClassName, referenceInfo.localbean ? InterfaceType.LOCALBEAN : InterfaceType.BUSINESS_LOCAL);
reference = new IntraVmJndiReference(jndiName);
}
bindings.put(normalize(referenceInfo.referenceName), reference);
}
for (final EnvEntryInfo entry : jndiEnc.envEntries) {
if (entry.location != null) {
final Reference reference = buildReferenceLocation(entry.location);
bindings.put(normalize(entry.referenceName), reference);
continue;
}
// It is possible that the value and location are both null, as it is allowed to use @Resource(name="java:global/env/abc") with no value is specified in DD
if (entry.value == null) {
continue;
}
try {
final Class type = Classes.deprimitivize(getType(entry.type, entry));
final Object obj;
if (type == String.class) {
obj = new String(entry.value);
} else if (type == Double.class) {
obj = new Double(entry.value);
} else if (type == Integer.class) {
obj = new Integer(entry.value);
} else if (type == Long.class) {
obj = new Long(entry.value);
} else if (type == Float.class) {
obj = new Float(entry.value);
} else if (type == Short.class) {
obj = new Short(entry.value);
} else if (type == Boolean.class) {
obj = Boolean.valueOf(entry.value);
} else if (type == Byte.class) {
obj = new Byte(entry.value);
} else if (type == Character.class) {
final StringBuilder sb = new StringBuilder(entry.value + " ");
obj = sb.charAt(0);
} else if (type == URL.class) {
obj = new URL(entry.value);
} else if (type == Class.class) {
obj = new ClassReference(entry.value.trim());
} else if (type.isEnum()) {
obj = Enum.valueOf(type, entry.value.trim());
} else {
throw new IllegalArgumentException("Invalid env-entry-type " + type);
}
bindings.put(normalize(entry.referenceName), obj);
} catch (final NumberFormatException e) {
throw new IllegalArgumentException("The env-entry-value for entry " + entry.referenceName + " was not recognizable as type " + entry.type + ". Received Message: " + e.getLocalizedMessage(), e);
} catch (final MalformedURLException e) {
throw new IllegalArgumentException("URL for reference " + entry.referenceName + " was not a valid URL: " + entry.value, e);
}
}
for (final ResourceReferenceInfo referenceInfo : jndiEnc.resourceRefs) {
if (!(referenceInfo instanceof ContextReferenceInfo)) {
if (referenceInfo.location != null) {
final Reference reference = buildReferenceLocation(referenceInfo.location);
bindings.put(normalize(referenceInfo.referenceName), reference);
continue;
}
final Class<?> type = getType(referenceInfo.referenceType, referenceInfo);
final Object reference;
if (URL.class.equals(type)) {
reference = new URLReference(referenceInfo.resourceID);
} else if (type.isAnnotationPresent(ManagedBean.class)) {
final ManagedBean managed = type.getAnnotation(ManagedBean.class);
final String name = managed.value().length() == 0 ? type.getSimpleName() : managed.value();
reference = new LinkRef("module/" + name);
} else if (referenceInfo.resourceID != null) {
final String jndiName = "openejb/Resource/" + referenceInfo.resourceID;
reference = new IntraVmJndiReference(jndiName);
} else {
final String jndiName = "openejb/Resource/" + referenceInfo.referenceName;
reference = new IntraVmJndiReference(jndiName);
}
bindings.put(normalize(referenceInfo.referenceName), reference);
} else {
final Class<?> type = getType(referenceInfo.referenceType, referenceInfo);
final Object reference;
if (Request.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.REQUEST);
} else if (HttpServletRequest.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.HTTP_SERVLET_REQUEST);
} else if (ServletRequest.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.SERVLET_REQUEST);
} else if (UriInfo.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.URI_INFO);
} else if (HttpHeaders.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.HTTP_HEADERS);
} else if (SecurityContext.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.SECURITY_CONTEXT);
} else if (ContextResolver.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.CONTEXT_RESOLVER);
} else if (Providers.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.PROVIDERS);
} else if (ServletConfig.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.SERVLET_CONFIG);
} else if (ServletContext.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.SERVLET_CONTEXT);
} else if (HttpServletResponse.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.HTTP_SERVLET_RESPONSE);
} else if (javax.ws.rs.container.ResourceInfo.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.RESOURCE_INFO);
} else if (ResourceContext.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.RESOURCE_CONTEXT);
} else if (Configuration.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.CONFIGURATION);
} else if (Application.class.equals(type)) {
reference = new ObjectReference(ThreadLocalContextManager.APPLICATION);
} else {
reference = new MapObjectReference(ThreadLocalContextManager.OTHERS, referenceInfo.referenceType);
}
bindings.put(normalize(referenceInfo.referenceName), reference);
}
}
for (final ResourceEnvReferenceInfo referenceInfo : jndiEnc.resourceEnvRefs) {
if (referenceInfo.location != null) {
final Reference reference = buildReferenceLocation(referenceInfo.location);
bindings.put(normalize(referenceInfo.referenceName), reference);
continue;
}
final Class<?> type = getType(referenceInfo.resourceEnvRefType, referenceInfo);
final Object reference;
if (EJBContext.class.isAssignableFrom(type)) {
final String jndiName = "comp/EJBContext";
reference = new LinkRef(jndiName);
// Let the container bind this into JNDI
if (jndiName.equals(referenceInfo.referenceName)) {
continue;
}
} else if (Validator.class.equals(type)) {
final String jndiName = "comp/Validator";
reference = new LinkRef(jndiName);
} else if (ValidatorFactory.class.equals(type)) {
final String jndiName = "comp/ValidatorFactory";
reference = new LinkRef(jndiName);
} else if (WebServiceContext.class.equals(type)) {
final String jndiName = "comp/WebServiceContext";
reference = new LinkRef(jndiName);
} else if (TimerService.class.equals(type)) {
final String jndiName = "comp/TimerService";
reference = new LinkRef(jndiName);
} else if (BeanManager.class.equals(type)) {
reference = new LazyObjectReference<>(new BeanManagerLazyReference());
} else if (UserTransaction.class.equals(type)) {
reference = new IntraVmJndiReference("comp/UserTransaction");
} else if (referenceInfo.resourceID != null) {
final String jndiName = "openejb/Resource/" + referenceInfo.resourceID;
reference = new IntraVmJndiReference(jndiName);
} else {
final String jndiName = "openejb/Resource/" + referenceInfo.referenceName;
reference = new IntraVmJndiReference(jndiName);
}
bindings.put(normalize(referenceInfo.referenceName), reference);
}
for (final PersistenceUnitReferenceInfo referenceInfo : jndiEnc.persistenceUnitRefs) {
if (referenceInfo.location != null) {
final Reference reference = buildReferenceLocation(referenceInfo.location);
bindings.put(normalize(referenceInfo.referenceName), reference);
continue;
}
final String jndiName = PersistenceBuilder.getOpenEJBJndiName(referenceInfo.unitId);
final Reference reference = new IntraVmJndiReference(jndiName);
bindings.put(normalize(referenceInfo.referenceName), reference);
}
for (final PersistenceContextReferenceInfo contextInfo : jndiEnc.persistenceContextRefs) {
if (contextInfo.location != null) {
final Reference reference = buildReferenceLocation(contextInfo.location);
bindings.put(normalize(contextInfo.referenceName), reference);
continue;
}
final Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
final EntityManagerFactory factory;
try {
final String jndiName = PersistenceBuilder.getOpenEJBJndiName(contextInfo.unitId);
factory = (EntityManagerFactory) context.lookup(jndiName);
} catch (final NamingException e) {
throw new OpenEJBException("PersistenceUnit '" + contextInfo.unitId + "' not found for EXTENDED ref '" + contextInfo.referenceName + "'");
}
final JtaEntityManager jtaEntityManager = new JtaEntityManager(contextInfo.persistenceUnitName, jtaEntityManagerRegistry, factory, contextInfo.properties, contextInfo.extended, contextInfo.synchronizationType);
final Reference reference = new PersistenceContextReference(jtaEntityManager);
bindings.put(normalize(contextInfo.referenceName), reference);
}
for (final ServiceReferenceInfo referenceInfo : jndiEnc.serviceRefs) {
if (referenceInfo.location != null) {
final Reference reference = buildReferenceLocation(referenceInfo.location);
bindings.put(normalize(referenceInfo.referenceName), reference);
continue;
}
// load service class which is used to construct the port
Class<? extends Service> serviceClass = Service.class;
if (referenceInfo.serviceType != null) {
try {
serviceClass = classLoader.loadClass(referenceInfo.serviceType).asSubclass(Service.class);
} catch (final Exception e) {
throw new OpenEJBException("Could not load service type class " + referenceInfo.serviceType, e);
}
}
// load the reference class which is the ultimate type of the port
Class<?> referenceClass = null;
if (referenceInfo.referenceType != null) {
try {
referenceClass = classLoader.loadClass(referenceInfo.referenceType);
} catch (final Exception e) {
throw new OpenEJBException("Could not load reference type class " + referenceInfo.referenceType, e);
}
}
// if ref class is a subclass of Service, use it for the service class
if (referenceClass != null && Service.class.isAssignableFrom(referenceClass)) {
serviceClass = referenceClass.asSubclass(Service.class);
}
// determine the location of the wsdl file
URL wsdlUrl = null;
if (referenceInfo.wsdlFile != null) {
try {
wsdlUrl = new URL(referenceInfo.wsdlFile);
} catch (final MalformedURLException e) {
wsdlUrl = classLoader.getResource(referenceInfo.wsdlFile);
if (wsdlUrl == null) {
logger.warning("Error obtaining WSDL: " + referenceInfo.wsdlFile, e);
}
}
}
// port refs
final List<PortRefData> portRefs = new ArrayList<>(referenceInfo.portRefs.size());
for (final PortRefInfo portRefInfo : referenceInfo.portRefs) {
final PortRefData portRef = new PortRefData();
portRef.setQName(portRefInfo.qname);
portRef.setServiceEndpointInterface(portRefInfo.serviceEndpointInterface);
portRef.setEnableMtom(portRefInfo.enableMtom);
portRef.getProperties().putAll(portRefInfo.properties);
portRefs.add(portRef);
}
// create the handle chains
List<HandlerChainData> handlerChains = null;
if (!referenceInfo.handlerChains.isEmpty()) {
handlerChains = WsBuilder.toHandlerChainData(referenceInfo.handlerChains, classLoader);
}
if (!client) {
final Reference reference = new JaxWsServiceReference(referenceInfo.id, referenceInfo.serviceQName, serviceClass, referenceInfo.portQName, referenceClass, wsdlUrl, portRefs, handlerChains, injections, properties);
bindings.put(normalize(referenceInfo.referenceName), reference);
} else {
final ServiceRefData serviceRefData = new ServiceRefData(referenceInfo.id, referenceInfo.serviceQName, serviceClass, referenceInfo.portQName, referenceClass, wsdlUrl, handlerChains, portRefs);
bindings.put(normalize(referenceInfo.referenceName), serviceRefData);
}
}
final OpenEjbConfiguration config = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
if (config != null) {
for (final ResourceInfo resource : config.facilities.resources) {
final String jndiName = resource.jndiName;
if (jndiName != null && !jndiName.isEmpty() && isNotGobalOrIsHoldByThisApp(resource, scope)) {
final String refName = "openejb/Resource/" + resource.id;
final Object reference = new IntraVmJndiReference(refName);
final String boundName = normalize(jndiName);
bindings.put(boundName, reference);
}
}
}
return bindings;
}
Aggregations