Java Code Examples for org.apache.meecrowave.Meecrowave#Builder

The following examples show how to use org.apache.meecrowave.Meecrowave#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Injector.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
public static void injectConfig(final Meecrowave.Builder config, final Object test) {
    if (test == null) {
        return;
    }
    final Class<?> aClass = test.getClass();
    Stream.of(aClass.getDeclaredFields())
            .filter(f -> f.isAnnotationPresent(ConfigurationInject.class))
            .forEach(f -> {
                if (!f.isAccessible()) {
                    f.setAccessible(true);
                }
                try {
                    f.set(test, config);
                } catch (final IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            });
}
 
Example 2
Source File: ComponentConfigurationLoader.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
void doInit(@Observes final AfterDeploymentValidation afterDeploymentValidation, final Config config,
        final Meecrowave.Builder builder) {
    StreamSupport
            .stream(config.getConfigSources().spliterator(), false)
            .filter(ComponentConfigurationLoader.class::isInstance)
            .map(ComponentConfigurationLoader.class::cast)
            .forEach(it -> it.doInit(builder));
}
 
Example 3
Source File: Server.java    From tutorials with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    final Meecrowave.Builder builder = new Meecrowave.Builder();
    builder.setScanningPackageIncludes("com.baeldung.meecrowave");
    builder.setJaxrsMapping("/api/*");
    builder.setJsonpPrettify(true);

    try (Meecrowave meecrowave = new Meecrowave(builder)) {
        meecrowave.bake().await();
    }
}
 
Example 4
Source File: Cli.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private static Meecrowave.Builder buildConfig(final CommandLine line, final List<Field> fields,
                                              final Map<Object, List<Field>> propertiesOptions) {
    final Meecrowave.Builder config = new Meecrowave.Builder();
    bind(config, line, fields, config);
    propertiesOptions.forEach((o, f) -> {
        bind(config, line, f, o);
        config.setExtension(o.getClass(), o);
    });
    return config;
}
 
Example 5
Source File: Cli.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
public static Meecrowave.Builder create(final String[] args) {
    final ParsedCommand command = new ParsedCommand(args).invoke();
    if (command.isFailed()) {
        return null;
    }
    return command.getBuilder();
}
 
Example 6
Source File: Cli.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
public void run() {
    final ParsedCommand parsedCommand = new ParsedCommand(args).invoke();
    if (parsedCommand.isFailed()) {
        return;
    }

    final Meecrowave.Builder builder = parsedCommand.getBuilder();
    final CommandLine line = parsedCommand.getLine();
    try (final Meecrowave meecrowave = new Meecrowave(builder)) {
        synchronized (this) {
            if (closed) {
                return;
            }
            this.instance = meecrowave;
        }

        final String ctx = line.getOptionValue("context", "");
        final String fixedCtx = !ctx.isEmpty() && !ctx.startsWith("/") ? '/' + ctx : ctx;
        final String war = line.getOptionValue("webapp");
        meecrowave.start();
        if (war == null) {
            meecrowave.deployClasspath(new Meecrowave.DeploymentMeta(
                    ctx,
                    ofNullable(line.getOptionValue("docbase")).map(File::new).orElseGet(() ->
                            Stream.of("base", "home")
                                .map(it -> System.getProperty("meecrowave." + it))
                                .filter(Objects::nonNull)
                                .map(it -> new File(it, "docBase"))
                                .filter(File::isDirectory)
                                .findFirst()
                                .orElse(null)),
                    null));
        } else {
            meecrowave.deployWebapp(fixedCtx, new File(war));
        }
        doWait(meecrowave, line);
    }
}
 
Example 7
Source File: GenerateCertificateAndActivateHttpsTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private void assertCert(final Meecrowave.Builder builder) throws Exception {
    final KeyStore keyStore = KeyStore.getInstance(builder.getKeystoreType());
    try (final InputStream inputStream = new FileInputStream(builder.getKeystoreFile())) {
        keyStore.load(inputStream, builder.getKeystorePass().toCharArray());
    }
    final Key key = keyStore.getKey(builder.getKeyAlias(), builder.getKeystorePass().toCharArray());
    assertTrue(PrivateKey.class.isInstance(key));
    final Certificate certificate = keyStore.getCertificate(builder.getKeyAlias());
    assertTrue(X509Certificate.class.isInstance(certificate));
    final Certificate[] chain = keyStore.getCertificateChain(builder.getKeyAlias());
    assertEquals(1, chain.length);
    assertEquals(certificate, chain[0]);
}
 
Example 8
Source File: GenerateCertificateAndActivateHttpsTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Test
void generate() throws Exception {
    final File cert = new File(
            "target/test/GenerateCertificateAndActivateHttpsTest/cert_" + UUID.randomUUID().toString() + ".p12");
    final Map<String, String> systemPropsToSet = new HashMap<>();
    systemPropsToSet.put("talend.component.server.ssl.active", "true");
    systemPropsToSet.put("talend.component.server.ssl.keystore.location", cert.getPath());
    systemPropsToSet.put("talend.component.server.ssl.port", "1234");
    // ensure we don't init there the repo otherwise next tests will be broken (@MonoMeecrowave)
    systemPropsToSet.put("org.talend.sdk.component.server.test.InitTestInfra.skip", "true");

    final Collection<Runnable> reset = systemPropsToSet.entrySet().stream().map(it -> {
        final String property = System.getProperty(it.getKey());
        System.setProperty(it.getKey(), it.getValue());
        if (property == null) {
            return (Runnable) () -> System.clearProperty(it.getKey());
        }
        return (Runnable) () -> System.setProperty(it.getKey(), property);
    }).collect(toList());
    try {
        final Meecrowave.Builder builder = new Meecrowave.Builder();
        assertTrue(cert.exists());
        assertTrue(builder.isSsl());
        assertEquals(1234, builder.getHttpsPort());
        assertEquals("talend", builder.getKeyAlias());
        assertEquals("changeit", builder.getKeystorePass());
        assertEquals("PKCS12", builder.getKeystoreType());
        assertEquals(cert.getAbsolutePath(), builder.getKeystoreFile());
        assertCert(builder);
    } finally {
        reset.forEach(Runnable::run);
    }
}
 
