001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.dao.jdbc;
016    
017    import com.liferay.portal.kernel.jndi.JNDIUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.dao.orm.UpgradeOptimizedConnectionHandler;
021    import com.liferay.portal.kernel.util.InfrastructureUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.ProxyUtil;
025    
026    import java.sql.Connection;
027    import java.sql.ResultSet;
028    import java.sql.SQLException;
029    import java.sql.Statement;
030    
031    import java.util.Properties;
032    
033    import javax.naming.Context;
034    import javax.naming.InitialContext;
035    import javax.naming.NamingException;
036    
037    import javax.sql.DataSource;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class DataAccess {
043    
044            public static void cleanUp(Connection connection) {
045                    try {
046                            if (connection != null) {
047                                    connection.close();
048                            }
049                    }
050                    catch (SQLException sqle) {
051                            if (_log.isWarnEnabled()) {
052                                    _log.warn(sqle.getMessage());
053                            }
054                    }
055            }
056    
057            public static void cleanUp(Connection connection, Statement statement) {
058                    cleanUp(statement);
059                    cleanUp(connection);
060            }
061    
062            public static void cleanUp(
063                    Connection connection, Statement statement, ResultSet resultSet) {
064    
065                    cleanUp(resultSet);
066                    cleanUp(statement);
067                    cleanUp(connection);
068            }
069    
070            public static void cleanUp(ResultSet resultSet) {
071                    try {
072                            if (resultSet != null) {
073                                    resultSet.close();
074                            }
075                    }
076                    catch (SQLException sqle) {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn(sqle.getMessage());
079                            }
080                    }
081            }
082    
083            public static void cleanUp(Statement statement) {
084                    try {
085                            if (statement != null) {
086                                    statement.close();
087                            }
088                    }
089                    catch (SQLException sqle) {
090                            if (_log.isWarnEnabled()) {
091                                    _log.warn(sqle.getMessage());
092                            }
093                    }
094            }
095    
096            public static Connection getConnection() throws SQLException {
097                    DataSource dataSource = InfrastructureUtil.getDataSource();
098    
099                    return dataSource.getConnection();
100            }
101    
102            public static Connection getConnection(String location)
103                    throws NamingException, SQLException {
104    
105                    Properties properties = PropsUtil.getProperties(
106                            PropsKeys.JNDI_ENVIRONMENT, true);
107    
108                    Context context = new InitialContext(properties);
109    
110                    DataSource dataSource = (DataSource)JNDIUtil.lookup(context, location);
111    
112                    return dataSource.getConnection();
113            }
114    
115            public static Connection getUpgradeOptimizedConnection()
116                    throws SQLException {
117    
118                    Connection con = getConnection();
119    
120                    Thread currentThread = Thread.currentThread();
121    
122                    ClassLoader classLoader = currentThread.getContextClassLoader();
123    
124                    return (Connection)ProxyUtil.newProxyInstance(
125                            classLoader, new Class[] {Connection.class},
126                            new UpgradeOptimizedConnectionHandler(con));
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
130    
131    }