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.kernel.security.pacl.permission;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.lang.reflect.Method;
023    
024    import java.security.BasicPermission;
025    
026    /**
027     * @author Raymond Aug??
028     */
029    public class PortalServicePermission extends BasicPermission {
030    
031            public static void checkService(
032                    Object object, Method method, Object[] arguments) {
033    
034                    _pacl.checkService(object, method, arguments);
035            }
036    
037            public PortalServicePermission(String name, String methodName) {
038                    super(name);
039    
040                    _methodName = methodName;
041    
042                    _init();
043            }
044    
045            public PortalServicePermission(
046                    String name, String servletContextName, String className,
047                    String methodName) {
048    
049                    super(_createLongName(name, servletContextName, className));
050    
051                    _methodName = methodName;
052    
053                    _init();
054            }
055    
056            @Override
057            public String getActions() {
058                    return _methodName;
059            }
060    
061            public String getClassName() {
062                    return _className;
063            }
064    
065            public String getMethodName() {
066                    return _methodName;
067            }
068    
069            public String getServletContextName() {
070                    return _servletContextName;
071            }
072    
073            public String getShortName() {
074                    return _shortName;
075            }
076    
077            private static String _createLongName(
078                    String name, String servletContextName, String className) {
079    
080                    StringBundler sb = new StringBundler(5);
081    
082                    sb.append(name);
083                    sb.append(StringPool.POUND);
084    
085                    if (Validator.isNull(servletContextName)) {
086                            sb.append("portal");
087                    }
088                    else {
089                            sb.append(servletContextName);
090                    }
091    
092                    sb.append(StringPool.POUND);
093                    sb.append(className);
094    
095                    return sb.toString();
096            }
097    
098            private void _init() {
099                    String[] nameParts = StringUtil.split(getName(), StringPool.POUND);
100    
101                    if (nameParts.length != 3) {
102                            throw new IllegalArgumentException(
103                                    "Name " + getName() + " does not follow the format " +
104                                            "[name]#[servletContextName]#[subject]");
105                    }
106    
107                    _shortName = nameParts[0];
108                    _servletContextName = nameParts[1];
109                    _className = nameParts[2];
110            }
111    
112            private static PACL _pacl = new NoPACL();
113    
114            private String _className;
115            private String _methodName;
116            private String _servletContextName;
117            private String _shortName;
118    
119            private static class NoPACL implements PACL {
120    
121                    @Override
122                    public void checkService(
123                            Object object, Method method, Object[] arguments) {
124                    }
125    
126            }
127    
128            public static interface PACL {
129    
130                    public void checkService(
131                            Object object, Method method, Object[] arguments);
132    
133            }
134    
135    }