blob: 36c6800fc69ba0578170ed26ff1fc366ef9df425 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * 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
26package sun.tools.jstat;
27
28import java.util.*;
29import java.net.*;
30import java.io.*;
31
32/**
33 * A class for finding a specific special option in the jstat_options file.
34 *
35 * @author Brian Doherty
36 * @since 1.5
37 */
38public class OptionFinder {
39
40 private static final boolean debug = false;
41
42 URL[] optionsSources;
43
44 public OptionFinder(URL[] optionsSources) {
45 this.optionsSources = optionsSources;
46 }
47
48 public OptionFormat getOptionFormat(String option, boolean useTimestamp) {
49 OptionFormat of = getOptionFormat(option, optionsSources);
50 OptionFormat tof = null;
51 if ((of != null) && (useTimestamp)) {
52 // prepend the timestamp column as first column
53 tof = getOptionFormat("timestamp", optionsSources);
54 if (tof != null) {
55 ColumnFormat cf = (ColumnFormat)tof.getSubFormat(0);
56 of.insertSubFormat(0, cf);
57 }
58 }
59 return of;
60 }
61
62 protected OptionFormat getOptionFormat(String option, URL[] sources) {
63 OptionFormat of = null;
64 for (int i = 0; (i < sources.length) && (of == null); i++) {
65 try {
66 URL u = sources[i];
67 Reader r = new BufferedReader(
68 new InputStreamReader(u.openStream()));
69 of = new Parser(r).parse(option);
70 } catch (IOException e) {
71 if (debug) {
72 System.err.println("Error processing " + sources[i]
73 + " : " + e.getMessage());
74 e.printStackTrace();
75 }
76 } catch (ParserException e) {
77 // Exception in parsing the options file.
78 System.err.println(sources[i] + ": " + e.getMessage());
79 System.err.println("Parsing of " + sources[i] + " aborted");
80 }
81 }
82 return of;
83 }
84}