001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.util;
020
021import javax.naming.Context;
022import javax.naming.NamingException;
023import java.io.Closeable;
024import java.io.Flushable;
025import java.io.IOException;
026import java.net.Socket;
027import java.sql.Connection;
028import java.sql.ResultSet;
029import java.sql.SQLException;
030import java.sql.Statement;
031
032public class Safe {
033
034  /**
035   * Close the socket and catch any exception thrown.
036   *
037   * @param socket the socket to close
038   * @return any Exception thrown during the <code>close</code> operation
039   */
040  public static Exception close(Socket socket) {
041    if (socket != null) {
042      try {
043        socket.close();
044      }
045      catch (Exception e) {
046        return e;
047      }
048    }
049    return null;
050  }
051
052  /**
053   * Close the closeable and catch any exception thrown.
054   *
055   * @param closeable the closeable to close
056   * @return any Exception thrown during the <code>close</code> operation
057   */
058  public static Exception close(Closeable closeable) {
059    if (closeable != null) {
060      try {
061        closeable.close();
062      }
063      catch (Exception e) {
064        return e;
065      }
066    }
067    return null;
068  }
069
070  /**
071   * Close the connection and catch any exception thrown.
072   *
073   * @param connection the socket to close
074   * @return any Exception thrown during the <code>close</code> operation
075   */
076  public static Exception close(Connection connection) {
077    if (connection != null) {
078      try {
079        connection.close();
080      }
081      catch (Exception e) {
082        return e;
083      }
084    }
085    return null;
086  }
087
088  /**
089   * Close the statement and catch any exception thrown.
090   *
091   * @param statement the statement to close
092   * @return any Exception thrown during the <code>close</code> operation
093   */
094  public static Exception close(Statement statement) {
095    if (statement != null) {
096      try {
097        statement.close();
098      }
099      catch (Exception e) {
100        return e;
101      }
102    }
103    return null;
104  }
105
106  /**
107   * Close the result set and catch any exception thrown.
108   *
109   * @param rs the result set to close
110   * @return any Exception thrown during the <code>close</code> operation
111   */
112  public static Exception close(ResultSet rs) {
113    if (rs != null) {
114      try {
115        rs.close();
116      }
117      catch (Exception e) {
118        return e;
119      }
120    }
121    return null;
122  }
123
124  /**
125   * Close the context and catch any exception thrown.
126   *
127   * @param context the context to close
128   * @return any Exception thrown during the <code>close</code> operation
129   */
130   public static Exception close(Context context) {
131      if (context != null) {
132         try {
133            context.close();
134         }
135         catch (Exception e) {
136           return e;
137         }
138      }
139     return null;
140   }
141
142   public static <T extends Throwable> void rethrow(Class<T> throwableClass, Throwable cause) throws T {
143    T throwable;
144
145    //
146    try {
147      throwable = throwableClass.newInstance();
148    }
149    catch (Exception e) {
150      throw new AssertionError(e);
151    }
152
153    //
154    throwable.initCause(cause);
155
156    //
157    throw throwable;
158  }
159
160  public static boolean equals(Object o1, Object o2) {
161    return o1 == null ? o2 == null : (o2 != null && o1.equals(o2));
162  }
163
164  public static boolean notEquals(Object o1, Object o2) {
165    return !equals(o1, o2);
166  }
167
168  /**
169   * Flush the flushable and catch any exception thrown.
170   *
171   * @param flushable the flushable to flush
172   * @return any Exception thrown during the <code>flush</code> operation
173   */
174  public static Exception flush(Flushable flushable) {
175    if (flushable != null) {
176      try {
177        flushable.flush();
178      }
179      catch (Exception e) {
180        return e;
181      }
182    }
183    return null;
184  }
185}