001    /*
002     * Copyright (C) 2003-2009 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.IOException;
025    import java.net.Socket;
026    import java.sql.Connection;
027    import java.sql.ResultSet;
028    import java.sql.SQLException;
029    import java.sql.Statement;
030    
031    /**
032     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
033     * @version $Revision$
034     */
035    public class Safe {
036    
037      public static void close(Socket socket) {
038        if (socket != null) {
039          try {
040            socket.close();
041          }
042          catch (IOException ignore) {
043          }
044        }
045      }
046    
047      public static void close(Closeable closeable) {
048        if (closeable != null) {
049          try {
050            closeable.close();
051          }
052          catch (IOException ignore) {
053          }
054        }
055      }
056    
057      public static void close(Connection connection) {
058        if (connection != null) {
059          try {
060            connection.close();
061          }
062          catch (SQLException ignore) {
063          }
064        }
065      }
066    
067      public static void close(Statement statement) {
068        if (statement != null) {
069          try {
070            statement.close();
071          }
072          catch (SQLException ignore) {
073          }
074        }
075      }
076    
077      public static void close(ResultSet rs) {
078        if (rs != null) {
079          try {
080            rs.close();
081          }
082          catch (SQLException ignore) {
083          }
084        }
085      }
086    
087       public static void close(Context rs) {
088          if (rs != null) {
089             try {
090                rs.close();
091             }
092             catch (NamingException ignore) {
093             }
094          }
095       }
096    
097       public static <T extends Throwable> void rethrow(Class<T> throwableClass, Throwable cause) throws T {
098        T throwable;
099    
100        //
101        try {
102          throwable = throwableClass.newInstance();
103        }
104        catch (Exception e) {
105          throw new AssertionError(e);
106        }
107    
108        //
109        throwable.initCause(cause);
110    
111        //
112        throw throwable;
113      }
114    
115      public static boolean equals(Object o1, Object o2) {
116        return o1 == null ? o2 == null : (o2 != null && o1.equals(o2));
117      }
118    
119      public static boolean notEquals(Object o1, Object o2) {
120        return !equals(o1, o2);
121      }
122    }