001: package net.sourceforge.squirrel_sql.client.mainframe.action;
002:
003: /*
004: * Copyright (C) 2002-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.FileWriter;
025: import java.io.IOException;
026: import java.io.PrintWriter;
027: import java.net.URL;
028: import java.util.ArrayList;
029: import java.util.Calendar;
030: import java.util.List;
031:
032: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetViewerTextFileDestination;
033: import net.sourceforge.squirrel_sql.fw.datasetviewer.HashtableDataSet;
034: import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSetViewer;
035: import net.sourceforge.squirrel_sql.fw.util.ICommand;
036: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
037: import net.sourceforge.squirrel_sql.fw.util.NullMessageHandler;
038: import net.sourceforge.squirrel_sql.fw.util.StringManager;
039: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
040: import net.sourceforge.squirrel_sql.fw.util.beanwrapper.URLWrapper;
041: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
042: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
043: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
044: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
045:
046: import net.sourceforge.squirrel_sql.client.ApplicationArguments;
047: import net.sourceforge.squirrel_sql.client.IApplication;
048: import net.sourceforge.squirrel_sql.client.Version;
049: import net.sourceforge.squirrel_sql.client.plugin.PluginInfo;
050: import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
051: import net.sourceforge.squirrel_sql.client.session.ISession;
052: import net.sourceforge.squirrel_sql.client.session.action.DumpSessionCommand;
053: import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
054:
055: /**
056: * This <CODE>ICommand</CODE> will dump the status of the application.
057: *
058: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
059: */
060: public class DumpApplicationCommand implements ICommand {
061: /** Logger for this class. */
062: private final ILogger s_log = LoggerController
063: .createLogger(DumpApplicationCommand.class);
064:
065: /** Prefix for temp file names. */
066: private static final String PREFIX = "dump";
067:
068: /** Suffix for temp file names. */
069: private static final String SUFFIX = "tmp";
070:
071: /** Used to separate lines of data in the dump file. */
072: private static String SEP = "===================================================";
073:
074: /** Application. */
075: private IApplication _app;
076:
077: /** File to dump application to. */
078: private File _outFile;
079:
080: /** Message handler to write status/error info to. */
081: private IMessageHandler _msgHandler;
082:
083: /** Internationalized strings for this class. */
084: private static final StringManager s_stringMgr = StringManagerFactory
085: .getStringManager(DumpApplicationCommand.class);
086:
087: /**
088: * Ctor.
089: *
090: * @param app Application
091: * @param outFile File to dump session to.
092: *
093: * @throws IllegalArgumentException
094: * Thrown if a <TT>null</TT> <TT>IApplication</TT> or <TT>File</TT> passed.
095: */
096: public DumpApplicationCommand(IApplication app, File outFile,
097: IMessageHandler msgHandler) {
098: super ();
099: if (app == null) {
100: throw new IllegalArgumentException(
101: "Null IApplication passed");
102: }
103: if (outFile == null) {
104: throw new IllegalArgumentException("Null File passed");
105: }
106: _app = app;
107: _outFile = outFile;
108:
109: _msgHandler = msgHandler != null ? msgHandler
110: : NullMessageHandler.getInstance();
111: }
112:
113: /**
114: * Dump the application.
115: */
116: public void execute() {
117: List<File> files = new ArrayList<File>();
118: List<String> titles = new ArrayList<String>();
119: synchronized (_app) {
120: ApplicationStatusBean bean = new ApplicationStatusBean();
121: bean.load(_app);
122: try {
123: files.add(createJavaBeanDumpFile(bean));
124: //i18n[DumpApplicationCommand.title.status=Application Status Bean]
125: titles
126: .add(s_stringMgr
127: .getString("DumpApplicationCommand.title.status"));
128: } catch (Throwable th) {
129: //i18n[DumpApplicationCommand.error.dumpingstatus=Error dumping Application Status bean]
130: final String msg = s_stringMgr
131: .getString("DumpApplicationCommand.error.dumpingstatus");
132: _msgHandler.showMessage(msg);
133: _msgHandler.showMessage(th, null);
134: s_log.error(msg, th);
135: }
136:
137: // Dump System Properties.
138: try {
139: File tempFile = File.createTempFile(PREFIX, SUFFIX);
140: IDataSetViewer dest = new DataSetViewerTextFileDestination(
141: tempFile);
142: dest.show(new HashtableDataSet(System.getProperties()));
143: files.add(tempFile);
144: //i18n[DumpApplicationCommand.title.systemprops=System Properties]
145: titles
146: .add(s_stringMgr
147: .getString("DumpApplicationCommand.title.systemprops"));
148: } catch (Throwable th) {
149: //i18n[DumpApplicationCommand.error.dumpingsystemprops=Error dumping metadata]
150: final String msg = s_stringMgr
151: .getString("DumpApplicationCommand.error.dumpingsystemprops");
152: _msgHandler.showMessage(msg);
153: _msgHandler.showMessage(th, null);
154: s_log.error(msg, th);
155: }
156:
157: // Dump drivers
158: try {
159: File tempFile = File.createTempFile(PREFIX, SUFFIX);
160: _app.getDataCache().saveDrivers(tempFile);
161: files.add(tempFile);
162: //i18n[DumpApplicationCommand.title.drivers=Drivers]
163: titles
164: .add(s_stringMgr
165: .getString("DumpApplicationCommand.title.drivers"));
166: } catch (Throwable th) {
167: //i18n[DumpApplicationCommand.error.dumpingdrivers=Error dumping drivers]
168: final String msg = s_stringMgr
169: .getString("DumpApplicationCommand.error.dumpingdrivers");
170: _msgHandler.showMessage(msg);
171: _msgHandler.showMessage(th, null);
172: s_log.error(msg, th);
173: }
174:
175: // Dump aliases.
176: try {
177: File tempFile = File.createTempFile(PREFIX, SUFFIX);
178: _app.getDataCache().saveAliases(tempFile);
179: files.add(tempFile);
180: //i18n[DumpApplicationCommand.title.aliases=Aliases]
181: titles
182: .add(s_stringMgr
183: .getString("DumpApplicationCommand.title.aliases"));
184: } catch (Throwable th) {
185: //i18n[DumpApplicationCommand.error.dumpingaliases=Error dumping aliases]
186: final String msg = s_stringMgr
187: .getString("DumpApplicationCommand.error.dumpingaliases");
188: _msgHandler.showMessage(msg);
189: _msgHandler.showMessage(th, null);
190: s_log.error(msg, th);
191: }
192:
193: // Dump sessions.
194: final ISession[] sessions = _app.getSessionManager()
195: .getConnectedSessions();
196: final DumpSessionCommand sessionCmd = new DumpSessionCommand();
197: for (int i = 0; i < sessions.length; ++i) {
198: try {
199: File tempFile = File.createTempFile(PREFIX, SUFFIX);
200: sessionCmd.setSession(sessions[i]);
201: sessionCmd.setDumpFile(tempFile);
202: sessionCmd.execute();
203: files.add(tempFile);
204: //i18n[DumpApplicationCommand.title.sessiondump=Session Dump: {0}]
205: String title = s_stringMgr.getString(
206: "DumpApplicationCommand.title.sessiondump",
207: sessions[i].getIdentifier());
208: titles.add(title);
209: } catch (Throwable th) {
210: //i18n[DumpApplicationCommand.error.sessiondump=Error dumping sessions]
211: final String msg = s_stringMgr
212: .getString("DumpApplicationCommand.error.sessiondump");
213: _msgHandler.showMessage(msg);
214: _msgHandler.showMessage(th, null);
215: s_log.error(msg, th);
216: }
217: }
218: }
219:
220: combineTempFiles(titles, files);
221: deleteTempFiles(files);
222: }
223:
224: private void combineTempFiles(List<String> titles, List<File> files) {
225: try {
226: PrintWriter wtr = new PrintWriter(new FileWriter(_outFile));
227: try {
228: //i18n[DumpApplicationCommand.header=SQuirreL SQL Client Application Dump {0}]
229: String header = s_stringMgr.getString(
230: "DumpApplicationCommand.header", Calendar
231: .getInstance().getTime());
232: wtr.println(header);
233: for (int i = 0, limit = files.size(); i < limit; ++i) {
234: wtr.println();
235: wtr.println();
236: wtr.println(SEP);
237: wtr.println(titles.get(i));
238: wtr.println(SEP);
239: BufferedReader rdr = new BufferedReader(
240: new FileReader(files.get(i)));
241: try {
242: String line = null;
243: while ((line = rdr.readLine()) != null) {
244: wtr.println(line);
245: }
246: } finally {
247: rdr.close();
248: }
249: }
250: } finally {
251: wtr.close();
252: }
253: } catch (IOException ex) {
254: //i18n[DumpApplicationCommand.error.combiningtempfiles=Error combining temp files into dump file]
255: final String msg = s_stringMgr
256: .getString("DumpApplicationCommand.error.combiningtempfiles");
257: _msgHandler.showMessage(msg);
258: _msgHandler.showMessage(ex.toString());
259: s_log.error(msg, ex);
260: }
261: }
262:
263: private void deleteTempFiles(List<File> files) {
264: for (int i = 0, limit = files.size(); i < limit; ++i) {
265: if (!(files.get(i)).delete()) {
266: //i18n[DumpApplicationCommand.error.deletetempfile=Couldn't delete temporary DumpSession file]
267: s_log
268: .error(s_stringMgr
269: .getString("DumpApplicationCommand.error.deletetempfile"));
270: }
271: }
272: }
273:
274: private File createJavaBeanDumpFile(Object obj) throws IOException,
275: XMLException {
276: File tempFile = File.createTempFile(PREFIX, SUFFIX);
277: XMLBeanWriter wtr = new XMLBeanWriter(obj);
278: wtr.save(tempFile);
279:
280: return tempFile;
281: }
282:
283: public final static class ApplicationStatusBean {
284: private SquirrelPreferences _prefs;
285: private PluginInfo[] _plugins;
286: private String[] _appArgs;
287: private String _version;
288: private String _pluginLoc;
289: private URLWrapper[] _pluginURLs;
290:
291: public ApplicationStatusBean() {
292: super ();
293: }
294:
295: void load(IApplication app) {
296: _prefs = app.getSquirrelPreferences();
297: _plugins = app.getPluginManager().getPluginInformation();
298: _appArgs = ApplicationArguments.getInstance()
299: .getRawArguments();
300: _version = Version.getVersion();
301: _pluginLoc = new ApplicationFiles().getPluginsDirectory()
302: .getAbsolutePath();
303: URL[] urls = app.getPluginManager().getPluginURLs();
304: _pluginURLs = new URLWrapper[urls.length];
305: for (int i = 0; i < urls.length; ++i) {
306: _pluginURLs[i] = new URLWrapper(urls[i]);
307: }
308: }
309:
310: public String getVersion() {
311: return _version;
312: }
313:
314: public SquirrelPreferences getPreferences() {
315: return _prefs;
316: }
317:
318: public String getPluginLocation() {
319: return _pluginLoc;
320: }
321:
322: public PluginInfo[] getPluginInfo() {
323: return _plugins;
324: }
325:
326: public URLWrapper[] getPluginURLs() {
327: return _pluginURLs;
328: }
329:
330: public PluginInfo getPluginInfo(int idx)
331: throws ArrayIndexOutOfBoundsException {
332: return _plugins[idx];
333: }
334:
335: public String[] getApplicationArgument() {
336: return _appArgs;
337: }
338:
339: public String getApplicationArgument(int idx)
340: throws ArrayIndexOutOfBoundsException {
341: return _appArgs[idx];
342: }
343: }
344: }
|