|
1 /* |
|
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * External Filter Processor |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine.dataprocessor; |
|
20 |
|
21 import java.io.IOException; |
|
22 import java.util.ArrayList; |
|
23 |
|
24 import com.nokia.traceviewer.TraceViewerPlugin; |
|
25 import com.nokia.traceviewer.engine.TraceProperties; |
|
26 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
27 import com.nokia.traceviewer.engine.preferences.PreferenceConstants; |
|
28 |
|
29 /** |
|
30 * External Filter Processor handles starting and stopping external filter |
|
31 * application |
|
32 * |
|
33 */ |
|
34 public final class ExternalFilterProcessor { |
|
35 |
|
36 /** |
|
37 * Quotation mark char |
|
38 */ |
|
39 private static final char QUOTATION_MARK = '"'; |
|
40 |
|
41 /** |
|
42 * The process running the current filter application |
|
43 */ |
|
44 private Process process; |
|
45 |
|
46 /** |
|
47 * Data sender |
|
48 */ |
|
49 private ExternalFilterDataWriter writer; |
|
50 |
|
51 /** |
|
52 * Data reader |
|
53 */ |
|
54 private ExternalFilterDataReader reader; |
|
55 |
|
56 /** |
|
57 * Start external application |
|
58 * |
|
59 * @return true if process was started |
|
60 */ |
|
61 public boolean startExternalApplication() { |
|
62 boolean success = true; |
|
63 String command = TraceViewerPlugin.getDefault().getPreferenceStore() |
|
64 .getString(PreferenceConstants.EXTERNAL_FILTER_COMMAND); |
|
65 |
|
66 // Split the command |
|
67 String[] args = splitCommand(command); |
|
68 |
|
69 if (process != null) { |
|
70 process.destroy(); |
|
71 process = null; |
|
72 } |
|
73 if (reader != null) { |
|
74 reader.shutDown(); |
|
75 reader = null; |
|
76 } |
|
77 |
|
78 if (args.length > 0) { |
|
79 |
|
80 // Create the process |
|
81 try { |
|
82 ProcessBuilder bb = new ProcessBuilder(args); |
|
83 process = bb.start(); |
|
84 |
|
85 // Create data fetcher and start it |
|
86 reader = new ExternalFilterDataReader(process.getInputStream()); |
|
87 reader.start(); |
|
88 |
|
89 // Create filtered file |
|
90 createFilteredFile(); |
|
91 |
|
92 } catch (IOException e) { |
|
93 e.printStackTrace(); |
|
94 success = false; |
|
95 } |
|
96 } else { |
|
97 success = false; |
|
98 } |
|
99 return success; |
|
100 } |
|
101 |
|
102 /** |
|
103 * Creates filtered file |
|
104 */ |
|
105 private void createFilteredFile() { |
|
106 // Create filtered file and open the progressbar |
|
107 TraceViewerGlobals.getTraceViewer().getDataProcessorAccess() |
|
108 .getFilterProcessor().getFilterDialog().applyFilters( |
|
109 TraceViewerGlobals.getTraceViewer() |
|
110 .getDataReaderAccess().getMainDataReader() |
|
111 .getTraceCount()); |
|
112 |
|
113 } |
|
114 |
|
115 /** |
|
116 * Splits the command into String array |
|
117 * |
|
118 * @param command |
|
119 * command |
|
120 * @return String array containing the splitted command |
|
121 */ |
|
122 private String[] splitCommand(String command) { |
|
123 boolean containsQuotationMark = false; |
|
124 ArrayList<String> commands = new ArrayList<String>(); |
|
125 |
|
126 // Split the command with space |
|
127 String[] args = command.split("\\s"); //$NON-NLS-1$ |
|
128 |
|
129 // Go through tokens |
|
130 for (int i = 0; i < args.length; i++) { |
|
131 String arg = args[i]; |
|
132 |
|
133 // No quotation marks going on |
|
134 if (!containsQuotationMark) { |
|
135 if (containsQuotationMark(arg)) { |
|
136 containsQuotationMark = true; |
|
137 } |
|
138 commands.add(arg); |
|
139 |
|
140 // Previous token has contained quotation mark |
|
141 } else { |
|
142 if (containsQuotationMark(arg)) { |
|
143 containsQuotationMark = false; |
|
144 } |
|
145 int idx = commands.size() - 1; |
|
146 String prev = commands.get(idx); |
|
147 String newStr = prev + ' ' + arg; |
|
148 commands.set(idx, newStr); |
|
149 } |
|
150 } |
|
151 |
|
152 // Move the commands from the arraylist to array |
|
153 String[] retArr = new String[commands.size()]; |
|
154 for (int i = 0; i < retArr.length; i++) { |
|
155 |
|
156 // Remove quotation marks, ProcessBuilder doesn't want them |
|
157 retArr[i] = commands.get(i).replace("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
|
158 } |
|
159 |
|
160 return retArr; |
|
161 } |
|
162 |
|
163 /** |
|
164 * Checks if given argument contains odd number of quotation marks |
|
165 * |
|
166 * @param arg |
|
167 * the argument |
|
168 * @return true if arg contains odd number of quotation marks |
|
169 */ |
|
170 private boolean containsQuotationMark(String arg) { |
|
171 boolean contains = false; |
|
172 int noOfQuotationMarks = 0; |
|
173 |
|
174 // Calculate quotation marks |
|
175 for (int i = 0; i < arg.length(); i++) { |
|
176 if (arg.charAt(i) == QUOTATION_MARK) { |
|
177 noOfQuotationMarks++; |
|
178 } |
|
179 } |
|
180 |
|
181 // Check that number of quotation marks is odd number |
|
182 if (noOfQuotationMarks % 2 != 0) { |
|
183 contains = true; |
|
184 } |
|
185 |
|
186 return contains; |
|
187 } |
|
188 |
|
189 /** |
|
190 * Stops external application |
|
191 */ |
|
192 public void stopExternalApplication() { |
|
193 if (process != null) { |
|
194 process.destroy(); |
|
195 process = null; |
|
196 } |
|
197 if (reader != null) { |
|
198 reader.shutDown(); |
|
199 reader = null; |
|
200 } |
|
201 if (writer != null) { |
|
202 writer.shutDown(); |
|
203 writer = null; |
|
204 } |
|
205 |
|
206 // Remove filters and open progressbar |
|
207 TraceViewerGlobals.getTraceViewer().getDataProcessorAccess() |
|
208 .getFilterProcessor().getFilterDialog().removeFilters( |
|
209 TraceViewerGlobals.getTraceViewer() |
|
210 .getDataReaderAccess().getMainDataReader() |
|
211 .getTraceCount()); |
|
212 } |
|
213 |
|
214 /** |
|
215 * Gets process |
|
216 * |
|
217 * @return the process |
|
218 */ |
|
219 public Process getProcess() { |
|
220 return process; |
|
221 } |
|
222 |
|
223 /** |
|
224 * Writes trace to process outputstream |
|
225 * |
|
226 * @param properties |
|
227 * trace properties |
|
228 */ |
|
229 public void writeTraceToProcess(TraceProperties properties) { |
|
230 if (writer == null) { |
|
231 writer = new ExternalFilterDataWriter(process.getOutputStream()); |
|
232 } |
|
233 writer.writeTrace(properties); |
|
234 } |
|
235 } |