use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class ApplicationWideTest method testShouldCreateAResourceAndNotRemoveOnUndeploy.
public void testShouldCreateAResourceAndNotRemoveOnUndeploy() throws Exception {
final EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
final EjbJar ejbJar = ejbModule.getEjbJar();
ejbJar.addEnterpriseBean(new StatelessBean(EchoBean.class));
final AppModule appModule = new AppModule(ejbModule);
final Container container = new Container();
container.setId("My Container");
container.setCtype("STATELESS");
container.getProperties().setProperty("ApplicationWide", "true");
appModule.getContainers().add(container);
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
{
// setup the system
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
}
final AppInfo appInfo = config.configureApplication(appModule);
assembler.createApplication(appInfo);
{
final ContainerSystem containerSystem = assembler.getContainerSystem();
final org.apache.openejb.Container appContainer = containerSystem.getContainer(ejbModule.getModuleId() + "/My Container");
assertNotNull(appContainer);
}
assembler.destroyApplication(appInfo);
{
final ContainerSystem containerSystem = assembler.getContainerSystem();
final org.apache.openejb.Container appContainer = containerSystem.getContainer(ejbModule.getModuleId() + "/My Container");
assertNotNull(appContainer);
}
}
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;
}
use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.
the class EjbModule method setModuleId.
public void setModuleId(final String moduleId) {
if (openejbJar == null) {
openejbJar = new OpenejbJar();
}
openejbJar.setModuleName(moduleId);
this.id = new ID(openejbJar, ejbJar, moduleId, id.getLocation(), id.getUri(), this);
}
Aggregations