diff --git a/cli/seppuku/pom.xml b/cli/seppuku/pom.xml index ccf16e0498f..df633f75e18 100644 --- a/cli/seppuku/pom.xml +++ b/cli/seppuku/pom.xml @@ -83,7 +83,7 @@ - com.evolveum.midpoint.tools.seppuku.Main + com.evolveum.midpoint.cli.seppuku.Main diff --git a/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/Main.java b/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/Main.java new file mode 100644 index 00000000000..2473870affd --- /dev/null +++ b/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/Main.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2010-2015 Evolveum + * + * Licensed 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 com.evolveum.midpoint.cli.seppuku; + +import com.beust.jcommander.JCommander; +import com.evolveum.midpoint.cli.common.DefaultCommand; +import com.evolveum.midpoint.cli.common.ToolsUtils; +import com.evolveum.midpoint.cli.seppuku.action.Action; +import com.evolveum.midpoint.cli.seppuku.command.Command; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Viliam Repan (lazyman) + */ +public class Main { + + private static final Logger LOG = LoggerFactory.getLogger(Main.class); + + private final Map ACTIONS = new HashMap<>(); + + { + //todo actions + } + + private static class ActionValue { + + Command command; + Class action; + + ActionValue(Command command, Class action) { + this.command = command; + this.action = action; + } + } + + public static void main(String[] args) { + new Main().start(args); + } + + private static final Logger STD_OUT = LoggerFactory.getLogger(ToolsUtils.LOGGER_SYS_OUT); + private static final Logger STD_ERR = LoggerFactory.getLogger(ToolsUtils.LOGGER_SYS_ERR); + + private void start(String[] args) { + LOG.debug("Arguments: {}", Arrays.toString(args)); + + DefaultCommand def = new DefaultCommand(); + JCommander commander = new JCommander(def); + + for (Map.Entry entry : ACTIONS.entrySet()) { + ActionValue value = entry.getValue(); + commander.addCommand(entry.getKey(), value.command); + } + + commander.parse(args); + + String cmd = commander.getParsedCommand(); + LOG.debug("Parsed command: '{}'", cmd); + if (StringUtils.isEmpty(cmd)) { + if (def.isVersion()) { + printVersion(); + } + if (def.isHelp()) { + printHelp(commander); + } + return; + } + + ActionValue actionValue = null; + for (Map.Entry entry : ACTIONS.entrySet()) { + if (cmd != null && cmd.equals(entry.getKey())) { + actionValue = entry.getValue(); + break; + } + } + + if (actionValue == null) { + printHelp(commander); + return; + } + + try { + LOG.debug("Executing action {} with params {}", actionValue.action.getSimpleName(), actionValue.command); + + Action action = createAction(actionValue.action, actionValue.command, commander); + action.execute(); + } catch (Exception ex) { + handleError(ex); + } + } + + private void printVersion() { + try { + String version = ToolsUtils.loadVersion(); + STD_OUT.info(version); + } catch (IOException ex) { + handleError(ex); + } + } + + private void handleError(Exception ex) { + //todo error handling + throw new RuntimeException(ex); + } + + private void printHelp(JCommander commander) { + StringBuilder sb = new StringBuilder(); + commander.usage(sb); + + STD_OUT.info(sb.toString()); + } + + private > Action createAction(Class clazz, T command, JCommander commander) + throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + + Constructor constructor = clazz.getConstructor(command.getClass(), JCommander.class); + return constructor.newInstance(command, commander); + } +} diff --git a/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/action/Action.java b/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/action/Action.java new file mode 100644 index 00000000000..97f1af73c14 --- /dev/null +++ b/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/action/Action.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2010-2015 Evolveum + * + * Licensed 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 com.evolveum.midpoint.cli.seppuku.action; + +import com.beust.jcommander.JCommander; +import com.evolveum.midpoint.cli.common.ToolsUtils; +import com.evolveum.midpoint.cli.seppuku.command.Command; +import org.apache.commons.lang.Validate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author Viliam Repan (lazyman) + */ +public abstract class Action { + + protected Logger STD_OUT = LoggerFactory.getLogger(ToolsUtils.LOGGER_SYS_OUT); + protected Logger STD_ERR = LoggerFactory.getLogger(ToolsUtils.LOGGER_SYS_ERR); + + private JCommander commander; + private T params; + + public Action(T params, JCommander commander) { + Validate.notNull(params, "Action parameters must not be null."); + Validate.notNull(commander, "Commander must not be null."); + + this.params = params; + this.commander = commander; + } + + public T getParams() { + return params; + } + + public void execute() throws Exception { + if (params.isHelp()) { + StringBuilder sb = new StringBuilder(); + commander.usage(commander.getParsedCommand(), sb); + + STD_OUT.info(sb.toString()); + + return; + } + + executeAction(); + } + + protected abstract void executeAction() throws Exception; + + protected ApplicationContext createApplicationContext() { + ApplicationContext ctx = new ClassPathXmlApplicationContext(); + + + return ctx; + } +} diff --git a/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/command/Command.java b/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/command/Command.java new file mode 100644 index 00000000000..281e0365528 --- /dev/null +++ b/cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/command/Command.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2010-2015 Evolveum + * + * Licensed 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 com.evolveum.midpoint.cli.seppuku.command; + +import com.beust.jcommander.Parameter; +import com.evolveum.midpoint.cli.common.UrlConverter; + +import java.net.URL; + +/** + * @author Viliam Repan (lazyman) + */ +public class Command { + + public static final String P_HELP = "-h"; + public static final String P_HELP_LONG = "--help"; + + public static final String P_VERBOSE = "-v"; + public static final String P_VERBOSE_LONG = "--verbose"; + + public static final String P_URL = "-U"; + public static final String P_URL_LONG = "--url"; + + public static final String P_USERNAME = "-u"; + public static final String P_USERNAME_LONG = "--username"; + + public static final String P_PASSWORD = "-p"; + public static final String P_PASSWORD_LONG = "--password"; + + public static final String P_ASK_PASSWORD = "-P"; + public static final String P_ASK_PASSWORD_LONG = "--password-ask"; + + public static final String P_MIDPOINT_HOME = "-m"; + public static final String P_MIDPOINT_HOME_LONG = "--midpoint-home"; + + @Parameter(names = {P_HELP, P_HELP_LONG}, help = true, + description = "Print this help") + private boolean help = false; + + @Parameter(names = {P_VERBOSE, P_VERBOSE_LONG}, + description = "Verbose output") + private boolean verbose = false; + + /** + * OPTIONS FOR JDBC CLIENT CONNECTION + */ + @Parameter(names = {P_URL, P_URL_LONG}, converter = UrlConverter.class, + validateWith = UrlConverter.class, required = true, + description = "Url to MidPoint model webservice endpoint") + private URL url; + + @Parameter(names = {P_USERNAME, P_USERNAME_LONG}, required = true, + description = "Username for MidPoint webservice login") + private String username; + + @Parameter(names = {P_PASSWORD, P_PASSWORD_LONG}, + description = "Password for MidPoint webservice login") + private String password; + + @Parameter(names = {P_ASK_PASSWORD, P_ASK_PASSWORD_LONG}, password = true, + echoInput = true, description = "Password for MidPoint webservice login") + private String askPassword; + + @Parameter(names = {P_MIDPOINT_HOME, P_MIDPOINT_HOME_LONG}, + description = "Path to MidPoint home folder") + private String midpointHome; + + public String getAskPassword() { + return askPassword; + } + + public boolean isHelp() { + return help; + } + + public String getPassword() { + return password; + } + + public URL getUrl() { + return url; + } + + public String getUsername() { + return username; + } + + public boolean isVerbose() { + return verbose; + } + + public String getMidpointHome() { + return midpointHome; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("Command{"); + sb.append("askPassword='").append(askPassword).append('\''); + sb.append(", help=").append(help); + sb.append(", verbose=").append(verbose); + sb.append(", url=").append(url); + sb.append(", username='").append(username).append('\''); + sb.append(", password='").append(password).append('\''); + sb.append(", midpointHome='").append(password).append('\''); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cli/seppuku/src/main/resources/logback-seppuku.xml b/cli/seppuku/src/main/resources/logback-seppuku.xml index 7598b7785d4..b08887e522f 100644 --- a/cli/seppuku/src/main/resources/logback-seppuku.xml +++ b/cli/seppuku/src/main/resources/logback-seppuku.xml @@ -15,5 +15,5 @@ ~ limitations under the License. --> - + \ No newline at end of file diff --git a/cli/seppuku/src/main/resources/logback.xml b/cli/seppuku/src/main/resources/logback.xml index 6060bb19f3c..eecbeac2c48 100644 --- a/cli/seppuku/src/main/resources/logback.xml +++ b/cli/seppuku/src/main/resources/logback.xml @@ -29,7 +29,7 @@ - + true ninja.log @@ -56,7 +56,7 @@ - + \ No newline at end of file diff --git a/cli/seppuku/src/test/resources/logback-test.xml b/cli/seppuku/src/test/resources/logback-test.xml index 70b6ee06153..10d8128db6c 100644 --- a/cli/seppuku/src/test/resources/logback-test.xml +++ b/cli/seppuku/src/test/resources/logback-test.xml @@ -34,7 +34,7 @@ - + \ No newline at end of file