use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class ActiveMQResourceAdapterControlTest method app.
@Module
@Classes(value = Mdb.class)
public EjbModule app() {
return new EjbModule(new EjbJar("test"), new OpenejbJar() {
{
setId("test");
getEjbDeployment().add(new EjbDeployment() {
{
setEjbName("ejb/Mdb");
getProperties().put("MdbActiveOnStartup", "false");
getProperties().put("MdbJMXControl", "default:type=test");
}
});
}
});
}
use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class CmpJpaConversion method processEntityBean.
/**
* Generate the CMP mapping data for an individual
* EntityBean.
*
* @param ejbModule The module containing the bean.
* @param entityMappings The accumulated set of entity mappings.
* @param bean The been we're generating the mapping for.
*/
private void processEntityBean(final EjbModule ejbModule, final EntityMappings entityMappings, final EntityBean bean) {
// try to add a new persistence-context-ref for cmp
if (!addPersistenceContextRef(bean)) {
// which means it has a mapping, so skip this bean
return;
}
// get the real bean class
final Class ejbClass = loadClass(ejbModule.getClassLoader(), bean.getEjbClass());
// and generate a name for the subclass that will be generated and handed to the JPA
// engine as the managed class.
final String jpaEntityClassName = CmpUtil.getCmpImplClassName(bean.getAbstractSchemaName(), ejbClass.getName());
// We don't use this mapping directly, instead we pull entries from it
// the reason being is that we intend to support mappings that aren't
// exactly correct. i.e. users should be able to write mappings completely
// ignorant of the fact that we subclass. The fact that we subclass means
// these user supplied mappings might need to be adjusted as the jpa orm.xml
// file is extremely subclass/supperclass aware and mappings specified in it
// need to be spot on.
final EntityMappings userMappings = getUserEntityMappings(ejbModule);
// chain of the bean looking for any that have user defined mappings.
for (Class clazz = ejbClass; clazz != null; clazz = clazz.getSuperclass()) {
final MappedSuperclass mappedSuperclass = removeMappedSuperclass(userMappings, clazz.getName());
// that the mapping is correct. Copy it from their mappings to ours
if (mappedSuperclass != null) {
entityMappings.getMappedSuperclass().add(mappedSuperclass);
}
}
// Look for an existing mapping using the openejb generated subclass name
Entity entity = removeEntity(userMappings, jpaEntityClassName);
// because we are going to ignore all other xml metadata.
if (entity != null) {
// XmlMetadataComplete is an OpenEJB specific flag that
// tells all other legacy descriptor converters to keep
// their hands off.
entity.setXmlMetadataComplete(true);
entityMappings.getEntity().add(entity);
return;
}
if (entity == null) {
entity = new Entity(jpaEntityClassName);
}
// have to check for null everywhere.
if (entity.getAttributes() == null) {
entity.setAttributes(new Attributes());
}
// add the entity
entityMappings.getEntity().add(entity);
// OVERWRITE: description: contains the name of the entity bean
entity.setDescription(ejbModule.getModuleId() + "#" + bean.getEjbName());
// PRESERVE has queries: name: the name of the entity in queries
final String entityName = bean.getAbstractSchemaName();
entity.setName(entityName);
entity.setEjbName(bean.getEjbName());
final ClassLoader classLoader = ejbModule.getClassLoader();
final Collection<MappedSuperclass> mappedSuperclasses;
if (bean.getCmpVersion() == CmpVersion.CMP2) {
// perform the 2.x class mapping. This really just identifies the primary key and
// other cmp fields that will be generated for the concrete class and identify them
// to JPA.
mappedSuperclasses = mapClass2x(entity, bean, classLoader);
} else {
// map the cmp class, but if we are using a mapped super class,
// generate attribute-override instead of id and basic
mappedSuperclasses = mapClass1x(bean.getEjbClass(), entity, bean, classLoader);
}
// configuration. f
if (mappedSuperclasses != null) {
// that will get passed to the JPA engine.
for (final MappedSuperclass mappedSuperclass : mappedSuperclasses) {
entityMappings.getMappedSuperclass().add(mappedSuperclass);
}
}
// process queries
for (final Query query : bean.getQuery()) {
final NamedQuery namedQuery = new NamedQuery();
final QueryMethod queryMethod = query.getQueryMethod();
// todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
final StringBuilder name = new StringBuilder();
name.append(entityName).append(".").append(queryMethod.getMethodName());
if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
name.append('(');
boolean first = true;
for (final String methodParam : queryMethod.getMethodParams().getMethodParam()) {
if (!first) {
name.append(",");
}
name.append(methodParam);
first = false;
}
name.append(')');
}
namedQuery.setName(name.toString());
namedQuery.setQuery(query.getEjbQl());
entity.getNamedQuery().add(namedQuery);
}
// todo: there should be a common interface between ejb query object and openejb query object
final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
if (ejbDeployment != null) {
for (final org.apache.openejb.jee.oejb3.Query query : ejbDeployment.getQuery()) {
final NamedQuery namedQuery = new NamedQuery();
final org.apache.openejb.jee.oejb3.QueryMethod queryMethod = query.getQueryMethod();
// todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
final StringBuilder name = new StringBuilder();
name.append(entityName).append(".").append(queryMethod.getMethodName());
if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
name.append('(');
boolean first = true;
for (final String methodParam : queryMethod.getMethodParams().getMethodParam()) {
if (!first) {
name.append(",");
}
name.append(methodParam);
first = false;
}
name.append(')');
}
namedQuery.setName(name.toString());
namedQuery.setQuery(query.getObjectQl());
entity.getNamedQuery().add(namedQuery);
}
}
}
use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class OpenEJBArchiveProcessor method createModule.
public static AppModule createModule(final Archive<?> archive, final TestClass testClass, final Closeables closeables) {
final Class<?> javaClass;
if (testClass != null) {
javaClass = testClass.getJavaClass();
} else {
javaClass = null;
}
final ClassLoader parent;
if (javaClass == null) {
parent = Thread.currentThread().getContextClassLoader();
} else {
parent = javaClass.getClassLoader();
}
final List<URL> additionalPaths = new ArrayList<>();
CompositeArchive scannedArchive = null;
final Map<URL, List<String>> earMap = new HashMap<>();
final Map<String, Object> altDD = new HashMap<>();
final List<Archive> earLibsArchives = new ArrayList<>();
final CompositeBeans earBeans = new CompositeBeans();
final boolean isEar = EnterpriseArchive.class.isInstance(archive);
final boolean isWebApp = WebArchive.class.isInstance(archive);
final String prefix = isWebApp ? WEB_INF : META_INF;
if (isEar || isWebApp) {
final Map<ArchivePath, Node> jars = archive.getContent(new IncludeRegExpPaths(isEar ? "/.*\\.jar" : "/WEB-INF/lib/.*\\.jar"));
scannedArchive = analyzeLibs(parent, additionalPaths, earMap, earLibsArchives, earBeans, jars, altDD);
}
final URL[] urls = additionalPaths.toArray(new URL[additionalPaths.size()]);
final URLClassLoaderFirst swParent = new URLClassLoaderFirst(urls, parent);
closeables.add(swParent);
if (!isEar) {
earLibsArchives.add(archive);
}
final SWClassLoader loader = new SWClassLoader(swParent, earLibsArchives.toArray(new Archive<?>[earLibsArchives.size()]));
closeables.add(loader);
final URLClassLoader tempClassLoader = ClassLoaderUtil.createTempClassLoader(loader);
closeables.add(tempClassLoader);
final AppModule appModule = new AppModule(loader, archive.getName());
if (WEB_INF.equals(prefix)) {
appModule.setDelegateFirst(false);
appModule.setStandloneWebModule();
final WebModule webModule = new WebModule(createWebApp(archive), contextRoot(archive.getName()), loader, "", appModule.getModuleId());
webModule.setUrls(additionalPaths);
appModule.getWebModules().add(webModule);
} else if (isEar) {
// mainly for CDI TCKs
final FinderFactory.OpenEJBAnnotationFinder earLibFinder = new FinderFactory.OpenEJBAnnotationFinder(new SimpleWebappAggregatedArchive(tempClassLoader, scannedArchive, earMap));
appModule.setEarLibFinder(earLibFinder);
final EjbModule earCdiModule = new EjbModule(appModule.getClassLoader(), DeploymentLoader.EAR_SCOPED_CDI_BEANS + appModule.getModuleId(), new EjbJar(), new OpenejbJar());
earCdiModule.setBeans(earBeans);
earCdiModule.setFinder(earLibFinder);
final EjbJar ejbJar;
final AssetSource ejbJarXml = AssetSource.class.isInstance(altDD.get(EJB_JAR_XML)) ? AssetSource.class.cast(altDD.get(EJB_JAR_XML)) : null;
if (ejbJarXml != null) {
try {
ejbJar = ReadDescriptors.readEjbJar(ejbJarXml.get());
} catch (final Exception e) {
throw new OpenEJBRuntimeException(e);
}
} else {
ejbJar = new EjbJar();
}
// EmptyEjbJar would prevent to add scanned EJBs but this is *here* an aggregator so we need to be able to do so
earCdiModule.setEjbJar(ejbJar);
appModule.getEjbModules().add(earCdiModule);
for (final Map.Entry<ArchivePath, Node> node : archive.getContent(new IncludeRegExpPaths("/.*\\.war")).entrySet()) {
final Asset asset = node.getValue().getAsset();
if (ArchiveAsset.class.isInstance(asset)) {
final Archive<?> webArchive = ArchiveAsset.class.cast(asset).getArchive();
if (WebArchive.class.isInstance(webArchive)) {
final Map<String, Object> webAltDD = new HashMap<>();
final Node beansXml = findBeansXml(webArchive, WEB_INF);
final List<URL> webappAdditionalPaths = new LinkedList<>();
final CompositeBeans webAppBeansXml = new CompositeBeans();
final List<Archive> webAppArchives = new LinkedList<Archive>();
final Map<URL, List<String>> webAppClassesByUrl = new HashMap<URL, List<String>>();
final CompositeArchive webAppArchive = analyzeLibs(parent, webappAdditionalPaths, webAppClassesByUrl, webAppArchives, webAppBeansXml, webArchive.getContent(new IncludeRegExpPaths("/WEB-INF/lib/.*\\.jar")), webAltDD);
webAppArchives.add(webArchive);
final SWClassLoader webLoader = new SWClassLoader(parent, webAppArchives.toArray(new Archive<?>[webAppArchives.size()]));
closeables.add(webLoader);
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(finderArchive(beansXml, webArchive, webLoader, webAppArchive, webAppClassesByUrl, webAppBeansXml));
final String contextRoot = contextRoot(webArchive.getName());
final WebModule webModule = new WebModule(createWebApp(webArchive), contextRoot, webLoader, "", appModule.getModuleId() + "_" + contextRoot);
webModule.setUrls(Collections.<URL>emptyList());
webModule.setScannableUrls(Collections.<URL>emptyList());
webModule.setFinder(finder);
final EjbJar webEjbJar = createEjbJar(prefix, webArchive);
final EjbModule ejbModule = new EjbModule(webLoader, webModule.getModuleId(), null, webEjbJar, new OpenejbJar());
ejbModule.setBeans(webAppBeansXml);
ejbModule.getAltDDs().putAll(webAltDD);
ejbModule.getAltDDs().put("beans.xml", webAppBeansXml);
ejbModule.setFinder(finder);
ejbModule.setClassLoader(webLoader);
ejbModule.setWebapp(true);
appModule.getEjbModules().add(ejbModule);
appModule.getWebModules().add(webModule);
addPersistenceXml(archive, WEB_INF, appModule);
addOpenEJbJarXml(archive, WEB_INF, ejbModule);
addValidationXml(archive, WEB_INF, new HashMap<String, Object>(), ejbModule);
addResourcesXml(archive, WEB_INF, ejbModule);
addEnvEntries(archive, WEB_INF, appModule, ejbModule);
}
}
}
}
if (isEar) {
// adding the test class as lib class can break test if tested against the web part of the ear
addTestClassAsManagedBean(javaClass, tempClassLoader, appModule);
return appModule;
}
// add the test as a managed bean to be able to inject into it easily
final Map<String, Object> testDD;
if (javaClass != null) {
final EjbModule testEjbModule = addTestClassAsManagedBean(javaClass, tempClassLoader, appModule);
testDD = testEjbModule.getAltDDs();
} else {
// ignore
testDD = new HashMap<>();
}
final EjbJar ejbJar = createEjbJar(prefix, archive);
if (ejbJar.getModuleName() == null) {
final String name = archive.getName();
if (name.endsWith("ar") && name.length() > 4) {
ejbJar.setModuleName(name.substring(0, name.length() - ".jar".length()));
} else {
ejbJar.setModuleName(name);
}
}
final EjbModule ejbModule = new EjbModule(ejbJar);
ejbModule.setClassLoader(tempClassLoader);
final Node beansXml = findBeansXml(archive, prefix);
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(finderArchive(beansXml, archive, loader, scannedArchive, earMap, earBeans));
ejbModule.setFinder(finder);
ejbModule.setBeans(earBeans);
ejbModule.getAltDDs().put("beans.xml", earBeans);
if (appModule.isWebapp()) {
// war
appModule.getWebModules().iterator().next().setFinder(ejbModule.getFinder());
}
appModule.getEjbModules().add(ejbModule);
addPersistenceXml(archive, prefix, appModule);
addOpenEJbJarXml(archive, prefix, ejbModule);
addValidationXml(archive, prefix, testDD, ejbModule);
addResourcesXml(archive, prefix, ejbModule);
addEnvEntries(archive, prefix, appModule, ejbModule);
if (!appModule.isWebapp()) {
appModule.getAdditionalLibraries().addAll(additionalPaths);
}
if (archive.getName().endsWith(".jar")) {
// otherwise global naming will be broken
appModule.setStandloneWebModule();
}
return appModule;
}
use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class OpenEJBArchiveProcessor method addTestClassAsManagedBean.
private static EjbModule addTestClassAsManagedBean(Class<?> javaClass, URLClassLoader tempClassLoader, AppModule appModule) {
final EjbJar ejbJar = new EjbJar();
final OpenejbJar openejbJar = new OpenejbJar();
final String ejbName = appModule.getModuleId() + "_" + javaClass.getName();
final ManagedBean bean = ejbJar.addEnterpriseBean(new ManagedBean(ejbName, javaClass.getName(), true));
bean.localBean();
bean.setTransactionType(TransactionType.BEAN);
final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(bean);
ejbDeployment.setDeploymentId(ejbName);
final EjbModule e = new EjbModule(ejbJar, openejbJar);
e.getProperties().setProperty("openejb.cdi.activated", "false");
e.getProperties().setProperty("openejb.test.module", "true");
e.setBeans(new Beans());
e.setClassLoader(tempClassLoader);
appModule.getEjbModules().add(e);
return e;
}
use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class TestClassDiscoverer method discover.
@Override
public AppModule discover(final AppModule module) {
final Set<Class<?>> testClasses = new HashSet<>();
final Map<Class<?>, WebModule> webTestClasses = new HashMap<>();
final Set<ClassLoader> saw = new HashSet<>();
if (module.getClassLoader() != null) {
addTests(findMarkers(module.getClassLoader()), findClassMarkers(module.getClassLoader()), module.getEarLibFinder(), testClasses);
saw.add(module.getClassLoader());
}
for (final WebModule web : module.getWebModules()) {
if (web.getClassLoader() != null && !saw.contains(web.getClassLoader())) {
final Set<Class<?>> classes = new HashSet<>();
addTests(findMarkers(web.getClassLoader()), findClassMarkers(web.getClassLoader()), web.getFinder(), classes);
saw.add(web.getClassLoader());
for (final Class<?> c : classes) {
webTestClasses.put(c, web);
}
// in case of an ear if we find the same test class in a webapp we don't want it in lib part
// this case can happen in tomee-embedded mainly
final Iterator<Class<?>> c = testClasses.iterator();
while (c.hasNext()) {
final String cl = c.next().getName();
for (final Class<?> wc : classes) {
if (cl.equals(wc.getName())) {
c.remove();
break;
}
}
}
testClasses.addAll(classes);
}
}
for (final EjbModule ejb : module.getEjbModules()) {
if (ejb.getClassLoader() != null && !saw.contains(ejb.getClassLoader())) {
addTests(findMarkers(ejb.getClassLoader()), findClassMarkers(ejb.getClassLoader()), ejb.getFinder(), testClasses);
saw.add(ejb.getClassLoader());
}
}
for (final ConnectorModule connector : module.getConnectorModules()) {
if (connector.getClassLoader() != null && !saw.contains(connector.getClassLoader())) {
addTests(findMarkers(connector.getClassLoader()), findClassMarkers(connector.getClassLoader()), connector.getFinder(), testClasses);
saw.add(connector.getClassLoader());
}
}
// keep it since CukeSpace doesn't rely on JUnit or TestNG @Test so it stays mandatory
final File file = module.getFile();
final String line = findTestName(file, module.getClassLoader());
if (line != null) {
String name;
final int endIndex = line.indexOf('#');
if (endIndex > 0) {
name = line.substring(0, endIndex);
if (file != null && !file.getName().equals(line.substring(endIndex + 1, line.length()))) {
name = null;
}
} else {
name = line;
}
if (name != null) {
boolean found = false;
for (final WebModule web : module.getWebModules()) {
try {
testClasses.add(web.getClassLoader().loadClass(name));
found = true;
break;
} catch (final Throwable e) {
// no-op
}
}
if (!found) {
try {
testClasses.add(module.getClassLoader().loadClass(name));
} catch (final Throwable e) {
// no-op
}
}
}
}
final Iterator<Class<?>> it = testClasses.iterator();
while (it.hasNext()) {
try {
// call some reflection methods to make it fail if some dep are missing...
Class<?> current = it.next();
if (!AnnotationDeployer.isInstantiable(current)) {
it.remove();
continue;
}
while (current != null) {
current.getDeclaredFields();
current.getDeclaredMethods();
current.getCanonicalName();
current = current.getSuperclass();
// TODO: more validations
}
} catch (final NoClassDefFoundError ncdfe) {
it.remove();
}
}
for (final Class<?> test : testClasses) {
final EjbJar ejbJar = new EjbJar();
final OpenejbJar openejbJar = new OpenejbJar();
final String name = test.getName();
final String ejbName = module.getModuleId() + "_" + name;
final ManagedBean bean = ejbJar.addEnterpriseBean(new ManagedBean(ejbName, name, true));
bean.localBean();
bean.setTransactionType(TransactionType.BEAN);
final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(bean);
ejbDeployment.setDeploymentId(ejbName);
final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
ejbModule.setClassLoader(test.getClassLoader());
final WebModule webModule = webTestClasses.get(test);
if (webModule != null) {
ejbModule.setWebapp(true);
ejbModule.getProperties().put("openejb.ejbmodule.webappId", webModule.getModuleId());
}
ejbModule.getProperties().put("openejb.ejbmodule.MergeWebappJndiContext", "true");
module.getEjbModules().add(ejbModule);
}
return module;
}
Aggregations