KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > services > server > AbstractSkeleton


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.services.server;
50
51 import java.io.InputStream JavaDoc;
52 import java.lang.reflect.Method JavaDoc;
53 import java.util.HashMap JavaDoc;
54
55 /**
56  * Proxy class for Hessian services.
57  */

58 abstract public class AbstractSkeleton {
59   private Class JavaDoc _apiClass;
60   private Class JavaDoc _homeClass;
61   private Class JavaDoc _objectClass;
62   
63   private HashMap JavaDoc _methodMap = new HashMap JavaDoc();
64
65   /**
66    * Create a new hessian skeleton.
67    *
68    * @param apiClass the API interface
69    */

70   protected AbstractSkeleton(Class JavaDoc apiClass)
71   {
72     _apiClass = apiClass;
73     
74     Method JavaDoc []methodList = apiClass.getMethods();
75
76     for (int i = 0; i < methodList.length; i++) {
77       Method JavaDoc method = methodList[i];
78
79       if (_methodMap.get(method.getName()) == null)
80         _methodMap.put(method.getName(), methodList[i]);
81
82       Class JavaDoc []param = method.getParameterTypes();
83       String JavaDoc mangledName = method.getName() + "__" + param.length;
84       _methodMap.put(mangledName, methodList[i]);
85       
86       _methodMap.put(mangleName(method, false), methodList[i]);
87     }
88   }
89
90   /**
91    * Returns the API class of the current object.
92    */

93   public String JavaDoc getAPIClassName()
94   {
95     return _apiClass.getName();
96   }
97
98   /**
99    * Returns the API class of the factory/home.
100    */

101   public String JavaDoc getHomeClassName()
102   {
103     if (_homeClass != null)
104       return _homeClass.getName();
105     else
106       return getAPIClassName();
107   }
108
109   /**
110    * Sets the home API class.
111    */

112   public void setHomeClass(Class JavaDoc homeAPI)
113   {
114     _homeClass = homeAPI;
115   }
116
117   /**
118    * Returns the API class of the object URLs
119    */

120   public String JavaDoc getObjectClassName()
121   {
122     if (_objectClass != null)
123       return _objectClass.getName();
124     else
125       return getAPIClassName();
126   }
127
128   /**
129    * Sets the object API class.
130    */

131   public void setObjectClass(Class JavaDoc objectAPI)
132   {
133     _objectClass = objectAPI;
134   }
135
136   /**
137    * Returns the method by the mangled name.
138    *
139    * @param mangledName the name passed by the protocol
140    */

141   protected Method JavaDoc getMethod(String JavaDoc mangledName)
142   {
143     return (Method JavaDoc) _methodMap.get(mangledName);
144   }
145
146   /**
147    * Creates a unique mangled method name based on the method name and
148    * the method parameters.
149    *
150    * @param method the method to mangle
151    * @param isFull if true, mangle the full classname
152    *
153    * @return a mangled string.
154    */

155   private String JavaDoc mangleName(Method JavaDoc method, boolean isFull)
156   {
157     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
158     
159     sb.append(method.getName());
160     
161     Class JavaDoc []params = method.getParameterTypes();
162     for (int i = 0; i < params.length; i++) {
163       sb.append('_');
164       mangleClass(sb, params[i], isFull);
165     }
166
167     return sb.toString();
168   }
169
170   /**
171    * Mangles a classname.
172    */

173   private void mangleClass(StringBuffer JavaDoc sb, Class JavaDoc cl, boolean isFull)
174   {
175     String JavaDoc name = cl.getName();
176
177     if (name.equals("boolean") || name.equals("java.lang.Boolean"))
178       sb.append("boolean");
179     else if (name.equals("int") || name.equals("java.lang.Integer") ||
180              name.equals("short") || name.equals("java.lang.Short") ||
181              name.equals("byte") || name.equals("java.lang.Byte"))
182       sb.append("int");
183     else if (name.equals("long") || name.equals("java.lang.Long"))
184       sb.append("long");
185     else if (name.equals("float") || name.equals("java.lang.Float") ||
186          name.equals("double") || name.equals("java.lang.Double"))
187       sb.append("double");
188     else if (name.equals("java.lang.String") ||
189              name.equals("com.caucho.util.CharBuffer") ||
190              name.equals("char") || name.equals("java.lang.Character") ||
191              name.equals("java.io.Reader"))
192       sb.append("string");
193     else if (name.equals("java.util.Date") ||
194              name.equals("com.caucho.util.QDate"))
195       sb.append("date");
196     else if (InputStream JavaDoc.class.isAssignableFrom(cl) ||
197              name.equals("[B"))
198       sb.append("binary");
199     else if (cl.isArray()) {
200       sb.append("[");
201       mangleClass(sb, cl.getComponentType(), isFull);
202     }
203     else if (name.equals("org.w3c.dom.Node") ||
204              name.equals("org.w3c.dom.Element") ||
205              name.equals("org.w3c.dom.Document"))
206       sb.append("xml");
207     else if (isFull)
208       sb.append(name);
209     else {
210       int p = name.lastIndexOf('.');
211       if (p > 0)
212         sb.append(name.substring(p + 1));
213       else
214         sb.append(name);
215     }
216   }
217 }
218
Popular Tags