001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source 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, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.cfg;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.types.Signature;
034: import com.caucho.util.CharBuffer;
035: import com.caucho.util.IntMap;
036: import com.caucho.util.L10N;
037:
038: import java.lang.reflect.Method;
039: import java.util.ArrayList;
040:
041: public class MethodSignature {
042: private static L10N L = new L10N(MethodSignature.class);
043:
044: private static IntMap _methodElements;
045:
046: private String _ejbName;
047: private EjbBean _bean;
048:
049: private String _methodName;
050: private String _methodIntf;
051:
052: private Object _value;
053:
054: private ArrayList<String> _paramTypes;
055:
056: public MethodSignature() {
057: }
058:
059: public void setEJBName(String ejbName) {
060: _ejbName = ejbName;
061: }
062:
063: public String getEJBName() {
064: if (_bean != null)
065: return _bean.getEJBName();
066: else
067: return _ejbName;
068: }
069:
070: public void setMethodName(String name) throws ConfigException {
071: setName(name);
072: }
073:
074: public String getName() {
075: return _methodName;
076: }
077:
078: public void setName(String methodName) throws ConfigException {
079: if (methodName.indexOf('(') < 0) {
080: _methodName = methodName;
081: return;
082: }
083:
084: Signature sig = new Signature();
085: sig.addText(methodName);
086: sig.init();
087:
088: _methodName = sig.getName();
089:
090: String[] params = sig.getParameterTypes();
091: if (params != null) {
092: _paramTypes = new ArrayList<String>();
093:
094: for (int i = 0; i < params.length; i++)
095: _paramTypes.add(params[i]);
096: }
097: }
098:
099: public void addText(String text) throws ConfigException {
100: setName(text);
101: }
102:
103: /**
104: * Adds a method parameter.
105: */
106: public void addParam(String typeName) {
107: if (_paramTypes == null)
108: _paramTypes = new ArrayList<String>();
109:
110: _paramTypes.add(typeName);
111: }
112:
113: /**
114: * Adds a method parameter.
115: */
116: public MethodParams createMethodParams() {
117: return new MethodParams();
118: }
119:
120: /**
121: * Sets the parameters to zero to distinguish between
122: * methods with zero arguments and methods which don't
123: * specify the requirements.
124: */
125: public void setHasParams() {
126: if (_paramTypes == null)
127: _paramTypes = new ArrayList<String>();
128: }
129:
130: /**
131: * Sets the method interface.
132: */
133: public void setMethodIntf(String intf) {
134: _methodIntf = intf;
135: }
136:
137: boolean isHome() {
138: return _methodIntf == null || _methodIntf.equals("Home");
139: }
140:
141: boolean isRemote() {
142: return _methodIntf == null || _methodIntf.equals("Remote");
143: }
144:
145: boolean isLocalHome() {
146: return _methodIntf == null || _methodIntf.equals("LocalHome");
147: }
148:
149: boolean isLocal() {
150: return _methodIntf == null || _methodIntf.equals("Local");
151: }
152:
153: int getCost() {
154: int cost = _methodIntf == null ? 0 : 1;
155:
156: if (_methodName.equals("*"))
157: return cost;
158: else if (_paramTypes == null)
159: return 2 + cost;
160: else
161: return 4 + cost;
162: }
163:
164: public boolean isMatch(Method method, String intf) {
165: if (method == null)
166: return _methodName.equals("*");
167: else
168: return isMatch(method.getName(),
169: method.getParameterTypes(), intf);
170: }
171:
172: public boolean isMatch(ApiMethod method, String intf) {
173: if (method == null)
174: return _methodName.equals("*");
175: else
176: return isMatch(method.getName(),
177: method.getParameterTypes(), intf);
178: }
179:
180: public boolean isMatch(String methodName, Class[] params,
181: String intf) {
182: if (_methodIntf != null && !_methodIntf.equals(intf))
183: return false;
184: else
185: return isMatch(methodName, params);
186: }
187:
188: public boolean isMatch(String methodName, Class[] params) {
189: if (_methodName == null)
190: return false;
191: else if (_methodName.equals("*"))
192: return true;
193: else if (!_methodName.equals(methodName))
194: return false;
195: else if (_paramTypes == null)
196: return true;
197:
198: if (params.length != _paramTypes.size())
199: return false;
200:
201: for (int i = 0; i < params.length; i++) {
202: String name = params[i].getName();
203: String param = (String) _paramTypes.get(i);
204:
205: if (!name.equals(param) && !name.endsWith("." + param))
206: return false;
207: }
208:
209: return true;
210: }
211:
212: void setValue(Object value) {
213: _value = value;
214: }
215:
216: Object getValue() {
217: return _value;
218: }
219:
220: public int hashCode() {
221: return _methodName.hashCode();
222: }
223:
224: public boolean equals(Object o) {
225: if (!(o instanceof MethodSignature))
226: return false;
227:
228: MethodSignature cfg = (MethodSignature) o;
229:
230: if (!_methodName.equals(cfg._methodName))
231: return false;
232:
233: if (_paramTypes == null || cfg._paramTypes == null)
234: return _paramTypes == cfg._paramTypes;
235:
236: if (_paramTypes.size() != cfg._paramTypes.size())
237: return false;
238:
239: for (int i = 0; i < _paramTypes.size(); i++)
240: if (!_paramTypes.get(i).equals(cfg._paramTypes.get(i)))
241: return false;
242:
243: if (_methodIntf == cfg._methodIntf)
244: return true;
245:
246: else if (_methodIntf == null || cfg._methodIntf == null)
247: return false;
248:
249: else
250: return _methodIntf.equals(cfg._methodIntf);
251: }
252:
253: public String toSignatureString() {
254: CharBuffer cb = new CharBuffer();
255:
256: cb.append(_methodName);
257: cb.append("(");
258: for (int i = 0; _paramTypes != null && i < _paramTypes.size(); i++) {
259: if (i != 0)
260: cb.append(", ");
261: cb.append(_paramTypes.get(i));
262: }
263: cb.append(")");
264:
265: return cb.toString();
266: }
267:
268: public String toString() {
269: return ("MethodSignature[" + toSignatureString() + "]");
270: }
271:
272: public class MethodParams {
273: public void addMethodParam(String value) {
274: addParam(value);
275: }
276: }
277: }
|