ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akuznet...@apache.org
Subject ignite git commit: IGNITE-7485 Added support for authentication parameters in control.sh.
Date Mon, 12 Feb 2018 03:52:06 GMT
Repository: ignite
Updated Branches:
  refs/heads/master a5a907b80 -> f53c48aa7


IGNITE-7485 Added support for authentication parameters in control.sh.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f53c48aa
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f53c48aa
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f53c48aa

Branch: refs/heads/master
Commit: f53c48aa731f1c69513c5ea474bb1e18de1b81f5
Parents: a5a907b
Author: Sergey Kosarev <macrergate@gmail.com>
Authored: Mon Feb 12 10:51:57 2018 +0700
Committer: Alexey Kuznetsov <akuznetsov@apache.org>
Committed: Mon Feb 12 10:51:57 2018 +0700

----------------------------------------------------------------------
 .../internal/client/impl/GridClientImpl.java    |  28 +--
 .../GridClientConnectionManagerAdapter.java     |  11 +-
 .../connection/GridClientNioTcpConnection.java  |   8 +-
 .../client/router/impl/GridTcpRouterImpl.java   |  25 +-
 .../ignite/internal/commandline/Arguments.java  | 117 ++++++++++
 .../internal/commandline/CommandHandler.java    | 232 +++++++++++--------
 .../commandline/CommandHandlerParsingTest.java  | 118 ++++++++++
 .../testsuites/IgniteUtilSelfTestSuite.java     |   2 +
 .../ignite/util/GridCommandHandlerTest.java     |  35 +--
 9 files changed, 405 insertions(+), 171 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/main/java/org/apache/ignite/internal/client/impl/GridClientImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/GridClientImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/GridClientImpl.java
index 9b71ae4..d045a62 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/GridClientImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/GridClientImpl.java
@@ -17,8 +17,6 @@
 
 package org.apache.ignite.internal.client.impl;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -40,6 +38,7 @@ import org.apache.ignite.IgniteSystemProperties;
 import org.apache.ignite.internal.client.GridClient;
 import org.apache.ignite.internal.client.GridClientCacheMode;
 import org.apache.ignite.internal.client.GridClientClosedException;
+import org.apache.ignite.internal.client.GridClientClusterState;
 import org.apache.ignite.internal.client.GridClientCompute;
 import org.apache.ignite.internal.client.GridClientConfiguration;
 import org.apache.ignite.internal.client.GridClientData;
@@ -48,7 +47,6 @@ import org.apache.ignite.internal.client.GridClientDataConfiguration;
 import org.apache.ignite.internal.client.GridClientDisconnectedException;
 import org.apache.ignite.internal.client.GridClientException;
 import org.apache.ignite.internal.client.GridClientFactory;
-import org.apache.ignite.internal.client.GridClientClusterState;
 import org.apache.ignite.internal.client.GridClientNode;
 import org.apache.ignite.internal.client.GridClientPartitionAffinity;
 import org.apache.ignite.internal.client.GridClientPredicate;
@@ -70,10 +68,6 @@ import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_MACS;
  * Client implementation.
  */
 public class GridClientImpl implements GridClient {
-    /** Enterprise connection manager class name. */
-    private static final String ENT_CONN_MGR_CLS =
-        "org.apache.ignite.internal.client.impl.connection.GridClientConnectionManagerEntImpl";
-
     /** Null mask object. */
     private static final Object NULL_MASK = new Object();
 
@@ -445,25 +439,7 @@ public class GridClientImpl implements GridClient {
         GridClientConfiguration cfg, Collection<InetSocketAddress> routers, GridClientTopology
top,
         @Nullable Byte marshId, boolean routerClient)
         throws GridClientException {
-        GridClientConnectionManager mgr;
-
-        try {
-            Class<?> cls = Class.forName(ENT_CONN_MGR_CLS);
-
-            Constructor<?> cons = cls.getConstructor(UUID.class, SSLContext.class,
GridClientConfiguration.class,
-                Collection.class, GridClientTopology.class, Byte.class, boolean.class);
-
-            mgr = (GridClientConnectionManager)cons.newInstance(clientId, sslCtx, cfg, routers,
top, marshId,
-                routerClient);
-        }
-        catch (ClassNotFoundException ignored) {
-            mgr = new GridClientConnectionManagerOsImpl(clientId, sslCtx, cfg, routers, top,
marshId, routerClient);
-        }
-        catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
-            throw new GridClientException("Failed to create client connection manager.",
e);
-        }
-
-        return mgr;
+        return new GridClientConnectionManagerOsImpl(clientId, sslCtx, cfg, routers, top,
marshId, routerClient);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java
b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java
index aa06322..87980b9 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java
@@ -472,7 +472,7 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo
                 try {
                     conn = new GridClientNioTcpConnection(srv, clientId, addr, sslCtx, pingExecutor,
                         cfg.getConnectTimeout(), cfg.getPingInterval(), cfg.getPingTimeout(),
-                        cfg.isTcpNoDelay(), marsh, marshId, top, cred, keepBinariesThreadLocal());
+                        cfg.isTcpNoDelay(), marsh, marshId, top, cred);
                 }
                 catch (GridClientException e) {
                     if (marsh instanceof GridClientZipOptimizedMarshaller) {
@@ -482,7 +482,7 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo
                         conn = new GridClientNioTcpConnection(srv, clientId, addr, sslCtx,
pingExecutor,
                             cfg.getConnectTimeout(), cfg.getPingInterval(), cfg.getPingTimeout(),
                             cfg.isTcpNoDelay(), ((GridClientZipOptimizedMarshaller)marsh).defaultMarshaller(),
marshId,
-                            top, cred, keepBinariesThreadLocal());
+                            top, cred);
                     }
                     else
                         throw e;
