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