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 JSONWebServiceActionConfig(
063                    String contextPath, Object actionObject, Class<?> actionClass,
064                    Method actionMethod, String path, String method) {
065    
066                    this(contextPath, actionClass, actionMethod, path, method);
067    
068                    _actionObject = actionObject;
069    
070                    try {
071                            Class<?> actionObjectClass = actionObject.getClass();
072    
073                            Method actionObjectClassActionMethod = actionObjectClass.getMethod(
074                                    actionMethod.getName(), actionMethod.getParameterTypes());
075    
076                            _actionMethod = actionObjectClassActionMethod;
077                    }
078                    catch (NoSuchMethodException nsme) {
079                            throw new IllegalArgumentException(nsme);
080                    }
081            }
082    
083            @Override
084            public int compareTo(
085                    JSONWebServiceActionConfig jsonWebServiceActionConfig) {
086    
087                    return _signature.compareTo(jsonWebServiceActionConfig._signature);
088            }
089    
090            @Override
091            public boolean equals(Object object) {
092                    if (this == object) {
093                            return true;
094                    }
095    
096                    if (!(object instanceof JSONWebServiceActionConfig)) {
097                            return false;
098                    }
099    
100                    JSONWebServiceActionConfig jsonWebServiceActionConfig =
101                            (JSONWebServiceActionConfig)object;
102    
103                    if (Validator.equals(
104                                    _signature, jsonWebServiceActionConfig._signature)) {
105    
106                            return true;
107                    }
108    
109                    return false;
110            }
111    
112            @Override
113            public Class<?> getActionClass() {
114                    return _actionClass;
115            }
116    
117            @Override
118            public Method getActionMethod() {
119                    return _actionMethod;
120            }
121    
122            @Override
123            public Object getActionObject() {
124                    return _actionObject;
125            }
126    
127            @Override
128            public String getContextPath() {
129                    return _contextPath;
130            }
131    
132            public String getFullPath() {
133                    return _fullPath;
134            }
135    
136            @Override
137            public String getMethod() {
138                    return _method;
139            }
140    
141            @Override
142            public MethodParameter[] getMethodParameters() {
143                    return _methodParameters;
144            }
145    
146            @Override
147            public String getPath() {
148                    return _path;
149            }
150    
151            @Override
152            public String getSignature() {
153                    return _signature;
154            }
155    
156            @Override
157            public int hashCode() {
158                    return _signature.hashCode();
159            }
160    
161            @Override
162            public String toString() {
163                    StringBundler sb = new StringBundler(17);
164    
165                    sb.append("{actionClass=");
166                    sb.append(_actionClass);
167                    sb.append(", actionMethod=");
168                    sb.append(_actionMethod);
169                    sb.append(", contextPath=");
170                    sb.append(_contextPath);
171                    sb.append(", fullPath=");
172                    sb.append(_fullPath);
173                    sb.append(", method=");
174                    sb.append(_method);
175                    sb.append(", methodParameters=");
176                    sb.append(_methodParameters);
177                    sb.append(", path=");
178                    sb.append(_path);
179                    sb.append(", signature=");
180                    sb.append(_signature);
181                    sb.append("}");
182    
183                    return sb.toString();
184            }
185    
186            private Class<?> _actionClass;
187            private Method _actionMethod;
188            private Object _actionObject;
189            private String _contextPath;
190            private String _fullPath;
191            private String _method;
192            private MethodParameter[] _methodParameters;
193            private String _path;
194            private String _signature;
195    
196    }