Removed `which` dependency, now using proper shell builtin.
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / threads / IOThread.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.netconf.ssh.threads;
9
10 import java.io.InputStream;
11 import java.io.OutputStream;
12
13 import javax.annotation.concurrent.ThreadSafe;
14
15 import org.apache.commons.io.IOUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import ch.ethz.ssh2.ServerConnection;
20 import ch.ethz.ssh2.ServerSession;
21
22 @ThreadSafe
23 public class IOThread extends Thread {
24
25     private static final Logger logger =  LoggerFactory.getLogger(IOThread.class);
26
27     private final InputStream inputStream;
28     private final OutputStream outputStream;
29     private final ServerSession servSession;
30     private final ServerConnection servconnection;
31     private String customHeader;
32
33
34     public IOThread (InputStream is, OutputStream os, String id,ServerSession ss, ServerConnection conn){
35         this.inputStream = is;
36         this.outputStream = os;
37         this.servSession = ss;
38         this.servconnection = conn;
39         super.setName(id);
40         logger.trace("IOThread {} created", super.getName());
41     }
42
43     public IOThread (InputStream is, OutputStream os, String id,ServerSession ss, ServerConnection conn,String header){
44         this.inputStream = is;
45         this.outputStream = os;
46         this.servSession = ss;
47         this.servconnection = conn;
48         this.customHeader = header;
49         super.setName(id);
50         logger.trace("IOThread {} created", super.getName());
51     }
52
53     @Override
54     public void run() {
55         logger.trace("thread {} started", super.getName());
56         try {
57             if (this.customHeader!=null && !this.customHeader.equals("")){
58                 this.outputStream.write(this.customHeader.getBytes());
59                 logger.trace("adding {} header", this.customHeader);
60             }
61             IOUtils.copy(this.inputStream, this.outputStream);
62         } catch (Exception e) {
63             logger.error("inputstream -> outputstream copy error ",e);
64         }
65         logger.trace("closing server session");
66         servSession.close();
67         servconnection.close();
68         logger.trace("thread {} is closing",super.getName());
69     }
70 }