Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 004abca8e16d6719557c387e858b40ef74d0ccb2 (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
/*******************************************************************************
 * Copyright (c) 2002-2005 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.config.impl;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

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.common.AddStyleSheet;
import org.eclipse.wst.wsi.internal.core.common.impl.AddStyleSheetImpl;
import org.eclipse.wst.wsi.internal.core.monitor.config.Comment;
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.MonitorConfigReader;
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;
import org.eclipse.wst.wsi.internal.core.xml.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Defines the implementation used to read the monitor config documents.
 * 
 * @version 1.0.1
 * @author Peter Brittenham (peterbr@us.ibm.com)
 */
public class MonitorConfigReaderImpl implements MonitorConfigReader
{
  /**
   * Message list reference.
   */
  protected MessageList messageList = null;

  /**
   * Document location.
   */
  protected String documentURI;

  /**
   * Initialize monitor config.
   */
  public void init(MessageList messageList)
  {
    this.messageList = messageList;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.MonitorConfigReader#readMonitorConfig(String)
   */
  public MonitorConfig readMonitorConfig(String monitorConfigURI)
    throws WSIException
  {
    FileReader fileReader = null;

    try
    {
      fileReader = new FileReader(monitorConfigURI);
    }

    catch (FileNotFoundException fnfe)
    {
      throw new WSIFileNotFoundException(
        getMessage("config02", monitorConfigURI, "Could not find file:"),
        fnfe);
    }

    catch (Exception e)
    {
      throw new WSIException(
        getMessage("config03", monitorConfigURI, "Could not read file:"),
        e);
    }

    return readMonitorConfig(fileReader);
  }

  /* (non-Javadoc)
   * @see org.wsi.test.monitor.config.MonitorConfigReader#readMonitorConfig(Reader)
   */
  public MonitorConfig readMonitorConfig(Reader reader) throws WSIException
  {
    MonitorConfig monitorConfig = new MonitorConfigImpl();

    // Parse XML
    Document doc = XMLUtils.parseXML(reader);

    // Parse elements in the config document
    parseConfigElement(monitorConfig, doc.getDocumentElement());

    return monitorConfig;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.document.DocumentReader#getLocation()
   */
  public String getLocation()
  {
    return this.documentURI;
  }

  /* (non-Javadoc)
   * @see org.wsi.test.document.DocumentReader#setLocation(String)
   */
  public void setLocation(String documentURI)
  {
    this.documentURI = documentURI;
  }

  /**
   * Parse config element.
   */
  private void parseConfigElement(MonitorConfig monitorConfig, Element element)
    throws WSIException
  {
    // ADD: Verify that this is the config element

    // Get first child element
    Element nextElement = XMLUtils.getFirstChild(element);

    boolean isLogDurationDefine = false;
    boolean isCleanupTimeoutSecondsDefine = false;

    // Process each child element
    while (nextElement != null)
    {
      // <logFile>
      if (isElement(nextElement, WSIConstants.ELEM_LOG_FILE))
      {
        // Parse the log element
        parseLogFileElement(monitorConfig, nextElement);
      }

      // <logDuration>
      else if (isElement(nextElement, WSIConstants.ELEM_LOG_DURATION))
      {
        monitorConfig.setLogDuration(getIntValue(nextElement));
        if (monitorConfig.getLogDuration() < 0)
          throw new IllegalArgumentException(
            getMessage(
              "config09",
              WSIConstants.ELEM_LOG_DURATION,
              "Parameter value can't be negative. Element name:"));
        isLogDurationDefine = true;
      }

      // <cleanupTimeoutSeconds>
      else if (
        isElement(nextElement, WSIConstants.ELEM_CLEANUP_TIMEOUT_SECONDS))
      {
        monitorConfig.setTimeout(getIntValue(nextElement));
        if (monitorConfig.getTimeout() < 0)
          throw new IllegalArgumentException(
            getMessage(
              "config09",
              WSIConstants.ELEM_CLEANUP_TIMEOUT_SECONDS,
              "Parameter value can't be negative. Element name:"));
        isCleanupTimeoutSecondsDefine = true;
      }

      // <manInTheMiddle>
      else if (isElement(nextElement, WSIConstants.ELEM_MAN_IN_THE_MIDDLE))
      {
        ManInTheMiddle manInTheMiddle = new ManInTheMiddleImpl();
        monitorConfig.setManInTheMiddle(manInTheMiddle);
        parseManInTheMiddleElement(manInTheMiddle, nextElement);
      }

      // <comment>
      else if (isElement(nextElement, WSIConstants.ELEM_COMMENT))
      {
        Comment comment = new CommentImpl();
        comment.setText(XMLUtils.getText(nextElement));
        monitorConfig.setComment(comment);
      }

      else
      {
        // ADD: handle invalid elements
      }

      // Get next element
      nextElement = XMLUtils.getNextSibling(nextElement);
    }

    if (!isLogDurationDefine)
    {
      throw new IllegalArgumentException(
        getMessage(
          "config10",
          WSIConstants.ELEM_LOG_DURATION,
          "Element is missing. Element name:"));
    }
    if (!isCleanupTimeoutSecondsDefine)
    {
      throw new IllegalArgumentException(
        getMessage(
          "config10",
          WSIConstants.ELEM_CLEANUP_TIMEOUT_SECONDS,
          "Element is missing. Element name:"));
    }
  }

  /**
   * Parse logFile element.
   */
  private void parseLogFileElement(
    MonitorConfig monitorConfig,
    Element element)
    throws WSIException
  {
    // Get the location attribute
    monitorConfig.setLogLocation(
      XMLUtils.getAttributeValue(element, WSIConstants.ATTR_LOCATION));

    // Get the replace attribute
    monitorConfig.setReplaceLog(
      XMLUtils.getBooleanValue(element, WSIConstants.ATTR_REPLACE, false));

    // Get first child element
    Element nextElement = XMLUtils.getFirstChild(element);

    // Process each child element
    while (nextElement != null)
    {
      // <addStyleSheet>
      if (isElement(nextElement, WSIConstants.ELEM_ADD_STYLE_SHEET))
      {
        AddStyleSheet addStyleSheet = new AddStyleSheetImpl();

        // Parse the element  
        TestUtils.parseAddStyleSheet(
          nextElement,
          addStyleSheet,
          WSIConstants.DEFAULT_LOG_XSL);

        // Set add style sheet
        monitorConfig.setAddStyleSheet(addStyleSheet);
      }

      else
      {
        // ADD: handle invalid elements
      }

      // Get next element
      nextElement = XMLUtils.getNextSibling(nextElement);
    }
  }

  /**
   * Parse manIntheMiddle element.
   */
  private void parseManInTheMiddleElement(
    ManInTheMiddle manInTheMiddle,
    Element element)
    throws WSIException
  {
    // Get first child element
    Element nextElement = XMLUtils.getFirstChild(element);

    // Process each child element
    while (nextElement != null)
    {
      // <redirect>
      if (isElement(nextElement, WSIConstants.ELEM_REDIRECT))
      {
        Redirect redirect = new RedirectImpl();
        manInTheMiddle.addRedirect(redirect);
        parseRedirectElement(redirect, nextElement);
      }

      // <comment>
      else if (isElement(nextElement, WSIConstants.ELEM_COMMENT))
      {
        Comment comment = new CommentImpl();
        comment.setText(XMLUtils.getText(nextElement));
        manInTheMiddle.setComment(comment);
      }

      else
      {
        // ADD: handle invalid elements
      }

      // Get next element
      nextElement = XMLUtils.getNextSibling(nextElement);
    }
  }

  /**
   * Parse redirect element.
   */
  private void parseRedirectElement(Redirect redirect, Element element)
    throws WSIException
  {
    // Get first child element
    Element nextElement = XMLUtils.getFirstChild(element);

    boolean isListenPortDefine = false;
    boolean isSchemeAndHostPortDefine = false;
    boolean isMaxConnectionsDefine = false;
    boolean isReadTimeoutSecondsDefine = false;

    // Process each child element
    while (nextElement != null)
    {
      // <listenPort>
      if (isElement(nextElement, WSIConstants.ELEM_LISTEN_PORT))
      {
        redirect.setListenPort(getIntValue(nextElement));
        isListenPortDefine = true;
      }

      // <schemeAndHostPort>
      else if (isElement(nextElement, WSIConstants.ELEM_SCHEME_AND_HOSTPORT))
      {
        redirect.setHost(XMLUtils.getText(nextElement));
        isSchemeAndHostPortDefine = true;
      }

      // <maxConnections>
      else if (isElement(nextElement, WSIConstants.ELEM_MAX_CONNECTIONS))
      {
        redirect.setMaxConnections(getIntValue(nextElement));
        isMaxConnectionsDefine = true;
      }

      // <readTimeoutSeconds>
      else if (isElement(nextElement, WSIConstants.ELEM_READ_TIMEOUT_SECONDS))
      {
        redirect.setReadTimeoutSeconds(getIntValue(nextElement));
        if (redirect.getReadTimeoutSeconds() < 0)
        {
          throw new IllegalArgumentException(
            getMessage(
              "config09",
              WSIConstants.ELEM_READ_TIMEOUT_SECONDS,
              "Parameter value can't be negative. Element name:"));
        }
        isReadTimeoutSecondsDefine = true;
      }

      // <comment>
      else if (isElement(nextElement, WSIConstants.ELEM_COMMENT))
      {
        Comment comment = new CommentImpl();
        comment.setText(XMLUtils.getText(nextElement));
        redirect.setComment(comment);
      }

      else
      {
        // ADD: handle invalid elements
      }

      // Get next element
      nextElement = XMLUtils.getNextSibling(nextElement);
    }

    if (!isListenPortDefine)
    {
      throw new IllegalArgumentException(
        getMessage(
          "config10",
          WSIConstants.ELEM_LISTEN_PORT,
          "Element is missing. Element name:"));
    }
    if (!isSchemeAndHostPortDefine)
    {
      throw new IllegalArgumentException(
        getMessage(
          "config10",
          WSIConstants.ELEM_SCHEME_AND_HOSTPORT,
          "Element is missing. Element name:"));

    }
    if (!isMaxConnectionsDefine)
    {
      throw new IllegalArgumentException(
        getMessage(
          "config10",
          WSIConstants.ELEM_MAX_CONNECTIONS,
          "Element is missing. Element name:"));
    }
    if (!isReadTimeoutSecondsDefine)
    {
      throw new IllegalArgumentException(
        getMessage(
          "config10",
          WSIConstants.ELEM_READ_TIMEOUT_SECONDS,
          "Element is missing. Element name:"));
    }
  }

  /**
   * Determine if this element matches specified local name in the analyzer config namespace.
   */
  private boolean isElement(Element element, String localName)
  {
    return isElement(
      element,
      WSIConstants.NS_URI_WSI_MONITOR_CONFIG,
      localName);
  }

  /**
   * Determine if this element matches specified local name in the analyzer config namespace.
   */
  private boolean isElement(
    Element element,
    String namespace,
    String localName)
  {
    return XMLUtils.isElement(element, namespace, localName);
  }

  /**
   * Get element text as an int.
   */
  private int getIntValue(Element element) throws WSIException
  {
    int returnValue;
    String intValue = null;

    // Get value as a string
    if ((intValue = XMLUtils.getText(element)) == null)
    {
      throw new WSIException(
        getMessage(
          "config04",
          element.getLocalName() + "Element must contain an integer value:"));
    }

    else
    {
      returnValue = Integer.valueOf(intValue).intValue();
    }

    // Return int
    return returnValue;
  }

  /**
   * Get message from resource bundle.
   */
  private String getMessage(String messageID, String defaultMessage)
  {
    return getMessage(messageID, null, defaultMessage);
  }

  /**
   * Get message from resource bundle.
   */
  private String getMessage(
    String messageID,
    String messageData,
    String defaultMessage)
  {
    String message = defaultMessage;
    if (messageList != null)
      message = messageList.getMessage(messageID, messageData, defaultMessage);
    else
      message += " " + messageData + ".";

    return message;
  }
}

Back to the top