KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > soap > reflect > WebServiceIntrospector


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.soap.reflect;
31
32 import com.caucho.jaxb.JAXBContextImpl;
33 import com.caucho.jaxb.JAXBUtil;
34 import com.caucho.soap.skeleton.DirectSkeleton;
35 import com.caucho.soap.skeleton.AbstractAction;
36 import com.caucho.util.L10N;
37
38 import org.w3c.dom.Node JavaDoc;
39
40 import javax.jws.WebMethod;
41 import javax.jws.WebService;
42 import javax.xml.bind.JAXBException;
43 import javax.xml.bind.Marshaller;
44 import javax.xml.bind.Unmarshaller;
45 import javax.xml.stream.XMLOutputFactory;
46 import javax.xml.stream.XMLStreamException;
47 import javax.xml.stream.XMLStreamWriter;
48 import javax.xml.transform.dom.DOMResult JavaDoc;
49 import java.lang.reflect.Method JavaDoc;
50 import java.lang.reflect.Modifier JavaDoc;
51 import java.util.HashSet JavaDoc;
52 import java.util.LinkedHashMap JavaDoc;
53
54 /**
55  * Introspects a web service
56  */

57 public class WebServiceIntrospector {
58   private static XMLOutputFactory _outputFactory = null;
59
60   public static final L10N L = new L10N(WebServiceIntrospector.class);
61   /**
62    * Introspects the class
63    */

64   public DirectSkeleton introspect(Class JavaDoc type)
65     //throws ConfigException
66
throws JAXBException
67   {
68     // matches RI stub for the WSDL location
69
return introspect(type, "REPLACE_WITH_ACTUAL_URL");
70   }
71        
72   /**
73    * Introspects the class
74    */

75   public DirectSkeleton introspect(Class JavaDoc type, String JavaDoc wsdlLocation)
76     //throws ConfigException
77
throws JAXBException
78   {
79     // server/4221 vs soap/0301
80
/*
81     if (! type.isAnnotationPresent(WebService.class))
82       throw new RuntimeException(L.l("{0}: needs a @WebService annotation.",
83                                      type.getName()));
84     */

85     
86     boolean isInterface = type.isInterface();
87
88     WebService webService = (WebService) type.getAnnotation(WebService.class);
89
90     // Get all the classes that JAXB needs, then generate schema for them
91
HashSet JavaDoc<Class JavaDoc> jaxbClasses = new HashSet JavaDoc<Class JavaDoc>();
92     JAXBUtil.introspectClass(type, jaxbClasses);
93     Class JavaDoc[] jaxbClassArray = new Class JavaDoc[jaxbClasses.size()];
94     jaxbClasses.toArray(jaxbClassArray);
95
96     JAXBContextImpl jaxbContext = new JAXBContextImpl(jaxbClassArray, null);
97     Marshaller marshaller = jaxbContext.createMarshaller();
98     Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
99
100     DirectSkeleton skel = new DirectSkeleton(type, wsdlLocation);
101     String JavaDoc namespace = skel.getNamespace();
102
103     Method[] methods = type.getMethods();
104
105     for (int i = 0; i < methods.length; i++) {
106       if ((methods[i].getModifiers() & Modifier.PUBLIC) == 0)
107         continue;
108
109       WebMethod webMethod = methods[i].getAnnotation(WebMethod.class);
110
111       if (webService == null && webMethod == null && ! isInterface)
112         continue;
113
114       if (webMethod == null && methods[i].getDeclaringClass() != type)
115         continue;
116
117       if (webMethod != null && webMethod.exclude())
118         continue;
119
120       AbstractAction action =
121         AbstractAction.createAction(methods[i], jaxbContext, namespace,
122                                     marshaller, unmarshaller);
123
124       String JavaDoc name = webMethod == null ? "" : webMethod.operationName();
125
126       if (name.equals(""))
127           name = methods[i].getName();
128
129       skel.addAction(name, action);
130     }
131
132     /* XXX
133     Node typesNode = skel.getTypesNode();
134     DOMResult result = new DOMResult(typesNode);
135
136     try {
137       XMLStreamWriter out = getStreamWriter(result);
138       jaxbContext.generateSchemaWithoutHeader(out);
139     }
140     catch (XMLStreamException e) {
141       throw new JAXBException(e);
142     }
143     */

144
145     return skel;
146   }
147
148   private static XMLStreamWriter getStreamWriter(DOMResult JavaDoc result)
149     throws XMLStreamException
150   {
151     if (_outputFactory == null)
152       _outputFactory = XMLOutputFactory.newInstance();
153
154     return _outputFactory.createXMLStreamWriter(result);
155   }
156 }
157
Popular Tags