001    /**
002     * Copyright (c) 2000-present 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.GetterUtil;
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.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.lang.reflect.Method;
026    
027    /**
028     * @author Igor Spasic
029     * @author Raymond Augé
030     */
031    public class JSONWebServiceActionConfig
032            implements Comparable<JSONWebServiceActionConfig>,
033                               JSONWebServiceActionMapping {
034    
035            public JSONWebServiceActionConfig(
036                    String contextName, String contextPath, Class<?> actionClass,
037                    Method actionMethod, String path, String method) {
038    
039                    this(
040                            contextName, contextPath, null, actionClass, actionMethod, path,
041                            method);
042            }
043    
044            public JSONWebServiceActionConfig(
045                    String contextName, String contextPath, Object actionObject,
046                    Class<?> actionClass, Method actionMethod, String path, String method) {
047    
048                    _contextName = GetterUtil.getString(contextName);
049                    _contextPath = GetterUtil.getString(contextPath);
050                    _actionObject = actionObject;
051                    _actionClass = actionClass;
052    
053                    Method newActionMethod = actionMethod;
054    
055                    if (actionObject != null) {
056                            try {
057                                    Class<?> actionObjectClass = actionObject.getClass();
058    
059                                    newActionMethod = actionObjectClass.getMethod(
060                                            actionMethod.getName(), actionMethod.getParameterTypes());
061                            }
062                            catch (NoSuchMethodException nsme) {
063                                    throw new IllegalArgumentException(nsme);
064                            }
065                    }
066    
067                    _actionMethod = newActionMethod;
068    
069                    if (Validator.isNotNull(_contextName)) {
070                            path = path.substring(1);
071    
072                            path = StringPool.SLASH.concat(_contextName).concat(
073                                    StringPool.PERIOD).concat(path);
074                    }
075    
076                    _path = path;
077    
078                    _method = method;
079    
080                    Deprecated deprecated = actionMethod.getAnnotation(Deprecated.class);
081    
082                    if (deprecated != null) {
083                            _deprecated = true;
084                    }
085                    else {
086                            _deprecated = false;
087                    }
088    
089                    _methodParameters =
090                            MethodParametersResolverUtil.resolveMethodParameters(actionMethod);
091    
092                    Method realActionMethod = null;
093    
094                    try {
095                            realActionMethod = _actionClass.getDeclaredMethod(
096                                    actionMethod.getName(), actionMethod.getParameterTypes());
097                    }
098                    catch (NoSuchMethodException nsme) {
099                    }
100    
101                    _realActionMethod = realActionMethod;
102    
103                    StringBundler sb = new StringBundler(_methodParameters.length * 2 + 3);
104    
105                    sb.append(_path);
106                    sb.append(StringPool.MINUS);
107                    sb.append(_methodParameters.length);
108    
109                    for (MethodParameter methodParameter : _methodParameters) {
110                            sb.append(StringPool.MINUS);
111                            sb.append(methodParameter.getName());
112                    }
113    
114                    _signature = sb.toString();
115            }
116    
117            @Override
118            public int compareTo(
119                    JSONWebServiceActionConfig jsonWebServiceActionConfig) {
120    
121                    return _signature.compareTo(jsonWebServiceActionConfig._signature);
122            }
123    
124            @Override
125            public boolean equals(Object object) {
126                    if (this == object) {
127                            return true;
128                    }
129    
130                    if (!(object instanceof JSONWebServiceActionConfig)) {
131                            return false;
132                    }
133    
134                    JSONWebServiceActionConfig jsonWebServiceActionConfig =
135                            (JSONWebServiceActionConfig)object;
136    
137                    if (Validator.equals(
138                                    _signature, jsonWebServiceActionConfig._signature)) {
139    
140                            return true;
141                    }
142    
143                    return false;
144            }
145    
146            @Override
147            public Class<?> getActionClass() {
148                    return _actionClass;
149            }
150    
151            @Override
152            public Method getActionMethod() {
153                    return _actionMethod;
154            }
155    
156            @Override
157            public Object getActionObject() {
158                    return _actionObject;
159            }
160    
161            @Override
162            public String getContextName() {
163                    return _contextName;
164            }
165    
166            @Override
167            public String getContextPath() {
168                    return _contextPath;
169            }
170    
171            @Override
172            public String getMethod() {
173                    return _method;
174            }
175    
176            @Override
177            public MethodParameter[] getMethodParameters() {
178                    return _methodParameters;
179            }
180    
181            @Override
182            public String getPath() {
183                    return _path;
184            }
185    
186            @Override
187            public Method getRealActionMethod() {
188                    return _realActionMethod;
189            }
190    
191            @Override
192            public String getSignature() {
193                    return _signature;
194            }
195    
196            @Override
197            public int hashCode() {
198                    return _signature.hashCode();
199            }
200    
201            @Override
202            public boolean isDeprecated() {
203                    return _deprecated;
204            }
205    
206            @Override
207            public String toString() {
208                    StringBundler sb = new StringBundler(21);
209    
210                    sb.append("{actionClass=");
211                    sb.append(_actionClass);
212                    sb.append(", actionMethod=");
213                    sb.append(_actionMethod);
214                    sb.append(", contextName=");
215                    sb.append(_contextName);
216                    sb.append(", contextPath=");
217                    sb.append(_contextPath);
218                    sb.append(", deprecated=");
219                    sb.append(_deprecated);
220                    sb.append(", method=");
221                    sb.append(_method);
222                    sb.append(", methodParameters=");
223                    sb.append(_methodParameters);
224                    sb.append(", path=");
225                    sb.append(_path);
226                    sb.append(", realActionMethod=");
227                    sb.append(_realActionMethod);
228                    sb.append(", signature=");
229                    sb.append(_signature);
230                    sb.append("}");
231    
232                    return sb.toString();
233            }
234    
235            private final Class<?> _actionClass;
236            private final Method _actionMethod;
237            private final Object _actionObject;
238            private final String _contextName;
239            private final String _contextPath;
240            private final boolean _deprecated;
241            private final String _method;
242            private final MethodParameter[] _methodParameters;
243            private final String _path;
244            private final Method _realActionMethod;
245            private final String _signature;
246    
247    }