001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }