Source Code Cross Referenced for DumpSessionCommand.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » client » session » action » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.client.session.action 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.squirrel_sql.client.session.action;
002:
003:        /*
004:         * Copyright (C) 2002 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.sql.Connection;
028:        import java.util.ArrayList;
029:        import java.util.Calendar;
030:        import java.util.List;
031:
032:        import net.sourceforge.squirrel_sql.client.session.ISession;
033:        import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetViewerTextFileDestination;
034:        import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSetViewer;
035:        import net.sourceforge.squirrel_sql.fw.datasetviewer.ObjectArrayDataSet;
036:        import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
037:        import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
038:        import net.sourceforge.squirrel_sql.fw.util.ICommand;
039:        import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
040:        import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
041:        import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
042:        import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
043:        import net.sourceforge.squirrel_sql.fw.xml.XMLException;
044:
045:        /**
046:         * This <CODE>ICommand</CODE> will dump the status of a session to a text
047:         * file.
048:         *
049:         * @author  <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
050:         */
051:        public class DumpSessionCommand implements  ICommand {
052:            /** Logger for this class. */
053:            private final ILogger s_log = LoggerController
054:                    .createLogger(DumpSessionCommand.class);
055:
056:            /** Prefix for temp file names. */
057:            private static final String PREFIX = "dump";
058:
059:            /** Suffix for temp file names. */
060:            private static final String SUFFIX = "tmp";
061:
062:            /** Used to separate lines of data in the dump file. */
063:            private static String SEP = "===================================================";
064:
065:            /** Session to be dumped. */
066:            private ISession _session;
067:
068:            /** File to dump session to. */
069:            private File _outFile;
070:
071:            /** Message handler to write errors to. */
072:            private IMessageHandler _msgHandler;
073:
074:            /**
075:             * Default ctor.
076:             */
077:            public DumpSessionCommand() {
078:                this (null, null);
079:            }
080:
081:            /**
082:             * Ctor using sessions message handler for errors.
083:             *
084:             * @param	outFile		File to dump session to.
085:             */
086:            public DumpSessionCommand(File outFile) {
087:                this (outFile, null);
088:            }
089:
090:            /**
091:             * Ctor.
092:             *
093:             * @param	outFile		File to dump session to.
094:             * @param	msgHandler	Message handler. If <TT>null</TT> then the sessions
095:             * 						message handler will be used for errors.
096:             */
097:            public DumpSessionCommand(File outFile, IMessageHandler msgHandler) {
098:                super ();
099:                _outFile = outFile;
100:                _msgHandler = msgHandler;
101:            }
102:
103:            /**
104:             * Set the file to dump to.
105:             *
106:             * @param	file	File to dump to.
107:             * 
108:             * @throws	IllegalArgumentException
109:             *			Thrown if a <TT>null</TT> <TT>ISession</TT> or <TT>File</TT> passed.
110:             */
111:            public void setDumpFile(File file) {
112:                if (file == null) {
113:                    throw new IllegalArgumentException("Null Dump File passed");
114:                }
115:                _outFile = file;
116:            }
117:
118:            /**
119:             * Set the session to dump.
120:             *
121:             * @param	session	Session to be dumped.
122:             * 
123:             * @throws	IllegalArgumentException
124:             *			Thrown if a <TT>null</TT> <TT>ISession</TT> or <TT>File</TT> passed.
125:             */
126:            public void setSession(ISession session) {
127:                if (session == null) {
128:                    throw new IllegalArgumentException("Null ISession passed");
129:                }
130:                _session = session;
131:            }
132:
133:            /**
134:             * Dump the session.
135:             */
136:            public void execute() {
137:                if (_session == null) {
138:                    throw new IllegalStateException(
139:                            "Trying to dump null session");
140:                }
141:                if (_outFile == null) {
142:                    throw new IllegalStateException(
143:                            "Trying to dump session to null file");
144:                }
145:
146:                final List<File> files = new ArrayList<File>();
147:                final List<String> titles = new ArrayList<String>();
148:                synchronized (_session) {
149:                    final ISQLConnection conn = _session.getSQLConnection();
150:                    final SQLDatabaseMetaData md = conn.getSQLMetaData();
151:
152:                    // Dump session properties.
153:                    try {
154:                        files.add(createJavaBeanDumpFile(_session
155:                                .getProperties()));
156:                        titles.add("Session Properties");
157:                    } catch (Throwable th) {
158:                        final String msg = "Error dumping driver info";
159:                        showErrorMessage(msg);
160:                        showErrorMessage(th);
161:                        s_log.error(msg, th);
162:                    }
163:
164:                    // Dump driver information.
165:                    try {
166:                        files.add(createJavaBeanDumpFile(_session.getDriver()));
167:                        titles.add("Driver");
168:                    } catch (Throwable th) {
169:                        final String msg = "Error dumping driver info";
170:                        showErrorMessage(msg);
171:                        showErrorMessage(th);
172:                        s_log.error(msg, th);
173:                    }
174:
175:                    // Dump alias information.
176:                    try {
177:                        files.add(createJavaBeanDumpFile(_session.getAlias()));
178:                        titles.add("Alias");
179:                    } catch (Throwable th) {
180:                        final String msg = "Error dumping alias info";
181:                        showErrorMessage(msg);
182:                        showErrorMessage(th);
183:                        s_log.error(msg, th);
184:                    }
185:
186:                    // Dump general connection info.
187:                    try {
188:                        files.add(createGeneralConnectionDumpFile(conn));
189:                        titles.add("Connection - General");
190:                    } catch (Throwable th) {
191:                        final String msg = "Error dumping general connection info";
192:                        showErrorMessage(msg);
193:                        showErrorMessage(th);
194:                        s_log.error(msg, th);
195:                    }
196:
197:                    // Dump meta data.
198:                    try {
199:                        File tempFile = File.createTempFile(PREFIX, SUFFIX);
200:                        IDataSetViewer dest = new DataSetViewerTextFileDestination(
201:                                tempFile);
202:                        dest.show(conn.getSQLMetaData().getMetaDataSet());
203:                        files.add(tempFile);
204:                        titles.add("Metadata");
205:                    } catch (Throwable th) {
206:                        final String msg = "Error dumping metadata";
207:                        showErrorMessage(msg);
208:                        showErrorMessage(th);
209:                        s_log.error(msg, th);
210:                    }
211:
212:                    // Dump catalogs.
213:                    try {
214:                        File tempFile = File.createTempFile(PREFIX, SUFFIX);
215:                        IDataSetViewer dest = new DataSetViewerTextFileDestination(
216:                                tempFile);
217:                        dest.show(new ObjectArrayDataSet(md.getCatalogs()));
218:                        files.add(tempFile);
219:                        titles.add("Catalogs");
220:                    } catch (Throwable th) {
221:                        final String msg = "Error dumping catalogs";
222:                        showErrorMessage(msg);
223:                        showErrorMessage(th);
224:                        s_log.error(msg, th);
225:                    }
226:
227:                    // Dump schemas.
228:                    try {
229:                        File tempFile = File.createTempFile(PREFIX, SUFFIX);
230:                        IDataSetViewer dest = new DataSetViewerTextFileDestination(
231:                                tempFile);
232:                        dest.show(new ObjectArrayDataSet(_session
233:                                .getSchemaInfo().getSchemas()));
234:                        files.add(tempFile);
235:                        titles.add("Schemas");
236:                    } catch (Throwable th) {
237:                        final String msg = "Error dumping schemas";
238:                        showErrorMessage(msg);
239:                        showErrorMessage(th);
240:                        s_log.error(msg, th);
241:                    }
242:
243:                    // Dump data types.
244:                    try {
245:                        File tempFile = File.createTempFile(PREFIX, SUFFIX);
246:                        IDataSetViewer dest = new DataSetViewerTextFileDestination(
247:                                tempFile);
248:                        dest.show(conn.getSQLMetaData().getTypesDataSet());
249:                        files.add(tempFile);
250:                        titles.add("Data Types");
251:                    } catch (Throwable th) {
252:                        final String msg = "Error dumping data types";
253:                        showErrorMessage(msg);
254:                        showErrorMessage(th);
255:                        s_log.error(msg, th);
256:                    }
257:
258:                    // Dump table types.
259:                    try {
260:                        File tempFile = File.createTempFile(PREFIX, SUFFIX);
261:                        IDataSetViewer dest = new DataSetViewerTextFileDestination(
262:                                tempFile);
263:                        dest.show(new ObjectArrayDataSet(md.getTableTypes()));
264:                        files.add(tempFile);
265:                        titles.add("Table Types");
266:                    } catch (Throwable th) {
267:                        final String msg = "Error dumping table types";
268:                        showErrorMessage(msg);
269:                        showErrorMessage(th);
270:                        s_log.error(msg, th);
271:                    }
272:                }
273:
274:                combineTempFiles(titles, files);
275:                deleteTempFiles(files);
276:            }
277:
278:            private void combineTempFiles(List<String> titles, List<File> files) {
279:                try {
280:                    PrintWriter wtr = new PrintWriter(new FileWriter(_outFile));
281:                    try {
282:                        wtr.println("SQuirreL SQL Client Session Dump "
283:                                + Calendar.getInstance().getTime());
284:                        for (int i = 0, limit = files.size(); i < limit; ++i) {
285:                            wtr.println();
286:                            wtr.println();
287:                            wtr.println(SEP);
288:                            wtr.println(titles.get(i));
289:                            wtr.println(SEP);
290:                            File file = files.get(i);
291:                            BufferedReader rdr = new BufferedReader(
292:                                    new FileReader(file));
293:                            try {
294:                                String line = null;
295:                                while ((line = rdr.readLine()) != null) {
296:                                    wtr.println(line);
297:                                }
298:                            } finally {
299:                                rdr.close();
300:                            }
301:                        }
302:                    } finally {
303:                        wtr.close();
304:                    }
305:                } catch (IOException ex) {
306:                    final String msg = "Error combining temp files into dump file";
307:                    showErrorMessage(msg);
308:                    showErrorMessage(ex);
309:                    s_log.error(msg, ex);
310:                }
311:            }
312:
313:            private void deleteTempFiles(List<File> files) {
314:                for (int i = 0, limit = files.size(); i < limit; ++i) {
315:                    if (!(files.get(i)).delete()) {
316:                        s_log
317:                                .error("Couldn't delete temporary DumpSession file");
318:                    }
319:                }
320:            }
321:
322:            private File createJavaBeanDumpFile(Object obj) throws IOException,
323:                    XMLException {
324:                File tempFile = File.createTempFile(PREFIX, SUFFIX);
325:                XMLBeanWriter wtr = new XMLBeanWriter(obj);
326:                wtr.save(tempFile);
327:
328:                return tempFile;
329:            }
330:
331:            private File createGeneralConnectionDumpFile(ISQLConnection conn)
332:                    throws IOException {
333:                Connection myConn = conn.getConnection();
334:
335:                File tempFile = File.createTempFile(PREFIX, SUFFIX);
336:                PrintWriter wtr = new PrintWriter(new FileWriter(tempFile));
337:                try {
338:                    // Dump general connection info.
339:                    String line = null;
340:                    try {
341:                        line = String.valueOf(myConn.getTransactionIsolation());
342:                    } catch (Throwable th) {
343:                        line = th.toString();
344:                    }
345:                    wtr.println("transIsolation: " + line);
346:                    try {
347:                        line = String.valueOf(myConn.isReadOnly());
348:                    } catch (Throwable th) {
349:                        line = th.toString();
350:                    }
351:                    wtr.println("readonly: " + line);
352:
353:                    return tempFile;
354:                } finally {
355:                    wtr.close();
356:                }
357:            }
358:
359:            private void showErrorMessage(String msg) {
360:                if (_session != null) {
361:                    _session.showErrorMessage(msg);
362:                } else if (_msgHandler != null) {
363:                    _msgHandler.showErrorMessage(msg);
364:                } else {
365:                    s_log.error("No IMessageHandler or ISession configured");
366:                }
367:            }
368:
369:            private void showErrorMessage(Throwable th) {
370:                if (_session != null) {
371:                    _session.showErrorMessage(th);
372:                } else if (_msgHandler != null) {
373:                    _msgHandler.showErrorMessage(th, null);
374:                } else {
375:                    s_log.error("No IMessageHandler or ISession configured");
376:                }
377:            }
378:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.