Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial import of new cli tools, typo fixes.
- Loading branch information
Showing
7 changed files
with
341 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, ActionValue> ACTIONS = new HashMap<>(); | ||
|
||
{ | ||
//todo actions | ||
} | ||
|
||
private static class ActionValue { | ||
|
||
Command command; | ||
Class<? extends Action> action; | ||
|
||
ActionValue(Command command, Class<? extends Action> 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<String, ActionValue> 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<String, ActionValue> 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 <T extends Command, A extends Action<T>> Action createAction(Class<A> clazz, T command, JCommander commander) | ||
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { | ||
|
||
Constructor<? extends Action> constructor = clazz.getConstructor(command.getClass(), JCommander.class); | ||
return constructor.newInstance(command, commander); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/action/Action.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T extends Command> { | ||
|
||
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; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
cli/seppuku/src/main/java/com/evolveum/midpoint/cli/seppuku/command/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters