KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > model > jsr181 > Jsr181MethodMetadataImpl


1 package org.apache.beehive.wsm.model.jsr181;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$
19  */

20
21 import java.lang.annotation.Annotation JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jws.Oneway;
28 import javax.jws.WebMethod;
29 import javax.jws.WebParam;
30 import javax.jws.WebResult;
31
32 import javax.xml.namespace.QName JavaDoc; // todo (mmerz@apache.org, 01/25/05): this needs to go...
33

34 import org.apache.beehive.wsm.model.BeehiveWsMethodMetadata;
35 import org.apache.beehive.wsm.model.BeehiveWsParameterMetadata;
36 import org.apache.beehive.wsm.model.ValidationException;
37 import org.apache.beehive.wsm.model.java.JavaMethodInfo;
38 import org.apache.beehive.wsm.model.java.JavaParameterInfo;
39
40 public class Jsr181MethodMetadataImpl implements BeehiveWsMethodMetadata, java.io.Serializable JavaDoc {
41
42     private static final long serialVersionUID = 1L;
43     private static final String JavaDoc DEFAULT_WRNAME = "return";
44     
45     private String JavaDoc wmOperationName;
46     private String JavaDoc wmAction;
47     private boolean oneway = false;
48     private List JavaDoc<BeehiveWsParameterMetadata> params =
49             new ArrayList JavaDoc<BeehiveWsParameterMetadata>();
50     private String JavaDoc wrName;
51     private String JavaDoc wrTargetNamespace;
52     private String JavaDoc javaMethodName;
53     private Class JavaDoc javaReturnType;
54
55     private QName JavaDoc mXMLReturnType;
56     
57     public void validate() throws ValidationException {
58         return; // no validation required
59
}
60     
61     public Jsr181MethodMetadataImpl(
62         String JavaDoc operationName,
63         Class JavaDoc javaType,
64         QName JavaDoc xmlReturnType)
65     {
66         this(operationName, javaType);
67         setXmlReturnType(xmlReturnType);
68     }
69
70     public Jsr181MethodMetadataImpl(String JavaDoc methodName, Class JavaDoc returnType) {
71         super();
72         javaMethodName = methodName;
73         javaReturnType = returnType;
74     }
75
76     public Jsr181MethodMetadataImpl(JavaMethodInfo jm) {
77
78         super();
79
80         // check input param
81
if (null == jm) {
82             jm.logError("illegal java method info: <null>");
83             return;
84         }
85         
86         javaMethodName = jm.getMethodName();
87         javaReturnType = jm.getReturnType();
88
89         // get webMethod's webParams
90
List JavaDoc<BeehiveWsParameterMetadata> webParams =
91             new ArrayList JavaDoc<BeehiveWsParameterMetadata>();
92         for (JavaParameterInfo param : jm.getParameters()) {
93             BeehiveWsParameterMetadata wspm = new Jsr181ParameterMetadataImpl(param);
94             if (null == wspm) {
95                 jm.logError("cannot create metadata for web param: " + param.getName());
96             }
97             webParams.add(wspm);
98         }
99
100         // set defaults
101
setWrName(DEFAULT_WRNAME);
102         
103         // enforce JSR-181 rules
104
if (! jm.isPublic()) {
105             jm.logError("@WebMethod must be public: " + javaMethodName);
106         }
107         if (null != jm.getAnnotation(Oneway.class)) {
108             // prohibit @Oneway with @WebResult
109
if (null != jm.getAnnotation(WebResult.class)) {
110                 jm.logError("@Oneway method " + javaMethodName + " has incompatible annotation: @WebResult");
111             }
112             // prohibit @Oneway with return types other than <code>void</code>
113
if (void.class != javaReturnType) {
114                 jm.logError("@Oneway method " + javaMethodName + " has illegal non-void return type: " + javaReturnType);
115             }
116             // prohibit @Oneway methods that throw Exceptions
117
if (jm.throwsExceptions()) {
118                 jm.logError("@Oneway method " + javaMethodName + " must not throw checked exceptions");
119             }
120             // todo??? prohibit @Oneway with "OUT" and "INOUT" parameters
121

122             if (null != webParams) {
123                 for (BeehiveWsParameterMetadata param : webParams) {
124                     if ((WebParam.Mode.OUT == param.getWpMode()) || (WebParam.Mode.INOUT == param.getWpMode())) {
125                         jm.logError("@Oneway method " + javaMethodName + " has illegal INOUT or OUT parameter: " + param.getWpName());
126                     }
127                 }
128             }
129         }
130         
131         // set optional annotations
132
Collection JavaDoc<Annotation JavaDoc> annotations = jm.getAnnotations();
133         if (null != annotations) {
134             for (Annotation JavaDoc a : annotations) {
135                 if (a.annotationType() == javax.jws.WebMethod.class) {
136                     initFromAnnotation((javax.jws.WebMethod) a);
137                 }
138                 else if (a.annotationType() == javax.jws.Oneway.class) {
139                     initFromAnnotation((javax.jws.Oneway) a);
140                 }
141                 else if (a.annotationType() == javax.jws.WebResult.class) {
142                     initFromAnnotation((javax.jws.WebResult) a);
143                 }
144                 else {
145                     // unknown annotation
146
}
147             }
148         }
149
150         // set default values
151
if ((null == getWmOperationName()) || (0 == getWmOperationName().length())) {
152             setWmOperationName(javaMethodName);
153         }
154         if ((null == getWmAction()) || (0 == getWmAction().length())) {
155             setWmAction("");
156         }
157         
158         // set WebServicePARAMETERMetadata
159
addParams(webParams);
160     }
161
162
163     private void initFromAnnotation(WebResult annotation) {
164         if (null != annotation) {
165             setWrName(annotation.name());
166             setWrTargetNamespace(annotation.targetNamespace());
167         }
168     }
169
170     private void initFromAnnotation(Oneway annotation) {
171         setOneWay(null != annotation);
172     }
173
174     private void initFromAnnotation(WebMethod annotation) {
175         if (null != annotation) {
176             setWmAction(annotation.action());
177             setWmOperationName(annotation.operationName());
178         }
179     }
180
181     public boolean isOneWay() {
182         return oneway;
183     }
184
185     public void setOneWay(boolean oneway) {
186         this.oneway = oneway;
187     }
188
189
190     public String JavaDoc getWmAction() {
191         return wmAction;
192     }
193
194     public void setWmAction(String JavaDoc wmAction) {
195         this.wmAction = wmAction;
196     }
197
198     public String JavaDoc getWmOperationName() {
199         return wmOperationName;
200     }
201
202     public void setWmOperationName(String JavaDoc wmOperationName) {
203         this.wmOperationName = wmOperationName;
204     }
205
206     public String JavaDoc getWrName() {
207         return wrName;
208     }
209
210     public void setWrName(String JavaDoc wrName) {
211         this.wrName = wrName;
212     }
213
214     public String JavaDoc getWrTargetNamespace() {
215         return wrTargetNamespace;
216     }
217
218     public void setWrTargetNamespace(String JavaDoc wrTargetNamespace) {
219         this.wrTargetNamespace = wrTargetNamespace;
220     }
221
222     public List JavaDoc<BeehiveWsParameterMetadata> getParams() {
223         return Collections.unmodifiableList(params);
224     }
225
226     public void addParams(List JavaDoc<? extends BeehiveWsParameterMetadata> parameters) {
227         if (null != parameters) {
228             params.addAll(parameters);
229         }
230         // todo: checks
231
}
232
233     public void addParam(BeehiveWsParameterMetadata parameter) {
234         if (null != parameter) {
235             params.add(parameter);
236         }
237         // todo: checks
238
}
239
240     public String JavaDoc getJavaMethodName() {
241         return javaMethodName;
242     }
243
244     public Class JavaDoc getJavaReturnType() {
245         return javaReturnType;
246     }
247
248     public String JavaDoc getSignature() {
249         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(getJavaMethodName());
250         sb.append('(');
251         boolean firstParam = true;
252         if (null != params) {
253             for (BeehiveWsParameterMetadata p : params) {
254                 if (firstParam) {
255                     firstParam = false;
256                 }
257                 else {
258                     sb.append(',');
259                 }
260                 sb.append(p.getJavaType());
261             }
262         }
263         sb.append(')');
264         return sb.toString();
265     }
266     
267     public String JavaDoc toString() {
268         String JavaDoc signature = getJavaReturnType().toString() + " " + getJavaMethodName() + "( ";
269         for (BeehiveWsParameterMetadata p : params) {
270             signature += p.getJavaType().toString() + " ";
271         }
272         return signature + ")";
273     }
274     
275     public boolean equals(Object JavaDoc o) {
276         if (! (o instanceof Jsr181MethodMetadataImpl)) {
277             return false;
278         }
279         Jsr181MethodMetadataImpl m = (Jsr181MethodMetadataImpl) o;
280
281         // check name
282
if (! getJavaMethodName().equals(m.getJavaMethodName())) {
283             return false;
284         }
285         
286         // check return type
287
if (! getJavaReturnType().equals(m.getJavaReturnType())) {
288             return false;
289         }
290         
291         // check all parameters (types, not names!)
292
if (params.size() != m.params.size()) {
293             return false;
294         }
295         for (int i = 0; i < params.size(); i++) {
296             if (! m.params.get(i).getJavaType().equals(params.get(i).getJavaType())) {
297                 return false;
298             }
299         }
300
301         // check exceptions?
302

303         return true;
304     }
305
306     /* (non-Javadoc)
307      * @see org.apache.beehive.wsm.jsr181.model.Jsr181MethodMetadata#getJavaReturnTypeFullName()
308      *
309      * NOTE THIS METHOD AND SUPPORTING METHOD SHOULD BE MOVED OUT TO A ELEMENT CLASS
310      * THAT ENCAPSULATES QNAME AND CLASS
311
312      */

313     public String JavaDoc getJavaReturnTypeFullName() {
314         return getClassName(getJavaReturnType());
315     }
316
317     private String JavaDoc getClassName(Class JavaDoc cls) {
318             if (cls.isArray()) {
319                 return getClassName(cls.getComponentType()) + "[]";
320             }
321             else {
322                 return cls.getName().replace('$', '.');
323             }
324     }
325     
326     public QName JavaDoc getXmlReturnType() {
327         return mXMLReturnType;
328    }
329
330     public void setXmlReturnType(QName JavaDoc xmlReturnType) {
331         mXMLReturnType = xmlReturnType;
332     }
333
334     /* (non-Javadoc)
335      * @see org.apache.beehive.wsm.jsr181.model.Jsr181MethodMetadata#setReturnType(java.lang.Class)
336      */

337     public void setReturnType(Class JavaDoc javaType) {
338         this.javaReturnType = javaType;
339         
340     }
341
342     /* (non-Javadoc)
343      * @see org.apache.beehive.wsm.jsr181.model.BeehiveWsMethodMetadata#findParam(java.lang.String)
344      */

345     public BeehiveWsParameterMetadata findParam(String JavaDoc parmName) {
346         // walk the list of parameters until the parameter with paramName is found
347
// there are small number of parameters, so walking the list should be ok
348
if( null == parmName) return null;
349         BeehiveWsParameterMetadata res = null;
350         for(BeehiveWsParameterMetadata nxtParam : params) {
351             if( parmName.equals(nxtParam.getWpName())) {
352                 res = nxtParam;
353                 break;
354             }
355         }
356         return res;
357     }
358 }
359
Popular Tags