Package com.meetup.memcached

Examples of com.meetup.memcached.MemcachedClient$NIOLoader$Connection


     * Check if we can authenticate using public key auth
     * @return true|false
     */
    public boolean checkConnection() {
        boolean status = false;
        Connection c = null;
        c = new Connection(host, port);
        try {
            c.connect();
            File f = new File(keyFile);
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("Checking connection...");
            }
            status = c.authenticateWithPublicKey(userName, f, rawKeyPassPhrase);
            if (status) {
                logger.info("Successfully connected to " + userName + "@" + host + " using keyfile " + keyFile);
            }
        } catch(IOException ioe) {
            Throwable t = ioe.getCause();
            if (t != null) {
                String msg = t.getMessage();
                logger.warning("Failed to connect or authenticate: " + msg);
            }
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "Failed to connect or autheticate: ", ioe);
            }
        } finally {
            c.close();
        }
        return status;
    }
View Full Code Here


     * Check if we can connect using password auth
     * @return true|false
     */
    public boolean checkPasswordAuth() {
        boolean status = false;
        Connection c = null;
        try {
            c = new Connection(host, port);
            c.connect();
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("Checking connection...");
            }
            status = c.authenticateWithPassword(userName, password);
            if (status) {
                logger.finer("Successfully connected to " + userName + "@" + host + " using password authentication");
            }
        } catch(IOException ioe) {
            //logger.printExceptionStackTrace(ioe);
            if (logger.isLoggable(Level.FINER)) {
                ioe.printStackTrace();
            }
        } finally {
            c.close();
        }
        return status;
    }
View Full Code Here

            int port = location.hasPort() ? location.getPort() : credentials.getPortNumber();
            if (port < 0) {
                port = 22;
            }
            if (!isUsePersistentConnection()) {
                Connection connection = openConnection(location, credentials, port, connectTimeout);
                return new SSHConnectionInfo(null, "unpersistent", connection, false);
            }
            String key = credentials.getUserName() + ":" + location.getHost() + ":" + port;
            String id = credentials.getUserName() + ":" + location.getHost() + ":" + port;
            if (credentials.getPrivateKeyFile() != null) {
                key += ":" + credentials.getPrivateKeyFile().getAbsolutePath();
            }
            if (credentials.getPassphrase() != null) {
                key += ":" + credentials.getPassphrase();
            }
            if (credentials.getPassword() != null) {
                key += ":" + credentials.getPassword();
            }
            SSHConnectionInfo connectionInfo = null;
            LinkedList connectionsList = (LinkedList) ourConnectionsPool.get(key);
            if (connectionsList == null) {
                connectionsList = new LinkedList();
                ourConnectionsPool.put(key, connectionsList);
            }
            SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                    ourRequestor + ": EXISTING CONNECTIONS COUNT: " + connectionsList.size());
            for (Iterator infos = connectionsList.iterator(); infos.hasNext();) {
                SSHConnectionInfo info = (SSHConnectionInfo) infos.next();
                // ping connection here. if it is stale - close connection and remove it from the pool.
                if (useConnectionPing) {
                    try {
                        info.myConnection.ping();
                    } catch (IOException e) {
                        // ping failed, remove _this_ info only and close its connection.
                        // the we may check next available connection.
                       
                        // all channels binded to the closed connection will be closed
                        // on the next attempt to access them.
                        SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                                ourRequestor + ": ROTTEN CONNECTION DETECTED, WILL CLOSE IT: " + info);
                        infos.remove();
                        // to let it be closed even if it is the last one.
                        info.setPersistent(false);
                        SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                                ourRequestor + ": ROTTEN CONNECTION MADE NOT PERSISTENT: " + info);
                        closeConnection(info);
                        continue;
                    }
                } else {
                    SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                            "SKIPPING CONNECTION PING, IT HAS BEEN DISABLED");
                }
                if (info.getSessionCount() < MAX_SESSIONS_PER_CONNECTION) {
                    info.resetTimeout();
                    SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                            ourRequestor + ": REUSING ONE WITH " + info.getSessionCount() + " SESSIONS: " + info.myConnection);
                    return info;
                }
            }
            SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": OPENING NEW CONNECTION");
            Connection connection = openConnection(location, credentials, port, connectTimeout);
            connectionInfo = new SSHConnectionInfo(key, id, connection, true);
            connectionsList.add(connectionInfo);
            SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": NEW CONNECTION OPENED, " +
                "TOTAL: " + connectionsList.size());
            return connectionInfo;
