Revision: 6092
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6092&view=rev
Author: manningr
Date: 2010-12-31 19:37:18 +0000 (Fri, 31 Dec 2010)
Log Message:
-----------
Added an example exception formatter that demonstrates the use of the ExceptionFormatter API.
Modified Paths:
--------------
trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java
Added Paths:
-----------
trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleExceptionFormatter.java
Added: trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleExceptionFormatter.java
===================================================================
--- trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleExceptionFormatter.java (rev 0)
+++ trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExampleExceptionFormatter.java 2010-12-31 19:37:18 UTC (rev 6092)
@@ -0,0 +1,61 @@
+package net.sourceforge.squirrel_sql.plugins.example;
+
+import static net.sourceforge.squirrel_sql.fw.util.Utilities.checkNull;
+import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
+
+/*
+ * Copyright (C) 2010 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+public class ExampleExceptionFormatter implements ExceptionFormatter
+{
+
+ /**
+ * In this example, we simply prefix the original exception message with a string that lets us know that
+ * the Example plugin applied a custom format. In a real implementation, you might want to call the
+ * getCause() method on the Throwable to determine if there were chained exceptions available to drill
+ * down to the actual exception that contains the stack trace and message to the very first place in code
+ * where an exception was encountered.
+ *
+ * @see net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter#format(java.lang.Throwable)
+ */
+ @Override
+ public String format(Throwable t) throws Exception
+ {
+ checkNull("format", "t", t);
+ return "ExampleExceptionFormatter: exception message was: "+t.getMessage();
+ }
+
+ /**
+ * In this example if the throwable is not null, we say that we want to format it. This lets SQuirreL
+ * know that it should not apply it's own formatting, but rather use the format applied by this formatter's
+ * format method. In a real implementation which is specific to a vendor's JDBC driver, the Throwable will
+ * be a particular vendor type and probably has a package like com.{vendor}. Also, if the vendor adds
+ * custom methods for accessing the message, they should be called using the Java reflection API so that
+ * compiling this class does not require proprietary class libraries.
+ *
+ * @see net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter#formatsException(java.lang.Throwable)
+ */
+ @Override
+ public boolean formatsException(Throwable t)
+ {
+ checkNull("formatsException", "t", t);
+ return true;
+ }
+
+}
Modified: trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java
===================================================================
--- trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java 2010-12-31 18:58:54 UTC (rev 6091)
+++ trunk/sql12/plugins/example/src/main/java/net/sourceforge/squirrel_sql/plugins/example/ExamplePlugin.java 2010-12-31 19:37:18 UTC (rev 6092)
@@ -144,6 +144,13 @@
ExampleSqlExecutionListener sqlExecutionListener = new ExampleSqlExecutionListener(messageHandler);
session.getSessionSheet().getSQLPaneAPI().addSQLExecutionListener(sqlExecutionListener);
+ // We will override the default behavior of formatting exception messages that SQuirreL provides for
+ // this session with our own. If this was a real plugin implementation, care would need to be taken
+ // that the custom formatter is only applied to database vendor sessions that this plugin was written
+ // for. SQuirreL doesn't support registering multiple exception formatters for a single session. The
+ // last one to register overrides all former registrations and results in a log warning message.
+ session.setExceptionFormatter(new ExampleExceptionFormatter());
+
return new PluginSessionCallbackAdaptor(this);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|