Search in sources :

Example 6 with Classes

use of org.apache.openejb.testing.Classes in project tomee by apache.

the class ResourceAdapterControlTest method app.

@Module
@Classes(value = Mdb.class)
public EjbModule app() {
    return new EjbModule(new EjbJar("test") {

        {
            addEnterpriseBean(new MessageDrivenBean("ejb/Mdb", Mdb.class) {

                {
                    setActivationConfig(new ActivationConfig());
                    getActivationConfig().addProperty("MdbActiveOnStartup", "false");
                    getActivationConfig().addProperty("MdbJMXControl", "default:type=test");
                }
            });
        }
    });
}
Also used : MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) EjbModule(org.apache.openejb.config.EjbModule) EjbJar(org.apache.openejb.jee.EjbJar) ActivationConfig(org.apache.openejb.jee.ActivationConfig) EjbModule(org.apache.openejb.config.EjbModule) Module(org.apache.openejb.testing.Module) Classes(org.apache.openejb.testing.Classes)

Example 7 with Classes

use of org.apache.openejb.testing.Classes in project tomee by apache.

the class StatelessWithAroundInvokeOnlyTest method bean.

@Module
@Classes(cdi = true)
public StatelessBean bean() {
    final StatelessBean bean = new StatelessBean(StatelessTestBean.class);
    bean.setLocalBean(new Empty());
    return bean;
}
Also used : Empty(org.apache.openejb.jee.Empty) StatelessBean(org.apache.openejb.jee.StatelessBean) Module(org.apache.openejb.testing.Module) Classes(org.apache.openejb.testing.Classes)

Example 8 with Classes

use of org.apache.openejb.testing.Classes in project tomee by apache.

the class WebAppEnvEntryTest method war.

@Module
@Classes(cdi = true, value = { CdiBean.class })
public WebApp war() {
    final WebApp webApp = new WebApp().contextRoot("/myapp");
    webApp.getEnvEntry().add(new EnvEntry("foo", String.class.getName(), "bar"));
    return webApp;
}
Also used : WebApp(org.apache.openejb.jee.WebApp) EnvEntry(org.apache.openejb.jee.EnvEntry) Module(org.apache.openejb.testing.Module) Classes(org.apache.openejb.testing.Classes)

Example 9 with Classes

use of org.apache.openejb.testing.Classes in project tomee by apache.

the class SimplePojoTest method war.

@Module
@Classes(cdi = true, value = { MyFirstRestClass.class })
public WebModule war() {
    final WebModule webModule = new WebModule(new WebApp(), "/foo", Thread.currentThread().getContextClassLoader(), "", "foo");
    webModule.getRestClasses().add(MyFirstRestClass.class.getName());
    return webModule;
}
Also used : MyFirstRestClass(org.apache.openejb.server.cxf.rs.beans.MyFirstRestClass) WebModule(org.apache.openejb.config.WebModule) WebApp(org.apache.openejb.jee.WebApp) WebModule(org.apache.openejb.config.WebModule) Module(org.apache.openejb.testing.Module) Classes(org.apache.openejb.testing.Classes)

Example 10 with Classes

use of org.apache.openejb.testing.Classes in project tomee by apache.

the class TomEEEmbeddedApplicationRunner method start.

