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 = _pacl.getDataSource();
098    
099                    return dataSource.getConnection();
100            }
101    
102            public static Connection getConnection(String location)
103                    throws NamingException, SQLException {
104    
105                    DataSource dataSource = _pacl.getDataSource(location);
106    
107                    return dataSource.getConnection();
108            }
109    
110            public static Connection getUpgradeOptimizedConnection()
111                    throws SQLException {
112    
113                    Connection con = getConnection();
114    
115                    Thread currentThread = Thread.currentThread();
116    
117                    ClassLoader classLoader = currentThread.getContextClassLoader();
118    
119                    return (Connection)ProxyUtil.newProxyInstance(
120                            classLoader, new Class[] {Connection.class},
121                            new UpgradeOptimizedConnectionHandler(con));
122            }
123    
124            public static interface PACL {
125    
126                    public DataSource getDataSource();
127    
128                    public DataSource getDataSource(String location) throws NamingException;
129    
130            }
131    
132            private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
133    
134            private static PACL _pacl = new NoPACL();
135    
136            private static class NoPACL implements PACL {
137    
138                    @Override
139                    public DataSource getDataSource() {
140                            return InfrastructureUtil.getDataSource();
141                    }
142    
143                    @Override
144                    public DataSource getDataSource(String location)
145                            throws NamingException {
146    
147                            Properties properties = PropsUtil.getProperties(
148                                    PropsKeys.JNDI_ENVIRONMENT, true);
149    
150                            Context context = new InitialContext(properties);
151    
152                            return (DataSource)JNDIUtil.lookup(context, location);
153                    }
154    
155            }
156    
157    }