1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
105 c = err.read();
106 }
107 } catch (IOException e) {
108 }
109 }
110 }).start();
111 return threads;
112 }
113 }