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.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    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.lang.reflect.Method;
025    
026    /**
027     * @author Igor Spasic
028     */
029    public class JSONWebServiceActionConfig
030            implements Comparable<JSONWebServiceActionConfig>,
031            JSONWebServiceActionMapping {
032    
033            public JSONWebServiceActionConfig(
034                    String contextPath, Class<?> actionClass, Method actionMethod,
035                    String path, String method) {
036    
037                    _contextPath = contextPath;
038                    _actionClass = actionClass;
039                    _actionMethod = actionMethod;
040                    _path = path;
041                    _method = method;
042    
043                    _methodParameters =
044                            MethodParametersResolverUtil.resolveMethodParameters(actionMethod);
045    
046                    _fullPath = _contextPath + _path;
047    
048                    StringBundler sb = new StringBundler(_methodParameters.length * 2 + 4);
049    
050                    sb.append(_fullPath);
051                    sb.append(CharPool.MINUS);
052                    sb.append(_methodParameters.length);
053    
054                    for (MethodParameter methodParameter : _methodParameters) {
055                            sb.append(CharPool.MINUS);
056                            sb.append(methodParameter.getName());
057                    }
058    
059                    _signature = sb.toString();
060            }
061    
062            public int compareTo(
063                    JSONWebServiceActionConfig jsonWebServiceActionConfig) {
064    
065                    return _signature.compareTo(jsonWebServiceActionConfig._signature);
066            }
067    
068            @Override
069            public boolean equals(Object object) {
070                    if (this == object) {
071                            return true;
072                    }
073    
074                    if (!(object instanceof JSONWebServiceActionConfig)) {
075                            return false;
076                    }
077    
078                    JSONWebServiceActionConfig jsonWebServiceActionConfig =
079                            (JSONWebServiceActionConfig)object;
080    
081                    if (Validator.equals(
082                                    _signature, jsonWebServiceActionConfig._signature)) {
083    
084                            return true;
085                    }
086    
087                    return false;
088            }
089    
090            public Class<?> getActionClass() {
091                    return _actionClass;
092            }
093    
094            public Method getActionMethod() {
095                    return _actionMethod;
096            }
097    
098            public String getContextPath() {
099                    return _contextPath;
100            }
101    
102            public String getFullPath() {
103                    return _fullPath;
104            }
105    
106            public String getMethod() {
107                    return _method;
108            }
109    
110            public MethodParameter[] getMethodParameters() {
111                    return _methodParameters;
112            }
113    
114            public String getPath() {
115                    return _path;
116            }
117    
118            public String getSignature() {
119                    return _signature;
120            }
121    
122            @Override
123            public int hashCode() {
124                    return _signature.hashCode();
125            }
126    
127            @Override
128            public String toString() {
129                    StringBundler sb = new StringBundler(17);
130    
131                    sb.append("{actionClass=");
132                    sb.append(_actionClass);
133                    sb.append(", actionMethod=");
134                    sb.append(_actionMethod);
135                    sb.append(", contextPath=");
136                    sb.append(_contextPath);
137                    sb.append(", fullPath=");
138                    sb.append(_fullPath);
139                    sb.append(", method=");
140                    sb.append(_method);
141                    sb.append(", methodParameters=");
142                    sb.append(_methodParameters);
143                    sb.append(", path=");
144                    sb.append(_path);
145                    sb.append(", signature=");
146                    sb.append(_signature);
147                    sb.append("}");
148    
149                    return sb.toString();
150            }
151    
152            private Class<?> _actionClass;
153            private Method _actionMethod;
154            private String _contextPath;
155            private String _fullPath;
156            private String _method;
157            private MethodParameter[] _methodParameters;
158            private String _path;
159            private String _signature;
160    
161    }