in progress...
[jalview.git] / forester / java / src / org / forester / util / ExternalProgram.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.util;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32
33 public class ExternalProgram {
34
35     public static boolean isExecuteableFile( final File path_to_cmd_f ) {
36         if ( !path_to_cmd_f.exists() ) {
37             return false;
38         }
39         else if ( path_to_cmd_f.isDirectory() ) {
40             return false;
41         }
42         else if ( !path_to_cmd_f.canExecute() ) {
43             return false;
44         }
45         return true;
46     }
47     private Process      _process;
48     private final String _path_to_cmd;
49
50     public ExternalProgram( final String path_to_cmd ) {
51         final File path_to_cmd_f = new File( path_to_cmd );
52         checkCmdFile( path_to_cmd_f );
53         _path_to_cmd = path_to_cmd_f.getAbsolutePath();
54     }
55
56     private void checkCmdFile( final File path_to_cmd_f ) {
57         if ( !path_to_cmd_f.exists() ) {
58             throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] does not exist" );
59         }
60         else if ( path_to_cmd_f.isDirectory() ) {
61             throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] is a directory" );
62         }
63         else if ( !path_to_cmd_f.canExecute() ) {
64             throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] is not executeable" );
65         }
66     }
67
68     public InputStream getErrorStream() {
69         return getProcess().getErrorStream();
70     }
71
72     public InputStream getInputStream() {
73         return getProcess().getInputStream();
74     }
75
76     public OutputStream getOutputStream() {
77         return getProcess().getOutputStream();
78     }
79
80     private String getPathToCmd() {
81         return _path_to_cmd;
82     }
83
84     private Process getProcess() {
85         return _process;
86     }
87
88     public Process launch( final String[] opts ) throws IOException, InterruptedException {
89         String[] cmd;
90         if ( ( opts == null ) || ( opts.length < 1 ) ) {
91             cmd = new String[ 1 ];
92         }
93         else {
94             cmd = new String[ opts.length + 1 ];
95             for( int i = 0; i < opts.length; i++ ) {
96                 cmd[ i + 1 ] = opts[ i ];
97             }
98         }
99         cmd[ 0 ] = getPathToCmd();
100         System.out.println();
101         for( final String element : cmd ) {
102             System.out.print( element + " " );
103         }
104         System.out.println();
105         setProcess( Runtime.getRuntime().exec( cmd ) );
106         return getProcess();
107     }
108
109     private void setProcess( final Process process ) {
110         _process = process;
111     }
112
113     public int waitFor() {
114         try {
115             return getProcess().waitFor();
116         }
117         catch ( final InterruptedException e ) {
118             // TODO Auto-generated catch block
119             getProcess().destroy();
120             e.printStackTrace();
121             return -1;
122         }
123     }
124 }