View Full Code Here

        if (privateKey == null && password == null) {
            SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "Either password or private key should be provided to establish SSH connection");
            SVNErrorManager.error(error, SVNLogType.NETWORK);
        }
       
        Connection connection = new Connection(location.getHost(), port);
        try {
            connection.connect(null, connectTimeout, connectTimeout);
            boolean authenticated = false;
            if (privateKey != null) {
                authenticated = connection.authenticateWithPublicKey(userName, privateKey, passphrase);
            } else if (password != null) {
                String[] methods = connection.getRemainingAuthMethods(userName);
                authenticated = false;
                for (int i = 0; i < methods.length; i++) {
                    if ("password".equals(methods[i])) {
                        authenticated = connection.authenticateWithPassword(userName, password);                   
                    } else if ("keyboard-interactive".equals(methods[i])) {
                        final String p = password;
                        authenticated = connection.authenticateWithKeyboardInteractive(userName, new InteractiveCallback() {
                            public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) throws Exception {
                                String[] reply = new String[numPrompts];
                                for (int i = 0; i < reply.length; i++) {
                                    reply[i] = p;
                                }
                                return reply;
                            }
                        });
                    }
                    if (authenticated) {
                        break;
                    }
                }
            } else {
                SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "Either password or private key should be provided to establish SSH connection");
                SVNErrorManager.error(error, SVNLogType.NETWORK);
            }
            if (!authenticated) {
                SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "SSH server rejects provided credentials");
                SVNErrorManager.error(error, SVNLogType.NETWORK);
            }
            return connection;
        } catch (IOException e) {
            if (connection != null) {
                connection.close();
            }
            SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_CONNECTION_CLOSED, "Cannot connect to ''{0}'': {1}", new Object[] {location.setPath("", false), e.getLocalizedMessage()});
            SVNErrorManager.error(err, e, SVNLogType.NETWORK);
        }
        return null;
View Full Code Here

  /**
   * Opens a SSH Connection
   * @throws IOException
   */
  private void openConnection() throws IOException {
    conn = new Connection(rProject.getHostname(), rProject.getPort());
    conn.connect();
    connected = true;
    try {
      authenticate();
    } catch (IOException e) {
View Full Code Here

            int port = location.hasPort() ? location.getPort() : credentials.getPortNumber();
            if (port < 0) {
                port = 22;
            }
            if (!isUsePersistentConnection()) {
                Connection connection = openConnection(location, credentials, port, connectTimeout);
                return new SSHConnectionInfo(null, "unpersistent", connection, false);
            }
            String key = credentials.getUserName() + ":" + location.getHost() + ":" + port;
            String id = credentials.getUserName() + ":" + location.getHost() + ":" + port;
            if (credentials.getPrivateKeyFile() != null) {
                key += ":" + credentials.getPrivateKeyFile().getAbsolutePath();
            }
            if (credentials.getPassphrase() != null) {
                key += ":" + credentials.getPassphrase();
            }
            if (credentials.getPassword() != null) {
                key += ":" + credentials.getPassword();
            }
            SSHConnectionInfo connectionInfo = null;
            LinkedList connectionsList = (LinkedList) ourConnectionsPool.get(key);
            if (connectionsList == null) {
                connectionsList = new LinkedList();
                ourConnectionsPool.put(key, connectionsList);
            }
            SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                    ourRequestor + ": EXISTING CONNECTIONS COUNT: " + connectionsList.size());
            for (Iterator infos = connectionsList.iterator(); infos.hasNext();) {
                SSHConnectionInfo info = (SSHConnectionInfo) infos.next();
                // ping connection here. if it is stale - close connection and remove it from the pool.
                if (useConnectionPing) {
                    try {
                        info.myConnection.ping();
                    } catch (IOException e) {
                        // ping failed, remove _this_ info only and close its connection.
                        // the we may check next available connection.
                       
                        // all channels binded to the closed connection will be closed
                        // on the next attempt to access them.
                        SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                                ourRequestor + ": ROTTEN CONNECTION DETECTED, WILL CLOSE IT: " + info);
                        infos.remove();
                        // to let it be closed even if it is the last one.
                        info.setPersistent(false);
                        SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                                ourRequestor + ": ROTTEN CONNECTION MADE NOT PERSISTENT: " + info);
                        closeConnection(info);
                        continue;
                    }
                } else {
                    SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                            "SKIPPING CONNECTION PING, IT HAS BEEN DISABLED");
                }
                if (info.getSessionCount() < MAX_SESSIONS_PER_CONNECTION) {
                    info.resetTimeout();
                    SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK,
                            ourRequestor + ": REUSING ONE WITH " + info.getSessionCount() + " SESSIONS: " + info.myConnection);
                    return info;
                }
            }
            SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": OPENING NEW CONNECTION");
            Connection connection = openConnection(location, credentials, port, connectTimeout);
            connectionInfo = new SSHConnectionInfo(key, id, connection, true);
            connectionsList.add(connectionInfo);
            SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, ourRequestor + ": NEW CONNECTION OPENED, " +
                "TOTAL: " + connectionsList.size());
            return connectionInfo;
