1 23 package com.sun.enterprise.webservice; 24 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.OutputStream ; 29 import java.io.PrintWriter ; 30 import java.util.Map ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.HashSet ; 34 import java.util.Set ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Properties ; 38 39 import javax.xml.namespace.QName ; 40 41 import com.sun.xml.rpc.spi.JaxRpcObjectFactory; 43 import com.sun.xml.rpc.spi.tools.CompileTool; 44 import com.sun.xml.rpc.spi.tools.CompileToolDelegate; 45 import com.sun.xml.rpc.spi.tools.HandlerChainInfo; 46 import com.sun.xml.rpc.spi.tools.HandlerInfo; 47 import com.sun.xml.rpc.spi.tools.Configuration; 48 import com.sun.xml.rpc.spi.tools.ModelInfo; 49 import com.sun.xml.rpc.spi.model.Model; 50 import com.sun.xml.rpc.spi.model.Service; 51 import com.sun.xml.rpc.spi.model.Port; 52 import com.sun.xml.rpc.spi.model.ModelProperties; 53 54 import com.sun.enterprise.deployment.WebBundleDescriptor; 55 import com.sun.enterprise.deployment.EjbBundleDescriptor; 56 import com.sun.enterprise.deployment.WebService; 57 import com.sun.enterprise.deployment.WebServicesDescriptor; 58 import com.sun.enterprise.deployment.WebServiceEndpoint; 59 import com.sun.enterprise.deployment.ServiceReferenceDescriptor; 60 import com.sun.enterprise.deployment.WebServiceHandler; 61 import com.sun.enterprise.deployment.NameValuePairDescriptor; 62 63 69 public final class WsCompile extends CompileToolDelegate { 70 71 private Collection generatedFiles; 72 private WebService webService; 73 private ServiceReferenceDescriptor serviceRef; 74 private WsUtil wsUtil = new WsUtil(); 75 private CompileTool wscompile; 76 private JaxRpcObjectFactory rpcFactory; 77 78 private ModelInfo modelInfo; 80 81 private boolean error = false; 83 84 public WsCompile(CompileTool compileTool, WebService webServiceDesc) { 85 wscompile = compileTool; 86 webService = webServiceDesc; 87 rpcFactory = JaxRpcObjectFactory.newInstance(); 88 } 89 90 public WsCompile(CompileTool compileTool, 91 ServiceReferenceDescriptor serviceRefDesc) { 92 wscompile = compileTool; 93 serviceRef = serviceRefDesc; 94 rpcFactory = JaxRpcObjectFactory.newInstance(); 95 } 96 97 public void setModelInfo(ModelInfo info) { 98 modelInfo = info; 99 } 100 101 public CompileTool getCompileTool() { 102 return wscompile; 103 } 104 105 108 109 public Configuration createConfiguration() { 110 Configuration configuration = null; 111 if( modelInfo != null ) { 112 configuration = 113 rpcFactory.createConfiguration(wscompile.getEnvironment()); 114 configuration.setModelInfo(modelInfo); 115 } 116 119 return configuration; 120 } 121 122 public void preOnError() { 123 error = true; 124 } 125 126 public void postRegisterProcessorActions() { 127 128 if( !error) { 129 130 if (webService != null) { 131 setupServiceHandlerChain(); 132 } 133 134 } 136 } 137 138 public void postRun() { 139 generatedFiles = new HashSet (); 140 141 if( !error ) { 142 for(Iterator iter = wscompile.getEnvironment().getGeneratedFiles(); 143 iter.hasNext(); ) { 144 generatedFiles.add( iter.next() ); 145 } 146 if( webService != null ) { 147 doServicePostProcessing(); 148 } else if( serviceRef != null ) { 149 doClientPostProcessing(); 150 } 151 } 152 } 153 154 158 public Collection getGeneratedFiles() { 159 return generatedFiles; 160 } 161 162 private void setupServiceHandlerChain() { 163 164 Model model = wscompile.getProcessor().getModel(); 165 166 Collection endpoints = webService.getEndpoints(); 167 for(Iterator eIter = endpoints.iterator(); eIter.hasNext();) { 168 WebServiceEndpoint nextEndpoint = (WebServiceEndpoint) eIter.next(); 169 170 if( !nextEndpoint.hasHandlers() ) { 171 continue; 172 } 173 174 Port port = wsUtil.getPortFromModel(model, 175 nextEndpoint.getWsdlPort()); 176 if( port == null ) { 177 throw new IllegalStateException ("Model port for endpoint " + 178 nextEndpoint.getEndpointName() + 179 " not found"); 180 } 181 182 List handlerChain = nextEndpoint.getHandlers(); 183 HandlerChainInfo modelHandlerChain = 184 port.getServerHCI(); 185 List handlerInfoList = new ArrayList (); 186 187 HandlerInfo preHandler = rpcFactory.createHandlerInfo(); 190 String handlerClassName = nextEndpoint.implementedByEjbComponent() ? 191 "com.sun.enterprise.webservice.EjbContainerPreHandler" : 192 "com.sun.enterprise.webservice.ServletPreHandler"; 193 preHandler.setHandlerClassName(handlerClassName); 194 handlerInfoList.add(preHandler); 195 196 Collection soapRoles = new HashSet (); 201 202 for(Iterator hIter = handlerChain.iterator(); hIter.hasNext();) { 203 WebServiceHandler nextHandler = 204 (WebServiceHandler) hIter.next(); 205 HandlerInfo handlerInfo = createHandlerInfo(nextHandler); 206 handlerInfoList.add(handlerInfo); 207 soapRoles.addAll(nextHandler.getSoapRoles()); 208 } 209 210 HandlerInfo postHandler = rpcFactory.createHandlerInfo(); 212 handlerClassName = nextEndpoint.implementedByEjbComponent() ? 213 "com.sun.enterprise.webservice.EjbContainerPostHandler" : 214 "com.sun.enterprise.webservice.ServletPostHandler"; 215 postHandler.setHandlerClassName(handlerClassName); 216 handlerInfoList.add(postHandler); 217 218 modelHandlerChain.setHandlersList(handlerInfoList); 221 222 for(Iterator roleIter = soapRoles.iterator(); roleIter.hasNext();) { 223 modelHandlerChain.addRole((String ) roleIter.next()); 224 } 225 } 226 } 227 228 private HandlerInfo createHandlerInfo(WebServiceHandler handler) { 229 HandlerInfo handlerInfo = rpcFactory.createHandlerInfo(); 230 231 handlerInfo.setHandlerClassName(handler.getHandlerClass()); 232 for(Iterator iter = handler.getSoapHeaders().iterator(); 233 iter.hasNext();) { 234 QName next = (QName ) iter.next(); 235 handlerInfo.addHeaderName(next); 236 } 237 238 Map properties = handlerInfo.getProperties(); 239 for(Iterator iter = handler.getInitParams().iterator(); 240 iter.hasNext();) { 241 NameValuePairDescriptor next = (NameValuePairDescriptor) 242 iter.next(); 243 properties.put(next.getName(), next.getValue()); 244 } 245 246 return handlerInfo; 247 } 248 249 private void doServicePostProcessing() { 250 251 Model model = wscompile.getProcessor().getModel(); 252 253 Collection endpoints = webService.getEndpoints(); 254 255 for(Iterator iter = endpoints.iterator(); iter.hasNext(); ) { 256 WebServiceEndpoint next = (WebServiceEndpoint) iter.next(); 257 Service service = wsUtil.getServiceForPort(model, 258 next.getWsdlPort()); 259 if( service == null ) { 260 service = (Service) model.getServices().next(); 261 262 System.out.println("Warning : Can't find Service for Endpoint '" 263 + next.getEndpointName() + "' Port '" + 264 next.getWsdlPort() + "'"); 265 266 System.out.println("Defaulting to "+ service.getName()); 267 } 268 269 QName serviceName = service.getName(); 270 271 next.setServiceNamespaceUri(serviceName.getNamespaceURI()); 272 next.setServiceLocalPart(serviceName.getLocalPart()); 273 274 Port port = wsUtil.getPortFromModel(model, next.getWsdlPort()); 275 if( port == null ) { 276 String msg = "Can't find model port for endpoint " 277 + next.getEndpointName() + " with wsdl-port " + 278 next.getWsdlPort(); 279 throw new IllegalStateException (msg); 280 } 281 282 String tieClassName = (String ) 288 port.getProperty(ModelProperties.PROPERTY_TIE_CLASS_NAME); 289 if( tieClassName == null ) { 290 tieClassName = next.getServiceEndpointInterface() + "_Tie"; 291 } 292 next.setTieClassName(tieClassName); 293 294 if( next.implementedByWebComponent() ) { 295 wsUtil.updateServletEndpointRuntime(next); 296 } else { 297 wsUtil.validateEjbEndpoint(next); 298 } 299 300 String endpointAddressUri = next.getEndpointAddressUri(); 301 if( endpointAddressUri == null ) { 302 String msg = "Endpoint address uri must be set for endpoint " + 303 next.getEndpointName(); 304 throw new IllegalStateException (msg); 305 } else if( endpointAddressUri.indexOf("*") >= 0 ) { 306 String msg = "Endpoint address uri " + endpointAddressUri + 307 " for endpoint " + next.getEndpointName() + 308 " is invalid. It must not contain the '*' character"; 309 throw new IllegalStateException (msg); 310 } else if( endpointAddressUri.endsWith("/") ) { 311 String msg = "Endpoint address uri " + endpointAddressUri + 312 " for endpoint " + next.getEndpointName() + 313 " is invalid. It must not end with '/'"; 314 throw new IllegalStateException (msg); 315 } 316 } 317 } 318 319 private void doClientPostProcessing() { 320 Model model = wscompile.getProcessor().getModel(); 321 322 Iterator serviceIter = model.getServices(); 323 Service service = null; 324 325 if( serviceRef.hasServiceName() ) { 326 while( serviceIter.hasNext() ) { 327 Service next = (Service) serviceIter.next(); 328 if( next.getName().equals(serviceRef.getServiceName()) ) { 329 service = next; 330 break; 331 } 332 } 333 if( service == null ) { 334 throw new IllegalStateException 335 ("Service " + serviceRef.getServiceName() + 336 " for service-ref " + serviceRef.getName() + " not found"); 337 } 338 } else { 339 if( serviceIter.hasNext() ) { 340 service = (Service) serviceIter.next(); 341 if( serviceIter.hasNext() ) { 342 throw new IllegalStateException 343 ("service ref " + serviceRef.getName() + " must specify" 344 + " service name since its wsdl declares multiple" 345 + " services"); 346 } 347 QName sName = service.getName(); 348 serviceRef.setServiceNamespaceUri(sName.getNamespaceURI()); 349 serviceRef.setServiceLocalPart(sName.getLocalPart()); 350 } else { 351 throw new IllegalStateException 352 ("service ref " + serviceRef.getName() + " WSDL must " + 353 "define at least one Service"); 354 } 355 } 356 357 String serviceImpl = service.getJavaIntf().getName() + "_Impl"; 360 serviceRef.setServiceImplClassName(serviceImpl); 361 362 } 363 364 } 365 | Popular Tags |