Search in sources :

Example 6 with Meecrowave

use of org.apache.meecrowave.Meecrowave in project meecrowave by apache.

the class ServiceTest method bval.

@Test
public void bval() {
    try (final Meecrowave container = new Meecrowave(new Meecrowave.Builder().includePackages(Service.class.getPackage().getName()).randomHttpPort()).bake()) {
        final String uri = "http://localhost:" + container.getConfiguration().getHttpPort() + "/test";
        final Client client = ClientBuilder.newClient();
        try {
            assertEquals("{\"value\":\"ok\"}", client.target(uri).queryParam("val", "ok").request(APPLICATION_JSON_TYPE).get(String.class));
            assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), client.target(uri).request(APPLICATION_JSON_TYPE).get().getStatus());
        } finally {
            client.close();
        }
    }
}
Also used : ClientBuilder(javax.ws.rs.client.ClientBuilder) Client(javax.ws.rs.client.Client) Meecrowave(org.apache.meecrowave.Meecrowave) Test(org.junit.Test)

Example 7 with Meecrowave

use of org.apache.meecrowave.Meecrowave in project meecrowave by apache.

the class MeecrowaveClientLifecycleListenerTest method autoClose.

@Test
public void autoClose() throws IOException, NoSuchFieldException, IllegalAccessException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder().randomHttpPort().includePackages(MeecrowaveClientLifecycleListenerTest.class.getName())).bake()) {
        final JohnzonCdiExtension johnzonCdiExtension = CDI.current().select(JohnzonCdiExtension.class).get();
        final Field jsonbs = JohnzonCdiExtension.class.getDeclaredField("jsonbs");
        jsonbs.setAccessible(true);
        final BeanManager beanManager = CDI.current().getBeanManager();
        final JohnzonCdiExtension extensionInstance = JohnzonCdiExtension.class.cast(beanManager.getContext(ApplicationScoped.class).get(beanManager.resolve(beanManager.getBeans(JohnzonCdiExtension.class))));
        final Collection<?> o = Collection.class.cast(jsonbs.get(extensionInstance));
        {
            // ensure server is init whatever test suite we run in
            final Client client = ClientBuilder.newClient();
            get(meecrowave, client);
            client.close();
        }
        final int origin = o.size();
        final Client client = ClientBuilder.newClient();
        final JsonbJaxrsProvider<?> provider = new JsonbJaxrsProvider<>();
        client.register(provider);
        get(meecrowave, client);
        assertEquals(origin + 1, o.size());
        client.close();
        assertEquals(origin, o.size());
    }
}
Also used : Field(java.lang.reflect.Field) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) ClientBuilder(javax.ws.rs.client.ClientBuilder) Client(javax.ws.rs.client.Client) BeanManager(javax.enterprise.inject.spi.BeanManager) Meecrowave(org.apache.meecrowave.Meecrowave) JohnzonCdiExtension(org.apache.johnzon.jsonb.cdi.JohnzonCdiExtension) Test(org.junit.Test)

Example 8 with Meecrowave

use of org.apache.meecrowave.Meecrowave in project meecrowave by apache.

the class MeecrowaveExtension method beforeAll.

@Override
public void beforeAll(final ExtensionContext context) throws Exception {
    final Meecrowave.Builder builder = new Meecrowave.Builder();
    final Optional<MeecrowaveConfig> meecrowaveConfig = context.getElement().map(e -> e.getAnnotation(MeecrowaveConfig.class));
    final String ctx;
    if (meecrowaveConfig.isPresent()) {
        final MeecrowaveConfig config = meecrowaveConfig.get();
        ctx = config.context();
        for (final Method method : MeecrowaveConfig.class.getMethods()) {
            if (MeecrowaveConfig.class != method.getDeclaringClass()) {
                continue;
            }
            try {
                final Object value = method.invoke(config);
                final Field configField = Meecrowave.Builder.class.getDeclaredField(method.getName());
                if (!configField.isAccessible()) {
                    configField.setAccessible(true);
                }
                if (value != null && (!String.class.isInstance(value) || !value.toString().isEmpty())) {
                    if (!configField.isAccessible()) {
                        configField.setAccessible(true);
                    }
                    configField.set(builder, File.class == configField.getType() ? /*we use string instead */
                    new File(value.toString()) : value);
                }
            } catch (final NoSuchFieldException nsfe) {
            // ignored
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }
        if (builder.getHttpPort() < 0) {
            builder.randomHttpPort();
        }
    } else {
        ctx = "";
    }
    final Meecrowave meecrowave = new Meecrowave(builder);
    context.getStore(NAMESPACE).put(Meecrowave.class.getName(), meecrowave);
    context.getStore(NAMESPACE).put(Meecrowave.Builder.class.getName(), builder);
    meecrowave.bake(ctx);
}
Also used : Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) File(java.io.File) Meecrowave(org.apache.meecrowave.Meecrowave)

Example 9 with Meecrowave

use of org.apache.meecrowave.Meecrowave in project meecrowave by apache.

the class MonoBase method doBoot.