View Full Code Here

        if (privateKey == null && password == null) {
            SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "Either password or private key should be provided to establish SSH connection");
            SVNErrorManager.error(error, SVNLogType.NETWORK);
        }
       
        Connection connection = new Connection(location.getHost(), port);
        try {
            connection.connect(null, connectTimeout, connectTimeout);
            boolean authenticated = false;
            if (privateKey != null) {
                authenticated = connection.authenticateWithPublicKey(userName, privateKey, passphrase);
            } else if (password != null) {
                String[] methods = connection.getRemainingAuthMethods(userName);
                authenticated = false;
                for (int i = 0; i < methods.length; i++) {
                    if ("password".equals(methods[i])) {
                        authenticated = connection.authenticateWithPassword(userName, password);                   
                    } else if ("keyboard-interactive".equals(methods[i])) {
                        final String p = password;
                        authenticated = connection.authenticateWithKeyboardInteractive(userName, new InteractiveCallback() {
                            public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) throws Exception {
                                String[] reply = new String[numPrompts];
                                for (int i = 0; i < reply.length; i++) {
                                    reply[i] = p;
                                }
                                return reply;
                            }
                        });
                    }
                    if (authenticated) {
                        break;
                    }
                }
            } else {
                SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "Either password or private key should be provided to establish SSH connection");
                SVNErrorManager.error(error, SVNLogType.NETWORK);
            }
            if (!authenticated) {
                SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "SSH server rejects provided credentials");
                SVNErrorManager.error(error, SVNLogType.NETWORK);
            }
            return connection;
        } catch (IOException e) {
            if (connection != null) {
                connection.close();
            }
            SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_CONNECTION_CLOSED, "Cannot connect to ''{0}'': {1}", new Object[] {location.setPath("", false), e.getLocalizedMessage()});
            SVNErrorManager.error(err, e, SVNLogType.NETWORK);
        }
        return null;
