From 468d50b04fd6c93dfa24f6b0399f2c5d43832d6a Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 18 May 2011 21:03:10 +0200 Subject: [PATCH] implement basic config methods --- .../src/main/java/sonia/scm/cli/App.java | 67 ++++---- .../src/main/java/sonia/scm/cli/Command.java | 1 - .../java/sonia/scm/cli/CommandDescriptor.java | 2 - .../sonia/scm/cli/ConfigOptionHandler.java | 121 +++++++++++++ .../java/sonia/scm/cli/ScmClientConfig.java | 126 ++++++++++++++ .../main/java/sonia/scm/cli/ServerConfig.java | 162 ++++++++++++++++++ .../main/java/sonia/scm/cli/SubCommand.java | 57 +++--- ...dler.java => SubCommandOptionHandler.java} | 8 +- 8 files changed, 480 insertions(+), 64 deletions(-) create mode 100644 scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ConfigOptionHandler.java create mode 100644 scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ScmClientConfig.java create mode 100644 scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ServerConfig.java rename scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/{SubCommandHandler.java => SubCommandOptionHandler.java} (95%) diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/App.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/App.java index 4c9d3fb24b..494cf36af6 100644 --- a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/App.java +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/App.java @@ -43,8 +43,6 @@ import org.kohsuke.args4j.Option; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import sonia.scm.client.ScmClient; -import sonia.scm.client.ScmClientSession; import sonia.scm.util.IOUtil; import sonia.scm.util.Util; @@ -141,6 +139,8 @@ public class App System.exit(1); } + loadConfig(); + I18n i18n = new I18n(); if ((args.length == 0) || (subcommand == null) || help) @@ -149,25 +149,8 @@ public class App } else { - ScmClientSession session = null; - - if (subcommand.isSessionRequired()) - { - session = createSession(); - } - - try - { - subcommand.init(input, output, i18n, session); - subcommand.run(arguments); - } - finally - { - if (session != null) - { - session.close(); - } - } + subcommand.init(input, output, i18n, config); + subcommand.run(arguments); } IOUtil.close(input); @@ -177,27 +160,41 @@ public class App /** * Method description * - * - * @return */ - private ScmClientSession createSession() + private void loadConfig() { - ScmClientSession session = null; - - if (Util.isNotEmpty(username) && Util.isNotEmpty(password)) + if (config == null) { - session = ScmClient.createSession(serverUrl, username, password); - } - else - { - session = ScmClient.createSession(serverUrl); - } + config = ScmClientConfig.getInstance().getDefaultConfig(); - return session; + if (Util.isNotEmpty(serverUrl)) + { + config.setServerUrl(serverUrl); + } + + if (Util.isNotEmpty(username)) + { + config.setUsername(username); + } + + if (Util.isNotEmpty(password)) + { + config.setPassword(password); + } + } } //~--- fields --------------------------------------------------------------- + /** Field description */ + @Option( + name = "--config", + usage = "optionConfig", + metaVar = "metaVar_config", + aliases = { "-c" } + ) + private ServerConfig config; + /** Field description */ @Option( name = "--help", @@ -236,7 +233,7 @@ public class App @Argument( index = 0, metaVar = "metaVar_command", - handler = SubCommandHandler.class + handler = SubCommandOptionHandler.class ) private SubCommand subcommand; diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/Command.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/Command.java index c5b565f000..0dc6cae0f8 100644 --- a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/Command.java +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/Command.java @@ -49,5 +49,4 @@ public @interface Command { String value() default ""; String usage() default ""; - boolean sessionRequired() default true; } diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/CommandDescriptor.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/CommandDescriptor.java index 148c8c18eb..8c9630a1d4 100644 --- a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/CommandDescriptor.java +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/CommandDescriptor.java @@ -71,7 +71,6 @@ public class CommandDescriptor { this.name = cmd.value(); this.usage = cmd.usage(); - this.sessionRequired = cmd.sessionRequired(); } if (Util.isEmpty(name)) @@ -114,7 +113,6 @@ public class CommandDescriptor { command = commandClass.newInstance(); command.setName(name); - command.setSessionRequired(sessionRequired); } catch (Exception ex) { diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ConfigOptionHandler.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ConfigOptionHandler.java new file mode 100644 index 0000000000..bd1cff398b --- /dev/null +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ConfigOptionHandler.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cli; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.OptionDef; +import org.kohsuke.args4j.spi.OptionHandler; +import org.kohsuke.args4j.spi.Parameters; +import org.kohsuke.args4j.spi.Setter; + +/** + * + * @author Sebastian Sdorra + */ +public class ConfigOptionHandler extends OptionHandler +{ + + /** + * Constructs ... + * + * + * @param parser + * @param option + * @param setter + */ + public ConfigOptionHandler(CmdLineParser parser, OptionDef option, + Setter setter) + { + super(parser, option, setter); + loadClientConfig(); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param parameters + * + * @return + * + * @throws CmdLineException + */ + @Override + public int parseArguments(Parameters parameters) throws CmdLineException + { + String name = parameters.getParameter(0); + ServerConfig config = clientConfig.getConfig(name); + + setter.addValue(config); + + return 1; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public String getDefaultMetaVariable() + { + return "metaVar_config"; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + */ + private void loadClientConfig() + { + + // todo + clientConfig = new ScmClientConfig(); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private ScmClientConfig clientConfig; +} diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ScmClientConfig.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ScmClientConfig.java new file mode 100644 index 0000000000..834440a524 --- /dev/null +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ScmClientConfig.java @@ -0,0 +1,126 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cli; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Sebastian Sdorra + */ +public class ScmClientConfig +{ + + /** Field description */ + public static final String DEFAULT_NAME = "default"; + + /** Field description */ + private static volatile ScmClientConfig instance; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public ScmClientConfig() + { + this.serverConfigMap = new HashMap(); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public static ScmClientConfig getInstance() + { + if (instance == null) + { + synchronized (ScmClientConfig.class) + { + if (instance == null) + { + + // TODO load config + instance = new ScmClientConfig(); + } + } + } + + return instance; + } + + /** + * Method description + * + * + * @param name + * + * @return + */ + public ServerConfig getConfig(String name) + { + ServerConfig config = serverConfigMap.get(name); + + if (config == null) + { + config = new ServerConfig(); + } + + return config; + } + + /** + * Method description + * + * + * @return + */ + public ServerConfig getDefaultConfig() + { + return getConfig(DEFAULT_NAME); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Map serverConfigMap; +} diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ServerConfig.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ServerConfig.java new file mode 100644 index 0000000000..0c8fa5b54e --- /dev/null +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/ServerConfig.java @@ -0,0 +1,162 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.cli; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.Validateable; + +/** + * + * @author Sebastian Sdorra + */ +public class ServerConfig implements Validateable +{ + + /** + * Constructs ... + * + */ + public ServerConfig() {} + + /** + * Constructs ... + * + * + * @param serverUrl + * @param username + * @param password + */ + public ServerConfig(String serverUrl, String username, String password) + { + this.serverUrl = serverUrl; + this.username = username; + this.password = password; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + public String getPassword() + { + return password; + } + + /** + * Method description + * + * + * @return + */ + public String getServerUrl() + { + return serverUrl; + } + + /** + * Method description + * + * + * @return + */ + public String getUsername() + { + return username; + } + + /** + * Method description + * + * + * @return + */ + @Override + public boolean isValid() + { + + // TODO + return true; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param password + */ + public void setPassword(String password) + { + this.password = password; + } + + /** + * Method description + * + * + * @param serverUrl + */ + public void setServerUrl(String serverUrl) + { + this.serverUrl = serverUrl; + } + + /** + * Method description + * + * + * @param username + */ + public void setUsername(String username) + { + this.username = username; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private String password; + + /** Field description */ + private String serverUrl; + + /** Field description */ + private String username; +} diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommand.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommand.java index 48d2628612..b1206b6452 100644 --- a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommand.java +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommand.java @@ -42,7 +42,10 @@ import org.kohsuke.args4j.Option; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sonia.scm.client.ScmClient; import sonia.scm.client.ScmClientSession; +import sonia.scm.util.IOUtil; +import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ @@ -77,15 +80,15 @@ public abstract class SubCommand * @param input * @param output * @param i18n - * @param session + * @param config */ public void init(BufferedReader input, PrintWriter output, I18n i18n, - ScmClientSession session) + ServerConfig config) { this.input = input; this.output = output; this.i18n = i18n; - this.session = session; + this.config = config; } /** @@ -109,7 +112,14 @@ public abstract class SubCommand } else { - run(); + try + { + run(); + } + finally + { + IOUtil.close(session); + } } } catch (CmdLineException ex) @@ -133,17 +143,6 @@ public abstract class SubCommand return name; } - /** - * Method description - * - * - * @return - */ - public boolean isSessionRequired() - { - return sessionRequired; - } - //~--- set methods ---------------------------------------------------------- /** @@ -157,19 +156,36 @@ public abstract class SubCommand this.name = name; } + //~--- methods -------------------------------------------------------------- + /** - * Method description + * Method description * * - * @param sessionRequired + * @return */ - public void setSessionRequired(boolean sessionRequired) + protected ScmClientSession createSession() { - this.sessionRequired = sessionRequired; + if (Util.isNotEmpty(config.getUsername()) + && Util.isNotEmpty(config.getPassword())) + { + session = ScmClient.createSession(config.getServerUrl(), + config.getUsername(), + config.getPassword()); + } + else + { + session = ScmClient.createSession(config.getServerUrl()); + } + + return session; } //~--- fields --------------------------------------------------------------- + /** Field description */ + protected ServerConfig config; + /** Field description */ protected I18n i18n; @@ -192,7 +208,4 @@ public abstract class SubCommand aliases = { "-h" } ) private boolean help = false; - - /** Field description */ - private boolean sessionRequired = true; } diff --git a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommandHandler.java b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommandOptionHandler.java similarity index 95% rename from scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommandHandler.java rename to scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommandOptionHandler.java index 2c74b90c08..886fba41b2 100644 --- a/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommandHandler.java +++ b/scm-clients/scm-cli-client/src/main/java/sonia/scm/cli/SubCommandOptionHandler.java @@ -61,16 +61,16 @@ import java.util.Map; * * @author Sebastian Sdorra */ -public class SubCommandHandler extends OptionHandler +public class SubCommandOptionHandler extends OptionHandler { /** Field description */ public static final String RESOURCE_SERVICES = "/META-INF/services/".concat(SubCommand.class.getName()); - /** the logger for SubCommandHandler */ + /** the logger for SubCommandOptionHandler */ private static final Logger logger = - LoggerFactory.getLogger(SubCommandHandler.class); + LoggerFactory.getLogger(SubCommandOptionHandler.class); //~--- constructors --------------------------------------------------------- @@ -82,7 +82,7 @@ public class SubCommandHandler extends OptionHandler * @param option * @param setter */ - public SubCommandHandler(CmdLineParser parser, OptionDef option, + public SubCommandOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) { super(parser, option, setter);