Example 9
Source File: JpaExtension.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
void initBeans(@Observes final AfterDeploymentValidation adv, final BeanManager bm) {
    if (entityManagerBeans.isEmpty()) {
        return;
    }

    // only not portable part is this config read, could be optional
    final ServletContext sc = ServletContext.class.cast(bm.getReference(bm.resolve(bm.getBeans(ServletContext.class)), ServletContext.class, bm.createCreationalContext(null)));
    final Meecrowave.Builder config = Meecrowave.Builder.class.cast(sc.getAttribute("meecrowave.configuration"));
    final Map<String, String> props = new HashMap<>();
    if (config != null) {
        ofNullable(config.getProperties()).ifPresent(p -> p.stringPropertyNames().stream()
                .filter(k -> k.startsWith("jpa.property."))
                .forEach(k -> props.put(k.substring("jpa.property.".length()), p.getProperty(k))));
    }

    final Map<String, PersistenceUnitInfo> infoIndex = unitBuilders.stream()
            .map(bean -> {
                final CreationalContext<?> cc = bm.createCreationalContext(null);
                try {
                    final Bean<?> resolvedBean = bm.resolve(bm.getBeans(
                            PersistenceUnitInfoBuilder.class,
                            bean.getQualifiers().toArray(new Annotation[bean.getQualifiers().size()])));
                    final PersistenceUnitInfoBuilder builder = PersistenceUnitInfoBuilder.class.cast(
                            bm.getReference(resolvedBean, PersistenceUnitInfoBuilder.class, cc));
                    if (builder.getManagedClasses().isEmpty()) {
                        builder.setManagedClassNames(jpaClasses).setExcludeUnlistedClasses(true);
                    }
                    props.forEach(builder::addProperty);
                    return builder.toInfo();
                } finally {
                    cc.release();
                }
            }).collect(toMap(PersistenceUnitInfo::getPersistenceUnitName, identity()));

    entityManagerBeans.forEach((k, e) -> {
        PersistenceUnitInfo info = infoIndex.get(k.unitName);
        if (info == null) {
            info = tryCreateDefaultPersistenceUnit(k.unitName, bm, props);
        }
        if (info == null) { // not valid
            adv.addDeploymentProblem(new IllegalArgumentException("Didn't find any PersistenceUnitInfoBuilder for " + k));
        } else {
            e.init(info, bm);
        }
    });
}
 
Example 10
Source File: Client.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
private WebTarget target(final javax.ws.rs.client.Client client) {
    final Meecrowave.Builder config = CDI.current().select(Meecrowave.Builder.class).get();
    return client.target("http://localhost:" + config.getHttpPort() + "/api");
}
 
Example 11
Source File: ClientProducer.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Produces
@ApplicationScoped
public WebTarget webTarget(final Client client, final Meecrowave.Builder config) {
    return client.target(String.format("http://localhost:%d/api/v1", config.getHttpPort()));
}
 
Example 12
Source File: Cli.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public Meecrowave.Builder getBuilder() {
    return builder;
}
 
Example 13
Source File: MeecrowaveRule.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public MeecrowaveRule(final Meecrowave.Builder configuration, final String context) {
    this.configuration = configuration;
    this.context = context;
}
 
Example 14
Source File: MeecrowaveRule.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public Meecrowave.Builder getConfiguration() {
    return configuration;
}
 
Example 15
Source File: MonoMeecrowave.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
@Override
public Meecrowave.Builder getConfiguration() {
    return BASE.getConfiguration();
}
 
Example 16
Source File: MonoBase.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public Meecrowave.Builder doBoot() {
    final Meecrowave.Builder configuration = new Meecrowave.Builder().randomHttpPort().noShutdownHook(/*the rule does*/);
    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)
                    .min(Comparator.comparingInt(Configuration::order))
                    .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();
}
 
Example 17
Source File: MonoBase.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public Meecrowave.Builder getConfiguration() {
    return CONTAINER.get().configuration;
}
 
Example 18
Source File: MonoBase.java    From openwebbeans-meecrowave with Apache License 2.0 4 votes vote down vote up
public Meecrowave.Builder getConfiguration() {
    return configuration;
}
 
Example 19
Source File: WebServer.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
public WebServer onOpen(final Consumer<Meecrowave.Builder> task) {
    onOpen.add(task);
    return this;
}
 
Example 20
Source File: MeecrowaveRuleBase.java    From openwebbeans-meecrowave with Apache License 2.0 votes vote down vote up
public abstract Meecrowave.Builder getConfiguration();