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.lang;
016    
017    import java.security.PrivilegedAction;
018    import java.security.PrivilegedExceptionAction;
019    
020    /**
021     * @author Raymond Augé
022     */
023    public class DoPrivilegedUtil {
024    
025            public static <T> T wrap(PrivilegedAction<T> privilegedAction) {
026                    return _pacl.wrap(privilegedAction);
027            }
028    
029            public static <T> T wrap(
030                            PrivilegedExceptionAction<T> privilegedExceptionAction)
031                    throws Exception {
032    
033                    return _pacl.wrap(privilegedExceptionAction);
034            }
035    
036            public static <T> T wrap(T t) {
037                    return _pacl.wrap(t);
038            }
039    
040            public static <T> T wrap(T t, boolean checkActive) {
041                    return _pacl.wrap(t, checkActive);
042            }
043    
044            private static PACL _pacl = new NoPACL();
045    
046            private static class NoPACL implements PACL {
047    
048                    public <T> T wrap(PrivilegedAction<T> privilegedAction) {
049                            return privilegedAction.run();
050                    }
051    
052                    public <T> T wrap(
053                                    PrivilegedExceptionAction<T> privilegedExceptionAction)
054                            throws Exception {
055    
056                            return privilegedExceptionAction.run();
057                    }
058    
059                    public <T> T wrap(T t) {
060                            return t;
061                    }
062    
063                    public <T> T wrap(T t, boolean checkActive) {
064                            return t;
065                    }
066    
067            }
068    
069            public static interface PACL {
070    
071                    public <T> T wrap(PrivilegedAction<T> privilegedAction);
072    
073                    public <T> T wrap(
074                                    PrivilegedExceptionAction<T> privilegedExceptionAction)
075                            throws Exception;
076    
077                    public <T> T wrap(T t);
078    
079                    public <T> T wrap(T t, boolean checkActive);
080    
081            }
082    
083    }