public Meecrowave.Builder doBoot() {
    final Meecrowave.Builder configuration = new Meecrowave.Builder().randomHttpPort().noShutdownHook();
    boolean unlocked = false;
    ClassLoaderLock.LOCK.lock();
    try {
        final ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
        ClassLoaderLock.LOCK.lock();
        final ClassLoader containerLoader = ClassLoaderLock.getUsableContainerLoader();
        final Meecrowave meecrowave = new Meecrowave(configuration);
        if (CONTAINER.compareAndSet(null, new Instance(meecrowave, configuration, containerLoader))) {
            final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class).spliterator(), false).sorted(Comparator.comparingInt(Configuration::order)).findFirst().orElseGet(() -> new Configuration() {
            });
            runnerConfig.beforeStarts();
            final File war = runnerConfig.application();
            final Thread thread = Thread.currentThread();
            if (containerLoader != originalCL) {
                thread.setContextClassLoader(containerLoader);
            }
            try {
                if (war == null) {
                    meecrowave.bake(runnerConfig.context());
                } else {
                    meecrowave.deployWebapp(runnerConfig.context(), runnerConfig.application());
                }
            } finally {
                if (containerLoader != originalCL) {
                    thread.setContextClassLoader(originalCL);
                }
            }
            ClassLoaderLock.LOCK.unlock();
            unlocked = true;
            runnerConfig.afterStarts();
            Runtime.getRuntime().addShutdownHook(new Thread() {

                {
                    setName("Meecrowave-mono-rule-stopping");
                }

                @Override
                public void run() {
                    try {
                        runnerConfig.beforeStops();
                    } finally {
                        try {
                            meecrowave.close();
                        } finally {
                            runnerConfig.afterStops();
                        }
                    }
                }
            });
        }
    } finally {
        if (!unlocked) {
            ClassLoaderLock.LOCK.unlock();
        }
    }
    return getConfiguration();
}
Also used : File(java.io.File) Meecrowave(org.apache.meecrowave.Meecrowave)

Example 10 with Meecrowave

use of org.apache.meecrowave.Meecrowave in project meecrowave by apache.

the class MeecrowaveRunMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().warn("Mojo skipped");
        return;
    }
    final Thread thread = Thread.currentThread();
    final ClassLoader loader = thread.getContextClassLoader();
    final ClassLoader appLoader = createClassLoader(loader);
    thread.setContextClassLoader(appLoader);
    try {
        final Meecrowave.Builder builder = getConfig();
        try (final Meecrowave meecrowave = new Meecrowave(builder) {

            @Override
            protected void beforeStart() {
                scriptCustomization(jsCustomizers, "js", singletonMap("meecrowaveBase", base.getAbsolutePath()));
            }
        }) {
            meecrowave.start();
            final String fixedContext = ofNullable(context).orElse("");
            final Meecrowave.DeploymentMeta deploymentMeta = new Meecrowave.DeploymentMeta(fixedContext, webapp != null && webapp.isDirectory() ? webapp : null, jsContextCustomizer == null ? null : ctx -> scriptCustomization(singletonList(jsContextCustomizer), "js", singletonMap("context", ctx)));
            if (useClasspathDeployment) {
                meecrowave.deployClasspath(deploymentMeta);
            } else {
                meecrowave.deployWebapp(deploymentMeta);
            }
            new Scanner(System.in).next();
        }
    } finally {
        if (forceLog4j2Shutdown) {
            LogManager.shutdown();
        }
        if (appLoader != loader) {
            try {
                URLClassLoader.class.cast(appLoader).close();
            } catch (final IOException e) {
                getLog().warn(e.getMessage(), e);
            }
        }
        thread.setContextClassLoader(loader);
    }
}
Also used : URL(java.net.URL) Scanner(java.util.Scanner) Parameter(org.apache.maven.plugins.annotations.Parameter) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) MavenProject(org.apache.maven.project.MavenProject) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact) Collections.singletonMap(java.util.Collections.singletonMap) LinkedList(java.util.LinkedList) ScriptException(javax.script.ScriptException) MalformedURLException(java.net.MalformedURLException) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Field(java.lang.reflect.Field) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SimpleBindings(javax.script.SimpleBindings) RUNTIME_PLUS_SYSTEM(org.apache.maven.plugins.annotations.ResolutionScope.RUNTIME_PLUS_SYSTEM) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine) Meecrowave(org.apache.meecrowave.Meecrowave) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Scanner(java.util.Scanner) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) Meecrowave(org.apache.meecrowave.Meecrowave)

Aggregations

Meecrowave (org.apache.meecrowave.Meecrowave)12 File (java.io.File)5 Field (java.lang.reflect.Field)3 ClientBuilder (javax.ws.rs.client.ClientBuilder)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Scanner (java.util.Scanner)2 Client (javax.ws.rs.client.Client)2 Orient (com.orientechnologies.orient.core.Orient)1 StringReader (java.io.StringReader)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)1 Path (java.nio.file.Path)1 ENTRY_CREATE (java.nio.file.StandardWatchEventKinds.ENTRY_CREATE)1 ENTRY_DELETE (java.nio.file.StandardWatchEventKinds.ENTRY_DELETE)1 ENTRY_MODIFY (java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY)1