Author: rmannibucau
Date: Tue Oct 18 21:45:28 2016
New Revision: 1765518

URL: http://svn.apache.org/viewvc?rev=1765518&view=rev
Log:
basic main

Added:
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/cli/
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/cli/CliOption.java
Modified:
    openwebbeans/microwave/trunk/microwave-core/pom.xml
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java

Modified: openwebbeans/microwave/trunk/microwave-core/pom.xml
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/pom.xml?rev=1765518&r1=1765517&r2=1765518&view=diff
==============================================================================
--- openwebbeans/microwave/trunk/microwave-core/pom.xml (original)
+++ openwebbeans/microwave/trunk/microwave-core/pom.xml Tue Oct 18 21:45:28 2016
@@ -78,6 +78,12 @@
     </dependency>
 
     <dependency>
+      <groupId>commons-cli</groupId>
+      <artifactId>commons-cli</artifactId>
+      <version>1.2</version>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.4</version>

Modified: 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java?rev=1765518&r1=1765517&r2=1765518&view=diff
==============================================================================
--- 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
 (original)
+++ 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
 Tue Oct 18 21:45:28 2016
@@ -44,6 +44,7 @@ import org.apache.coyote.http2.Http2Prot
 import org.apache.cxf.helpers.FileUtils;
 import org.apache.microwave.cxf.CxfCdiAutoSetup;
 import org.apache.microwave.openwebbeans.OWBAutoSetup;
+import org.apache.microwave.runner.cli.CliOption;
 import org.apache.microwave.tomcat.ProvidedLoader;
 import org.apache.tomcat.util.descriptor.web.LoginConfig;
 import org.apache.tomcat.util.descriptor.web.SecurityCollection;
