| 1 | /* |
| 2 | Copyright (C) 2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License version 2 as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | |
| 17 | */ |
| 18 | package com.mysql.management.util; |
| 19 | |
| 20 | import java.io.PrintStream; |
| 21 | import java.sql.SQLException; |
| 22 | |
| 23 | /** |
| 24 | * @author Eric Herman <eric@mysql.com> |
| 25 | * @version $Id: Exceptions.java,v 1.9 2005/10/25 19:11:16 eherman Exp $ |
| 26 | */ |
| 27 | public class Exceptions { |
| 28 | private PrintStream log; |
| 29 | |
| 30 | public Exceptions() { |
| 31 | this(null); |
| 32 | } |
| 33 | |
| 34 | public Exceptions(PrintStream log) { |
| 35 | this.log = log; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Convienence funciton to convert checked exceptions to RuntimeException |
| 40 | * |
| 41 | * @param e |
| 42 | * java.lang.Exception |
| 43 | * @return a RuntimeException if e is already a Runtime Exception, e is |
| 44 | * simply returned otherwise e is wrapped by a RuntimeException: new |
| 45 | * WrappedException(e) |
| 46 | * |
| 47 | * This is final simply as a hint to the compiler, it may be un-finalized |
| 48 | * safely. |
| 49 | */ |
| 50 | public final RuntimeException toRuntime(Exception e) { |
| 51 | if (e instanceof RuntimeException) { |
| 52 | return (RuntimeException) e; |
| 53 | } |
| 54 | return new WrappedException(e); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * This is final simply as a hint to the compiler, it may be un-finalized |
| 59 | * safely. |
| 60 | */ |
| 61 | public final SQLException toSQLException(Exception e) { |
| 62 | if (e instanceof SQLException) { |
| 63 | return (SQLException) e; |
| 64 | } |
| 65 | return new CausedSQLException(e); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * This is final simply as a hint to the compiler, it may be un-finalized |
| 70 | * safely. |
| 71 | */ |
| 72 | protected final void log(Exception e) { |
| 73 | if (log != null) { |
| 74 | e.printStackTrace(log); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // ------------------------------------ |
| 79 | |
| 80 | public static abstract class Block extends Exceptions { |
| 81 | abstract protected Object inner() throws Exception; |
| 82 | |
| 83 | public Object exec() { |
| 84 | try { |
| 85 | return inner(); |
| 86 | } catch (Exception e) { |
| 87 | log(e); |
| 88 | throw toRuntime(e); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public static abstract class StringBlock extends Exceptions { |
| 94 | abstract protected String inner() throws Exception; |
| 95 | |
| 96 | public String exec() { |
| 97 | try { |
| 98 | return inner(); |
| 99 | } catch (Exception e) { |
| 100 | log(e); |
| 101 | throw toRuntime(e); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public static abstract class SQLBlock extends Exceptions { |
| 107 | public SQLBlock(PrintStream log) { |
| 108 | super(log); |
| 109 | } |
| 110 | |
| 111 | abstract protected Object inner() throws Exception; |
| 112 | |
| 113 | public Object exec() throws SQLException { |
| 114 | try { |
| 115 | return inner(); |
| 116 | } catch (Exception e) { |
| 117 | log(e); |
| 118 | throw toSQLException(e); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public static abstract class VoidBlock extends Exceptions { |
| 124 | abstract protected void inner() throws Exception; |
| 125 | |
| 126 | public void exec() { |
| 127 | try { |
| 128 | inner(); |
| 129 | } catch (Exception e) { |
| 130 | log(e); |
| 131 | throw toRuntime(e); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void execNotThrowingExceptions(PrintStream err) { |
| 136 | try { |
| 137 | inner(); |
| 138 | } catch (Exception e) { |
| 139 | e.printStackTrace(err); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |