001    /**
002     * Copyright (c) 2000-present 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 void deepCleanUp(ResultSet resultSet) {
097                    try {
098                            if (resultSet != null) {
099                                    Statement statement = resultSet.getStatement();
100    
101                                    Connection con = statement.getConnection();
102    
103                                    cleanUp(con, statement, resultSet);
104                            }
105                    }
106                    catch (SQLException sqle) {
107                            if (_log.isWarnEnabled()) {
108                                    _log.warn(sqle.getMessage());
109                            }
110                    }
111            }
112    
113            public static Connection getConnection() throws SQLException {
114                    DataSource dataSource = _pacl.getDataSource();
115    
116                    return dataSource.getConnection();
117            }
118    
119            public static Connection getConnection(String location)
120                    throws NamingException, SQLException {
121    
122                    DataSource dataSource = _pacl.getDataSource(location);
123    
124                    return dataSource.getConnection();
125            }
126    
127            public static Connection getUpgradeOptimizedConnection()
128                    throws SQLException {
129    
130                    Connection con = getConnection();
131    
132                    Thread currentThread = Thread.currentThread();
133    
134                    ClassLoader classLoader = currentThread.getContextClassLoader();
135    
136                    return (Connection)ProxyUtil.newProxyInstance(
137                            classLoader, new Class[] {Connection.class},
138                            new UpgradeOptimizedConnectionHandler(con));
139            }
140    
141            public interface PACL {
142    
143                    public DataSource getDataSource();
144    
145                    public DataSource getDataSource(String location) throws NamingException;
146    
147            }
148    
149            private static final Log _log = LogFactoryUtil.getLog(DataAccess.class);
150    
151            private static final PACL _pacl = new NoPACL();
152    
153            private static class NoPACL implements PACL {
154    
155                    @Override
156                    public DataSource getDataSource() {
157                            return InfrastructureUtil.getDataSource();
158                    }
159    
160                    @Override
161                    public DataSource getDataSource(String location)
162                            throws NamingException {
163    
164                            Properties properties = PropsUtil.getProperties(
165                                    PropsKeys.JNDI_ENVIRONMENT, true);
166    
167                            Context context = new InitialContext(properties);
168    
169                            return (DataSource)JNDIUtil.lookup(context, location);
170                    }
171    
172            }
173    
174    }