View Full Code Here

            int port = location.hasPort() ? location.getPort() : credentials.getPortNumber();
            if (port < 0) {
                port = 22;
            }
            if (!isUsePersistentConnection()) {
                Connection connection = openConnection(location, credentials, port);
                return new SSHConnectionInfo(null, "unpersistent", connection, false);
            }
            String key = credentials.getUserName() + ":" + location.getHost() + ":" + port;
            String id = credentials.getUserName() + ":" + location.getHost() + ":" + port;
            if (credentials.getPrivateKeyFile() != null) {
                key += ":" + credentials.getPrivateKeyFile().getAbsolutePath();
            }
            if (credentials.getPassphrase() != null) {
                key += ":" + credentials.getPassphrase();
            }
            if (credentials.getPassword() != null) {
                key += ":" + credentials.getPassword();
            }
            SSHConnectionInfo connectionInfo = null;
            LinkedList connectionsList = (LinkedList) ourConnectionsPool.get(key);
            if (connectionsList == null) {
                connectionsList = new LinkedList();
                ourConnectionsPool.put(key, connectionsList);
            }
            SVNDebugLog.getDefaultLog().info(ourRequestor + ": EXISTING CONNECTIONS COUNT: " + connectionsList.size());
            for (Iterator infos = connectionsList.iterator(); infos.hasNext();) {
                SSHConnectionInfo info = (SSHConnectionInfo) infos.next();
                // ping connection here. if it is stale - close connection and remove it from the pool.
                try {
                    info.myConnection.ping();
                } catch (IOException e) {
                    // ping failed, remove _this_ info only and close its connection.
                    // the we may check next available connection.
                   
                    // all channels binded to the closed connection will be closed
                    // on the next attempt to access them.
                    SVNDebugLog.getDefaultLog().info(ourRequestor + ": ROTTEN CONNECTION DETECTED, WILL CLOSE IT: " + info);
                    infos.remove();
                    // to let it be closed even if it is the last one.
                    info.setPersistent(false);
                    SVNDebugLog.getDefaultLog().info(ourRequestor + ": ROTTEN CONNECTION MADE NOT PERSISTENT: " + info);
                    closeConnection(info);
                    continue;
                }
                if (info.getSessionCount() < MAX_SESSIONS_PER_CONNECTION) {
                    info.resetTimeout();
                    SVNDebugLog.getDefaultLog().info(ourRequestor + ": REUSING ONE WITH " + info.getSessionCount() + " SESSIONS: " + info.myConnection);
                    return info;
                }
            }
            SVNDebugLog.getDefaultLog().info(ourRequestor + ": OPENING NEW CONNECTION");
            Connection connection = openConnection(location, credentials, port);
            connectionInfo = new SSHConnectionInfo(key, id, connection, true);
            connectionsList.add(connectionInfo);
            SVNDebugLog.getDefaultLog().info(ourRequestor + ": NEW CONNECTION OPENED, TOTAL: " + connectionsList.size());
            return connectionInfo;
        } finally {
View Full Code Here

        if (privateKey == null && password == null) {
            SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "Either password or private key should be provided to establish SSH connection");
            SVNErrorManager.error(error);
        }
       
        Connection connection = new Connection(location.getHost(), port);
        try {
            connection.connect();
            boolean authenticated = false;
            if (privateKey != null) {
                authenticated = connection.authenticateWithPublicKey(userName, privateKey, passphrase);
            } else if (password != null) {
                String[] methods = connection.getRemainingAuthMethods(userName);
                authenticated = false;
                for (int i = 0; i < methods.length; i++) {
                    if ("password".equals(methods[i])) {
                        authenticated = connection.authenticateWithPassword(userName, password);                   
                    } else if ("keyboard-interactive".equals(methods[i])) {
                        final String p = password;
                        authenticated = connection.authenticateWithKeyboardInteractive(userName, new InteractiveCallback() {
                            public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) throws Exception {
                                String[] reply = new String[numPrompts];
                                for (int i = 0; i < reply.length; i++) {
                                    reply[i] = p;
                                }
                                return reply;
                            }
                        });
                    }
                    if (authenticated) {
                        break;
                    }
                }
            } else {
                SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "Either password or private key should be provided to establish SSH connection");
                SVNErrorManager.error(error);
            }
            if (!authenticated) {
                SVNErrorMessage error = SVNErrorMessage.create(SVNErrorCode.RA_NOT_AUTHORIZED, "SSH server rejects provided credentials");
                SVNErrorManager.error(error);
            }
            return connection;
        } catch (IOException e) {
            if (connection != null) {
                connection.close();
            }
            SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_CONNECTION_CLOSED, "Cannot connect to ''{0}'': {1}", new Object[] {location.setPath("", false), e.getLocalizedMessage()});
            SVNErrorManager.error(err, e);
        }
        return null;
View Full Code Here

                    Thread.sleep(120000);
                }
                for (VirtualMachine vm : this.virtualMachines) {

                    s_logger.info("Attempting to SSH into linux host " + this.publicIp + " with retry attempt: " + retry);
                    Connection conn = new Connection(this.publicIp);
                    conn.connect(null, 600000, 600000);

                    s_logger.info("SSHed successfully into linux host " + this.publicIp);

                    boolean isAuthenticated = conn.authenticateWithPassword("root", "password");

                    if (isAuthenticated == false) {
                        s_logger.info("Authentication failed");
                    }
                    //execute copy command
                    Session sess = conn.openSession();
                    String fileName;
                    Random ran = new Random();
                    fileName = Math.abs(ran.nextInt()) + "-file";
                    String copyCommand = new String("./scpScript " + vm.getPrivateIp() + " " + fileName);
                    s_logger.info("Executing " + copyCommand);
                    sess.execCommand(copyCommand);
                    Thread.sleep(120000);
                    sess.close();

                    //execute wget command
                    sess = conn.openSession();
                    String downloadCommand =
                        new String("wget http://172.16.0.220/scripts/checkDiskSpace.sh; chmod +x *sh; ./checkDiskSpace.sh; rm -rf checkDiskSpace.sh");
                    s_logger.info("Executing " + downloadCommand);
                    sess.execCommand(downloadCommand);
                    Thread.sleep(120000);
                    sess.close();

                    //close the connection
                    conn.close();
                }
            } catch (Exception ex) {
                s_logger.error(ex);
                retry++;
                if (retry == retryNum) {
View Full Code Here

TOP

Related Classes of com.meetup.memcached.MemcachedClient$NIOLoader$Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.