public synchronized void start(final Class<?> marker, final Properties config, final String... args) throws Exception {
    if (started) {
        return;
    }
    ensureAppInit(marker);
    started = true;
    final Class<?> appClass = app.getClass();
    final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ancestors(appClass)));
    // setup the container config reading class annotation, using a randome http port and deploying the classpath
    final Configuration configuration = new Configuration();
    final ContainerProperties props = appClass.getAnnotation(ContainerProperties.class);
    if (props != null) {
        final Properties runnerProperties = new Properties();
        for (final ContainerProperties.Property p : props.value()) {
            final String name = p.name();
            if (name.startsWith("tomee.embedded.application.runner.")) {
                // allow to tune the Configuration
                // no need to filter there since it is done in loadFromProperties()
                runnerProperties.setProperty(name.substring("tomee.embedded.application.runner.".length()), p.value());
            } else {
                configuration.property(name, StrSubstitutor.replaceSystemProperties(p.value()));
            }
        }
        if (!runnerProperties.isEmpty()) {
            configuration.loadFromProperties(runnerProperties);
        }
    }
    // overrides, note that some config are additive by design
    configuration.loadFromProperties(System.getProperties());
    final List<Method> annotatedMethods = finder.findAnnotatedMethods(org.apache.openejb.testing.Configuration.class);
    if (annotatedMethods.size() > 1) {
        throw new IllegalArgumentException("Only one @Configuration is supported: " + annotatedMethods);
    }
    for (final Method m : annotatedMethods) {
        final Object o = m.invoke(app);
        if (Properties.class.isInstance(o)) {
            final Properties properties = Properties.class.cast(o);
            if (configuration.getProperties() == null) {
                configuration.setProperties(new Properties());
            }
            configuration.getProperties().putAll(properties);
        } else {
            throw new IllegalArgumentException("Unsupported " + o + " for @Configuration");
        }
    }
    final Collection<org.apache.tomee.embedded.LifecycleTask> lifecycleTasks = new ArrayList<>();
    final Collection<Closeable> postTasks = new ArrayList<>();
    final LifecycleTasks tasks = appClass.getAnnotation(LifecycleTasks.class);
    if (tasks != null) {
        for (final Class<? extends org.apache.tomee.embedded.LifecycleTask> type : tasks.value()) {
            final org.apache.tomee.embedded.LifecycleTask lifecycleTask = type.newInstance();
            lifecycleTasks.add(lifecycleTask);
            postTasks.add(lifecycleTask.beforeContainerStartup());
        }
    }
    final Map<String, Field> ports = new HashMap<>();
    {
        Class<?> type = appClass;
        while (type != null && type != Object.class) {
            for (final Field f : type.getDeclaredFields()) {
                final RandomPort annotation = f.getAnnotation(RandomPort.class);
                final String value = annotation == null ? null : annotation.value();
                if (value != null && value.startsWith("http")) {
                    f.setAccessible(true);
                    ports.put(value, f);
                }
            }
            type = type.getSuperclass();
        }
    }
    if (ports.containsKey("http")) {
        configuration.randomHttpPort();
    }
    // at least after LifecycleTasks to inherit from potential states (system properties to get a port etc...)
    final Configurers configurers = appClass.getAnnotation(Configurers.class);
    if (configurers != null) {
        for (final Class<? extends Configurer> type : configurers.value()) {
            type.newInstance().configure(configuration);
        }
    }
    final Classes classes = appClass.getAnnotation(Classes.class);
    String context = classes != null ? classes.context() : "";
    context = !context.isEmpty() && context.startsWith("/") ? context.substring(1) : context;
    Archive archive = null;
    if (classes != null && classes.value().length > 0) {
        archive = new ClassesArchive(classes.value());
    }
    final Jars jars = appClass.getAnnotation(Jars.class);
    final List<URL> urls;
    if (jars != null) {
        final Collection<File> files = ApplicationComposers.findFiles(jars);
        urls = new ArrayList<>(files.size());
        for (final File f : files) {
            urls.add(f.toURI().toURL());
        }
    } else {
        urls = null;
    }
    final WebResource resources = appClass.getAnnotation(WebResource.class);
    if (resources != null && resources.value().length > 1) {
        throw new IllegalArgumentException("Only one docBase is supported for now using @WebResource");
    }
    String webResource = null;
    if (resources != null && resources.value().length > 0) {
        webResource = resources.value()[0];
    } else {
        final File webapp = new File("src/main/webapp");
        if (webapp.isDirectory()) {
            webResource = "src/main/webapp";
        }
    }
    if (config != null) {
        // override other config from annotations
        configuration.loadFromProperties(config);
    }
    final Container container = new Container(configuration);
    SystemInstance.get().setComponent(TomEEEmbeddedArgs.class, new TomEEEmbeddedArgs(args, null));
    SystemInstance.get().setComponent(LifecycleTaskAccessor.class, new LifecycleTaskAccessor(lifecycleTasks));
    container.deploy(new Container.DeploymentRequest(context, // call ClasspathSearcher that lazily since container needs to be started to not preload logging
    urls == null ? new DeploymentsResolver.ClasspathSearcher().loadUrls(Thread.currentThread().getContextClassLoader()).getUrls() : urls, webResource != null ? new File(webResource) : null, true, null, archive));
    for (final Map.Entry<String, Field> f : ports.entrySet()) {
        switch(f.getKey()) {
            case "http":
                setPortField(f.getKey(), f.getValue(), configuration, context, app);
                break;
            case "https":
                break;
            default:
                throw new IllegalArgumentException("port " + f.getKey() + " not yet supported");
        }
    }
    SystemInstance.get().addObserver(app);
    composerInject(app);
    final AnnotationFinder appFinder = new AnnotationFinder(new ClassesArchive(appClass));
    for (final Method mtd : appFinder.findAnnotatedMethods(PostConstruct.class)) {
        if (mtd.getParameterTypes().length == 0) {
            if (!mtd.isAccessible()) {
                mtd.setAccessible(true);
            }
            mtd.invoke(app);
        }
    }
    hook = new Thread() {

        @Override
        public void run() {
            // ensure to log errors but not fail there
            for (final Method mtd : appFinder.findAnnotatedMethods(PreDestroy.class)) {
                if (mtd.getParameterTypes().length == 0) {
                    if (!mtd.isAccessible()) {
                        mtd.setAccessible(true);
                    }
                    try {
                        mtd.invoke(app);
                    } catch (final IllegalAccessException e) {
                        throw new IllegalStateException(e);
                    } catch (final InvocationTargetException e) {
                        throw new IllegalStateException(e.getCause());
                    }
                }
            }
            try {
                container.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
            for (final Closeable c : postTasks) {
                try {
                    c.close();
                } catch (final IOException e) {
                    e.printStackTrace();
                }
            }
            postTasks.clear();
            app = null;
            try {
                SHUTDOWN_TASKS.remove(this);
            } catch (final Exception e) {
            // no-op: that's ok at that moment if not called manually
            }
        }
    };
    SHUTDOWN_TASKS.put(hook, hook);
}
Also used : FileArchive(org.apache.xbean.finder.archive.FileArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) Archive(org.apache.xbean.finder.archive.Archive) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) WebResource(org.apache.openejb.testing.WebResource) ContainerProperties(org.apache.openejb.testing.ContainerProperties) DeploymentsResolver(org.apache.openejb.config.DeploymentsResolver) Classes(org.apache.openejb.testing.Classes) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) RandomPort(org.apache.openejb.testing.RandomPort) PreDestroy(javax.annotation.PreDestroy) File(java.io.File) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) TomEEEmbeddedArgs(org.apache.tomee.embedded.component.TomEEEmbeddedArgs) ContainerProperties(org.apache.openejb.testing.ContainerProperties) Properties(java.util.Properties) URL(java.net.URL) Field(java.lang.reflect.Field) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) Jars(org.apache.openejb.testing.Jars)

Aggregations

Classes (org.apache.openejb.testing.Classes)12 Module (org.apache.openejb.testing.Module)10 EjbModule (org.apache.openejb.config.EjbModule)5 EjbJar (org.apache.openejb.jee.EjbJar)5 WebApp (org.apache.openejb.jee.WebApp)3 Method (java.lang.reflect.Method)2 Properties (java.util.Properties)2 ActivationConfig (org.apache.openejb.jee.ActivationConfig)2 Empty (org.apache.openejb.jee.Empty)2 MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)2 StatelessBean (org.apache.openejb.jee.StatelessBean)2 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)2 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)2 AnnotationFinder (org.apache.xbean.finder.AnnotationFinder)2 ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)2 Closeable (java.io.Closeable)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1