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 wrapWhenActive(T t) {
041                    return _pacl.wrapWhenActive(t);
042            }
043    
044            public static interface PACL {
045    
046                    public <T> T wrap(PrivilegedAction<T> privilegedAction);
047    
048                    public <T> T wrap(
049                                    PrivilegedExceptionAction<T> privilegedExceptionAction)
050                            throws Exception;
051    
052                    public <T> T wrap(T t);
053    
054                    public <T> T wrapWhenActive(T t);
055    
056            }
057    
058            private static PACL _pacl = new NoPACL();
059    
060            private static class NoPACL implements PACL {
061    
062                    @Override
063                    public <T> T wrap(PrivilegedAction<T> privilegedAction) {
064                            return privilegedAction.run();
065                    }
066    
067                    @Override
068                    public <T> T wrap(
069                                    PrivilegedExceptionAction<T> privilegedExceptionAction)
070                            throws Exception {
071    
072                            return privilegedExceptionAction.run();
073                    }
074    
075                    @Override
076                    public <T> T wrap(T t) {
077                            return t;
078                    }
079    
080                    @Override
081                    public <T> T wrapWhenActive(T t) {
082                            return t;
083                    }
084    
085            }
086    
087    }