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.ResourceBundle;
026
027/**
028 * Utility methods
029 * @since 2.0
030 */
031public final class Utils {
032
033    private static final ResourceBundle messages = ResourceBundle.getBundle(
034            Utils.class.getPackage().getName() + ".LocalStrings");
035
036    public static final boolean IS_SECURITY_ENABLED =
037            System.getSecurityManager() != null;
038
039
040    private Utils() {
041        // not instantiable
042    }
043
044    /**
045     * Closes the ResultSet (which may be null).
046     *
047     * @param rset a ResultSet, may be {@code null}
048     */
049    public static void closeQuietly(ResultSet rset) {
050        if (rset != null) {
051            try {
052                rset.close();
053            } catch (Exception e) {
054                // ignored
055            }
056        }
057    }
058
059    /**
060     * Closes the Connection (which may be null).
061     *
062     * @param conn a Connection, may be {@code null}
063     */
064    public static void closeQuietly(Connection conn) {
065        if (conn != null) {
066            try {
067                conn.close();
068            } catch (Exception e) {
069                // ignored
070            }
071        }
072    }
073
074    /**
075     * Closes the Statement (which may be null).
076     *
077     * @param stmt a Statement, may be {@code null}
078     */
079    public static void closeQuietly(Statement stmt) {
080        if (stmt != null) {
081            try {
082                stmt.close();
083            } catch (Exception e) {
084                // ignored
085            }
086        }
087    }
088
089
090    /**
091     * Obtain the correct i18n message for the given key.
092     */
093    public static String getMessage(String key) {
094        return getMessage(key, (Object[]) null);
095    }
096
097
098    /**
099     * Obtain the correct i18n message for the given key with placeholders
100     * replaced by the supplied arguments.
101     */
102    public static String getMessage(String key, Object... args) {
103        String msg =  messages.getString(key);
104        if (args == null || args.length == 0) {
105            return msg;
106        }
107        MessageFormat mf = new MessageFormat(msg);
108        return mf.format(args, new StringBuffer(), null).toString();
109    }
110}