001: /*
002: * Created on Mar 2, 2003
003: *
004: * Dbmjui is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License version 2 as
006: * published by the Free Software Foundation.
007: *
008: * Dbmjui is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Library General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public
014: * License along with dbmjui; see the file COPYING. If not,
015: * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
016: * Boston, MA 02111-1307, USA.
017: *
018: */
019: package fr.aliacom.dbmjui.components.information;
020:
021: import java.util.ArrayList;
022:
023: import com.sap.dbtech.powertoys.DBM;
024:
025: import fr.aliacom.dbmjui.*;
026: import fr.aliacom.dbmjui.beans.CacheInfoEntry;
027: import fr.aliacom.dbmjui.beans.DataInfoEntry;
028: import fr.aliacom.dbmjui.beans.IOInfoEntry;
029: import fr.aliacom.dbmjui.beans.LockInfoEntry;
030: import fr.aliacom.dbmjui.beans.LogInfoEntry;
031: import fr.aliacom.dbmjui.beans.SessionInfoEntry;
032: import fr.aliacom.dbmjui.beans.VersionInfoEntry;
033: import fr.aliacom.dbmjui.driver.*;
034: import fr.aliacom.util.StringFunctions;
035:
036: /**
037: * Some utility functions to parse the output
038: * from the dbm info commands.
039: *
040: * Those functions turns the string output into ArrayList of
041: * JavaBeans.
042: *
043: *
044: * @author tom
045: *
046: * (c) 2001, 2003 Thomas Cataldo
047: */
048: public class InformationHelper implements IInformationHelper {
049:
050: public InformationHelper() {
051: }
052:
053: /* (non-Javadoc)
054: * @see fr.aliacom.dbmjui.IInformationHelper#getIOInfo(fr.aliacom.dbmjui.DbInstance)
055: */
056: public ArrayList getIOInfo(DbInstance dbi) {
057: return parseIOInfo(executeInfoCommand(dbi, "io", false));
058: }
059:
060: public ArrayList getCachesInfo(DbInstance dbi) {
061: return parseCachesInfo(executeInfoCommand(dbi, "caches", false));
062: }
063:
064: public ArrayList getDataInfo(DbInstance dbi) {
065: return parseDataInfo(executeInfoCommand(dbi, "data", false));
066: }
067:
068: public ArrayList getLocksInfo(DbInstance dbi) {
069: return parseLocksInfo(executeInfoCommand(dbi, "locks", false));
070: }
071:
072: public ArrayList getLogsInfo(DbInstance dbi) {
073: return parseLogsInfo(executeInfoCommand(dbi, "log", false));
074: }
075:
076: public ArrayList getSessionsInfo(DbInstance dbi) {
077: return parseSessionsInfo(executeInfoCommand(dbi, "users", false));
078: }
079:
080: public ArrayList getVersionsInfo(DbInstance dbi) {
081: return parseVersionsInfo(executeInfoCommand(dbi, "versions",
082: false));
083: }
084:
085: private String executeInfoCommand(DbInstance dbi, String info,
086: boolean needSql) {
087: String ret = "";
088: try {
089: DBM dbm = dbi.getDbmConnection();
090: if (needSql) {
091: dbm.cmd("sql_connect");
092: }
093: ret = dbm.cmd("info " + info);
094: dbm.release();
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098: return ret;
099: }
100:
101: /**
102: * Parses the "info caches" output and turns it CacheInfoEntry beans.
103: *
104: * @param info the output of "info caches" command
105: * @return an ArrayList of <code>CacheInfoEntry</code> beans.
106: */
107: protected ArrayList parseCachesInfo(String info) {
108: ArrayList untyped = getInfoAsStrings(info, "\\|");
109: ArrayList ret = new ArrayList(untyped.size());
110: for (int i = 0, n = untyped.size(); i < n; i++) {
111: String[] line = (String[]) untyped.get(i);
112: ret.add(new CacheInfoEntry(line[0], line[1], line[2],
113: line[3], line[4]));
114: }
115:
116: return ret;
117: }
118:
119: /**
120: * Parses the "info data" output and turns it DataInfoEntry beans.
121: *
122: * @param info the output of "info data" command
123: * @return an ArrayList of <code>DataInfoEntry</code> beans.
124: */
125: protected ArrayList parseDataInfo(String info) {
126: ArrayList untyped = getInfoAsStrings(info, "=");
127: ArrayList ret = new ArrayList(untyped.size());
128: for (int i = 0, n = untyped.size(); i < n; i++) {
129: String[] line = (String[]) untyped.get(i);
130: ret.add(new DataInfoEntry(line[0], line[1]));
131: }
132:
133: return ret;
134: }
135:
136: /**
137: * Parses the "info io" output and turns it IOInfoEntry beans.
138: *
139: * @param info the output of "info io" command
140: * @return an ArrayList of <code>IOInfoEntry</code> beans.
141: */
142: protected ArrayList parseIOInfo(String info) {
143: ArrayList untyped = getInfoAsStrings(info, "\\|");
144: ArrayList ret = new ArrayList(untyped.size());
145: for (int i = 0, n = untyped.size(); i < n; i++) {
146: String[] line = (String[]) untyped.get(i);
147: ret.add(new IOInfoEntry(line[0], line[1], line[2], line[3],
148: line[4]));
149: }
150:
151: return ret;
152: }
153:
154: /**
155: * Parses the "info log" output and turns it LogInfoEntry beans.
156: *
157: * @param info the output of "info log" command
158: * @return an ArrayList of <code>LogInfoEntry</code> beans.
159: */
160: protected ArrayList parseLogsInfo(String info) {
161: ArrayList untyped = getInfoAsStrings(info, "=");
162: ArrayList ret = new ArrayList(untyped.size());
163: for (int i = 0, n = untyped.size(); i < n; i++) {
164: String[] line = (String[]) untyped.get(i);
165: ret.add(new LogInfoEntry(line[0], line[1]));
166: }
167:
168: return ret;
169: }
170:
171: /**
172: * Parses the "info locks" output and turns it LockInfoEntry beans.
173: *
174: * @param info the output of "info locks" command
175: * @return an ArrayList of <code>LockInfoEntry</code> beans.
176: */
177: protected ArrayList parseLocksInfo(String info) {
178: ArrayList untyped = getInfoAsStrings(info, "=");
179: ArrayList ret = new ArrayList(untyped.size());
180: for (int i = 0, n = untyped.size(); i < n; i++) {
181: String[] line = (String[]) untyped.get(i);
182: ret.add(new LockInfoEntry(line[0], line[1]));
183: }
184:
185: return ret;
186: }
187:
188: /**
189: * Parses the "info version" output and turns it VersionInfoEntry beans.
190: *
191: * @param info the output of "info caches" command
192: * @return an ArrayList of <code>VersionInfoEntry</code> beans.
193: */
194: protected ArrayList parseVersionsInfo(String info) {
195: ArrayList untyped = getInfoAsStrings(info, "=");
196: ArrayList ret = new ArrayList(untyped.size());
197: for (int i = 0, n = untyped.size(); i < n; i++) {
198: String[] line = (String[]) untyped.get(i);
199: ret.add(new VersionInfoEntry(line[0], line[1]));
200: }
201:
202: return ret;
203: }
204:
205: /**
206: * Parses the "info caches" output and turns it CacheInfoEntry beans.
207: *
208: * @param info the output of "info caches" command
209: * @return an ArrayList of <code>CacheInfoEntry</code> beans.
210: */
211: protected ArrayList parseSessionsInfo(String info) {
212: ArrayList untyped = getInfoAsStrings(info, "\\|");
213: ArrayList ret = new ArrayList(untyped.size());
214: for (int i = 0, n = untyped.size(); i < n; i++) {
215: String[] line = (String[]) untyped.get(i);
216: ret.add(new SessionInfoEntry(line[0], line[1], line[2],
217: line[3], line[4], line[5]));
218: }
219:
220: return ret;
221: }
222:
223: /**
224: * Parses the output of any of the info command,
225: * and turns it into an ArrayList of String arrays.
226: * Each the string array element is a field for the info
227: * command. Whitespaces at the end are trimmed.
228: *
229: * @param info
230: * @param separatorChar the separator used between the info fields
231: * @return an ArrayList<String[]>
232: */
233: protected ArrayList getInfoAsStrings(String info,
234: String separatorChar) {
235: String[] lines = info.split("\n");
236: ArrayList data = new ArrayList(lines.length);
237: for (int i = 3; i < lines.length; i++) {
238: String[] line = lines[i].split(separatorChar);
239: for (int j = 0; j < line.length; j++) {
240: line[j] = StringFunctions.rtrim(line[j]);
241: }
242: data.add(line);
243: }
244: return data;
245:
246: }
247:
248: }
|