001    /**
002     * Copyright (c) 2000-2012 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.jsonwebservice;
016    
017    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionMapping;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.MethodParameter;
020    import com.liferay.portal.kernel.util.MethodParametersResolverUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    
023    import java.lang.reflect.Method;
024    
025    /**
026     * @author Igor Spasic
027     */
028    public class JSONWebServiceActionConfig
029            implements Comparable<JSONWebServiceActionConfig>,
030            JSONWebServiceActionMapping {
031    
032            public JSONWebServiceActionConfig(
033                    String servletContextPath, Class<?> actionClass, Method actionMethod,
034                    String path, String method) {
035    
036                    _servletContextPath = servletContextPath;
037                    _actionClass = actionClass;
038                    _actionMethod = actionMethod;
039                    _path = path;
040                    _method = method;
041    
042                    _methodParameters =
043                            MethodParametersResolverUtil.resolveMethodParameters(actionMethod);
044    
045                    _fullPath = _servletContextPath + _path;
046    
047                    StringBundler sb = new StringBundler(_methodParameters.length * 2 + 4);
048    
049                    sb.append(_fullPath);
050                    sb.append(CharPool.MINUS);
051                    sb.append(_methodParameters.length);
052    
053                    for (MethodParameter methodParameter : _methodParameters) {
054                            sb.append(CharPool.MINUS);
055                            sb.append(methodParameter.getName());
056                    }
057    
058                    _signature = sb.toString();
059            }
060    
061            public int compareTo(
062                    JSONWebServiceActionConfig jsonWebServiceActionConfig) {
063    
064                    return _signature.compareTo(jsonWebServiceActionConfig._signature);
065            }
066    
067            public Class<?> getActionClass() {
068                    return _actionClass;
069            }
070    
071            public Method getActionMethod() {
072                    return _actionMethod;
073            }
074    
075            public String getFullPath() {
076                    return _fullPath;
077            }
078    
079            public String getMethod() {
080                    return _method;
081            }
082    
083            public MethodParameter[] getMethodParameters() {
084                    return _methodParameters;
085            }
086    
087            public String getPath() {
088                    return _path;
089            }
090    
091            public String getServletContextPath() {
092                    return _servletContextPath;
093            }
094    
095            public String getSignature() {
096                    return _signature;
097            }
098    
099            @Override
100            public String toString() {
101                    StringBundler sb = new StringBundler(17);
102    
103                    sb.append("{actionClass=");
104                    sb.append(_actionClass);
105                    sb.append(", actionMethod=");
106                    sb.append(_actionMethod);
107                    sb.append(", fullPath=");
108                    sb.append(_fullPath);
109                    sb.append(", method=");
110                    sb.append(_method);
111                    sb.append(", methodParameters=");
112                    sb.append(_methodParameters);
113                    sb.append(", path=");
114                    sb.append(_path);
115                    sb.append(", servletContextPath=");
116                    sb.append(_servletContextPath);
117                    sb.append(", signature=");
118                    sb.append(_signature);
119                    sb.append("}");
120    
121                    return sb.toString();
122            }
123    
124            private Class<?> _actionClass;
125            private Method _actionMethod;
126            private String _fullPath;
127            private String _method;
128            private MethodParameter[] _methodParameters;
129            private String _path;
130            private String _servletContextPath;
131            private String _signature;
132    
133    }