Coverage Report - com.jcabi.ssh.AbstractSSHShell
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSSHShell
84%
11/13
0%
0/20
1
 
 1  
 /**
 2  
  * Copyright (c) 2014-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.ssh;
 31  
 
 32  
 import com.jcraft.jsch.Session;
 33  
 import java.io.IOException;
 34  
 import java.io.InputStream;
 35  
 import java.io.OutputStream;
 36  
 import java.net.InetAddress;
 37  
 import java.net.UnknownHostException;
 38  
 import lombok.EqualsAndHashCode;
 39  
 import lombok.Getter;
 40  
 import lombok.ToString;
 41  
 import org.apache.commons.lang3.Validate;
 42  
 
 43  
 /**
 44  
  * Common module for any ssh shell.
 45  
  * @author Jose Volmei Dal Pra Junior (jrdalpra@gmail.com)
 46  
  * @version $Id: 49c889e2fc6a75ec150efa6ce1cc75fdbb79569f $
 47  
  * @since 1.5.2
 48  
  */
 49  0
 @ToString
 50  0
 @EqualsAndHashCode(of = { "addr", "port", "login" })
 51  
 @Getter
 52  
 @SuppressWarnings({ "PMD.UnusedPrivateField", "PMD.SingularField" })
 53  
 abstract class AbstractSSHShell implements Shell {
 54  
 
 55  
     /**
 56  
      * IP address of the server.
 57  
      */
 58  8
     private final transient String addr;
 59  
 
 60  
     /**
 61  
      * Port to use.
 62  
      */
 63  8
     private final transient int port;
 64  
 
 65  
     /**
 66  
      * User name.
 67  
      */
 68  8
     private final transient String login;
 69  
 
 70  
     /**
 71  
      * Constructor.
 72  
      * @param adr Address that you want to connect to.
 73  
      * @param prt Port that you want to reach.
 74  
      * @param user User that will be used when connecting.
 75  
      * @throws UnknownHostException when host is unkwon.
 76  
      */
 77  
     AbstractSSHShell(
 78  
         final String adr,
 79  
         final int prt,
 80  4
         final String user) throws UnknownHostException {
 81  4
         this.addr = InetAddress.getByName(adr).getHostAddress();
 82  4
         this.port = prt;
 83  4
         this.login = user;
 84  4
         Validate.matchesPattern(
 85  
             this.addr,
 86  
             "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}",
 87  
             "Invalid IP address of the server `%s`",
 88  
             this.addr
 89  
         );
 90  4
         Validate.notEmpty(user, "user name can't be empty");
 91  4
     }
 92  
 
 93  
     // @checkstyle ParameterNumberCheck (2 lines)
 94  
     @Override
 95  
     public int exec(final String command, final InputStream stdin,
 96  
                     final OutputStream stdout, final OutputStream stderr)
 97  
         throws IOException {
 98  4
         return new Execution.Default(
 99  
             command,
 100  
             stdin,
 101  
             stdout,
 102  
             stderr,
 103  
             this.session()
 104  
         ).exec();
 105  
     }
 106  
 
 107  
     /**
 108  
      * Create and return a session, connected.
 109  
      * @return JSch session
 110  
      * @throws IOException If some IO problem inside
 111  
      */
 112  
     protected abstract Session session()  throws IOException;
 113  
 
 114  
 }