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();
}
}
}
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());
}
}
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);
}
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();
}
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);
}
}
Aggregations