Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5031dd075f4a387235d5d70f8d3e3a2570931547 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*******************************************************************************
 * Copyright (c) 2002-2007 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.monitor;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Vector;

import org.eclipse.wst.wsi.internal.core.ToolInfo;
import org.eclipse.wst.wsi.internal.core.WSIConstants;
import org.eclipse.wst.wsi.internal.core.WSIException;
import org.eclipse.wst.wsi.internal.core.WSIFileNotFoundException;
import org.eclipse.wst.wsi.internal.core.document.DocumentFactory;
import org.eclipse.wst.wsi.internal.core.log.Log;
import org.eclipse.wst.wsi.internal.core.log.LogWriter;
import org.eclipse.wst.wsi.internal.core.monitor.config.ManInTheMiddle;
import org.eclipse.wst.wsi.internal.core.monitor.config.MonitorConfig;
import org.eclipse.wst.wsi.internal.core.monitor.config.Redirect;
import org.eclipse.wst.wsi.internal.core.util.MessageList;
import org.eclipse.wst.wsi.internal.core.util.TestUtils;

/**
 * Message Monitor.
 *
 * @author Peter Brittenham (peterbr.us.ibm.com)
 */

public class Monitor
{
  /**
   * Message list.
   */
  protected MessageList messageList = null;

  private final static String RESOURCE_BUNDLE_NAME =
    "org.eclipse.wst.wsi.internal.core.monitor.Monitor";

  public final static String USAGE_MESSAGE =
    "Usage: Monitor -config <configFilename>";

  /**
   * Conversation ID.
   */
  private int conversationId = 1;

  private Log log = null;
  private LogWriter logWriter = null;
  private MonitorConfig monitorConfig = null;

  /**
  * Tool information.
  */
  public static final String TOOL_NAME = "Monitor";

  protected ToolInfo toolInfo = null;

  protected Vector listenerList = new Vector();

  protected MessageEntryQueue messageEntryQueue = null;

  /**
   * Message monitor.
   * @param args command line arguments.
   * @throws WSIException if there is a problem creating a Monitor object.
   */
  public Monitor(String[] args) throws WSIException
  {
    // Create message list
    this.messageList = new MessageList(RESOURCE_BUNDLE_NAME);

    // Tool information
    toolInfo = new ToolInfo(TOOL_NAME);

    // Create document factory
    DocumentFactory documentFactory = DocumentFactory.newInstance();

    // Get new config object
    monitorConfig = documentFactory.newMonitorConfig();
    monitorConfig.init(this.messageList);

    // Parse command line arguments
    monitorConfig.parseArgs(args);

    String logLocation = monitorConfig.getLogLocation();
    if (logLocation.indexOf(WSIConstants.PATH_SEPARATOR) > -1)
    {
      throw new WSIException(
        messageList.getMessage(
          "config11",
          monitorConfig.getLogLocation(),
          "The log file location value cannot contain the pass separator character:"));
    }

    File file = null;
    try
    {
      // Get file object for log file
      file = new File(monitorConfig.getLogLocation());
    }

    catch (Exception e)
    {
      throw new WSIException(
        messageList.getMessage("config07", "Could not get log file location."),
        e);
    }

    // If replace flag is false and file exists, then throw exception
    if (file.exists() && !monitorConfig.getReplaceLog())
    {
      throw new IllegalArgumentException(
        messageList.getMessage(
          "config08",
          monitorConfig.getLogLocation(),
          "Log file already exists:"));
    }

    try
    {
      // Create output file
      log = documentFactory.newLog();

      // Set style sheet string
      log.setStyleSheetString(
        monitorConfig.getAddStyleSheet().getStyleSheetString());

      // Get log writer
      logWriter = documentFactory.newLogWriter();
      logWriter.setWriter(monitorConfig.getLogLocation());

      // Write start of log file
      logWriter.write(new StringReader(log.getStartXMLString("")));

      // Write monitor tool information
      logWriter.write(new StringReader(toXMLString("")));

      // Create log entry queue
      messageEntryQueue = new MessageEntryQueue(this, log, logWriter);
    }

    catch (Exception e)
    {
      throw new WSIException(
        messageList.getMessage(
          "error03",
          "Could not create log or log writer."),
        e);
    }

    // Get manInTheMiddle settings
    ManInTheMiddle manInTheMiddle = monitorConfig.getManInTheMiddle();

    // Get list of redirects
    Iterator iterator = manInTheMiddle.getRedirectList().iterator();

    // Process each redirect
    Redirect redirect;
    while (iterator.hasNext())
    {
      // Get next redirect
      redirect = (Redirect) iterator.next();

      // Create server socket socket listener
      listenerList.add(new ServerSocketListener(this, redirect));
    }

    // Add shutdown hook
    Runtime.getRuntime().addShutdownHook(new ShutdownHook());

    // Create and start console
    Console console = new Console();
    console.start();
  }

  /**
   * Get the monitor config file.
   * @return the monitor config file.
   */
  public MonitorConfig getMonitorConfig()
  {
    return monitorConfig;
  }

  /**
   * Get the log object.
   * @return the log object.
   */
  public Log getLog()
  {
    return this.log;
  }

  /**
   * Get the log entry queue object.
   * @return the log entry queue object.
   */
  public MessageEntryQueue getMessageEntryQueue()
  {
    return this.messageEntryQueue;
  }

  /**
   * Terminate the monitor.
   */
  void exitMonitor()
  {
    printMessage("stopping01", "Stopping the monitor...");
    System.exit(0);
  }

  /**
   * Stop the monitor because an exception occurred.
   */
  void exitMonitor(Exception e)
  {
    // Display error message
    printMessage(
      "stopping02",
      "Stopping monitor because an exception occurred.");
    System.err.println("EXCEPTION: " + e.toString());
    if (this.monitorConfig.getVerboseOption())
      e.printStackTrace();

    // Exit monitor
    exitMonitor();
  }

