Search in sources :

Example 1 with MavenConfiguration

use of org.apache.meecrowave.doc.generator.MavenConfiguration in project meecrowave by apache.

the class JBake method main.

// if you want to switch off PDF generation use as arguments: src/main/jbake target/site-tmp true false
public static void main(final String[] args) throws Exception {
    // try to have parallelStream better than default
    System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "64");
    final File source = args == null || args.length < 1 ? new File("src/main/jbake") : new File(args[0]);
    final File pdfSource = new File(source, "content");
    final File destination = args == null || args.length < 2 ? new File("target/site-tmp") : new File(args[1]);
    // by default we dev
    final boolean startHttp = args == null || args.length < 2 || Boolean.parseBoolean(args[2]);
    // by default...too slow sorry
    final boolean skipPdf = args != null && args.length > 3 && !Boolean.parseBoolean(args[3]);
    // generation of dynamic content
    new Configuration().run();
    new CliConfiguration().run();
    new ArquillianConfiguration().run();
    new MavenConfiguration().run();
    new OAuth2Configuration().run();
    final Runnable build = () -> {
        System.out.println("Building Meecrowave website in " + destination);
        final Orient orient = Orient.instance();
        try {
            orient.startup();
            final Oven oven = new Oven(source, destination, new CompositeConfiguration() {

                {
                    final CompositeConfiguration config = new CompositeConfiguration();
                    config.addConfiguration(new MapConfiguration(new HashMap<String, Object>() {

                        {
                            put("asciidoctor.attributes", new ArrayList<String>() {

                                {
                                    add("source-highlighter=highlightjs");
                                    add("highlightjs-theme=idea");
                                    add("context_rootpath=/meecrowave");
                                }
                            });
                        }
                    }));
                    config.addConfiguration(ConfigUtil.load(source));
                    addConfiguration(config);
                }
            }, true);
            oven.setupPaths();
            System.out.println("  > baking");
            oven.bake();
            if (!skipPdf) {
                System.out.println("  > pdfifying");
                PDFify.generatePdf(pdfSource, destination);
            }
            System.out.println("  > done :)");
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            orient.shutdown();
        }
    };
    build.run();
    if (startHttp) {
        final Path watched = source.toPath();
        final WatchService watchService = watched.getFileSystem().newWatchService();
        watched.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        final AtomicBoolean run = new AtomicBoolean(true);
        final AtomicLong render = new AtomicLong(-1);
        final Thread renderingThread = new Thread() {

            {
                setName("jbake-renderer");
            }

            @Override
            public void run() {
                long last = System.currentTimeMillis();
                while (run.get()) {
                    if (render.get() > last) {
                        last = System.currentTimeMillis();
                        try {
                            build.run();
                        } catch (final Throwable oops) {
                            oops.printStackTrace();
                        }
                    }
                    try {
                        sleep(TimeUnit.SECONDS.toMillis(1));
                    } catch (final InterruptedException e) {
                        Thread.interrupted();
                        break;
                    }
                }
                System.out.println("Exiting renderer");
            }
        };
        final Thread watcherThread = new Thread() {

            {
                setName("jbake-file-watcher");
            }

            @Override
            public void run() {
                while (run.get()) {
                    try {
                        final WatchKey key = watchService.poll(1, TimeUnit.SECONDS);
                        if (key == null) {
                            continue;
                        }
                        for (final WatchEvent<?> event : key.pollEvents()) {
                            final WatchEvent.Kind<?> kind = event.kind();
                            if (kind != ENTRY_CREATE && kind != ENTRY_DELETE && kind != ENTRY_MODIFY) {
                                // unlikely but better to protect ourself
                                continue;
                            }
                            final Path updatedPath = Path.class.cast(event.context());
                            if (kind == ENTRY_DELETE || updatedPath.toFile().isFile()) {
                                final String path = updatedPath.toString();
                                if (!path.contains("___jb") && !path.endsWith("~")) {
                                    render.set(System.currentTimeMillis());
                                }
                            }
                        }
                        key.reset();
                    } catch (final InterruptedException e) {
                        Thread.interrupted();
                        run.compareAndSet(true, false);
                    } catch (final ClosedWatchServiceException cwse) {
                        if (!run.get()) {
                            throw new IllegalStateException(cwse);
                        }
                    }
                }
                System.out.println("Exiting file watcher");
            }
        };
        renderingThread.start();
        watcherThread.start();
        final Runnable onQuit = () -> {
            run.compareAndSet(true, false);
            Stream.of(watcherThread, renderingThread).forEach(thread -> {
                try {
                    thread.join();
                } catch (final InterruptedException e) {
                    Thread.interrupted();
                }
            });
            try {
                watchService.close();
            } catch (final IOException ioe) {
            // not important
            }
        };
        try (final Meecrowave container = new Meecrowave(new Meecrowave.Builder() {

            {
                setWebResourceCached(false);
            }
        }) {

            {
                start();
                deployWebapp("/meecrowave", destination);
            }
        }) {
            System.out.println("Started on http://localhost:" + container.getConfiguration().getHttpPort() + "/meecrowave");
            final Scanner console = new Scanner(System.in);
            String cmd;
            while (((cmd = console.nextLine())) != null) {
                if ("quit".equals(cmd)) {
                    break;
                } else if ("r".equals(cmd) || "rebuild".equals(cmd) || "build".equals(cmd) || "b".equals(cmd)) {
                    render.set(System.currentTimeMillis());
                } else {
                    System.err.println("Ignoring " + cmd + ", please use 'build' or 'quit'");
                }
            }
        }
        onQuit.run();
    }
}
Also used : OAuth2Configuration(org.apache.meecrowave.doc.generator.OAuth2Configuration) Orient(com.orientechnologies.orient.core.Orient) Scanner(java.util.Scanner) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) ENTRY_CREATE(java.nio.file.StandardWatchEventKinds.ENTRY_CREATE) ArrayList(java.util.ArrayList) WatchKey(java.nio.file.WatchKey) MapConfiguration(org.apache.commons.configuration.MapConfiguration) Path(java.nio.file.Path) ConfigUtil(org.jbake.app.ConfigUtil) ENTRY_MODIFY(java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException) CliConfiguration(org.apache.meecrowave.doc.generator.CliConfiguration) File(java.io.File) MavenConfiguration(org.apache.meecrowave.doc.generator.MavenConfiguration) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) WatchService(java.nio.file.WatchService) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) Stream(java.util.stream.Stream) Oven(org.jbake.app.Oven) ENTRY_DELETE(java.nio.file.StandardWatchEventKinds.ENTRY_DELETE) Meecrowave(org.apache.meecrowave.Meecrowave) ArquillianConfiguration(org.apache.meecrowave.doc.generator.ArquillianConfiguration) Configuration(org.apache.meecrowave.doc.generator.Configuration) Scanner(java.util.Scanner) OAuth2Configuration(org.apache.meecrowave.doc.generator.OAuth2Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) MapConfiguration(org.apache.commons.configuration.MapConfiguration) CliConfiguration(org.apache.meecrowave.doc.generator.CliConfiguration) MavenConfiguration(org.apache.meecrowave.doc.generator.MavenConfiguration) ArquillianConfiguration(org.apache.meecrowave.doc.generator.ArquillianConfiguration) Configuration(org.apache.meecrowave.doc.generator.Configuration) HashMap(java.util.HashMap) MapConfiguration(org.apache.commons.configuration.MapConfiguration) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) Oven(org.jbake.app.Oven) ArquillianConfiguration(org.apache.meecrowave.doc.generator.ArquillianConfiguration) OAuth2Configuration(org.apache.meecrowave.doc.generator.OAuth2Configuration) WatchEvent(java.nio.file.WatchEvent) CliConfiguration(org.apache.meecrowave.doc.generator.CliConfiguration) Meecrowave(org.apache.meecrowave.Meecrowave) Path(java.nio.file.Path) MavenConfiguration(org.apache.meecrowave.doc.generator.MavenConfiguration) Orient(com.orientechnologies.orient.core.Orient) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException) IOException(java.io.IOException) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) WatchService(java.nio.file.WatchService)

Aggregations

Orient (com.orientechnologies.orient.core.Orient)1 File (java.io.File)1 IOException (java.io.IOException)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 WatchEvent (java.nio.file.WatchEvent)1 WatchKey (java.nio.file.WatchKey)1 WatchService (java.nio.file.WatchService)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Scanner (java.util.Scanner)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Stream (java.util.stream.Stream)1 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)1 MapConfiguration (org.apache.commons.configuration.MapConfiguration)1