KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > deployment > WSDDGenerator


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: WSDDGenerator.java,v 1.22.2.9 2005/06/19 10:07:15 bill Exp $
9
package org.jboss.webservice.deployment;
10
11 // $Id: WSDDGenerator.java,v 1.22.2.9 2005/06/19 10:07:15 bill Exp $
12

13 import org.jboss.axis.Constants;
14 import org.jboss.axis.enums.Style;
15 import org.jboss.axis.enums.Use;
16 import org.jboss.logging.Logger;
17 import org.jboss.xb.binding.NamespaceRegistry;
18
19 import javax.xml.namespace.QName JavaDoc;
20 import javax.xml.parsers.DocumentBuilder JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.LinkedHashMap JavaDoc;
31
32 /**
33  * Generate the Axis deployment wsdd
34  * <p/>
35  * Although there is a <code>wsdlFile</code> element in the wsdd it not used by axis to describe the service
36  * the only purpose is, to return that file upon the ?WSDL operation
37  *
38  * @author Thomas.Diesler@jboss.org
39  * @since 08-Jun-2004
40  */

41 public class WSDDGenerator
42 {
43    // provide logging
44
private final Logger log = Logger.getLogger(WSDDGenerator.class);
45
46    // The generated service description
47
private ServiceDescription serviceDesc;
48
49    /**
50     * A wsdd generator for a preconfigured ServiceDescription
51     */

52    public WSDDGenerator(ServiceDescription service)
53    {
54       this.serviceDesc = service;
55    }
56
57    /**
58     * Generate the entire WSDD file
59     */

60    public void generateDeployment(PrintWriter JavaDoc out, String JavaDoc serviceName, String JavaDoc provider)
61    {
62       appendHeader(out);
63       appendServiceElement(out, serviceName, provider);
64       appendOperations(out);
65       appendTypeMappings(out);
66       appendFooter(out);
67       out.close();
68    }
69
70    public void appendHeader(PrintWriter JavaDoc out)
71    {
72       out.println("<deployment");
73       out.println(" xmlns='http://xml.apache.org/axis/wsdd/'");
74       out.println(" xmlns:java='http://xml.apache.org/axis/wsdd/providers/java'");
75       out.println(" xmlns:soap='" + Constants.URI_SOAP11_ENC + "'");
76       out.println(" xmlns:" + Constants.NS_PREFIX_SCHEMA_XSI + "='" + Constants.URI_DEFAULT_SCHEMA_XSI + "'");
77       out.println(" xmlns:" + Constants.NS_PREFIX_SCHEMA_XSD + "='" + Constants.URI_DEFAULT_SCHEMA_XSD + "'>");
78       out.println();
79    }
80
81
82    /** Append the service element
83     */

84    public void appendServiceElement(PrintWriter JavaDoc out, String JavaDoc serviceID, String JavaDoc provider)
85    {
86       Style style = serviceDesc.getStyle();
87       String JavaDoc styleStr = (style != null ? "style='" + style + "' " : " ");
88
89       Use use = serviceDesc.getUse();
90       String JavaDoc useStr = (use != null ? "use='" + use + "' " : " ");
91
92       out.println("<service name='" + serviceID + "' " + styleStr + useStr + "provider='" + provider + "'>");
93       out.println();
94    }
95
96    /** Append the operation information, which is taken from the wsdl
97     */

98    public void appendOperations(PrintWriter JavaDoc out)
99    {
100       String JavaDoc[] opNames = serviceDesc.getOperationNames();
101       Arrays.sort(opNames);
102
103       for (int i = 0; i < opNames.length; i++)
104       {
105          String JavaDoc opName = opNames[i];
106          OperationDescription operation = serviceDesc.getOperation(opName);
107          operation.writeWSDD(out);
108       }
109       out.println();
110    }
111
112    /** Append the typeMapping information, which is taken from the wsdl and jaxrpc-mapping.xml
113     */

114    public void appendTypeMappings(PrintWriter JavaDoc out)
115    {
116       QName JavaDoc[] typeQNames = serviceDesc.getTypMappingNames();
117
118       // Sort the QNames independent of QName implements Compareable
119
// The jdk1.5 version of QName does not implement Compareable
120
LinkedHashMap JavaDoc typeQNameMap = new LinkedHashMap JavaDoc();
121       for (int i = 0; i < typeQNames.length; i++)
122          typeQNameMap.put(typeQNames[i].toString(), typeQNames[i]);
123
124       String JavaDoc[] typeNames = new String JavaDoc[typeQNames.length];
125       typeQNameMap.keySet().toArray(typeNames);
126       Arrays.sort(typeNames);
127
128       for (int i = 0; i < typeNames.length; i++)
129       {
130          QName JavaDoc typeQName = (QName JavaDoc)typeQNameMap.get(typeNames[i]);
131          TypeMappingDescription typeMapping = serviceDesc.getTypMapping(typeQName);
132          typeMapping.writeWSDD(out);
133          out.println();
134       }
135       out.println();
136    }
137
138    public void appendFooter(PrintWriter JavaDoc out)
139    {
140       out.println("</service>");
141       out.println("</deployment>");
142    }
143
144    public static String JavaDoc getQNameAttrValue(QName JavaDoc qname)
145    {
146       if (qname == null)
147          throw new IllegalArgumentException JavaDoc("Cannot convert null qname");
148
149       String JavaDoc qnamePrefix = qname.getPrefix();
150       if (qnamePrefix.length() > 0)
151          qnamePrefix += ":";
152
153       String JavaDoc qnameAttr = qnamePrefix + qname.getLocalPart();
154       return qnameAttr;
155    }
156
157    /**
158     * WSDDGenerator [wsdl] [jaxrpc-mapping] [portName]
159     */

160    public static void main(String JavaDoc[] args) throws Exception JavaDoc
161    {
162
163       URL JavaDoc wsdlURL = null;
164       URL JavaDoc jaxrpcURL = null;
165       String JavaDoc portName = null;
166
167       if (args.length > 0)
168       {
169          try
170          {
171             wsdlURL = new URL JavaDoc(args[0]);
172          }
173          catch (MalformedURLException JavaDoc e)
174          {
175             wsdlURL = new File JavaDoc(args[0]).toURL();
176          }
177       }
178
179       if (args.length > 1)
180       {
181          try
182          {
183             jaxrpcURL = new URL JavaDoc(args[1]);
184          }
185          catch (MalformedURLException JavaDoc e)
186          {
187             jaxrpcURL = new File JavaDoc(args[1]).toURL();
188          }
189       }
190
191       if (args.length > 2)
192          portName = args[2];
193
194       ServiceDescription serviceDesc = new ServiceDescription(wsdlURL, jaxrpcURL, portName);
195       serviceDesc.dumpWsdlDefinition(System.out);
196
197       System.out.println("-------------------------------------------------------------------");
198
199       WSDDGenerator generator = new WSDDGenerator(serviceDesc);
200
201       StringWriter JavaDoc buffer = new StringWriter JavaDoc(1024);
202       PrintWriter JavaDoc out = new PrintWriter JavaDoc(buffer);
203
204       String JavaDoc serviceName = serviceDesc.getWsdlService().getQName().getLocalPart();
205       generator.generateDeployment(out, serviceName, "Handler");
206
207       System.out.println(buffer.toString());
208
209       // sanity check if we can parse it
210
DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
211       factory.setNamespaceAware(true);
212       factory.setValidating(false);
213       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
214       builder.parse(new ByteArrayInputStream JavaDoc(buffer.toString().getBytes()));
215
216       File JavaDoc file = new File JavaDoc("ws4ee-deployment.xml");
217       out = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
218       out.println(buffer.toString());
219       out.close();
220
221       System.out.println(file.getCanonicalPath());
222    }
223 }
224
Popular Tags