001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *   http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018
019package org.apache.commons.dbcp2;
020
021import java.sql.Connection;
022import java.sql.ResultSet;
023import java.sql.Statement;
024import java.text.MessageFormat;
025import java.util.HashSet;
026import java.util.ResourceBundle;
027import java.util.Set;
028
029/**
030 * Utility methods.
031 * @version $Id: Utils.java 1655301 2015-01-28 13:33:18Z psteitz $
032 * @since 2.0
033 */
034public final class Utils {
035
036    private static final ResourceBundle messages = ResourceBundle.getBundle(
037            Utils.class.getPackage().getName() + ".LocalStrings");
038
039    public static final boolean IS_SECURITY_ENABLED =
040            System.getSecurityManager() != null;
041
042    /** Any SQL_STATE starting with this value is considered a fatal disconnect */
043    public static final String DISCONNECTION_SQL_CODE_PREFIX = "08";
044    
045    /**
046     * SQL codes of fatal connection errors.
047     * <ul>
048     *  <li>57P01 (ADMIN SHUTDOWN)</li>
049     *  <li>57P02 (CRASH SHUTDOWN)</li>
050     *  <li>57P03 (CANNOT CONNECT NOW)</li>
051     *  <li>01002 (SQL92 disconnect error)</li>
052     *  <li>JZ0C0 (Sybase disconnect error)</li>
053     *  <li>JZ0C1 (Sybase disconnect error)</li>
054     * </ul>
055     */
056    public static final Set<String> DISCONNECTION_SQL_CODES;
057
058    static {
059        DISCONNECTION_SQL_CODES = new HashSet<String>();
060        DISCONNECTION_SQL_CODES.add("57P01"); // ADMIN SHUTDOWN
061        DISCONNECTION_SQL_CODES.add("57P02"); // CRASH SHUTDOWN
062        DISCONNECTION_SQL_CODES.add("57P03"); // CANNOT CONNECT NOW
063        DISCONNECTION_SQL_CODES.add("01002"); // SQL92 disconnect error
064        DISCONNECTION_SQL_CODES.add("JZ0C0"); // Sybase disconnect error
065        DISCONNECTION_SQL_CODES.add("JZ0C1"); // Sybase disconnect error
066    }
067
068    private Utils() {
069        // not instantiable
070    }
071
072    /**
073     * Closes the ResultSet (which may be null).
074     *
075     * @param rset a ResultSet, may be {@code null}
076     */
077    public static void closeQuietly(ResultSet rset) {
078        if (rset != null) {
079            try {
080                rset.close();
081            } catch (Exception e) {
082                // ignored
083            }
084        }
085    }
086
087    /**
088     * Closes the Connection (which may be null).
089     *
090     * @param conn a Connection, may be {@code null}
091     */
092    public static void closeQuietly(Connection conn) {
093        if (conn != null) {
094            try {
095                conn.close();
096            } catch (Exception e) {
097                // ignored
098            }
099        }
100    }
101
102    /**
103     * Closes the Statement (which may be null).
104     *
105     * @param stmt a Statement, may be {@code null}
106     */
107    public static void closeQuietly(Statement stmt) {
108        if (stmt != null) {
109            try {
110                stmt.close();
111            } catch (Exception e) {
112                // ignored
113            }
114        }
115    }
116
117
118    /**
119     * Obtain the correct i18n message for the given key.
120     */
121    public static String getMessage(String key) {
122        return getMessage(key, (Object[]) null);
123    }
124
125
126    /**
127     * Obtain the correct i18n message for the given key with placeholders
128     * replaced by the supplied arguments.
129     */
130    public static String getMessage(String key, Object... args) {
131        String msg =  messages.getString(key);
132        if (args == null || args.length == 0) {
133            return msg;
134        }
135        MessageFormat mf = new MessageFormat(msg);
136        return mf.format(args, new StringBuffer(), null).toString();
137    }
138}