use of org.apache.openejb.jee.oejb3.ResourceLink in project tomee by apache.
the class AutoConfig method resolveDestinationLinks.
/**
* Set resource id in all message-destination-refs and MDBs that are using message destination links.
*/
private void resolveDestinationLinks(final AppModule appModule) throws OpenEJBException {
// build up a link resolver
final LinkResolver<MessageDestination> destinationResolver = new LinkResolver<>();
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly != null) {
for (final MessageDestination destination : assembly.getMessageDestination()) {
destinationResolver.add(ejbModule.getModuleUri(), destination.getMessageDestinationName(), destination);
}
}
}
for (final ClientModule clientModule : appModule.getClientModules()) {
for (final MessageDestination destination : clientModule.getApplicationClient().getMessageDestination()) {
destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
}
}
for (final WebModule webModule : appModule.getWebModules()) {
for (final MessageDestination destination : webModule.getWebApp().getMessageDestination()) {
destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
}
}
// remember the type of each destination so we can use it to fillin MDBs that don't declare destination type
final Map<MessageDestination, String> destinationTypes = new HashMap<>();
// if MessageDestination does not have a mapped name assigned, give it the destination from the MDB
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly == null) {
continue;
}
final URI moduleUri = ejbModule.getModuleUri();
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
// MDB destination is deploymentId if none set
if (bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
// skip destination refs without a destination link
final String link = mdb.getMessageDestinationLink();
if (link == null || link.length() == 0) {
continue;
}
// resolve the destination... if we don't find one it is a configuration bug
final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
if (destination == null) {
throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
}
// get the destinationId is the mapped name
String destinationId = destination.getMappedName();
if (destinationId == null) {
// if we don't have a mapped name use the destination of the mdb
final Properties properties = mdb.getActivationConfig().toProperties();
destinationId = properties.getProperty("destination");
destination.setMappedName(destinationId);
}
if (mdb.getMessageDestinationType() != null && !destinationTypes.containsKey(destination)) {
destinationTypes.put(destination, mdb.getMessageDestinationType());
}
// destination identifier
ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
if (resourceLink == null) {
resourceLink = new ResourceLink();
resourceLink.setResRefName("openejb/destination");
ejbDeployment.addResourceLink(resourceLink);
}
resourceLink.setResId(destinationId);
}
}
}
// resolve all message destination refs with links and assign a ref id to the reference
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly == null) {
continue;
}
final URI moduleUri = ejbModule.getModuleUri();
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
// skip destination refs with a resource link already assigned
if (ref.getMappedName() == null && ejbDeployment.getResourceLink(ref.getName()) == null) {
final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
if (destinationId != null) {
// build the link and add it
final ResourceLink resourceLink = new ResourceLink();
resourceLink.setResId(destinationId);
resourceLink.setResRefName(ref.getName());
ejbDeployment.addResourceLink(resourceLink);
}
}
}
}
}
for (final ClientModule clientModule : appModule.getClientModules()) {
final URI moduleUri = clientModule.getModuleUri();
for (final MessageDestinationRef ref : clientModule.getApplicationClient().getMessageDestinationRef()) {
final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
if (destinationId != null) {
// for client modules we put the destinationId in the mapped name
ref.setMappedName(destinationId);
}
}
}
for (final WebModule webModule : appModule.getWebModules()) {
final URI moduleUri = URLs.uri(webModule.getModuleId());
for (final MessageDestinationRef ref : webModule.getWebApp().getMessageDestinationRef()) {
final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
if (destinationId != null) {
// for web modules we put the destinationId in the mapped name
ref.setMappedName(destinationId);
}
}
}
// the info from the destination (which got filled in from the references)
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
if (assembly == null) {
continue;
}
final URI moduleUri = URLs.uri(ejbModule.getModuleId());
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
// MDB destination is deploymentId if none set
if (bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
if (!isJms(mdb)) {
continue;
}
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
// if destination type is already set in, continue
String destinationType = mdb.getMessageDestinationType();
if (destinationType != null) {
continue;
}
final String link = mdb.getMessageDestinationLink();
if (link != null && link.length() != 0) {
// resolve the destination... if we don't find one it is a configuration bug
final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
if (destination == null) {
throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
}
destinationType = destinationTypes.get(destination);
}
if (destinationType == null) {
// couldn't determine type... we'll have to guess
// if destination name contains the string "queue" or "topic" we use that
final Properties properties = mdb.getActivationConfig().toProperties();
final String destination = properties.getProperty("destination").toLowerCase();
if (destination.contains("queue")) {
destinationType = Queue.class.getName();
} else if (destination.contains("topic")) {
destinationType = Topic.class.getName();
} else {
// Queue is the default
destinationType = Queue.class.getName();
}
logger.info("Auto-configuring a message driven bean " + ejbDeployment.getDeploymentId() + " destination " + properties.getProperty("destination") + " to be destinationType " + destinationType);
}
if (destinationType != null) {
mdb.getActivationConfig().addProperty("destinationType", destinationType);
mdb.setMessageDestinationType(destinationType);
// topics need a clientId and subscriptionName
if ("javax.jms.Topic".equals(destinationType)) {
final Properties properties = mdb.getActivationConfig().toProperties();
if (!properties.containsKey("clientId")) {
mdb.getActivationConfig().addProperty("clientId", ejbDeployment.getDeploymentId());
}
if (!properties.containsKey("subscriptionName")) {
mdb.getActivationConfig().addProperty("subscriptionName", ejbDeployment.getDeploymentId() + "_subscription");
}
}
}
}
}
}
}
use of org.apache.openejb.jee.oejb3.ResourceLink in project tomee by apache.
the class AutoConfig method processResourceRef.
private void processResourceRef(final ResourceRef ref, final EjbDeployment ejbDeployment, final AppResources appResources, final EjbModule ejbModule) throws OpenEJBException {
// skip destinations with lookup name
if (ref.getLookupName() != null) {
return;
}
// skip destinations with a global jndi name
final String mappedName = ref.getMappedName() == null ? "" : ref.getMappedName();
if (mappedName.startsWith("jndi:")) {
return;
}
final String refName = ref.getName();
final String refType = getType(ref, ejbModule.getClassLoader());
// skip references such as URLs which are automatically handled by the server
if (ignoredReferenceTypes.contains(refType)) {
final ResourceInfo resourceInfo = configFactory.getResourceInfo(refName.replace("java:", "").replace("comp/env/", ""));
if (resourceInfo != null) {
ref.setMappedName("jndi:" + (resourceInfo.id.startsWith("java:") ? resourceInfo.id : "openejb:Resource/" + resourceInfo.id));
}
return;
}
try {
final Class<?> clazz = ejbModule.getClassLoader().loadClass(refType);
if (clazz.isAnnotationPresent(ManagedBean.class)) {
return;
}
} catch (final Throwable t) {
// no-op
}
try {
ResourceLink link = ejbDeployment.getResourceLink(refName);
if (link == null) {
String id = mappedName.length() == 0 ? ref.getName() : mappedName;
if (id.startsWith("java:")) {
id = id.substring("java:".length());
}
if (id.startsWith("/")) {
id = id.substring(1);
}
try {
final AppModule appModule = ejbModule.getAppModule();
if (appModule != null) {
final String newId = findResourceId(appModule.getModuleId() + '/' + id.replace("java:", "").replaceAll("^comp/env/", ""), refType, new Properties(), appResources);
if (newId != null) {
// app scoped resources, try to find it without creating it first
id = getResourceId(ejbModule.getModuleId(), newId, refType, appResources);
} else {
id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, appResources);
}
} else {
id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, appResources);
}
} catch (final OpenEJBException e) {
// changing the message to be explicit
throw new OpenEJBException("Can't find resource for " + ref.getOrigin() + ". (" + e.getMessage() + ")", e.getCause());
}
logger.info("Auto-linking resource-ref '" + refName + "' in bean " + ejbDeployment.getDeploymentId() + " to Resource(id=" + id + ")");
link = new ResourceLink();
link.setResId(id);
link.setResRefName(refName);
ejbDeployment.addResourceLink(link);
} else {
final String id = getResourceId(ejbDeployment.getDeploymentId(), link.getResId(), refType, appResources);
link.setResId(id);
link.setResRefName(refName);
}
} catch (final OpenEJBException ex) {
if (!(ref instanceof ContextRef)) {
throw ex;
}
}
}
use of org.apache.openejb.jee.oejb3.ResourceLink in project tomee by apache.
the class AutoConfig method deploy.
private void deploy(final EjbModule ejbModule, final AppResources appResources) throws OpenEJBException {
final OpenejbJar openejbJar;
if (ejbModule.getOpenejbJar() != null) {
openejbJar = ejbModule.getOpenejbJar();
} else {
openejbJar = new OpenejbJar();
ejbModule.setOpenejbJar(openejbJar);
}
final Map<String, EjbDeployment> deployments = openejbJar.getDeploymentsByEjbName();
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
final EjbDeployment ejbDeployment = deployments.get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
final String beanType = getType(bean);
final Class<? extends ContainerInfo> containerInfoType = ConfigurationFactory.getContainerInfoType(beanType);
logger.debug("Bean type of bean {0} is {1}", bean.getEjbName(), beanType);
if (ejbDeployment.getContainerId() == null && !skipMdb(bean)) {
logger.debug("Container for bean {0} is not set, looking for a suitable container", bean.getEjbName());
String containerId = getUsableContainer(containerInfoType, bean, appResources);
if (containerId == null) {
logger.debug("Suitable container for bean {0} not found, creating one", bean.getEjbName());
containerId = createContainer(containerInfoType, ejbDeployment, bean);
}
logger.debug("Setting container ID {0} for bean {1}", containerId, bean.getEjbName());
ejbDeployment.setContainerId(containerId);
}
logger.debug("Container ID for bean {0} is {1}", bean.getEjbName(), ejbDeployment.getContainerId());
// create the container if it doesn't exist
final List<String> containerIds = configFactory.getContainerIds();
final Collection<ContainerInfo> containerInfos = appResources.getContainerInfos();
for (final ContainerInfo containerInfo : containerInfos) {
containerIds.add(containerInfo.id);
}
if (!containerIds.contains(ejbDeployment.getContainerId()) && !skipMdb(bean)) {
logger.debug("Desired container {0} not found. Containers available: {1}. Creating a new container.", ejbDeployment.getContainerId(), Join.join(", ", containerIds));
createContainer(containerInfoType, ejbDeployment, bean);
}
// Resource reference
for (final ResourceRef ref : bean.getResourceRef()) {
processResourceRef(ref, ejbDeployment, appResources, ejbModule);
}
// Resource env reference
for (final JndiReference ref : bean.getResourceEnvRef()) {
processResourceEnvRef(ref, ejbDeployment, appResources, ejbModule.getClassLoader());
}
// Message destination reference
for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
processResourceEnvRef(ref, ejbDeployment, appResources, ejbModule.getClassLoader());
}
// mdb message destination id
if (autoCreateResources && bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
final ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
if (resourceLink != null) {
try {
final String destinationId = getResourceEnvId(bean.getEjbName(), resourceLink.getResId(), mdb.getMessageDestinationType(), appResources);
resourceLink.setResId(destinationId);
} catch (final OpenEJBException e) {
// The MDB doesn't need the auto configured "openejb/destination" env entry
ejbDeployment.removeResourceLink("openejb/destination");
}
}
}
}
}
use of org.apache.openejb.jee.oejb3.ResourceLink in project tomee by apache.
the class ApplyOpenejbJar method deploy.
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
for (final EjbModule ejbModule : appModule.getEjbModules()) {
final Map<String, EjbDeployment> ejbDeployments = ejbModule.getOpenejbJar().getDeploymentsByEjbName();
for (final EnterpriseBean enterpriseBean : ejbModule.getEjbJar().getEnterpriseBeans()) {
// Get the OpenEJB deployment from openejb-jar.xml
final EjbDeployment ejbDeployment = ejbDeployments.get(enterpriseBean.getEjbName());
enterpriseBean.setId(ejbDeployment.getDeploymentId());
for (final ResourceRef ref : enterpriseBean.getResourceRef()) {
final ResourceLink resourceLink = ejbDeployment.getResourceLink(ref.getName());
if (resourceLink != null && resourceLink.getResId() != null) /* don't overwrite with null */
{
ref.setMappedName(resourceLink.getResId());
}
}
for (final ResourceEnvRef ref : enterpriseBean.getResourceEnvRef()) {
final ResourceLink resourceLink = ejbDeployment.getResourceLink(ref.getName());
if (resourceLink != null && resourceLink.getResId() != null) /* don't overwrite with null */
{
ref.setMappedName(resourceLink.getResId());
}
}
for (final MessageDestinationRef ref : enterpriseBean.getMessageDestinationRef()) {
final ResourceLink resourceLink = ejbDeployment.getResourceLink(ref.getName());
if (resourceLink != null && resourceLink.getResId() != null) /* don't overwrite with null */
{
ref.setMappedName(resourceLink.getResId());
}
}
for (final EjbRef ref : enterpriseBean.getEjbRef()) {
final EjbLink ejbLink = ejbDeployment.getEjbLink(ref.getName());
if (ejbLink != null && ejbLink.getDeployentId() != null) /* don't overwrite with null */
{
ref.setMappedName(ejbLink.getDeployentId());
}
}
for (final EjbLocalRef ref : enterpriseBean.getEjbLocalRef()) {
final EjbLink ejbLink = ejbDeployment.getEjbLink(ref.getName());
if (ejbLink != null && ejbLink.getDeployentId() != null) /* don't overwrite with null */
{
ref.setMappedName(ejbLink.getDeployentId());
}
}
}
}
return appModule;
}
use of org.apache.openejb.jee.oejb3.ResourceLink in project tomee by apache.
the class EjbJarInfoBuilder method initMessageBean.
private EnterpriseBeanInfo initMessageBean(final MessageDrivenBean mdb, final Map m) throws OpenEJBException {
final MessageDrivenBeanInfo bean = new MessageDrivenBeanInfo();
bean.timeoutMethod = toInfo(mdb.getTimeoutMethod());
copyCallbacks(mdb.getAroundTimeout(), bean.aroundTimeout);
copyCallbacks(mdb.getAroundInvoke(), bean.aroundInvoke);
copyCallbacks(mdb.getPostConstruct(), bean.postConstruct);
copyCallbacks(mdb.getPreDestroy(), bean.preDestroy);
copySchedules(mdb.getTimer(), bean.methodScheduleInfos);
final EjbDeployment d = (EjbDeployment) m.get(mdb.getEjbName());
if (d == null) {
throw new OpenEJBException("No deployment information in openejb-jar.xml for bean " + mdb.getEjbName() + ". Please redeploy the jar");
}
bean.ejbDeploymentId = d.getDeploymentId();
bean.containerId = d.getContainerId();
final Icon icon = mdb.getIcon();
bean.largeIcon = icon == null ? null : icon.getLargeIcon();
bean.smallIcon = icon == null ? null : icon.getSmallIcon();
bean.description = mdb.getDescription();
bean.displayName = mdb.getDisplayName();
bean.ejbClass = mdb.getEjbClass();
bean.ejbName = mdb.getEjbName();
final TransactionType txType = mdb.getTransactionType();
bean.transactionType = txType != null ? txType.toString() : TransactionType.CONTAINER.toString();
bean.properties.putAll(d.getProperties());
if (mdb.getMessagingType() != null) {
bean.mdbInterface = mdb.getMessagingType();
} else {
bean.mdbInterface = "javax.jms.MessageListener";
}
final ResourceLink resourceLink = d.getResourceLink("openejb/destination");
if (resourceLink != null) {
bean.destinationId = resourceLink.getResId();
}
if (mdb.getMessageDestinationType() != null) {
bean.activationProperties.put("destinationType", mdb.getMessageDestinationType());
}
final ActivationConfig activationConfig = mdb.getActivationConfig();
if (activationConfig != null) {
for (final ActivationConfigProperty property : activationConfig.getActivationConfigProperty()) {
final String name = property.getActivationConfigPropertyName();
final String value = property.getActivationConfigPropertyValue();
bean.activationProperties.put(name, value);
}
}
return bean;
}
Aggregations