use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.
the class Assembler method postConstructResources.
private void postConstructResources(final Set<String> resourceIds, final ClassLoader classLoader, final Context containerSystemContext, final AppContext appContext) throws NamingException, OpenEJBException {
final Thread thread = Thread.currentThread();
final ClassLoader oldCl = thread.getContextClassLoader();
try {
thread.setContextClassLoader(classLoader);
final List<ResourceInfo> resourceList = config.facilities.resources;
for (final ResourceInfo resourceInfo : resourceList) {
if (!resourceIds.contains(resourceInfo.id)) {
continue;
}
if (isTemplatizedResource(resourceInfo)) {
continue;
}
try {
Class<?> clazz;
try {
clazz = classLoader.loadClass(resourceInfo.className);
} catch (final ClassNotFoundException cnfe) {
// custom classpath
clazz = containerSystemContext.lookup(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id).getClass();
}
final boolean initialize = "true".equalsIgnoreCase(String.valueOf(resourceInfo.properties.remove("InitializeAfterDeployment")));
final AnnotationFinder finder = Proxy.isProxyClass(clazz) ? null : new AnnotationFinder(new ClassesArchive(ancestors(clazz)));
final List<Method> postConstructs = finder == null ? Collections.<Method>emptyList() : finder.findAnnotatedMethods(PostConstruct.class);
final List<Method> preDestroys = finder == null ? Collections.<Method>emptyList() : finder.findAnnotatedMethods(PreDestroy.class);
resourceInfo.postConstructMethods = new ArrayList<>();
resourceInfo.preDestroyMethods = new ArrayList<>();
addMethodsToResourceInfo(resourceInfo.postConstructMethods, PostConstruct.class, postConstructs);
addMethodsToResourceInfo(resourceInfo.preDestroyMethods, PreDestroy.class, preDestroys);
CreationalContext<?> creationalContext = null;
Object originalResource = null;
if (!postConstructs.isEmpty() || initialize) {
originalResource = containerSystemContext.lookup(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id);
Object resource = originalResource;
if (resource instanceof Reference) {
resource = unwrapReference(resource);
this.bindResource(resourceInfo.id, resource, true);
}
try {
// wire up CDI
if (appContext != null && appContext.getWebBeansContext() != null) {
final BeanManagerImpl beanManager = appContext.getWebBeansContext().getBeanManagerImpl();
if (beanManager.isInUse()) {
creationalContext = beanManager.createCreationalContext(null);
OWBInjector.inject(beanManager, resource, creationalContext);
}
}
if (!"none".equals(resourceInfo.postConstruct)) {
if (resourceInfo.postConstruct != null) {
final Method p = clazz.getDeclaredMethod(resourceInfo.postConstruct);
if (!p.isAccessible()) {
SetAccessible.on(p);
}
p.invoke(resource);
}
for (final Method m : postConstructs) {
if (!m.isAccessible()) {
SetAccessible.on(m);
}
m.invoke(resource);
}
}
} catch (final Exception e) {
logger.fatal("Error calling @PostConstruct method on " + resource.getClass().getName());
throw new OpenEJBException(e);
}
}
if (!"none".equals(resourceInfo.preDestroy)) {
if (resourceInfo.preDestroy != null) {
final Method p = clazz.getDeclaredMethod(resourceInfo.preDestroy);
if (!p.isAccessible()) {
SetAccessible.on(p);
}
preDestroys.add(p);
}
if (!preDestroys.isEmpty() || creationalContext != null) {
final String name = OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id;
if (originalResource == null) {
originalResource = containerSystemContext.lookup(name);
}
this.bindResource(resourceInfo.id, new ResourceInstance(name, originalResource, preDestroys, creationalContext), true);
}
}
// log unused now for these resources now we built the resource completely and @PostConstruct can have used injected properties
if (resourceInfo.unsetProperties != null && !isPassthroughType(resourceInfo)) {
final Set<String> unsetKeys = resourceInfo.unsetProperties.stringPropertyNames();
for (final String key : unsetKeys) {
// don't use keySet to auto filter txMgr for instance and not real properties!
unusedProperty(resourceInfo.id, logger, key);
}
}
} catch (final Exception e) {
logger.fatal("Error calling PostConstruct method on " + resourceInfo.id);
logger.fatal("Resource " + resourceInfo.id + " could not be initialized. Application will be undeployed.");
throw new OpenEJBException(e);
}
}
} finally {
thread.setContextClassLoader(oldCl);
}
}
use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.
the class ApplicationComposerDeployer method configureClasses.
private void configureClasses(final WebModule web, final EjbModule ejbModule, final ApplicationComposer applicationComposer, final Classes classes) {
ejbModule.getEjbJar().setMetadataComplete(applicationComposer.metadataComplete());
final Collection<Archive> archives = new LinkedList<>();
if (classes.value().length > 0) {
archives.add(new ClassesArchive(classes.value()));
}
if (classes.cdi()) {
final Beans beans = new Beans();
for (final Class<?> c : classes.cdiAlternatives()) {
beans.addAlternativeClass(c);
}
for (final Class<?> c : classes.cdiDecorators()) {
beans.addDecorator(c);
}
for (final Class<?> c : classes.cdiInterceptors()) {
beans.addInterceptor(c);
}
ejbModule.setBeans(beans);
if (applicationComposer.metadataComplete()) {
for (final Class<?> c : classes.value()) {
beans.addManagedClass(null, c.getName());
}
final String name = BeanContext.Comp.openejbCompName(web.getModuleId());
final org.apache.openejb.jee.ManagedBean managedBean = new CompManagedBean(name, BeanContext.Comp.class);
managedBean.setTransactionType(TransactionType.BEAN);
ejbModule.getEjbJar().addEnterpriseBean(managedBean);
}
}
final CompositeArchive archive = new CompositeArchive(archives);
final Archive finalArchive = classes.excludes().length > 0 ? new FilteredArchive(archive, Filters.invert(Filters.prefixes(classes.excludes()))) : archive;
ejbModule.setFinder(new FinderFactory.OpenEJBAnnotationFinder(finalArchive).link());
web.setFinder(ejbModule.getFinder());
web.getWebApp().setMetadataComplete(ejbModule.getEjbJar().isMetadataComplete());
}
use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.
the class ApplicationComposerDeployer method deploy.
@Override
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
if (!appModule.isStandaloneModule()) {
return appModule;
}
for (final EjbModule ejbModule : appModule.getEjbModules()) {
if (ejbModule.getFinder() == null) {
continue;
}
WebModule webModule = null;
for (final WebModule web : appModule.getWebModules()) {
if (!web.getModuleId().equals(ejbModule.getModuleId())) {
continue;
}
webModule = web;
break;
}
if (webModule == null) {
continue;
}
for (final Class<?> clazz : ejbModule.getFinder().findAnnotatedClasses(ApplicationComposer.class)) {
final ApplicationComposer applicationComposer = clazz.getAnnotation(ApplicationComposer.class);
final Descriptor descriptor = clazz.getAnnotation(Descriptor.class);
if (descriptor != null) {
configureDescriptor(appModule, descriptor);
}
final Descriptors descriptors = clazz.getAnnotation(Descriptors.class);
if (descriptors != null) {
for (final Descriptor d : descriptors.value()) {
configureDescriptor(appModule, descriptor);
}
}
final Classes classes = clazz.getAnnotation(Classes.class);
if (classes != null) {
configureClasses(webModule, ejbModule, applicationComposer, classes);
}
Object instance = null;
final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(org.apache.openejb.util.Classes.ancestors(clazz)));
for (final Method m : finder.findAnnotatedMethods(org.apache.openejb.testing.Module.class)) {
instance = configureModule(appModule, ejbModule, clazz, instance, m);
}
for (final Method m : finder.findAnnotatedMethods(Configuration.class)) {
instance = configureConfiguration(appModule, clazz, instance, m);
}
final JaxrsProviders jaxrsProviders = clazz.getAnnotation(JaxrsProviders.class);
if (jaxrsProviders != null) {
for (final Class<?> c : jaxrsProviders.value()) {
webModule.getJaxrsProviders().add(c.getName());
}
}
}
}
return appModule;
}
use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.
the class FinderFactory method fallbackAnnotationFinder.
private OpenEJBAnnotationFinder fallbackAnnotationFinder(DeploymentModule module) {
final OpenEJBAnnotationFinder finder = new OpenEJBAnnotationFinder(new ClassesArchive(ensureMinimalClasses(module)));
finder.enableMetaAnnotations();
return finder;
}
use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.
the class AnnotationDeployerTest method findRestClasses.
@Test
public void findRestClasses() throws Exception {
final WebApp webApp = new WebApp();
webApp.setContextRoot("/");
webApp.setId("web");
webApp.setVersion("2.5");
WebModule webModule = new WebModule(webApp, webApp.getContextRoot(), Thread.currentThread().getContextClassLoader(), "myapp", webApp.getId());
webModule.setFinder(new AnnotationFinder(new ClassesArchive(RESTClass.class, RESTMethod.class, RESTApp.class)).link());
final AnnotationDeployer annotationDeployer = new AnnotationDeployer();
webModule = annotationDeployer.deploy(webModule);
final Set<String> classes = webModule.getRestClasses();
final Set<String> applications = webModule.getRestApplications();
assertEquals(1, classes.size());
assertTrue(classes.contains(RESTClass.class.getName()));
// assertTrue(classes.contains(RESTMethod.class.getName()));
assertEquals(1, applications.size());
assertEquals(RESTApp.class.getName(), applications.iterator().next());
}
Aggregations