View Javadoc

1   /**
2    * Pyx4me framework
3    * Copyright (C) 2006-2008 pyx4j.com.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing,
12   * software distributed under the License is distributed on an
13   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   * KIND, either express or implied.  See the License for the
15   * specific language governing permissions and limitations
16   * under the License.
17   * 
18   * @author vlads
19   * @version $Id: ExecuteGammu.java 2066 2008-06-21 23:16:18Z vlads $
20   */
21  package com.pyx4me.maven.gammu;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.plugin.MojoFailureException;
31  
32  public class ExecuteGammu {
33  
34  	File exe;
35  
36  	ExecuteGammu(File gammuexe, File gammurc, List pluginArtifacts) throws MojoFailureException {
37  		if (gammuexe != null) {
38  			this.exe = gammuexe;
39  		} else {
40  			for (Iterator i = pluginArtifacts.iterator(); i.hasNext();) {
41  				Artifact artifact = (Artifact) i.next();
42  				if ("gammu".equals(artifact.getArtifactId())) {
43  					exe = artifact.getFile();
44  					break;
45  				}
46  			}
47  		}
48  		if ((exe == null) || (!exe.canRead())) {
49  			throw new MojoFailureException("Can't find gammu.exe in plugin dependency");
50  		}
51  	}
52  
53  	boolean run(String[] args, File dir) {
54  		String[] cmdarray = new String[args.length + 1];
55  		cmdarray[0] = exe.getAbsolutePath();
56  		for (int i = 0; i < args.length; i++) {
57  			cmdarray[i + 1] = args[i];
58  		}
59  		try {
60  			Process proc = Runtime.getRuntime().exec(cmdarray, null, dir);
61  			Thread[] threads = printProcessOutput(proc);
62  			try {
63  				int exitVal = proc.waitFor();
64  				threads[0].join();
65  				threads[1].join();
66  				if (exitVal == 0) {
67  					return true;
68  				} else {
69  					return false;
70  				}
71  			} catch (InterruptedException e) {
72  				return false;
73  			}
74  		} catch (IOException e) {
75  			throw new Error(e);
76  		}
77  	}
78  
79  	public Thread[] printProcessOutput(Process proc) throws IOException {
80  		Thread[] threads = new Thread[2];
81  		final InputStream out = proc.getInputStream();
82  		(threads[0] = new Thread("gammu-stdout") {
83  			public void run() {
84  				try {
85  					int c = out.read();
86  					while (c != -1) {
87  						System.out.print((char) c);
88  						// System.out.flush();
89  						c = out.read();
90  					}
91  				} catch (IOException e) {
92  				}
93  				System.out.println("");
94  			}
95  		}).start();
96  
97  		final InputStream err = proc.getErrorStream();
98  		(threads[1] = new Thread("gammu-errout") {
99  			public void run() {
100 				try {
101 					int c = err.read();
102 					while (c != -1) {
103 						System.err.print((char) c);
104 						// System.err.flush();
105 						c = err.read();
106 					}
107 				} catch (IOException e) {
108 				}
109 			}
110 		}).start();
111 		return threads;
112 	}
113 }