Search in sources :

Example 41 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project meecrowave by apache.

the class Cli method toValue.

private static Object toValue(final Meecrowave.Builder builder, final String name, final String[] optionValues, final Class<?> type) {
    if (optionValues == null || optionValues.length == 0) {
        return null;
    }
    // decode options while it is strings
    IntStream.range(0, optionValues.length).forEach(i -> optionValues[i] = builder.getExtension(Meecrowave.ValueTransformers.class).apply(optionValues[i]));
    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 // org.foo.Impl:attr1=val1;attr2=val2
        "realm":
            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 // attr1=val1;attr2=val2
        "security-constraint":
            return Stream.of(optionValues).map(item -> {
                try {
                    final ObjectRecipe recipe = new ObjectRecipe(Meecrowave.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(Meecrowave.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 // org.foo.Impl:attr1=val1;attr2=val2
        "connector":
            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);
    }
}
Also used : IntStream(java.util.stream.IntStream) Connector(org.apache.catalina.connector.Connector) HashMap(java.util.HashMap) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultParser(org.apache.commons.cli.DefaultParser) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) CommandLine(org.apache.commons.cli.CommandLine) StreamSupport(java.util.stream.StreamSupport) Server(org.apache.catalina.Server) Properties(java.util.Properties) CommandLineParser(org.apache.commons.cli.CommandLineParser) Vetoed(javax.enterprise.inject.Vetoed) Optional.ofNullable(java.util.Optional.ofNullable) ServiceLoader(java.util.ServiceLoader) StandardServer(org.apache.catalina.core.StandardServer) Field(java.lang.reflect.Field) File(java.io.File) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) ParseException(org.apache.commons.cli.ParseException) Function.identity(java.util.function.Function.identity) CliOption(org.apache.meecrowave.runner.cli.CliOption) Optional(java.util.Optional) Meecrowave(org.apache.meecrowave.Meecrowave) Connector(org.apache.catalina.connector.Connector) HashMap(java.util.HashMap) Properties(java.util.Properties) ParseException(org.apache.commons.cli.ParseException) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) File(java.io.File) Meecrowave(org.apache.meecrowave.Meecrowave)

Aggregations

ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)41 Map (java.util.Map)18 HashMap (java.util.HashMap)13 Properties (java.util.Properties)12 IOException (java.io.IOException)11 OpenEJBException (org.apache.openejb.OpenEJBException)9 NamingException (javax.naming.NamingException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 Set (java.util.Set)4 TreeMap (java.util.TreeMap)4 ApplicationException (org.apache.openejb.ApplicationException)4 SystemException (org.apache.openejb.SystemException)4 SuperProperties (org.apache.openejb.util.SuperProperties)4 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 TimeoutException (java.util.concurrent.TimeoutException)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 Realm (org.apache.catalina.Realm)3 Connector (org.apache.catalina.connector.Connector)3