001 /* 002 * Copyright © 2008, 2009 Pedro Agulló Soliveres. 003 * 004 * This file is part of DirectJNgine. 005 * 006 * DirectJNgine is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License. 009 * 010 * Commercial use is permitted to the extent that the code/component(s) 011 * do NOT become part of another Open Source or Commercially developed 012 * licensed development library or toolkit without explicit permission. 013 * 014 * DirectJNgine is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with DirectJNgine. If not, see <http://www.gnu.org/licenses/>. 021 * 022 * This software uses the ExtJs library (http://extjs.com), which is 023 * distributed under the GPL v3 license (see http://extjs.com/license). 024 */ 025 026 package com.softwarementors.extjs.djn.api; 027 028 import java.lang.reflect.Method; 029 import java.util.Map; 030 031 import org.apache.log4j.Logger; 032 033 import com.google.gson.JsonArray; 034 import com.softwarementors.extjs.djn.StringUtils; 035 import com.softwarementors.extjs.djn.config.ApiConfigurationException; 036 037 public class RegisteredMethod { 038 039 private static Logger logger = Logger.getLogger( RegisteredMethod.class); 040 041 private RegisteredAction action; 042 private String name; 043 private Method method; 044 private boolean formHandler; 045 private boolean handleDataAsJsonArray; 046 047 /* package */ RegisteredMethod( RegisteredAction action, String name, Method method, boolean formHandler) { 048 assert action != null; 049 assert !StringUtils.isEmpty(name); 050 assert method != null; 051 052 this.action = action; 053 this.name = name; 054 this.method = method; 055 this.formHandler = formHandler; 056 this.handleDataAsJsonArray = !formHandler && method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals( JsonArray.class); 057 058 if( this.formHandler ) { 059 if( method.getParameterTypes().length != 2 || !method.getParameterTypes()[0].equals( Map.class) || !method.getParameterTypes()[1].equals( Map.class) ) { 060 ApiConfigurationException ex = ApiConfigurationException.forMethodHasWrongParametersForAFormHandler( this ); 061 logger.fatal( ex.getMessage(), ex ); 062 throw ex; 063 } 064 } 065 } 066 067 public RegisteredAction getAction() { 068 return this.action; 069 } 070 071 public String getName() { 072 return this.name; 073 } 074 075 public Method getMethod() { 076 return this.method; 077 } 078 079 public Class<?>[] getParameterTypes() { 080 return this.method.getParameterTypes(); 081 } 082 083 public int getParameterCount() { 084 return getParameterTypes().length; 085 } 086 087 public Class<?> getReturnType() { 088 return this.method.getReturnType(); 089 } 090 091 public boolean getFormHandler() { 092 return this.formHandler; 093 } 094 095 public String getActionName() { 096 return getAction().getName(); 097 } 098 099 public String getFullName() { 100 return getActionName() + "." + getName(); 101 } 102 103 public Class<?> getActionClass() { 104 return getAction().getActionClass(); 105 } 106 107 public int getClientParameterCount() { 108 // If this is a form handler, then the client must pass just one parameter 109 if( getFormHandler() ) { 110 return 1; 111 } 112 return getParameterCount(); 113 } 114 115 public boolean getHandleDataAsJsonArray() { 116 return this.handleDataAsJsonArray; 117 } 118 }