  /**
   * Stop the monitor.
   */
  void stopMonitor()
  {
    try
    {
      // Get list of listeners to stop
      Iterator iterator = listenerList.iterator();

      while (iterator.hasNext())
      {
        ((ServerSocketListener) iterator.next()).shutdown();
      }

      // Wait for the cleanup timeout seconds
      Thread.sleep(monitorConfig.getTimeout() * 1000);

      // Write end of log file
      if (logWriter != null)
      {
        logWriter.write(new StringReader(log.getEndXMLString("")));

        logWriter.close();
      }
    }

    catch (Exception e)
    {
      // ADD: How should this execption be handled?
    }

    printMessage("stopped01", "Monitor stopped.");
  }

  /**
   * Command line interface.
   * @param args command line arguments.
   */
  public static void main(String[] args)
  {
    Monitor monitor = null;

    try
    {
      if (args.length < 2)
      {
        staticPrintMessage("usage01", USAGE_MESSAGE);
        System.exit(1);
      }

      if (!args[0].equalsIgnoreCase("-config"))
      {
        staticPrintMessage("usage01", USAGE_MESSAGE);
        System.exit(1);
      }

      // Run the monitor
      monitor = new Monitor(args);
    }

    catch (Exception e)
    {
      boolean printStackTrace = true;
      String messageID;
      String defaultMessage;
      String messageData;

      if ((e instanceof WSIFileNotFoundException)
        || (e instanceof IllegalArgumentException))
      {
        printStackTrace = false;
        messageID = "error01";
        defaultMessage = "Monitor Error:";
        messageData = e.getMessage();
      }

      else
      {
        printStackTrace = true;
        messageID = "error02";
        defaultMessage = "Monitor Stopped By Exception:";
        messageData = e.toString();
      }

      if (monitor != null)
        monitor.printMessage(messageID, messageData, defaultMessage);
      else
        Monitor.staticPrintMessage(messageID, messageData, defaultMessage);

      if (printStackTrace)
        e.printStackTrace();

      // Exit
      if (monitor != null)
        monitor.exitMonitor();
      else
        System.exit(2);
    }
  }

  /**
   * Print a message from the resource bundle.
   * @param key a key.
   * @param defaultMessage a default message.
   */
  public void printMessage(String key, String defaultMessage)
  {
    printMessage(key, null, defaultMessage);
  }

  /**
   * Print a message from the resource bundle.
   * @param key a key.
   * @param messageData message data.
   * @param defaultMessage a default message.
   */
  public void printMessage(
    String key,
    String messageData,
    String defaultMessage)
  {
    messageList.printMessage(key, messageData, defaultMessage);
  }

  /**
   * Print message.
   * @param key a key.
   * @param defaultMessage a default message.
   */
  public static void staticPrintMessage(String key, String defaultMessage)
  {
    staticPrintMessage(key, null, defaultMessage);
  }

  /**
   * Print message.
   * @param key a key.
   * @param messageData message data.
   * @param defaultMessage a default message.
  
   */
  public static void staticPrintMessage(
    String key,
    String messageData,
    String defaultMessage)
  {
    MessageList.printMessage(
      RESOURCE_BUNDLE_NAME,
      key,
      messageData,
      defaultMessage);
  }

  /**
   * Get the next conversation identifier.
   * @return the next conversation identifier.
   */
  synchronized int getNextConversationId()
  {
    return conversationId++;
  }

  /**
   * Return XML string representation of this object.
   * @param namespaceName namespace prefix.
   * @return XML string representation of this object.
   */
  public String toXMLString(String namespaceName)
  {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    // Start
    pw.print(toolInfo.getStartXMLString(namespaceName));

    // Config
    pw.print(
      monitorConfig.toXMLString(WSIConstants.NS_NAME_WSI_MONITOR_CONFIG));

    // End
    pw.println(toolInfo.getEndXMLString(namespaceName));

    return sw.toString();
  }

  /**
   * Shutdown hook.
   */
  class ShutdownHook extends Thread
  {
    /**
     * Run shutdown procedure.
     */
    public void run()
    {
      stopMonitor();
    }
  }

  /**
   * Run command from console.
   */
  class Console extends Thread
  {
    /**
     * Prompt user and wait for input.
     */
    public void run()
    {
      // Get the exit string
      String exitString = messageList.getMessage("exit01", "exit");

      // Display options and how to stop application
      TestUtils.printToolInfo(toolInfo);
      System.out.print(monitorConfig.toString());
      System.out.println(" ");
      printMessage(
        "start01",
        "The "
          + toolInfo.getName()
          + " tool is ready to intercept and log web service messages.");
      printMessage(
        "start02",
        "Type \"exit\" to stop the " + toolInfo.getName() + ".");
      System.out.println(" ");

      // Get the time to stop accepting connections
      long stopTime =
        System.currentTimeMillis() + (monitorConfig.getLogDuration() * 1000);
      // SS

      try
      {
        // Get stdin as a buffered reader
        BufferedReader reader =
          new BufferedReader(new InputStreamReader(System.in));

        // Process input from console
        boolean exit = false;
        while ((!exit) && (System.currentTimeMillis() < stopTime))
        { // SS
          // Sleep
          Thread.sleep(500);

          // Check for user input
          if (reader.ready())
          {
            if (reader.readLine().equalsIgnoreCase(exitString))
              exit = true;
          }
        }
      }

      catch (Exception e)
      {
        // ADD: How should this be handled?
        System.err.println(e.toString());
      }

      // Exit
      exitMonitor();
    }
  }
}

Back to the top