001    /**
002     * Copyright (c) 2000-2012 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.dao.jdbc.pacl;
016    
017    import com.liferay.portal.dao.jdbc.util.DataSourceWrapper;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ProxyUtil;
021    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
022    import com.liferay.portal.security.pacl.PACLClassUtil;
023    import com.liferay.portal.security.pacl.PACLPolicy;
024    import com.liferay.portal.security.pacl.PACLPolicyManager;
025    
026    import java.sql.Connection;
027    import java.sql.SQLException;
028    
029    import javax.sql.DataSource;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PACLDataSource extends DataSourceWrapper {
035    
036            public PACLDataSource(DataSource dataSource) {
037                    super(dataSource);
038    
039                    _dataSource = dataSource;
040    
041                    _log.debug("Loading " + PACLConnectionHandler.class.getName());
042            }
043    
044            @Override
045            public Connection getConnection() throws SQLException {
046                    Connection connection = _dataSource.getConnection();
047    
048                    if (!PACLPolicyManager.isActive() ||
049                            !PortalSecurityManagerThreadLocal.isCheckSQL() ||
050                            !PortalSecurityManagerThreadLocal.isEnabled()) {
051    
052                            return connection;
053                    }
054    
055                    PACLPolicy paclPolicy = PACLClassUtil.getPACLPolicy(
056                            false, _log.isDebugEnabled());
057    
058                    if ((paclPolicy == null) || !paclPolicy.isActive()) {
059                            return connection;
060                    }
061    
062                    return (Connection)ProxyUtil.newProxyInstance(
063                            paclPolicy.getClassLoader(), new Class<?>[] {Connection.class},
064                            new PACLConnectionHandler(connection, paclPolicy));
065            }
066    
067            private static Log _log = LogFactoryUtil.getLog(
068                    PACLDataSource.class.getName());
069    
070            private DataSource _dataSource;
071    
072    }