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.security.pacl.dao.jdbc;
016    
017    import com.liferay.portal.dao.jdbc.util.DataSourceWrapper;
018    import com.liferay.portal.kernel.util.ProxyUtil;
019    import com.liferay.portal.security.lang.DoPrivilegedFactory;
020    import com.liferay.portal.security.pacl.PACLPolicy;
021    import com.liferay.portal.security.pacl.PACLUtil;
022    
023    import java.security.AccessController;
024    import java.security.PrivilegedAction;
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    
042            @Override
043            public Connection getConnection() throws SQLException {
044                    Connection connection = _dataSource.getConnection();
045    
046                    PACLPolicy paclPolicy = PACLUtil.getPACLPolicy();
047    
048                    if (paclPolicy == null) {
049                            return connection;
050                    }
051    
052                    connection = DoPrivilegedFactory.wrap(connection);
053                    paclPolicy = DoPrivilegedFactory.wrap(paclPolicy);
054    
055                    return AccessController.doPrivileged(
056                            new ConnectionPrivilegedAction(connection, paclPolicy));
057            }
058    
059            private DataSource _dataSource;
060    
061            private class ConnectionPrivilegedAction
062                    implements PrivilegedAction<Connection> {
063    
064                    public ConnectionPrivilegedAction(
065                            Connection connection, PACLPolicy paclPolicy) {
066    
067                            _connection = connection;
068                            _paclPolicy = paclPolicy;
069                    }
070    
071                    public Connection run() {
072                            return (Connection)ProxyUtil.newProxyInstance(
073                                    _paclPolicy.getClassLoader(), new Class<?>[] {Connection.class},
074                                    new PACLConnectionHandler(_connection, _paclPolicy));
075                    }
076    
077                    private Connection _connection;
078                    private PACLPolicy _paclPolicy;
079    
080            }
081    
082    }