001: /*
002: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Scott Ferguson
047: */
048:
049: package com.caucho.services.server;
050:
051: import java.io.InputStream;
052: import java.lang.reflect.Method;
053: import java.util.HashMap;
054:
055: /**
056: * Proxy class for Hessian services.
057: */
058: abstract public class AbstractSkeleton {
059: private Class _apiClass;
060: private Class _homeClass;
061: private Class _objectClass;
062:
063: private HashMap _methodMap = new HashMap();
064:
065: /**
066: * Create a new hessian skeleton.
067: *
068: * @param apiClass the API interface
069: */
070: protected AbstractSkeleton(Class apiClass) {
071: _apiClass = apiClass;
072:
073: Method[] methodList = apiClass.getMethods();
074:
075: for (int i = 0; i < methodList.length; i++) {
076: Method method = methodList[i];
077:
078: if (_methodMap.get(method.getName()) == null)
079: _methodMap.put(method.getName(), methodList[i]);
080:
081: Class[] param = method.getParameterTypes();
082: String mangledName = method.getName() + "__" + param.length;
083: _methodMap.put(mangledName, methodList[i]);
084:
085: _methodMap.put(mangleName(method, false), methodList[i]);
086: }
087: }
088:
089: /**
090: * Returns the API class of the current object.
091: */
092: public String getAPIClassName() {
093: return _apiClass.getName();
094: }
095:
096: /**
097: * Returns the API class of the factory/home.
098: */
099: public String getHomeClassName() {
100: if (_homeClass != null)
101: return _homeClass.getName();
102: else
103: return getAPIClassName();
104: }
105:
106: /**
107: * Sets the home API class.
108: */
109: public void setHomeClass(Class homeAPI) {
110: _homeClass = homeAPI;
111: }
112:
113: /**
114: * Returns the API class of the object URLs
115: */
116: public String getObjectClassName() {
117: if (_objectClass != null)
118: return _objectClass.getName();
119: else
120: return getAPIClassName();
121: }
122:
123: /**
124: * Sets the object API class.
125: */
126: public void setObjectClass(Class objectAPI) {
127: _objectClass = objectAPI;
128: }
129:
130: /**
131: * Returns the method by the mangled name.
132: *
133: * @param mangledName the name passed by the protocol
134: */
135: protected Method getMethod(String mangledName) {
136: return (Method) _methodMap.get(mangledName);
137: }
138:
139: /**
140: * Creates a unique mangled method name based on the method name and
141: * the method parameters.
142: *
143: * @param method the method to mangle
144: * @param isFull if true, mangle the full classname
145: *
146: * @return a mangled string.
147: */
148: public static String mangleName(Method method, boolean isFull) {
149: StringBuffer sb = new StringBuffer();
150:
151: sb.append(method.getName());
152:
153: Class[] params = method.getParameterTypes();
154: for (int i = 0; i < params.length; i++) {
155: sb.append('_');
156: sb.append(mangleClass(params[i], isFull));
157: }
158:
159: return sb.toString();
160: }
161:
162: /**
163: * Mangles a classname.
164: */
165: public static String mangleClass(Class cl, boolean isFull) {
166: String name = cl.getName();
167:
168: if (name.equals("boolean") || name.equals("java.lang.Boolean"))
169: return "boolean";
170: else if (name.equals("int") || name.equals("java.lang.Integer")
171: || name.equals("short")
172: || name.equals("java.lang.Short")
173: || name.equals("byte") || name.equals("java.lang.Byte"))
174: return "int";
175: else if (name.equals("long") || name.equals("java.lang.Long"))
176: return "long";
177: else if (name.equals("float") || name.equals("java.lang.Float")
178: || name.equals("double")
179: || name.equals("java.lang.Double"))
180: return "double";
181: else if (name.equals("java.lang.String")
182: || name.equals("com.caucho.util.CharBuffer")
183: || name.equals("char")
184: || name.equals("java.lang.Character")
185: || name.equals("java.io.Reader"))
186: return "string";
187: else if (name.equals("java.util.Date")
188: || name.equals("com.caucho.util.QDate"))
189: return "date";
190: else if (InputStream.class.isAssignableFrom(cl)
191: || name.equals("[B"))
192: return "binary";
193: else if (cl.isArray()) {
194: return "[" + mangleClass(cl.getComponentType(), isFull);
195: } else if (name.equals("org.w3c.dom.Node")
196: || name.equals("org.w3c.dom.Element")
197: || name.equals("org.w3c.dom.Document"))
198: return "xml";
199: else if (isFull)
200: return name;
201: else {
202: int p = name.lastIndexOf('.');
203: if (p > 0)
204: return name.substring(p + 1);
205: else
206: return name;
207: }
208: }
209:
210: public String toString() {
211: return getClass().getSimpleName() + "[" + _apiClass.getName()
212: + "]";
213: }
214: }
|