@@ -506,13 +506,6 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo
         }
     }
 
-    /**
-     * @return Get thread local used to enable keep binary mode.
-     */
-    protected ThreadLocal<Boolean> keepBinariesThreadLocal() {
-        return null;
-    }
-
     /** {@inheritDoc} */
     @Override public void terminateConnection(GridClientConnection conn, GridClientNode node,
Throwable e) {
         if (log.isLoggable(Level.FINE))

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientNioTcpConnection.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientNioTcpConnection.java
b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientNioTcpConnection.java
index 3bedd5f..e117cc8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientNioTcpConnection.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientNioTcpConnection.java
@@ -158,9 +158,6 @@ public class GridClientNioTcpConnection extends GridClientConnection {
     /** Marshaller. */
     private final GridClientMarshaller marsh;
 
-    /** */
-    private final ThreadLocal<Boolean> keepBinariesMode;
-
     /**
      * Creates a client facade, tries to connect to remote server, in case of success starts
reader thread.
      *
@@ -192,8 +189,7 @@ public class GridClientNioTcpConnection extends GridClientConnection {
         GridClientMarshaller marsh,
         Byte marshId,
         GridClientTopology top,
-        Object cred,
-        ThreadLocal<Boolean> keepBinariesMode
+        Object cred
     ) throws IOException, GridClientException {
         super(clientId, srvAddr, sslCtx, top, cred);
 
@@ -202,7 +198,6 @@ public class GridClientNioTcpConnection extends GridClientConnection {
         this.marsh = marsh;
         this.pingInterval = pingInterval;
         this.pingTimeout = pingTimeout;
-        this.keepBinariesMode = keepBinariesMode;
 
         SocketChannel ch = null;
         Socket sock = null;
@@ -1062,7 +1057,6 @@ public class GridClientNioTcpConnection extends GridClientConnection
{
         private GridClientMessage pendingMsg;
 
         /** Flag indicating whether authentication retry was attempted for this request.
*/
-        @SuppressWarnings("RedundantFieldInitialization")
         private int authRetry = STATE_INITIAL;
 
         /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java
index da55ec7..d6daf99 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java
@@ -18,8 +18,6 @@
 package org.apache.ignite.internal.client.router.impl;
 
 import java.lang.management.ManagementFactory;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteOrder;
@@ -53,9 +51,6 @@ import org.jetbrains.annotations.Nullable;
  * Wrapper class for router process.
  */
 public class GridTcpRouterImpl implements GridTcpRouter, GridTcpRouterMBean, LifecycleAware
{
-    /** */
-    private static final String ENT_NIO_LSNR_CLS = "org.apache.ignite.client.router.impl.GridTcpRouterNioListenerEntImpl";
-
     /** Id. */
     private final UUID id = UUID.randomUUID();
 
@@ -108,23 +103,7 @@ public class GridTcpRouterImpl implements GridTcpRouter, GridTcpRouterMBean,
Lif
             throw new IgniteException("Failed to initialise embedded client.", e);
         }
 
-        GridNioServerListener<GridClientMessage> lsnr;
-
-        try {
-            Class<?> cls = Class.forName(ENT_NIO_LSNR_CLS);
-
-            Constructor<?> cons = cls.getDeclaredConstructor(IgniteLogger.class, GridRouterClientImpl.class);
-
-            cons.setAccessible(true);
-
-            lsnr = (GridNioServerListener<GridClientMessage>)cons.newInstance(log,
client);
-        }
-        catch (ClassNotFoundException ignored) {
-            lsnr = new GridTcpRouterNioListenerOsImpl(log, client);
-        }
-        catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
-            throw new IgniteException("Failed to create NIO listener.", e);
-        }
+        GridNioServerListener<GridClientMessage> lsnr = new GridTcpRouterNioListenerOsImpl(log,
client);
 
         parser = new GridTcpRouterNioParser();
 
@@ -379,4 +358,4 @@ public class GridTcpRouterImpl implements GridTcpRouter, GridTcpRouterMBean,
Lif
 
         return id.equals(that.id);
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/main/java/org/apache/ignite/internal/commandline/Arguments.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/commandline/Arguments.java
b/modules/core/src/main/java/org/apache/ignite/internal/commandline/Arguments.java
new file mode 100644
index 0000000..6a9a3f7
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/commandline/Arguments.java
@@ -0,0 +1,117 @@
+/*
+ * 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.ignite.internal.commandline;
+
+/**
+ * Bean with all parsed and validated arguments.
+ */
+public class Arguments {
+    /** Command. */
+    private String cmd;
+
+    /** Host. */
+    private String host;
+
+    /** Port. */
+    private String port;
+
+    /** User. */
+    private String user;
+
+    /** Password. */
+    private String pwd;
+
+    /**
+     * Action for baseline command.
+     */
+    private String baselineAct;
+
+    /**
+     * Arguments for baseline command.
+     */
+    private String baselineArgs;
+
+    /**
+     * @param cmd Command.
+     * @param host Host.
+     * @param port Port.
+     * @param user User.
+     * @param pwd Password.
+     * @param baselineAct Baseline action.
+     * @param baselineArgs Baseline args.
+     */
+    public Arguments(String cmd, String host, String port, String user, String pwd, String
baselineAct,
+        String baselineArgs) {
+        this.cmd = cmd;
+        this.host = host;
+        this.port = port;
+        this.user = user;
+        this.pwd = pwd;
+        this.baselineAct = baselineAct;
+        this.baselineArgs = baselineArgs;
+    }
+
+    /**
+     * @return command
+     */
+    public String command() {
+        return cmd;
+    }
+
+    /**
+     * @return host name
+     */
+    public String host() {
+        return host;
+    }
+
+    /**
+     * @return port number
+     */
+    public String port() {
+        return port;
+    }
+
+    /**
+     * @return user name
+     */
+    public String user() {
+        return user;
+    }
+
+    /**
+     * @return password
+     */
+    public String password() {
+        return pwd;
+    }
+
+    /**
+     * @return baseline action
+     */
+    public String baselineAction() {
+        return baselineAct;
+    }
+
+    /**
+     * @return baseline arguments
+     */
+    public String baselineArguments() {
+        return baselineArgs;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/main/java/org/apache/ignite/internal/commandline/CommandHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/commandline/CommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/commandline/CommandHandler.java
index cf331f8..5895ad2 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/commandline/CommandHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/commandline/CommandHandler.java
@@ -44,6 +44,9 @@ import org.apache.ignite.internal.visor.baseline.VisorBaselineOperation;
 import org.apache.ignite.internal.visor.baseline.VisorBaselineTask;
 import org.apache.ignite.internal.visor.baseline.VisorBaselineTaskArg;
 import org.apache.ignite.internal.visor.baseline.VisorBaselineTaskResult;
+import org.apache.ignite.plugin.security.SecurityCredentials;
+import org.apache.ignite.plugin.security.SecurityCredentialsBasicProvider;
+import org.jetbrains.annotations.NotNull;
 
 import static org.apache.ignite.internal.IgniteVersionUtils.ACK_VER_STR;
 import static org.apache.ignite.internal.IgniteVersionUtils.COPYRIGHT;
@@ -58,10 +61,10 @@ import static org.apache.ignite.internal.visor.baseline.VisorBaselineOperation.V
  */
 public class CommandHandler {
     /** */
-    private static final String DFLT_HOST = "127.0.0.1";
+    static final String DFLT_HOST = "127.0.0.1";
 
     /** */
-    private static final String DFLT_PORT = "11211";
+    static final String DFLT_PORT = "11211";
 
     /** */
     private static final String CMD_HELP = "--help";
@@ -73,16 +76,10 @@ public class CommandHandler {
     private static final String CMD_PORT = "--port";
 
     /** */
-    private static final String CMD_ACTIVATE = "--activate";
+    private static final String CMD_PASSWORD = "--password";
 
     /** */
-    private static final String CMD_DEACTIVATE = "--deactivate";
-
-    /** */
-    private static final String CMD_STATE = "--state";
-
-    /** */
-    private static final String CMD_BASE_LINE = "--baseline";
+    private static final String CMD_USER = "--user";
 
     /** */
     private static final String BASELINE_ADD = "add";
@@ -100,6 +97,18 @@ public class CommandHandler {
     private static final String DELIM = "--------------------------------------------------------------------------------";
 
     /** */
+    static final String CMD_ACTIVATE = "--activate";
+
+    /** */
+    static final String CMD_BASE_LINE = "--baseline";
+
+    /** */
+    static final String CMD_DEACTIVATE = "--deactivate";
+
+    /** */
+    static final String CMD_STATE = "--state";
+
+    /** */
     public static final int EXIT_CODE_OK = 0;
 
     /** */
@@ -158,18 +167,6 @@ public class CommandHandler {
     }
 
     /**
-     * Print command usage.
-     *
-     * @param desc Command description.
-     * @param cmd Command.
-     */
-    private void usage(String desc, String cmd) {
-        log(desc);
-        log("    control.sh [--host HOST_OR_IP] [--port PORT] " + cmd);
-        nl();
-    }
-
-    /**
      * Extract next argument.
      *
      * @param it Arguments iterator.
@@ -190,6 +187,102 @@ public class CommandHandler {
     }
 
     /**
+     * Parses and validates arguments.
+     *
+     * @param rawArgs Array of arguments.
+     * @return Arguments bean.
+     * @throws IllegalArgumentException In case arguments aren't valid.
+     */
+    @NotNull Arguments parseAndValidate(String... rawArgs) {
+        String host = DFLT_HOST;
+
+        String port = DFLT_PORT;
+
+        String user = null;
+
+        String pwd = null;
+
+        String baselineAct = "";
+
+        String baselineArgs = "";
+
+        List<String> commands = new ArrayList<>();
+
+        Iterator<String> it = Arrays.asList(rawArgs).iterator();
+
+        while (it.hasNext()) {
+            String str = it.next().toLowerCase();
+
+            switch (str) {
+                case CMD_HOST:
+                    host = nextArg(it, "Expected host name");
+                    break;
+
+                case CMD_PORT:
+                    port = nextArg(it, "Expected port number");
+
+                    try {
+                        int p = Integer.parseInt(port);
+
+                        if (p <= 0 || p > 65535)
+                            throw new IllegalArgumentException("Invalid value for port: "
+ port);
+                    }
+                    catch (NumberFormatException ignored) {
+                        throw new IllegalArgumentException("Invalid value for port: " + port);
+                    }
+                    break;
+
+                case CMD_USER:
+                    user = nextArg(it, "Expected user name");
+                    break;
+
+                case CMD_PASSWORD:
+                    pwd = nextArg(it, "Expected password");
+                    break;
+
+                case CMD_ACTIVATE:
+                case CMD_DEACTIVATE:
+                case CMD_STATE:
+                    commands.add(str);
+                    break;
+
+                case CMD_BASE_LINE:
+                    commands.add(CMD_BASE_LINE);
+
+                    if (it.hasNext()) {
+                        baselineAct = it.next().toLowerCase();
+
+                        if (BASELINE_ADD.equals(baselineAct) || BASELINE_REMOVE.equals(baselineAct)
||
+                            BASELINE_SET.equals(baselineAct) || BASELINE_SET_VERSION.equals(baselineAct))
+                            baselineArgs = nextArg(it, "Expected baseline arguments");
+                        else
+                            throw new IllegalArgumentException("Unexpected argument for "
+ CMD_BASE_LINE + ": "
+                                + baselineAct);
+                    }
+
+            }
+        }
+
+        int sz = commands.size();
+
+        if (sz < 1)
+            throw new IllegalArgumentException("No action was specified");
+
+        if (sz > 1)
+            throw new IllegalArgumentException("Only one action can be specified, but found:
" + sz);
+
+        String cmd = commands.get(0);
+
+        boolean hasUsr = F.isEmpty(user);
+        boolean hasPwd = F.isEmpty(pwd);
+
+        if (hasUsr != hasPwd)
+            throw new IllegalArgumentException("Both user and password should be specified");
+
+        return new Arguments(cmd, host, port, user, pwd, baselineAct, baselineArgs);
+    }
+
+    /**
      * Activate cluster.
      *
      * @param client Client.
@@ -501,19 +594,31 @@ public class CommandHandler {
     }
 
     /**
+     * Print command usage.
+     *
+     * @param desc Command description.
+     * @param cmd Command.
+     */
+    private void usage(String desc, String cmd) {
+        log(desc);
+        log("    control.sh [--host HOST_OR_IP] [--port PORT] [--user USER] [--password PASSWORD]
" + cmd);
+        nl();
+    }
+
+    /**
      * Parse and execute command.
      *
-     * @param args Arguments to parse and execute.
+     * @param rawArgs Arguments to parse and execute.
      * @return Exit code.
      */
-    public int execute(String... args) {
+    public int execute(String... rawArgs) {
         log("Control utility [ver. " + ACK_VER_STR + "]");
         log(COPYRIGHT);
         log("User: " + System.getProperty("user.name"));
         log(DELIM);
 
         try {
-            if (F.isEmpty(args) || (args.length == 1 && CMD_HELP.equalsIgnoreCase(args[0]))){
+            if (F.isEmpty(rawArgs) || (rawArgs.length == 1 && CMD_HELP.equalsIgnoreCase(rawArgs[0])))
{
                 log("This utility can do the following commands:");
 
                 usage("  Activate cluster:", CMD_ACTIVATE);
@@ -540,79 +645,20 @@ public class CommandHandler {
                 return EXIT_CODE_OK;
             }
 
-            String host = DFLT_HOST;
-
-            String port = DFLT_PORT;
-
-            String baselineAct = "";
-
-            String baselineArgs = "";
-
-            List<String> commands = new ArrayList<>();
-
-            Iterator<String> it = Arrays.asList(args).iterator();
-
-            while (it.hasNext()) {
-                String str = it.next().toLowerCase();
-
-                switch (str) {
-                    case CMD_HOST:
-                        host = nextArg(it, "Expected host name");
-                        break;
-
-                    case CMD_PORT:
-                        port = nextArg(it, "Expected port number");
-
-                        try {
-                            int p = Integer.parseInt(port);
-
-                            if (p <= 0 || p > 65535)
-                                throw new IllegalArgumentException("Invalid value for port:
" + port);
-                        }
-                        catch (NumberFormatException ignored) {
-                            throw new IllegalArgumentException("Invalid value for port: "
+ port);
-                        }
-                        break;
+            Arguments args = parseAndValidate(rawArgs);
 
-                    case CMD_ACTIVATE:
-                    case CMD_DEACTIVATE:
-                    case CMD_STATE:
-                        commands.add(str);
-                        break;
-
-                    case CMD_BASE_LINE:
-                        commands.add(CMD_BASE_LINE);
-
-                        if (it.hasNext()) {
-                            baselineAct = it.next().toLowerCase();
+            GridClientConfiguration cfg = new GridClientConfiguration();
 
-                            if (BASELINE_ADD.equals(baselineAct) || BASELINE_REMOVE.equals(baselineAct)
||
-                                BASELINE_SET.equals(baselineAct) || BASELINE_SET_VERSION.equals(baselineAct))
-                                baselineArgs = nextArg(it, "Expected baseline arguments");
-                            else
-                                throw new IllegalArgumentException("Unexpected argument for
" + CMD_BASE_LINE + ": "
-                                        + baselineAct);
-                        }
+            cfg.setServers(Collections.singletonList(args.host() + ":" + args.port()));
 
-                }
+            if (!F.isEmpty(args.user())) {
+                cfg.setSecurityCredentialsProvider(
+                    new SecurityCredentialsBasicProvider(new SecurityCredentials(args.user(),
args.password())));
             }
 
-            int sz = commands.size();
-
-            if (sz < 1)
-                throw new IllegalArgumentException("No action was specified");
-
-            if (sz > 1)
-                throw new IllegalArgumentException("Only one action can be specified, but
found: " + sz);
-
-            GridClientConfiguration cfg = new GridClientConfiguration();
-
-            cfg.setServers(Collections.singletonList(host + ":" + port));
-
             try (GridClient client = GridClientFactory.start(cfg)) {
-                String cmd = commands.get(0);
 
-                switch (cmd) {
+                switch (args.command()) {
                     case CMD_ACTIVATE:
                         activate(client);
                         break;
@@ -626,7 +672,7 @@ public class CommandHandler {
                         break;
 
                     case CMD_BASE_LINE:
-                        baseline(client, baselineAct, baselineArgs);
+                        baseline(client, args.baselineAction(), args.baselineArguments());
                         break;
                 }
             }
@@ -643,7 +689,6 @@ public class CommandHandler {
             if (isConnectionError(e))
                 return error(EXIT_CODE_CONNECTION_FAILED, "Connection to cluster failed.",
e);
 
-
             return error(EXIT_CODE_UNEXPECTED_ERROR, "", e);
         }
     }
@@ -657,3 +702,4 @@ public class CommandHandler {
         System.exit(hnd.execute(args));
     }
 }
+

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/test/java/org/apache/ignite/internal/commandline/CommandHandlerParsingTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/commandline/CommandHandlerParsingTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/commandline/CommandHandlerParsingTest.java
new file mode 100644
index 0000000..1ba8b3d
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/commandline/CommandHandlerParsingTest.java
@@ -0,0 +1,118 @@
+/*
+ *
+ *  * 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.ignite.internal.commandline;
+
+import junit.framework.TestCase;
+
+import static org.apache.ignite.internal.commandline.CommandHandler.CMD_ACTIVATE;
+import static org.apache.ignite.internal.commandline.CommandHandler.CMD_BASE_LINE;
+import static org.apache.ignite.internal.commandline.CommandHandler.CMD_DEACTIVATE;
+import static org.apache.ignite.internal.commandline.CommandHandler.CMD_STATE;
+import static org.apache.ignite.internal.commandline.CommandHandler.DFLT_HOST;
+import static org.apache.ignite.internal.commandline.CommandHandler.DFLT_PORT;
+
+/**
+ * Tests Command Handler parsing arguments.
+ */
+public class CommandHandlerParsingTest extends TestCase {
+    /** Commands to test. */
+    private static final String[] Commands = new String[] {CMD_STATE, CMD_ACTIVATE, CMD_DEACTIVATE,
CMD_BASE_LINE};
+
+    /**
+     * Test parsing and validation for user and password arguments
+     */
+    public void testParseAndValidateUserAndPassword() {
+        CommandHandler hnd = new CommandHandler();
+
+        for (String cmd : Commands) {
+            try {
+                hnd.parseAndValidate("--user");
+
+                fail("expected exception: Expected user name");
+            }
+            catch (IllegalArgumentException e) {
+                e.printStackTrace();
+            }
+
+            try {
+                hnd.parseAndValidate("--password");
+
+                fail("expected exception: Expected password");
+            }
+            catch (IllegalArgumentException e) {
+                e.printStackTrace();
+            }
+
+            try {
+                hnd.parseAndValidate("--user", "testUser", cmd);
+
+                fail("expected exception: Both user and password should be specified");
+            }
+            catch (IllegalArgumentException e) {
+                e.printStackTrace();
+            }
+
+            try {
+                hnd.parseAndValidate("--password", "testPass", cmd);
+
+                fail("expected exception: Both user and password should be specified");
+            }
+            catch (IllegalArgumentException e) {
+                e.printStackTrace();
+            }
+
+            Arguments args = hnd.parseAndValidate("--user", "testUser", "--password", "testPass",
cmd);
+
+            assertEquals("testUser", args.user());
+            assertEquals("testPass", args.password());
+            assertEquals(cmd, args.command());
+        }
+    }
+
+    /**
+     * tests host and port arguments
+     */
+    public void testHostAndPort() {
+        CommandHandler hnd = new CommandHandler();
+
+        for (String cmd : Commands) {
+            Arguments args = hnd.parseAndValidate(cmd);
+
+            assertEquals(cmd, args.command());
+            assertEquals(DFLT_HOST, args.host());
+            assertEquals(DFLT_PORT, args.port());
+
+            args = hnd.parseAndValidate("--port", "12345", "--host", "test-host", cmd);
+
+            assertEquals(cmd, args.command());
+            assertEquals("test-host", args.host());
+            assertEquals("12345", args.port());
+
+            try {
+                hnd.parseAndValidate("--port", "wrong-port", cmd);
+
+                fail("expected exception: Invalid value for port:");
+            }
+            catch (IllegalArgumentException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
index e524d04..e3be1e3 100644
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
@@ -19,6 +19,7 @@ package org.apache.ignite.testsuites;
 
 import java.util.Set;
 import junit.framework.TestSuite;
+import org.apache.ignite.internal.commandline.CommandHandlerParsingTest;
 import org.apache.ignite.internal.pagemem.impl.PageIdUtilsSelfTest;
 import org.apache.ignite.internal.processors.cache.GridCacheUtilsSelfTest;
 import org.apache.ignite.internal.util.GridArraysSelfTest;
@@ -113,6 +114,7 @@ public class IgniteUtilSelfTestSuite extends TestSuite {
         suite.addTestSuite(PageIdUtilsSelfTest.class);
 
         // control.sh
+        suite.addTestSuite(CommandHandlerParsingTest.class);
         suite.addTestSuite(GridCommandHandlerTest.class);
 
         return suite;

http://git-wip-us.apache.org/repos/asf/ignite/blob/f53c48aa/modules/core/src/test/java/org/apache/ignite/util/GridCommandHandlerTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/util/GridCommandHandlerTest.java
b/modules/core/src/test/java/org/apache/ignite/util/GridCommandHandlerTest.java
index b119bfa..a4e9ac1 100644
--- a/modules/core/src/test/java/org/apache/ignite/util/GridCommandHandlerTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/util/GridCommandHandlerTest.java
@@ -96,12 +96,21 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         CommandHandler cmd = new CommandHandler();
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--activate"));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--activate"));
 
         assertTrue(ignite.active());
     }
 
     /**
+     * @param cmd CommandHandler
+     * @param args arguments
+     * @return result of execution
+     */
+    protected int execute(CommandHandler cmd, String... args) {
+        return cmd.execute(args);
+    }
+
+    /**
      * Test deactivation works via control.sh
      *
      * @throws Exception If failed.
@@ -117,7 +126,7 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         CommandHandler cmd = new CommandHandler();
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--deactivate"));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--deactivate"));
 
         assertFalse(ignite.active());
     }
@@ -134,11 +143,11 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         CommandHandler cmd = new CommandHandler();
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--state"));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--state"));
 
         ignite.active(true);
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--state"));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--state"));
     }
 
     /**
@@ -155,7 +164,7 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         CommandHandler cmd = new CommandHandler();
 
-        cmd.execute("--baseline");
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline"));
 
         assertEquals(1, ignite.cluster().currentBaselineTopology().size());
     }
@@ -195,8 +204,8 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         Ignite other = startGrid(2);
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline", "add", consistentIds(other)));
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline", "add", consistentIds(other)));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline", "add", consistentIds(other)));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline", "add", consistentIds(other)));
 
         assertEquals(2, ignite.cluster().currentBaselineTopology().size());
     }
@@ -220,8 +229,8 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         CommandHandler cmd = new CommandHandler();
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline"));
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline", "remove", offlineNodeConsId));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline"));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline", "remove", offlineNodeConsId));
 
         assertEquals(1, ignite.cluster().currentBaselineTopology().size());
     }
@@ -242,11 +251,11 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         CommandHandler cmd = new CommandHandler();
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline", "set", consistentIds(ignite,
other)));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline", "set", consistentIds(ignite,
other)));
 
         assertEquals(2, ignite.cluster().currentBaselineTopology().size());
 
-        assertEquals(EXIT_CODE_UNEXPECTED_ERROR, cmd.execute("--baseline", "set", "invalidConsistentId"));
+        assertEquals(EXIT_CODE_UNEXPECTED_ERROR, execute(cmd, "--baseline", "set", "invalidConsistentId"));
     }
 
     /**
@@ -265,9 +274,9 @@ public class GridCommandHandlerTest extends GridCommonAbstractTest {
 
         startGrid(2);
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline"));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline"));
 
-        assertEquals(EXIT_CODE_OK, cmd.execute("--baseline", "version", String.valueOf(ignite.cluster().topologyVersion())));
+        assertEquals(EXIT_CODE_OK, execute(cmd, "--baseline", "version", String.valueOf(ignite.cluster().topologyVersion())));
 
         assertEquals(2, ignite.cluster().currentBaselineTopology().size());
     }


Mime
View raw message