1 7 8 package org.jboss.webservice.deployment; 10 11 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 ; 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.PrintWriter ; 26 import java.io.StringWriter ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.Arrays ; 30 import java.util.LinkedHashMap ; 31 32 41 public class WSDDGenerator 42 { 43 private final Logger log = Logger.getLogger(WSDDGenerator.class); 45 46 private ServiceDescription serviceDesc; 48 49 52 public WSDDGenerator(ServiceDescription service) 53 { 54 this.serviceDesc = service; 55 } 56 57 60 public void generateDeployment(PrintWriter out, String serviceName, String 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 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 84 public void appendServiceElement(PrintWriter out, String serviceID, String provider) 85 { 86 Style style = serviceDesc.getStyle(); 87 String styleStr = (style != null ? "style='" + style + "' " : " "); 88 89 Use use = serviceDesc.getUse(); 90 String useStr = (use != null ? "use='" + use + "' " : " "); 91 92 out.println("<service name='" + serviceID + "' " + styleStr + useStr + "provider='" + provider + "'>"); 93 out.println(); 94 } 95 96 98 public void appendOperations(PrintWriter out) 99 { 100 String [] opNames = serviceDesc.getOperationNames(); 101 Arrays.sort(opNames); 102 103 for (int i = 0; i < opNames.length; i++) 104 { 105 String opName = opNames[i]; 106 OperationDescription operation = serviceDesc.getOperation(opName); 107 operation.writeWSDD(out); 108 } 109 out.println(); 110 } 111 112 114 public void appendTypeMappings(PrintWriter out) 115 { 116 QName [] typeQNames = serviceDesc.getTypMappingNames(); 117 118 LinkedHashMap typeQNameMap = new LinkedHashMap (); 121 for (int i = 0; i < typeQNames.length; i++) 122 typeQNameMap.put(typeQNames[i].toString(), typeQNames[i]); 123 124 String [] typeNames = new String [typeQNames.length]; 125 typeQNameMap.keySet().toArray(typeNames); 126 Arrays.sort(typeNames); 127 128 for (int i = 0; i < typeNames.length; i++) 129 { 130 QName typeQName = (QName )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 out) 139 { 140 out.println("</service>"); 141 out.println("</deployment>"); 142 } 143 144 public static String getQNameAttrValue(QName qname) 145 { 146 if (qname == null) 147 throw new IllegalArgumentException ("Cannot convert null qname"); 148 149 String qnamePrefix = qname.getPrefix(); 150 if (qnamePrefix.length() > 0) 151 qnamePrefix += ":"; 152 153 String qnameAttr = qnamePrefix + qname.getLocalPart(); 154 return qnameAttr; 155 } 156 157 160 public static void main(String [] args) throws Exception 161 { 162 163 URL wsdlURL = null; 164 URL jaxrpcURL = null; 165 String portName = null; 166 167 if (args.length > 0) 168 { 169 try 170 { 171 wsdlURL = new URL (args[0]); 172 } 173 catch (MalformedURLException e) 174 { 175 wsdlURL = new File (args[0]).toURL(); 176 } 177 } 178 179 if (args.length > 1) 180 { 181 try 182 { 183 jaxrpcURL = new URL (args[1]); 184 } 185 catch (MalformedURLException e) 186 { 187 jaxrpcURL = new File (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 buffer = new StringWriter (1024); 202 PrintWriter out = new PrintWriter (buffer); 203 204 String serviceName = serviceDesc.getWsdlService().getQName().getLocalPart(); 205 generator.generateDeployment(out, serviceName, "Handler"); 206 207 System.out.println(buffer.toString()); 208 209 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 211 factory.setNamespaceAware(true); 212 factory.setValidating(false); 213 DocumentBuilder builder = factory.newDocumentBuilder(); 214 builder.parse(new ByteArrayInputStream (buffer.toString().getBytes())); 215 216 File file = new File ("ws4ee-deployment.xml"); 217 out = new PrintWriter (new FileWriter (file)); 218 out.println(buffer.toString()); 219 out.close(); 220 221 System.out.println(file.getCanonicalPath()); 222 } 223 } 224 | Popular Tags |