01: /*
02: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.tools.jstat;
27:
28: import java.util.*;
29: import java.net.*;
30: import java.io.*;
31:
32: /**
33: * A class for finding a specific special option in the jstat_options file.
34: *
35: * @author Brian Doherty
36: * @version 1.8, 05/05/07
37: * @since 1.5
38: */
39: public class OptionFinder {
40:
41: private static final boolean debug = false;
42:
43: URL[] optionsSources;
44:
45: public OptionFinder(URL[] optionsSources) {
46: this .optionsSources = optionsSources;
47: }
48:
49: public OptionFormat getOptionFormat(String option,
50: boolean useTimestamp) {
51: OptionFormat of = getOptionFormat(option, optionsSources);
52: OptionFormat tof = null;
53: if ((of != null) && (useTimestamp)) {
54: // prepend the timestamp column as first column
55: tof = getOptionFormat("timestamp", optionsSources);
56: if (tof != null) {
57: ColumnFormat cf = (ColumnFormat) tof.getSubFormat(0);
58: of.insertSubFormat(0, cf);
59: }
60: }
61: return of;
62: }
63:
64: protected OptionFormat getOptionFormat(String option, URL[] sources) {
65: OptionFormat of = null;
66: for (int i = 0; (i < sources.length) && (of == null); i++) {
67: try {
68: URL u = sources[i];
69: Reader r = new BufferedReader(new InputStreamReader(u
70: .openStream()));
71: of = new Parser(r).parse(option);
72: } catch (IOException e) {
73: if (debug) {
74: System.err.println("Error processing " + sources[i]
75: + " : " + e.getMessage());
76: e.printStackTrace();
77: }
78: } catch (ParserException e) {
79: // Exception in parsing the options file.
80: System.err.println(sources[i] + ": " + e.getMessage());
81: System.err.println("Parsing of " + sources[i]
82: + " aborted");
83: }
84: }
85: return of;
86: }
87: }
|