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