Coding Style Check Results
12 || Summary | 15 ||
|---|---|
| Total files checked | 18 ||
| Files with errors | 22 ||
| Total errors | 26 ||
| Errors per file | 30 ||
The following are violations of the Coding-Style Standards:
35 | 36 || File: | 45 |
46 | |
48 |
|---|
| Line Number | 53 |Error Message | 54 |
|---|
hostname cannot be resolved
113 | * @throws org.h3270.host.HostUnreachableException
114 | * if the host cannot be reached
115 | * @throws org.h3270.host.S3270Exception
116 | * for any other error not matched by the above
117 | */
118 | public S3270(final String s3270Path, final String hostname, final int port, final TerminalType type,
119 | final TerminalMode mode) {
120 |
121 | this.s3270Path = s3270Path;
122 | this.hostname = hostname;
123 | this.port = port;
124 | this.type = type;
125 | this.mode = mode;
126 | this.screen = new S3270Screen();
127 |
128 | checkS3270PathValid(s3270Path);
129 |
130 | final String commandLine = String.format("%s -model %s-%d %s:%d", s3270Path, type.getType(), mode.getMode(),
131 | hostname, port);
132 | try {
133 | logger.info("starting " + commandLine);
134 | s3270 = Runtime.getRuntime().exec(commandLine);
135 |
136 | out = new PrintWriter(new OutputStreamWriter(s3270.getOutputStream(), "ISO-8859-1"));
137 | in = new BufferedReader(new InputStreamReader(s3270.getInputStream(), "ISO-8859-1"));
138 | errorReader = new ErrorReader();
139 | errorReader.start();
140 |
141 | waitFormat();
142 | } catch (final IOException ex) {
143 | throw new RuntimeException("IO Exception while starting s3270", ex);
144 | }
145 | }
146 |
147 | private void checkS3270PathValid(String path) {
148 | try {
149 | Runtime.getRuntime().exec(path + " -v");
150 | } catch (Exception e) {
151 | throw new RuntimeException("could not find s3270 executable in the path");
152 | }
153 | }
154 |
155 | private void assertConnected() {
156 | if (s3270 == null) {
157 | throw new RuntimeException("not connected");
158 | }
159 | }
160 |
161 | public String getS3270Path() {
162 | return s3270Path;
163 | }
164 |
165 | public String getHostname() {
166 | return hostname;
167 | }
168 |
169 | public int getPort() {
170 | return port;
171 | }
172 |
173 | public TerminalType getType() {
174 | return type;
175 | }
176 |
177 | public TerminalMode getMode() {
178 | return mode;
179 | }
180 |
181 | /**
182 | * Represents the result of an s3270 command.
183 | */
184 | private class Result {
185 | private final Listmessage for later retrieval.
247 | */
248 | private class ErrorReader extends Thread {
249 | private String message = null;
250 |
251 | public void run() {
252 | final BufferedReader err = new BufferedReader(new InputStreamReader(s3270.getErrorStream()));
253 | try {
254 | while (true) {
255 | final String msg = err.readLine();
256 | if (msg == null) {
257 | break;
258 | }
259 | message = msg;
260 | }
261 | } catch (final IOException ex) {
262 | // ignore
263 | }
264 | }
265 | }
266 |
267 | private static final Pattern unknownHostPattern = Pattern.compile(
268 | // This message is hard-coded in s3270 as of version 3.3.5,
269 | // so we can rely on it not being localized.
270 | "Unknown host: (.*)");
271 | private static final Pattern unreachablePattern = Pattern.compile(
272 | // This is the hard-coded part of the error message in s3270 version 3.3.5.
273 | "Connect to ([^,]+), port ([0-9]+): (.*)");
274 |
275 | /**
276 | * Checks whether the s3270 process is still running, and if it isn't, tries to determine the cause why it failed.
277 | * This method throws an exception of appropriate type to indicate what went wrong.
278 | */
279 | private void checkS3270Process() {
280 | // Ideally, we'd like to call Process.waitFor() with a timeout,
281 | // but that is so complicated to implement that we take a
282 | // second-rate approach: wait a little while, and then check if
283 | // the process is already terminated.
284 | try {
285 | Thread.sleep(100);
286 | } catch (final InterruptedException ex) {
287 | }
288 | try {
289 | final int exitValue = s3270.exitValue();
290 | final String message = errorReader.message;
291 | if (exitValue == 1 && message != null) {
292 | Matcher m = unknownHostPattern.matcher(message);
293 | if (m.matches()) {
294 | throw new UnknownHostException(m.group(1));
295 | } else {
296 | m = unreachablePattern.matcher(message);
297 | if (m.matches()) {
298 | throw new HostUnreachableException(m.group(1), m.group(3));
299 | }
300 | }
301 | throw new S3270Exception("s3270 terminated with code " + exitValue + ", message: "
302 | + errorReader.message);
303 | }
304 | } catch (final IllegalThreadStateException ex) {
305 | // we get here if the process has still been running in the
306 | // call to s3270.exitValue() above
307 | throw new S3270Exception("s3270 not terminated, error: " + errorReader.message);
308 | }
309 | }
310 |
311 | /**
312 | * waits for a formatted screen
313 | */
314 | private void waitFormat() {
315 | for (int i = 0; i < 50; i++) {
316 | final Result r = doCommand("");
317 | if (r.getStatus().startsWith("U F")) {
318 | return;
319 | }
320 | try {
321 | Thread.sleep(100);
322 | } catch (final InterruptedException ex) {
323 | }
324 | }
325 | }
326 |
327 | public void disconnect() {
328 | assertConnected();
329 | out.println("quit");
330 | out.flush();
331 |
332 | new Thread(new Runnable() {
333 | public void run() {
334 | try {
335 | Thread.sleep(1000);
336 | if (s3270 != null) {
337 | s3270.destroy();
338 | }
339 | } catch (final InterruptedException ex) {
340 | if (s3270 != null) {
341 | s3270.destroy();
342 | }
343 | }
344 | }
345 | }).start();
346 |
347 | try {
348 | s3270.waitFor();
349 | } catch (final InterruptedException ex) { /* ignore */
350 | }
351 | try {
352 | in.close();
353 | } catch (final IOException ex) { /* ignore */
354 | }
355 | out.close();
356 | in = null;
357 | out = null;
358 | s3270 = null;
359 | }
360 |
361 | public boolean isConnected() {
362 | if (s3270 == null || in == null || out == null) {
363 | return false;
364 | } else {
365 | final Result r = doCommand("");
366 | if (r.getStatus().matches(". . . C.*")) {
367 | return true;
368 | } else {
369 | out.println("quit");
370 | out.flush();
371 | s3270.destroy();
372 | s3270 = null;
373 | in = null;
374 | out = null;
375 | return false;
376 | }
377 | }
378 | }
379 |
380 | public void dumpScreen(final String filename) {
381 | assertConnected();
382 | screen.dump(filename);
383 | }
384 |
385 | /**
386 | * Updates the screen object with s3270's buffer data.
387 | */
388 | public void updateScreen() {
389 | assertConnected();
390 | while (true) {
391 | final Result r = doCommand("readbuffer ascii");
392 | if (r.getData().size() > 0) {
393 | final String firstLine = (String) r.getData().get(0);
394 | if (firstLine.startsWith("data: Keyboard locked")) {
395 | continue;
396 | }
397 | }
398 | screen.update(r.getStatus(), r.getData());
399 | break;
400 | }
401 | }
402 |
403 | public Screen getScreen() {
404 | assertConnected();
405 | return screen;
406 | }
407 |
408 | /**
409 | * Writes all changed fields back to s3270.
410 | */
411 | public void submitScreen() {
412 | assertConnected();
413 | for (final Iterator