use of org.apache.openejb.jee.MessageDrivenBean$JAXB.readMessageDrivenBean in project tomee by apache.
the class NoMessageDeliveryTest method test.
public void test() throws Exception {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
// Setup the descriptor information
EmailBean.lifecycle.clear();
final AppModule app = new AppModule(this.getClass().getClassLoader(), "testapp");
final Connector connector = new Connector("email-ra");
final ResourceAdapter adapter = new ResourceAdapter(EmailResourceAdapter.class);
connector.setResourceAdapter(adapter);
final InboundResourceadapter inbound = adapter.setInboundResourceAdapter(new InboundResourceadapter());
final MessageAdapter messageAdapter = inbound.setMessageAdapter(new MessageAdapter());
final MessageListener listener = messageAdapter.addMessageListener(new MessageListener(EmailConsumer.class, EmailAccountInfo.class));
listener.getActivationSpec().addRequiredConfigProperty("address");
app.getConnectorModules().add(new ConnectorModule(connector));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new MessageDrivenBean(EmailBean.class));
app.getEjbModules().add(new EjbModule(ejbJar));
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
final InitialContext initialContext = new InitialContext();
final EmailResourceAdapter ra = (EmailResourceAdapter) initialContext.lookup("java:openejb/Resource/email-raRA");
final Properties headers = new Properties();
headers.put("To", "dblevins@apache.org");
headers.put("From", "dblevins@visi.com");
headers.put("Subject", "Hello");
ra.deliverEmail(headers, "How's it going?");
final Stack<Lifecycle> lifecycle = EmailBean.lifecycle;
final List expected = Arrays.asList(Lifecycle.CONSTRUCTOR, Lifecycle.INJECTION, Lifecycle.POST_CONSTRUCT);
Assert.assertEquals(Join.join("\n", expected), Join.join("\n", lifecycle));
}
use of org.apache.openejb.jee.MessageDrivenBean$JAXB.readMessageDrivenBean in project tomee by apache.
the class EjbJarInfoBuilder method buildInfo.
public EjbJarInfo buildInfo(final EjbModule jar) throws OpenEJBException {
deploymentIds.clear();
securityRoles.clear();
final Map<String, EjbDeployment> ejbds = jar.getOpenejbJar().getDeploymentsByEjbName();
final int beansDeployed = jar.getOpenejbJar().getEjbDeploymentCount();
final int beansInEjbJar = jar.getEjbJar().getEnterpriseBeans().length;
if (beansInEjbJar != beansDeployed) {
for (final EnterpriseBean bean : jar.getEjbJar().getEnterpriseBeans()) {
if (!ejbds.containsKey(bean.getEjbName())) {
ConfigUtils.logger.warning("conf.0018", bean.getEjbName(), jar.getJarLocation());
}
}
final String message = messages.format("conf.0008", jar.getJarLocation(), String.valueOf(beansInEjbJar), String.valueOf(beansDeployed));
logger.warning(message);
throw new OpenEJBException(message);
}
final Map<String, EnterpriseBeanInfo> infos = new HashMap<>();
final Map<String, EnterpriseBean> items = new HashMap<>();
final EjbJarInfo ejbJar = new EjbJarInfo();
ejbJar.path = jar.getJarLocation();
ejbJar.moduleUri = jar.getModuleUri();
ejbJar.moduleId = jar.getModuleId();
if (jar.getEjbJar() != null && jar.getEjbJar().getModuleName() != null) {
ejbJar.moduleName = jar.getEjbJar().getModuleName();
} else {
ejbJar.moduleName = jar.getModuleId();
}
ejbJar.watchedResources.addAll(jar.getWatchedResources());
ejbJar.properties.putAll(jar.getProperties());
ejbJar.properties.putAll(jar.getOpenejbJar().getProperties());
for (final EnterpriseBean bean : jar.getEjbJar().getEnterpriseBeans()) {
final EnterpriseBeanInfo beanInfo;
if (bean instanceof SessionBean) {
beanInfo = initSessionBean((SessionBean) bean, ejbJar, ejbds);
} else if (bean instanceof EntityBean) {
beanInfo = initEntityBean((EntityBean) bean, ejbds);
} else if (bean instanceof MessageDrivenBean) {
beanInfo = initMessageBean((MessageDrivenBean) bean, ejbds);
} else {
throw new OpenEJBException("Unknown bean type: " + bean.getClass().getName());
}
ejbJar.enterpriseBeans.add(beanInfo);
if (deploymentIds.contains(beanInfo.ejbDeploymentId)) {
final String message = messages.format("conf.0100", beanInfo.ejbDeploymentId, jar.getJarLocation(), beanInfo.ejbName);
logger.warning(message);
throw new OpenEJBException(message);
}
deploymentIds.add(beanInfo.ejbDeploymentId);
beanInfo.codebase = jar.getJarLocation();
infos.put(beanInfo.ejbName, beanInfo);
items.put(beanInfo.ejbName, bean);
if (bean.getSecurityIdentity() != null) {
beanInfo.runAs = bean.getSecurityIdentity().getRunAs();
final EjbDeployment deployment = ejbds.get(beanInfo.ejbName);
if (deployment != null) {
for (final RoleMapping mapping : deployment.getRoleMapping()) {
if (mapping.getRoleName().equals(beanInfo.runAs)) {
beanInfo.runAsUser = mapping.getPrincipalName();
break;
}
}
}
}
initJndiNames(ejbds, beanInfo);
}
if (jar.getEjbJar().getAssemblyDescriptor() != null) {
initInterceptors(jar, ejbJar);
initSecurityRoles(jar, ejbJar);
initMethodPermissions(jar, ejbds, ejbJar);
initExcludesList(jar, ejbds, ejbJar);
initMethodTransactions(jar, ejbds, ejbJar);
initMethodConcurrency(jar, ejbds, ejbJar);
initApplicationExceptions(jar, ejbJar);
for (final EnterpriseBeanInfo bean : ejbJar.enterpriseBeans) {
resolveRoleLinks(bean, items.get(bean.ejbName));
}
}
if (jar.getEjbJar().getRelationships() != null) {
initRelationships(jar, infos);
}
final Beans beans = jar.getBeans();
if (beans != null) {
ejbJar.beans = new BeansInfo();
ejbJar.beans.version = beans.getVersion();
ejbJar.beans.discoveryMode = beans.getBeanDiscoveryMode();
if (beans.getScan() != null) {
for (final Beans.Scan.Exclude exclude : beans.getScan().getExclude()) {
final ExclusionInfo exclusionInfo = new ExclusionInfo();
for (final Object config : exclude.getIfClassAvailableOrIfClassNotAvailableOrIfSystemProperty()) {
if (Beans.Scan.Exclude.IfAvailableClassCondition.class.isInstance(config)) {
exclusionInfo.availableClasses.add(Beans.Scan.Exclude.ClassCondition.class.cast(config).getName());
} else if (Beans.Scan.Exclude.IfNotAvailableClassCondition.class.isInstance(config)) {
exclusionInfo.notAvailableClasses.add(Beans.Scan.Exclude.ClassCondition.class.cast(config).getName());
} else if (Beans.Scan.Exclude.IfSystemProperty.class.isInstance(config)) {
final Beans.Scan.Exclude.IfSystemProperty systemProperty = Beans.Scan.Exclude.IfSystemProperty.class.cast(config);
if (systemProperty.getValue() == null) {
exclusionInfo.systemPropertiesPresence.add(systemProperty.getName());
} else {
exclusionInfo.systemProperties.put(systemProperty.getName(), systemProperty.getValue());
}
} else {
throw new IllegalArgumentException("Not supported: " + config);
}
}
final BeansInfo.ExclusionEntryInfo exclusionEntryInfo = new BeansInfo.ExclusionEntryInfo();
exclusionEntryInfo.name = exclude.getName();
exclusionEntryInfo.exclusion = exclusionInfo;
ejbJar.beans.excludes.add(exclusionEntryInfo);
}
}
ejbJar.beans.duplicatedAlternativeClasses.addAll(beans.getDuplicatedAlternatives().getClasses());
ejbJar.beans.duplicatedAlternativeStereotypes.addAll(beans.getDuplicatedAlternatives().getStereotypes());
ejbJar.beans.duplicatedInterceptors.addAll(beans.getDuplicatedInterceptors());
ejbJar.beans.duplicatedDecorators.addAll(beans.getDuplicatedDecorators());
ejbJar.beans.startupClasses.addAll(beans.getStartupBeans());
final Map<URL, String> discoveryModeByUrl = new HashMap<>();
final CompositeBeans composite;
final boolean isComposite = CompositeBeans.class.isInstance(beans);
if (isComposite) {
composite = CompositeBeans.class.cast(beans);
discoveryModeByUrl.putAll(composite.getDiscoveryByUrl());
} else {
composite = null;
URL key = DEFAULT_BEANS_XML_KEY;
if (beans.getUri() != null) {
try {
key = new URL(beans.getUri());
} catch (final MalformedURLException e) {
// no-op
}
}
discoveryModeByUrl.put(key, beans.getBeanDiscoveryMode());
}
for (final Map.Entry<URL, List<String>> next : beans.getManagedClasses().entrySet()) {
final URL key = next.getKey();
final BeansInfo.BDAInfo bdaInfo = new BeansInfo.BDAInfo();
bdaInfo.discoveryMode = discoveryModeByUrl.get(key);
merge(composite, key == null ? DEFAULT_BEANS_XML_KEY : key, bdaInfo, next.getValue());
ejbJar.beans.bdas.add(bdaInfo);
}
for (final Map.Entry<URL, List<String>> next : beans.getNotManagedClasses().entrySet()) {
final URL key = next.getKey();
final BeansInfo.BDAInfo bdaInfo = new BeansInfo.BDAInfo();
bdaInfo.discoveryMode = BeanArchiveService.BeanDiscoveryMode.ANNOTATED.name();
merge(composite, key == null ? DEFAULT_BEANS_XML_KEY : key, bdaInfo, next.getValue());
ejbJar.beans.noDescriptorBdas.add(bdaInfo);
}
// app composer case mainly,we should really not use it anywhere else
if (composite == null && ejbJar.beans.bdas.size() == 1) {
final BeansInfo.BDAInfo bda = ejbJar.beans.bdas.iterator().next();
bda.alternatives.addAll(beans.getAlternativeClasses());
bda.interceptors.addAll(beans.getInterceptors());
bda.decorators.addAll(beans.getDecorators());
bda.stereotypeAlternatives.addAll(beans.getAlternativeStereotypes());
}
}
return ejbJar;
}
use of org.apache.openejb.jee.MessageDrivenBean$JAXB.readMessageDrivenBean in project tomee by apache.
the class AutoConfig method getUsableContainer.
private String getUsableContainer(final Class<? extends ContainerInfo> containerInfoType, final EnterpriseBean bean, final AppResources appResources) {
if (logger.isDebugEnabled()) {
logger.debug("Searching for usable container for bean: {0}. Available application containers: {1}, available system containers {2}", bean.getEjbName(), getContainerIds(appResources.getContainerInfos()), getContainerIds(configFactory.getContainerInfos()));
}
if (MessageDrivenBean.class.isInstance(bean)) {
final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
final String messagingType = messageDrivenBean.getMessagingType();
final List<String> containerIds = appResources.containerIdsByType.get(messagingType);
if (logger.isDebugEnabled()) {
logger.debug("Searching for usable container for bean: {0} by messaging type: {1}. Potential application containers: {2}", bean.getEjbName(), messagingType, containerIds == null ? "" : Join.join(",", containerIds));
}
if (containerIds != null && !containerIds.isEmpty()) {
if (logger.isDebugEnabled()) {
logger.debug("Returning first application container matching by type: {0} - {1}", messagingType, containerIds.get(0));
}
return containerIds.get(0);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Attempting to find a matching container for bean: {0} from application containers {1}", bean.getEjbName(), getContainerIds(appResources.getContainerInfos()));
}
String containerInfo = matchContainer(containerInfoType, bean, appResources.getContainerInfos());
if (containerInfo == null) {
if (logger.isDebugEnabled()) {
logger.debug("Matching application container not found. Attempting to find a matching container for bean: {0} from system containers {1}", bean.getEjbName(), getContainerIds(appResources.getContainerInfos()));
}
containerInfo = matchContainer(containerInfoType, bean, configFactory.getContainerInfos());
}
if (containerInfo != null) {
if (logger.isDebugEnabled()) {
logger.debug("Using container {0} for bean {1}", containerInfo, bean.getEjbName());
}
return containerInfo;
}
if (logger.isDebugEnabled()) {
logger.debug("No suitable existing container found for bean {0}", bean.getEjbName());
}
return null;
}
use of org.apache.openejb.jee.MessageDrivenBean$JAXB.readMessageDrivenBean in project tomee by apache.
the class AutoConfig method processActivationConfig.
/**
* Set destination, destinationType, clientId and subscriptionName in the MDB activation config.
*/
private void processActivationConfig(final EjbModule ejbModule) 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()) {
if (bean instanceof MessageDrivenBean) {
final MessageDrivenBean mdb = (MessageDrivenBean) bean;
if (mdb.getActivationConfig() == null) {
mdb.setActivationConfig(new ActivationConfig());
}
if (!isJms(mdb)) {
continue;
}
final EjbDeployment ejbDeployment = deployments.get(bean.getEjbName());
if (ejbDeployment == null) {
throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
}
final Properties properties = mdb.getActivationConfig().toProperties();
String destination = properties.getProperty("destinationName", properties.getProperty("destinationLookup"));
if (destination != null) {
if (destination.startsWith("openejb:Resource/")) {
destination = destination.substring("openejb:Resource/".length());
}
if (destination.startsWith("java:openejb/Resource/")) {
destination = destination.substring("java:openejb/Resource/".length());
}
mdb.getActivationConfig().addProperty("destination", destination);
// Remove destinationName as it is not in the standard ActivationSpec
final List<ActivationConfigProperty> list = mdb.getActivationConfig().getActivationConfigProperty();
final Iterator<ActivationConfigProperty> iterator = list.iterator();
while (iterator.hasNext()) {
final ActivationConfigProperty configProperty = iterator.next();
final String activationConfigPropertyName = configProperty.getActivationConfigPropertyName();
if (activationConfigPropertyName.equals("destinationName") || activationConfigPropertyName.equals("destinationLookup")) {
iterator.remove();
// we suppose we have only one of both we should be the case
break;
}
}
} else {
destination = properties.getProperty("destination");
}
if (destination == null) {
// EE 7/EJB 3.2
destination = properties.getProperty("destinationLookup");
}
// String destination = properties.getProperty("destination", properties.getProperty("destinationName"));
if (destination == null) {
destination = ejbDeployment.getDeploymentId();
mdb.getActivationConfig().addProperty("destination", destination);
}
// destination identifier
ResourceLink link = ejbDeployment.getResourceLink("openejb/destination");
if (link == null && mdb.getMessageDestinationLink() == null) {
link = new ResourceLink();
link.setResId(destination);
link.setResRefName("openejb/destination");
ejbDeployment.addResourceLink(link);
}
// destination type
String destinationType = properties.getProperty("destinationType");
if (destinationType == null && mdb.getMessageDestinationType() != null) {
destinationType = mdb.getMessageDestinationType();
mdb.getActivationConfig().addProperty("destinationType", destinationType);
}
if (mdb.getMessageDestinationType() == null) {
mdb.setMessageDestinationType(destinationType);
}
// topics need a clientId and subscriptionName
if ("javax.jms.Topic".equals(destinationType)) {
if (Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.activemq.deploymentId-as-clientId", ejbModule.getProperties().getProperty("openejb.activemq.deploymentId-as-clientId", "true"))) && !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.MessageDrivenBean$JAXB.readMessageDrivenBean in project tomee by apache.
the class OpenEjb2Conversion method convertMdbConfigs.
public final void convertMdbConfigs(final EjbJar ejbJar, final OpenejbJarType openejbJarType) {
final Map<String, MessageDrivenBean> mdbs = new TreeMap<String, MessageDrivenBean>();
for (final EnterpriseBean enterpriseBean : ejbJar.getEnterpriseBeans()) {
if (!(enterpriseBean instanceof MessageDrivenBean)) {
continue;
}
mdbs.put(enterpriseBean.getEjbName(), (MessageDrivenBean) enterpriseBean);
}
for (final org.apache.openejb.jee.oejb2.EnterpriseBean enterpriseBean : openejbJarType.getEnterpriseBeans()) {
if (!(enterpriseBean instanceof MessageDrivenBeanType)) {
continue;
}
final MessageDrivenBeanType bean = (MessageDrivenBeanType) enterpriseBean;
final MessageDrivenBean mdb = mdbs.get(bean.getEjbName());
if (mdb == null) {
// todo warn no such ejb in the ejb-jar.xml
continue;
}
final ActivationConfigType activationConfigType = bean.getActivationConfig();
if (activationConfigType != null) {
ActivationConfig activationConfig = mdb.getActivationConfig();
if (activationConfig == null) {
activationConfig = new ActivationConfig();
mdb.setActivationConfig(activationConfig);
}
for (final ActivationConfigPropertyType propertyType : activationConfigType.getActivationConfigProperty()) {
final ActivationConfigProperty property = new ActivationConfigProperty(propertyType.getActivationConfigPropertyName(), propertyType.getActivationConfigPropertyValue());
activationConfig.getActivationConfigProperty().add(property);
}
}
}
}
Aggregations