3333import java .net .URL ;
3434import java .net .URLDecoder ;
3535import java .net .UnknownHostException ;
36+ import java .nio .charset .StandardCharsets ;
3637import java .util .Optional ;
3738import java .util .logging .Level ;
3839import java .util .logging .Logger ;
@@ -50,23 +51,39 @@ public class DeployController {
5051 private final static URL LOCAL_GRIP_URL = Project .class .getProtectionDomain ().getCodeSource ().getLocation ();
5152 private final static String LOCAL_GRIP_PATH = URLDecoder .decode (LOCAL_GRIP_URL .getPath ());
5253
53- @ FXML private TextField address ;
54- @ FXML private TextField user ;
55- @ FXML private TextField password ;
56- @ FXML private TextField javaHome ;
57- @ FXML private TextField jvmArgs ;
58- @ FXML private TextField deployDir ;
59- @ FXML private TextField projectFile ;
60- @ FXML private ProgressIndicator progress ;
61- @ FXML private Label status ;
62- @ FXML private TextArea console ;
63- @ FXML private BooleanProperty deploying ;
64- @ FXML private StringProperty command ;
65-
66- @ Inject private EventBus eventBus ;
67- @ Inject private Project project ;
68- @ Inject private Pipeline pipeline ;
69- @ Inject private Logger logger ;
54+ @ FXML
55+ private TextField address ;
56+ @ FXML
57+ private TextField user ;
58+ @ FXML
59+ private TextField password ;
60+ @ FXML
61+ private TextField javaHome ;
62+ @ FXML
63+ private TextField jvmArgs ;
64+ @ FXML
65+ private TextField deployDir ;
66+ @ FXML
67+ private TextField projectFile ;
68+ @ FXML
69+ private ProgressIndicator progress ;
70+ @ FXML
71+ private Label status ;
72+ @ FXML
73+ private TextArea console ;
74+ @ FXML
75+ private BooleanProperty deploying ;
76+ @ FXML
77+ private StringProperty command ;
78+
79+ @ Inject
80+ private EventBus eventBus ;
81+ @ Inject
82+ private Project project ;
83+ @ Inject
84+ private Pipeline pipeline ;
85+ @ Inject
86+ private Logger logger ;
7087
7188 private Optional <Thread > deployThread = Optional .empty ();
7289
@@ -164,9 +181,12 @@ public StreamCopier.Listener file(String name, long size) {
164181 // Upload the core GRIP JAR only if there isn't already one with the same hash on the robot. This prevents
165182 // us from redundantly deploying the same JAR over and over again (so deploy times after the first are
166183 // much faster), while still ensuring that the JAR is deployed if it has to be.
167- try (Session session = ssh .startSession ()) {
168- Session .Command md5Cmd = session .exec ("md5sum " + pathStr + GRIP_JAR );
169- String remoteMd5Sum = new DataInputStream (md5Cmd .getInputStream ()).readLine ();
184+ try (Session session = ssh .startSession ()) { // SSH doesn't store a reference to the session to close it.
185+ final Session .Command md5Cmd = session .exec ("md5sum " + pathStr + GRIP_JAR );
186+ final String remoteMd5Sum ;
187+ try (DataInputStream stream = new DataInputStream (md5Cmd .getInputStream ())) {
188+ remoteMd5Sum = stream .readLine ();
189+ }
170190 String localMd5Sum = Resources .asByteSource (LOCAL_GRIP_URL ).hash (Hashing .md5 ()).toString ();
171191
172192 // If the MD5 sum doesn't match up or there's any kind of error (there isn't a JAR there, etc...),
@@ -195,18 +215,21 @@ public StreamCopier.Listener file(String name, long size) {
195215 setStatusAsync ("Running GRIP" , false );
196216 logger .info ("Running " + commandStr );
197217
198- Session session = ssh .startSession ();
199- session .allocateDefaultPTY ();
200- Session .Command cmd = session .exec (String .format ("'%s/%s'" , pathStr , GRIP_WRAPPER ));
218+ try (Session session = ssh .startSession ()) {
219+ session .allocateDefaultPTY ();
220+ Session .Command cmd = session .exec (String .format ("'%s/%s'" , pathStr , GRIP_WRAPPER ));
221+
222+ try (final InputStreamReader reader = new InputStreamReader (cmd .getInputStream (), StandardCharsets .UTF_8 )) {
223+ final LineReader inputReader = new LineReader (reader );
224+ while (isNotCanceled ()) {
225+ String line = inputReader .readLine ();
226+ if (line == null ) {
227+ return ;
228+ }
201229
202- LineReader inputReader = new LineReader (new InputStreamReader (cmd .getInputStream ()));
203- while (isNotCanceled ()) {
204- String line = inputReader .readLine ();
205- if (line == null ) {
206- return ;
230+ Platform .runLater (() -> console .setText (console .getText () + line + "\n " ));
231+ }
207232 }
208-
209- Platform .runLater (() -> console .setText (console .getText () + line + "\n " ));
210233 }
211234 } catch (UnknownHostException e ) {
212235 setStatusAsync ("Unknown host: " + address .getText (), true );
0 commit comments