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     */
019    package org.crsh.util;
020    
021    import javax.naming.Context;
022    import javax.naming.NamingException;
023    import java.io.Closeable;
024    import java.io.Flushable;
025    import java.io.IOException;
026    import java.net.Socket;
027    import java.sql.Connection;
028    import java.sql.ResultSet;
029    import java.sql.SQLException;
030    import java.sql.Statement;
031    
032    public class Safe {
033    
034      /**
035       * Close the socket and catch any exception thrown.
036       *
037       * @param socket the socket to close
038       * @return true when the close operation returned
039       */
040      public static boolean close(Socket socket) {
041        if (socket != null) {
042          try {
043            socket.close();
044            return true;
045          }
046          catch (Exception ignore) {
047          }
048        }
049        return false;
050      }
051    
052      /**
053       * Close the closeable and catch any exception thrown.
054       *
055       * @param closeable the closeable to close
056       * @return true when the close operation returned
057       */
058      public static boolean close(Closeable closeable) {
059        if (closeable != null) {
060          try {
061            closeable.close();
062            return true;
063          }
064          catch (Exception ignore) {
065          }
066        }
067        return false;
068      }
069    
070      /**
071       * Close the connection and catch any exception thrown.
072       *
073       * @param connection the socket to close
074       * @return true when the connection operation returned
075       */
076      public static boolean close(Connection connection) {
077        if (connection != null) {
078          try {
079            connection.close();
080            return true;
081          }
082          catch (Exception ignore) {
083          }
084        }
085        return false;
086      }
087    
088      /**
089       * Close the statement and catch any exception thrown.
090       *
091       * @param statement the statement to close
092       * @return true when the close operation returned
093       */
094      public static boolean close(Statement statement) {
095        if (statement != null) {
096          try {
097            statement.close();
098            return true;
099          }
100          catch (Exception ignore) {
101          }
102        }
103        return false;
104      }
105    
106      /**
107       * Close the result set and catch any exception thrown.
108       *
109       * @param rs the result set to close
110       * @return true when the close operation returned
111       */
112      public static boolean close(ResultSet rs) {
113        if (rs != null) {
114          try {
115            rs.close();
116            return true;
117          }
118          catch (Exception ignore) {
119          }
120        }
121        return false;
122      }
123    
124      /**
125       * Close the context and catch any exception thrown.
126       *
127       * @param context the context to close
128       * @return true when the close operation returned
129       */
130       public static boolean close(Context context) {
131          if (context != null) {
132             try {
133                context.close();
134               return true;
135             }
136             catch (Exception ignore) {
137             }
138          }
139         return false;
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 true when the flush operation returned
173       */
174      public static boolean flush(Flushable flushable) {
175        if (flushable != null) {
176          try {
177            flushable.flush();
178            return true;
179          }
180          catch (Exception ignore) {
181          }
182        }
183        return false;
184      }
185    }