use of org.apache.openejb.jee.EnterpriseBean in project tomee by apache.
the class StatelessInterceptorTest method buildTestApp.
public static EjbModule buildTestApp() throws Exception {
final EjbJar ejbJar = new EjbJar();
ejbJar.setId(StatelessInterceptorTest.class.getName());
final AssemblyDescriptor ad = ejbJar.getAssemblyDescriptor();
ejbJar.addEnterpriseBean(new StatelessBean(Target2Bean.class));
final EnterpriseBean bean = ejbJar.addEnterpriseBean(new StatelessBean(TargetBean.class));
Interceptor interceptor;
interceptor = ejbJar.addInterceptor(new Interceptor(DefaultInterceptor.class));
ad.addInterceptorBinding(new InterceptorBinding("*", interceptor));
{
interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptorViaDD.class));
final InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
binding.setMethod(new NamedMethod(TargetBean.class.getMethod("echo", List.class)));
}
{
interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptorViaDD.class));
final InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
binding.setMethod(new NamedMethod(TargetBean.class.getMethod("echo", int.class)));
}
{
interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptorViaDD.class));
final InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
binding.setMethod(new NamedMethod(TargetBean.class.getMethod("echo", boolean.class)));
}
final EnterpriseBean bean3 = ejbJar.addEnterpriseBean(new StatelessBean(Target3Bean.class));
final InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean3));
binding.setExcludeDefaultInterceptors(true);
binding.setExcludeClassInterceptors(true);
return new EjbModule(ejbJar);
}
use of org.apache.openejb.jee.EnterpriseBean 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.EnterpriseBean in project tomee by apache.
the class FinderFactory method ensureMinimalClasses.
public static Class<?>[] ensureMinimalClasses(final DeploymentModule module) {
final Collection<Class<?>> finderClasses = new HashSet<>();
if (EjbModule.class.isInstance(module)) {
final EjbModule ejb = EjbModule.class.cast(module);
final EnterpriseBean[] enterpriseBeans = ejb.getEjbJar().getEnterpriseBeans();
ClassLoader classLoader = ejb.getClassLoader();
if (classLoader == null) {
classLoader = Thread.currentThread().getContextClassLoader();
}
for (final EnterpriseBean bean : enterpriseBeans) {
final String name;
if (SessionBean.class.isInstance(bean)) {
final SessionBean sessionBean = SessionBean.class.cast(bean);
if (sessionBean.getProxy() == null) {
name = sessionBean.getEjbClass();
} else {
name = sessionBean.getProxy();
}
} else {
name = bean.getEjbClass();
}
try {
final Class<?> clazz = classLoader.loadClass(name);
finderClasses.addAll(ancestors(clazz));
} catch (final ClassNotFoundException e) {
// no-op
}
}
if (ejb.getWebservices() != null) {
for (final WebserviceDescription webservice : ejb.getWebservices().getWebserviceDescription()) {
for (final PortComponent port : webservice.getPortComponent()) {
if (port.getHandlerChains() == null) {
continue;
}
for (final HandlerChain handlerChain : port.getHandlerChains().getHandlerChain()) {
for (final Handler handler : handlerChain.getHandler()) {
if (handler.getHandlerClass() != null) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(handler.getHandlerClass())));
} catch (final ClassNotFoundException e) {
// no-op
}
}
}
}
}
}
}
for (final org.apache.openejb.jee.Interceptor interceptor : ejb.getEjbJar().getInterceptors()) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(interceptor.getInterceptorClass())));
} catch (final ClassNotFoundException e) {
// no-op
}
}
final Beans beans = ejb.getBeans();
if (beans != null && ejb.getEjbJar() != null) {
for (final List<String> managedClasses : beans.getManagedClasses().values()) {
for (final String name : managedClasses) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(name)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
}
for (final String name : beans.getInterceptors()) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(name)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
for (final String name : beans.getAlternativeClasses()) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(name)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
for (final String name : beans.getDecorators()) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(name)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
}
} else if (WebModule.class.isInstance(module)) {
final WebModule web = WebModule.class.cast(module);
final ClassLoader classLoader = web.getClassLoader();
if (web.getWebApp() != null) {
for (final Servlet s : web.getWebApp().getServlet()) {
final String servletClass = s.getServletClass();
if (servletClass == null) {
continue;
}
try {
finderClasses.addAll(ancestors(classLoader.loadClass(servletClass)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
for (final String s : web.getRestClasses()) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(s)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
for (final String s : web.getEjbWebServices()) {
try {
finderClasses.addAll(ancestors(classLoader.loadClass(s)));
} catch (final ClassNotFoundException e) {
// no-op
}
}
}
}
return finderClasses.toArray(new Class<?>[finderClasses.size()]);
}
use of org.apache.openejb.jee.EnterpriseBean in project tomee by apache.
the class InitEjbDeployments method deploy.
private EjbModule deploy(final EjbModule ejbModule, final Map<String, String> contextData, final Set<String> abstractSchemaNames) throws OpenEJBException {
contextData.put("moduleId", ejbModule.getModuleId());
contextData.put("moduleUri", ejbModule.getModuleUri().toString());
final OpenejbJar openejbJar;
if (ejbModule.getOpenejbJar() != null) {
openejbJar = ejbModule.getOpenejbJar();
} else {
openejbJar = new OpenejbJar();
ejbModule.setOpenejbJar(openejbJar);
}
StringTemplate deploymentIdTemplate = this.deploymentIdTemplate;
if (openejbJar.getProperties().containsKey(DEPLOYMENT_ID_FORMAT)) {
final String format = openejbJar.getProperties().getProperty(DEPLOYMENT_ID_FORMAT);
logger.info("Using " + DEPLOYMENT_ID_FORMAT + " '" + format + "'");
deploymentIdTemplate = new StringTemplate(format);
}
for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
StringTemplate template = deploymentIdTemplate;
final org.apache.openejb.api.EjbDeployment annotation = getEjbDeploymentAnnotation(ejbModule, bean);
EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment == null) {
ejbDeployment = new EjbDeployment();
ejbDeployment.setEjbName(bean.getEjbName());
if (annotation != null && isDefined(annotation.id())) {
template = new StringTemplate(annotation.id());
ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
} else {
ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
if (!(bean instanceof ManagedBean) || !((ManagedBean) bean).isHidden()) {
logger.info("Auto-deploying ejb " + bean.getEjbName() + ": EjbDeployment(deployment-id=" + ejbDeployment.getDeploymentId() + ")");
}
}
openejbJar.getEjbDeployment().add(ejbDeployment);
} else if (ejbDeployment.getDeploymentId() == null) {
if (annotation != null && isDefined(annotation.id())) {
template = new StringTemplate(annotation.id());
ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
} else {
ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
logger.info("Auto-assigning deployment-id for ejb " + bean.getEjbName() + ": EjbDeployment(deployment-id=" + ejbDeployment.getDeploymentId() + ")");
}
}
if (ejbDeployment.getContainerId() == null && annotation != null && isDefined(annotation.container())) {
ejbDeployment.setContainerId(annotation.container());
}
if (isCmpEntity(bean)) {
final EntityBean entity = (EntityBean) bean;
if (entity.getAbstractSchemaName() == null) {
String abstractSchemaName = bean.getEjbName().trim().replaceAll("[ \\t\\n\\r-]+", "_");
// The AbstractSchemaName must be unique, we should check that it is
if (abstractSchemaNames.contains(abstractSchemaName)) {
int i = 2;
while (abstractSchemaNames.contains(abstractSchemaName + i)) {
i++;
}
abstractSchemaName = abstractSchemaName + i;
}
abstractSchemaNames.add(abstractSchemaName);
entity.setAbstractSchemaName(abstractSchemaName);
}
}
}
return ejbModule;
}
use of org.apache.openejb.jee.EnterpriseBean 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");
}
}
}
}
}
Aggregations