b9c9c174dcb3fd10a626c080cca5d75479c3bd62
[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 ch.ethz.ssh2.ServerConnection;
11 import ch.ethz.ssh2.ServerSession;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import javax.annotation.concurrent.ThreadSafe;
15 import org.apache.commons.io.IOUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 @ThreadSafe
20 public class IOThread extends Thread {
21
22     private static final Logger logger =  LoggerFactory.getLogger(IOThread.class);
23
24     private InputStream inputStream;
25     private OutputStream outputStream;
26     private String id;
27     private ServerSession servSession;
28     private ServerConnection servconnection;
29     private String customHeader;
30
31
32     public IOThread (InputStream is, OutputStream os, String id,ServerSession ss, ServerConnection conn){
33         this.inputStream = is;
34         this.outputStream = os;
35         this.servSession = ss;
36         this.servconnection = conn;
37         super.setName(id);
38         logger.trace("IOThread {} created", super.getName());
39     }
40     public IOThread (InputStream is, OutputStream os, String id,ServerSession ss, ServerConnection conn,String header){
41         this.inputStream = is;
42         this.outputStream = os;
43         this.servSession = ss;
44         this.servconnection = conn;
45         this.customHeader = header;
46         super.setName(id);
47         logger.trace("IOThread {} created", super.getName());
48     }
49
50     @Override
51     public void run() {
52         logger.trace("thread {} started", super.getName());
53         try {
54             if (this.customHeader!=null && !this.customHeader.equals("")){
55                 this.outputStream.write(this.customHeader.getBytes());
56                 logger.trace("adding  {} header", this.customHeader);
57             }
58             IOUtils.copy(this.inputStream, this.outputStream);
59         } catch (Exception e) {
60             logger.error("inputstream -> outputstream copy error ",e);
61         }
62         logger.trace("closing server session");
63         servSession.close();
64         servconnection.close();
65         logger.trace("thread {} is closing",super.getName());
66     }
67 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.