View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.geronimo.ews.ws4j2ee.context;
18  
19  import org.apache.axis.wsdl.fromJava.Emitter;
20  import org.apache.axis.wsdl.symbolTable.BindingEntry;
21  import org.apache.axis.wsdl.symbolTable.Parameter;
22  import org.apache.axis.wsdl.symbolTable.Parameters;
23  import org.apache.axis.wsdl.symbolTable.PortEntry;
24  import org.apache.axis.wsdl.symbolTable.PortTypeEntry;
25  import org.apache.axis.wsdl.symbolTable.ServiceEntry;
26  import org.apache.geronimo.ews.ws4j2ee.context.impl.EJBDDContextImpl;
27  import org.apache.geronimo.ews.ws4j2ee.context.impl.SEIOperationImpl;
28  import org.apache.geronimo.ews.ws4j2ee.context.j2eeDD.EJBContext;
29  import org.apache.geronimo.ews.ws4j2ee.context.webservices.server.interfaces.WSCFPortComponent;
30  import org.apache.geronimo.ews.ws4j2ee.context.webservices.server.interfaces.WSCFWebserviceDescription;
31  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
32  import org.apache.geronimo.ews.ws4j2ee.toWs.UnrecoverableGenerationFault;
33  
34  import javax.wsdl.Fault;
35  import javax.wsdl.Operation;
36  import javax.wsdl.Port;
37  import javax.wsdl.Service;
38  import javax.xml.namespace.QName;
39  import java.util.Collection;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  
44  /***
45   * <p>Before the code genaration this class is used to analyis the parse inforemation
46   * and make sure that the data populated in the different *Context* are
47   * consistent. This class will populate the most of the infomation in the
48   * MiscInfo.</p>
49   *
50   * @author Srinath Perera(hemapani@opensource.lk)
51   */
52  public class ContextValidator {
53      private J2EEWebServiceContext context;
54  
55      public ContextValidator(J2EEWebServiceContext context) throws GenerationFault {
56          try {
57              this.context = context;
58          } catch (Exception e) {
59              e.printStackTrace();
60              throw GenerationFault.createGenerationFault(e);
61          }
62      }
63  
64      /***
65       * <p>Just as in the server side this code find the WSDL Service and WSDLport and
66       * populate them in the MiscInfo</p>
67       *
68       * @param emitter
69       * @throws GenerationFault
70       */
71      public void validateWithOutWSDL(Emitter emitter) throws GenerationFault {
72          try {
73              Collection servicesCollection = context.getWSDLContext().getServices();
74              if (servicesCollection.size() > 1)
75                  throw new UnrecoverableGenerationFault("we are supporting one service yet");
76              Iterator services = servicesCollection.iterator();
77              Service service = null;
78              if (services.hasNext()) {
79                  service = ((Service) services.next());
80                  context.getWSDLContext().settargetService(new ServiceEntry(service));
81              }
82              
83              //get the service ports 
84              Collection portCollecton = service.getPorts().values();
85              //just as before if there are more than one servie just let
86              //it failed
87              if (servicesCollection.size() > 1)
88                  throw new UnrecoverableGenerationFault("we are supporting one port yet");
89              Iterator portList = portCollecton.iterator();
90              Port wsdlport = null;
91              while (portList.hasNext()) {
92                  wsdlport = (Port) portList.next();
93                  context.getWSDLContext().setTargetPort(wsdlport);
94              }
95              if (wsdlport == null)
96                  throw new UnrecoverableGenerationFault("no port discription not match with the wsdl file");
97              QName bindingName = wsdlport.getBinding().getQName();
98              BindingEntry wsdlbinding = context.getWSDLContext().getBinding(bindingName);
99              context.getWSDLContext().settargetBinding(wsdlbinding);
100             QName portTypename = wsdlbinding.getBinding().getPortType().getQName();
101             PortTypeEntry port = context.getWSDLContext().getPortType(portTypename);
102             if (port == null)
103                 throw new UnrecoverableGenerationFault("port type must exits");
104             context.getWSDLContext().setTargetPortType(port);
105         } catch (Exception e) {
106             e.printStackTrace();
107             throw GenerationFault.createGenerationFault(e);
108         }
109     }
110 
111     public void validateWithWSDL() {
112         WSCFWebserviceDescription wscfwsdis = context.getWSCFContext().getWscfdWsDesxription();
113         WSCFPortComponent[] ports = wscfwsdis.getPortComponent();
114         if (ports == null || ports.length == 0)
115             throw new UnrecoverableGenerationFault("no port discription found in the" +
116                     "webservices.xml file");
117         WSCFPortComponent port = ports[0];
118         context.getWSCFContext().setWscfport(port);
119         String ejbLink = port.getServiceImplBean().getEjblink();
120         // context.getMiscInfo().setJaxrpcSEI(port.getServiceEndpointInterface());
121         if (ejbLink != null) {
122             String bean = port.getServiceEndpointInterface() + "Bean";
123             String home = port.getServiceEndpointInterface() + "Home";
124             String remote = port.getServiceEndpointInterface() + "EJB";
125             String local = port.getServiceEndpointInterface() + "LocalEJB";
126             String localHome = port.getServiceEndpointInterface() + "LocalHome";
127             EJBContext ejbcontext = context.getEJBDDContext();
128             if (ejbcontext == null) {
129                 ejbcontext = new EJBDDContextImpl(ejbLink, bean, home, remote, localHome, local);
130             }
131 //TODO remove this if not needed
132             if (ejbcontext.getImplBean() == null)
133                 ejbcontext.setImplBean(bean);
134             if (ejbcontext.getEjbhomeInterface() == null)
135                 ejbcontext.setEjbhomeInterface(home);
136             if (ejbcontext.getEjbRemoteInterface() == null)
137                 ejbcontext.setEjbRemoteInterface(remote);
138             if (ejbcontext.getEjbLocalHomeInterfce() == null)
139                 ejbcontext.setEjbLocalHomeInterfce(localHome);
140             if (ejbcontext.getEjbLocalInterface() == null)
141                 ejbcontext.setEjbLocalHomeInterfce(local);
142             context.getMiscInfo().setJ2eeComponetLink(port.getServiceImplBean().getEjblink());
143         } else {
144             context.getMiscInfo().setImplwithEJB(false);
145         }
146         QName portName = new QName(port.getWsdlPort().getNamespaceURI(), port.getWsdlPort().getLocalpart());
147         PortEntry wsdlport = context.getWSDLContext().getPort(portName);
148         if (wsdlport == null)
149             throw new UnrecoverableGenerationFault("wsdl port can not be null, you may have the " +
150                     "wsdlport define wrongly on the webservices.xml");
151         Iterator services = context.getWSDLContext().getServices().iterator();
152         while (services.hasNext()) {
153             ServiceEntry service = (ServiceEntry) services.next();
154             Iterator portList = service.getService().getPorts().values().iterator();
155             while (portList.hasNext()) {
156                 if (((Port) portList.next()) == wsdlport.getPort()) {
157                     context.getWSDLContext().settargetService(service);
158                     break;
159                 }
160             }
161         }
162         if (wsdlport == null)
163             throw new UnrecoverableGenerationFault("no port discription not match with the wsdl file");
164         QName bindingName = wsdlport.getPort().getBinding().getQName();
165         BindingEntry wsdlbinding = context.getWSDLContext().getBinding(bindingName);
166         context.getWSDLContext().settargetBinding(wsdlbinding);
167         QName portTypename = wsdlbinding.getBinding().getPortType().getQName();
168         context.getWSDLContext().setTargetPortType(context.getWSDLContext().getPortType(portTypename));
169         context.validate();
170 			
171         //find and populate the information about the SEI in the 
172         //MiscInfo
173         String seiName = context.getJAXRPCMappingContext()
174                 .getServiceEndpointInterfaceName(context.getWSDLContext().getTargetPortType(), context.getWSDLContext().gettargetBinding());
175         context.getMiscInfo().setJaxrpcSEI(seiName);
176         List operations = context.getWSDLContext().getTargetPortType().getPortType().getOperations();
177         for (int i = 0; i < operations.size(); i++) {
178             SEIOperation seiOp = new SEIOperationImpl();
179             Operation op = (Operation) operations.get(i);
180             BindingEntry binding = context.getWSDLContext().gettargetBinding();
181             
182             //got to get the same parameter order as the JAXRPC mapper does
183             //So I am using the same methods. Can somebody find something better?? 
184             Parameters parms = binding.getParameters(op);
185             //set return type	
186             String returnType = context.getJAXRPCMappingContext().getJavaMethodReturnType(binding, op);
187             seiOp.setReturnType(returnType);
188 
189 
190             //set method name 
191             String methodName = context.getJAXRPCMappingContext().getJavaMethodName(binding, op);
192             seiOp.setMethodName(methodName);
193 
194             //Iterator paramlist = parms.list.iterator();
195             String parameterName = null;
196             String parameterType = null;	  
197 			
198             //find the parameters and add each parameter to parameter list 
199             for (int paramCount = 0; paramCount < parms.list.size(); paramCount++) {
200                 Parameter p = (Parameter) parms.list.get(paramCount);
201                 parameterName = p.getName();
202                 parameterType = context.getJAXRPCMappingContext().getJavaMethodParamType(binding, op, paramCount, p.getType().getQName());
203                 seiOp.addParameter(parameterType, parameterName);
204             }
205             //let us find the faults
206             Map faults = parms.faults;
207             if (faults != null) {
208                 Iterator it = faults.values().iterator();
209                 while (it.hasNext()) {
210                     Fault fault = (Fault) it.next();
211                     String faulltName = context.getJAXRPCMappingContext()
212                             .getExceptionType(fault.getMessage().getQName());
213                     seiOp.addFault(faulltName);
214                 }
215             }
216             context.getMiscInfo().setSEIOperations(seiOp);
217         }
218 //        
219 //        String serviceName = context.getJAXRPCMappingContext()
220 //                .getServiceInterfaceName(context.getMiscInfo().gettargetService());
221     }
222 }