@@ -492,36 +493,97 @@ public class Microwave implements AutoCl
     @Data
     @Accessors(fluent = true)
     public static class Builder {
+        @CliOption(name = "http", description = "HTTP port")
         private int httpPort = 8080;
+
+        @CliOption(name = "https", description = "HTTPS port")
         private int httpsPort = 8443;
-        private int stopPort = 8005;
+
+        @CliOption(name = "stop", description = "Shutdown port if used or -1")
+        private int stopPort = -1;
+
+        @CliOption(name = "host", description = "Default host")
         private String host = "localhost";
+
+        @CliOption(name = "dir", description = "Root folder if provided 
otherwise a fake one is created in tmp-dir")
         protected String dir;
+
+        @CliOption(name = "server-xml", description = "Provided server.xml")
         private File serverXml;
+
+        @CliOption(name = "keep-server-xml-as-this", description = "Don't 
replace ports in server.xml")
         private boolean keepServerXmlAsThis;
+
+        @CliOption(name = "properties", description = "Passthrough properties")
         private Properties properties = new Properties();
+
+        @CliOption(name = "quick-session", description = "Should an unsecured 
but fast session id generator be used")
         private boolean quickSession = true;
+
+        @CliOption(name = "skip-http", description = "Skip HTTP connector")
         private boolean skipHttp;
+
+        @CliOption(name = "ssl", description = "Use HTTPS")
         private boolean ssl;
+
+        @CliOption(name = "keystore-file", description = "HTTPS keystore 
location")
         private String keystoreFile;
+
+        @CliOption(name = "keystore-password", description = "HTTPS keystore 
password")
         private String keystorePass;
+
+        @CliOption(name = "keystore-type", description = "HTTPS keystore type")
         private String keystoreType = "JKS";
+
+        @CliOption(name = "client-auth", description = "HTTPS keystore client 
authentication")
         private String clientAuth;
+
+        @CliOption(name = "keystore-alias", description = "HTTPS keystore 
alias")
         private String keyAlias;
+
+        @CliOption(name = "ssl-protocol", description = "HTTPS protocol")
         private String sslProtocol;
+
+        @CliOption(name = "web-xml", description = "Global web.xml")
         private String webXml;
+
+        @CliOption(name = "login-config", description = "web.xml login config")
         private LoginConfigBuilder loginConfig;
+
+        @CliOption(name = "security-constraint", description = "web.xml 
security constraint")
         private Collection<SecurityConstaintBuilder> securityConstraints = new 
LinkedList<>();
+
+        @CliOption(name = "realm", description = "realm")
         private Realm realm;
+
+        @CliOption(name = "users", description = "In memory users")
         private Map<String, String> users;
+
+        @CliOption(name = "roles", description = "In memory roles")
         private Map<String, String> roles;
+
+        @CliOption(name = "http2", description = "Activate HTTP 2")
         private boolean http2;
+
+        @CliOption(name = "connector", description = "Custom connectors")
         private final Collection<Connector> connectors = new ArrayList<>();
+
+        @CliOption(name = "tmp-dir", description = "Temporary directory")
         private String tempDir = new 
File(System.getProperty("java.io.tmpdir"), "microwave_" + 
System.nanoTime()).getAbsolutePath();
+
+        @CliOption(name = "web-resource-cached", description = "Cache web 
resources")
         private boolean webResourceCached = true;
+
+        @CliOption(name = "conf", description = "Conf folder to synchronize")
         private String conf;
+
+        @CliOption(name = "delete-on-startup", description = "Should the 
directory be cleaned on startup if existing")
         private boolean deleteBaseOnStartup = true;
+
+        @CliOption(name = "jaxrs-mapping", description = "Default jaxrs 
mapping")
         private String jaxrsMapping = "/*";
+
+        @CliOption(name = "cdi-conversation", description = "Should CDI 
conversation be activated")
         private boolean cdiConversation;
 
         public Builder() { // load defaults

Added: 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java?rev=1765518&view=auto
==============================================================================
--- 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
 (added)
+++ 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
 Tue Oct 18 21:45:28 2016
@@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.microwave.runner;
+
+import lombok.NoArgsConstructor;
+import org.apache.catalina.connector.Connector;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.apache.microwave.Microwave;
+import org.apache.microwave.runner.cli.CliOption;
+import org.apache.xbean.recipe.ObjectRecipe;
+
+import javax.enterprise.inject.Vetoed;
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.stream.Stream;
+
+import static java.util.Optional.ofNullable;
+import static java.util.stream.Collectors.toList;
+import static lombok.AccessLevel.PRIVATE;
+
+@Vetoed
+@NoArgsConstructor(access = PRIVATE)
+public class Cli {
+    public static void main(final String[] args) {
+        final Options options = new Options();
+        options.addOption("help", false, "Show help");
+        final List<Field> fields = 
Stream.of(Microwave.Builder.class.getDeclaredFields())
+                .filter(f -> f.isAnnotationPresent(CliOption.class))
+                .collect(toList());
+        fields.forEach(f -> {
+            final CliOption opt = f.getAnnotation(CliOption.class);
+            options.addOption(null, opt.name(), f.getType() != boolean.class, 
opt.description());
+        });
+
+        final CommandLineParser parser = new PosixParser();
+        final CommandLine line;
+        try {
+            line = parser.parse(options, args, true);
+        } catch (final ParseException exp) {
+            help(options);
+            return;
+        }
+
+        if (line.hasOption("help")) {
+            help(options);
+            return;
+        }
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        final Thread hook = new Thread() {
+            @Override
+            public void run() {
+                latch.countDown();
+            }
+        };
+        Runtime.getRuntime().addShutdownHook(hook);
+        try (final Microwave microwave = new Microwave(buildConfig(line, 
fields)).bake()) {
+            try {
+                latch.await();
+            } catch (final InterruptedException e) {
+                Thread.interrupted();
+            }
+        } finally {
+            try {
+                Runtime.getRuntime().removeShutdownHook(hook);
+            } catch (final IllegalStateException ise) {
+                // no-op
+            }
+        }
+    }
+
+    private static Microwave.Builder buildConfig(final CommandLine line, final 
List<Field> fields) {
+        final Microwave.Builder config = new Microwave.Builder();
+        fields.forEach(f -> {
+            final CliOption opt = f.getAnnotation(CliOption.class);
+            final String name = opt.name();
+            if (line.hasOption(name)) {
+                ofNullable(f.getType() == boolean.class ?
+                        
ofNullable(line.getOptionValue(name)).map(Boolean::parseBoolean).orElse(true) :
+                        toValue(name, line.getOptionValues(name), f.getType()))
+                        .ifPresent(v -> {
+                            try {
+                                f.set(config, v);
+                            } catch (final IllegalAccessException e) {
+                                throw new IllegalStateException(e);
+                            }
+                        });
+            }
+        });
+        return config;
+    }
+
+    /*
+    Collection<SecurityConstaintBuilder>
+    Collection<Connector>
+    LoginConfigBuilder
+    Realm
+     */
+    private static Object toValue(final String name, final String[] 
optionValues, final Class<?> type) {
+        if (optionValues == null || optionValues.length == 0) {
+            return null;
+        }
+        if (String.class == type) {
+            return optionValues[0];
+        }
+        if (int.class == type) {
+            return Integer.parseInt(optionValues[0]);
+        }
+        if (File.class == type) {
+            return new File(optionValues[0]);
+        }
+        if (Properties.class == type) {
+            final Properties props = new Properties();
+            Stream.of(optionValues).map(v -> v.split("=")).forEach(v -> 
props.setProperty(v[0], v[1]));
+            return props;
+        }
+        if (Map.class == type) {
+            final Map<String, String> props = new HashMap<>();
+            Stream.of(optionValues).map(v -> v.split("=")).forEach(v -> 
props.put(v[0], v[1]));
+            return props;
+        }
+
+        final ClassLoader loader = 
Thread.currentThread().getContextClassLoader();
+        switch (name) {
+            case "realm": // org.foo.Impl:attr1=val1;attr2=val2
+                try {
+                    int end = optionValues[0].indexOf(':');
+                    if (end < 0) {
+                        return loader.loadClass(optionValues[0]).newInstance();
+                    }
+                    final ObjectRecipe recipe = new 
ObjectRecipe(optionValues[0].substring(0, end));
+                    Stream.of(optionValues[0].substring(end + 1, 
optionValues[0].length()).split(";"))
+                            .map(v -> v.split("="))
+                            .forEach(v -> recipe.setProperty(v[0], v[1]));
+                    return recipe.create(loader);
+                } catch (final Exception cnfe) {
+                    throw new IllegalArgumentException(optionValues[0]);
+                }
+            case "security-constraint": // attr1=val1;attr2=val2
+                return Stream.of(optionValues)
+                        .map(item -> {
+                            try {
+                                final ObjectRecipe recipe = new 
ObjectRecipe(Microwave.SecurityConstaintBuilder.class);
+                                Stream.of(item.split(";"))
+                                        .map(v -> v.split("="))
+                                        .forEach(v -> recipe.setProperty(v[0], 
v[1]));
+                                return recipe.create(loader);
+                            } catch (final Exception cnfe) {
+                                throw new 
IllegalArgumentException(optionValues[0]);
+                            }
+                        }).collect(toList());
+            case "login-config":
+                try {
+                    final ObjectRecipe recipe = new 
ObjectRecipe(Microwave.LoginConfigBuilder.class);
+                    Stream.of(optionValues[0].split(";"))
+                            .map(v -> v.split("="))
+                            .forEach(v -> recipe.setProperty(v[0], v[1]));
+                    return recipe.create(loader);
+                } catch (final Exception cnfe) {
+                    throw new IllegalArgumentException(optionValues[0]);
+                }
+            case "connector": // org.foo.Impl:attr1=val1;attr2=val2
+                return Stream.of(optionValues)
+                        .map(v -> {
+                            try {
+                                int end = v.indexOf(':');
+                                if (end < 0) {
+                                    return new Connector(v);
+                                }
+                                final Connector connector = new 
Connector(optionValues[0].substring(0, end));
+                                Stream.of(v.substring(end + 1, 
v.length()).split(";"))
+                                        .map(i -> i.split("="))
+                                        .forEach(i -> 
connector.setProperty(i[0], i[1]));
+                                return connector;
+                            } catch (final Exception cnfe) {
+                                throw new 
IllegalArgumentException(optionValues[0]);
+                            }
+                        }).collect(toList());
+            default:
+                throw new IllegalArgumentException("Unsupported " + name);
+        }
+        return null;
+    }
+
+    private static void help(final Options options) {
+        new HelpFormatter().printHelp("java -jar microwave.jar", options);
+    }
+}

Added: 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/cli/CliOption.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/cli/CliOption.java?rev=1765518&view=auto
==============================================================================
--- 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/cli/CliOption.java
 (added)
+++ 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/cli/CliOption.java
 Tue Oct 18 21:45:28 2016
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.microwave.runner.cli;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target(FIELD)
+@Retention(RUNTIME)
+public @interface CliOption {
+    String name();
+    String description();
+}


Reply via email to