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