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 *
032 * @since 2.0
033 */
034public final class Utils {
035
036    private static final ResourceBundle messages = ResourceBundle
037            .getBundle(Utils.class.getPackage().getName() + ".LocalStrings");
038
039    /**
040     * Whether the security manager is enabled.
041     */
042    public static final boolean IS_SECURITY_ENABLED = System.getSecurityManager() != null;
043
044    /** Any SQL_STATE starting with this value is considered a fatal disconnect */
045    public static final String DISCONNECTION_SQL_CODE_PREFIX = "08";
046
047    /**
048     * SQL codes of fatal connection errors.
049     * <ul>
050     * <li>57P01 (ADMIN SHUTDOWN)</li>
051     * <li>57P02 (CRASH SHUTDOWN)</li>
052     * <li>57P03 (CANNOT CONNECT NOW)</li>
053     * <li>01002 (SQL92 disconnect error)</li>
054     * <li>JZ0C0 (Sybase disconnect error)</li>
055     * <li>JZ0C1 (Sybase disconnect error)</li>
056     * </ul>
057     */
058    public static final Set<String> DISCONNECTION_SQL_CODES;
059
060    static {
061        DISCONNECTION_SQL_CODES = new HashSet<>();
062        DISCONNECTION_SQL_CODES.add("57P01"); // ADMIN SHUTDOWN
063        DISCONNECTION_SQL_CODES.add("57P02"); // CRASH SHUTDOWN
064        DISCONNECTION_SQL_CODES.add("57P03"); // CANNOT CONNECT NOW
065        DISCONNECTION_SQL_CODES.add("01002"); // SQL92 disconnect error
066        DISCONNECTION_SQL_CODES.add("JZ0C0"); // Sybase disconnect error
067        DISCONNECTION_SQL_CODES.add("JZ0C1"); // Sybase disconnect error
068    }
069
070    private Utils() {
071        // not instantiable
072    }
073
074    /**
075     * Closes the ResultSet (which may be null).
076     *
077     * @param resultSet
078     *            a ResultSet, may be {@code null}
079     */
080    public static void closeQuietly(final ResultSet resultSet) {
081        if (resultSet != null) {
082            try {
083                resultSet.close();
084            } catch (final Exception e) {
085                // ignored
086            }
087        }
088    }
089
090    /**
091     * Closes the Connection (which may be null).
092     *
093     * @param connection
094     *            a Connection, may be {@code null}
095     */
096    public static void closeQuietly(final Connection connection) {
097        if (connection != null) {
098            try {
099                connection.close();
100            } catch (final Exception e) {
101                // ignored
102            }
103        }
104    }
105
106    /**
107     * Closes the Statement (which may be null).
108     *
109     * @param statement
110     *            a Statement, may be {@code null}.
111     */
112    public static void closeQuietly(final Statement statement) {
113        if (statement != null) {
114            try {
115                statement.close();
116            } catch (final Exception e) {
117                // ignored
118            }
119        }
120    }
121
122    /**
123     * Gets the correct i18n message for the given key.
124     *
125     * @param key
126     *            The key to look up an i18n message.
127     * @return The i18n message.
128     */
129    public static String getMessage(final String key) {
130        return getMessage(key, (Object[]) null);
131    }
132
133    /**
134     * Gets the correct i18n message for the given key with placeholders replaced by the supplied arguments.
135     *
136     * @param key
137     *            A message key.
138     * @param args
139     *            The message arguments.
140     * @return An i18n message.
141     */
142    public static String getMessage(final String key, final Object... args) {
143        final String msg = messages.getString(key);
144        if (args == null || args.length == 0) {
145            return msg;
146        }
147        final MessageFormat mf = new MessageFormat(msg);
148        return mf.format(args, new StringBuffer(), null).toString();
149    }
150
151    /**
152     * Converts the given String to a char[].
153     *
154     * @param value
155     *            may be null.
156     * @return a char[] or null.
157     */
158    public static char[] toCharArray(final String value) {
159        return value != null ? value.toCharArray() : null;
160    }
161
162    /**
163     * Converts the given char[] to a String.
164     *
165     * @param value
166     *            may be null.
167     * @return a String or null.
168     */
169    public static String toString(final char[] value) {
170        return value == null ? null : String.valueOf(value);
171    }
172}