View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.axis.builder;
20  
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.xml.namespace.QName;
31  
32  import org.apache.geronimo.common.DeploymentException;
33  import org.apache.geronimo.gbean.GBeanInfo;
34  import org.apache.geronimo.gbean.GBeanInfoBuilder;
35  import org.apache.geronimo.j2ee.deployment.HandlerInfoInfo;
36  import org.apache.geronimo.j2ee.deployment.Module;
37  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38  import org.apache.geronimo.kernel.ClassLoading;
39  import org.apache.geronimo.kernel.config.Configuration;
40  import org.apache.geronimo.kernel.repository.Environment;
41  import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
42  import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefDocument;
43  import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
44  import org.apache.geronimo.xbeans.j2ee.ParamValueType;
45  import org.apache.geronimo.xbeans.j2ee.PortComponentRefType;
46  import org.apache.geronimo.xbeans.j2ee.ServiceRefHandlerType;
47  import org.apache.geronimo.xbeans.j2ee.ServiceRefType;
48  import org.apache.geronimo.xbeans.j2ee.XsdQNameType;
49  import org.apache.xmlbeans.QNameSet;
50  import org.apache.xmlbeans.XmlObject;
51  
52  /**
53   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
54   */
55  public class AxisServiceRefBuilder extends AbstractNamingBuilder {
56      private final QNameSet serviceRefQNameSet;
57      private static final QName GER_SERVICE_REF_QNAME = GerServiceRefDocument.type.getDocumentElementName();
58      private static final QNameSet GER_SERVICE_REF_QNAME_SET = QNameSet.singleton(GER_SERVICE_REF_QNAME);
59  
60      private final AxisBuilder axisBuilder;
61  
62      public AxisServiceRefBuilder(Environment defaultEnvironment, String[] eeNamespaces, AxisBuilder axisBuilder) {
63          super(defaultEnvironment);
64          this.axisBuilder = axisBuilder;
65          serviceRefQNameSet = buildQNameSet(eeNamespaces, "service-ref");
66      }
67  
68      protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
69           return getServiceRefs(specDD).length > 0;
70      }
71      
72      public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException {
73          XmlObject[] serviceRefsUntyped = getServiceRefs(specDD);
74          XmlObject[] gerServiceRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GER_SERVICE_REF_QNAME_SET);
75          Map serviceRefMap = mapServiceRefs(gerServiceRefsUntyped);
76          ClassLoader cl = module.getEarContext().getClassLoader();
77  
78          for (int i = 0; i < serviceRefsUntyped.length; i++) {
79              ServiceRefType serviceRef = (ServiceRefType) serviceRefsUntyped[i];
80              String name = getStringValue(serviceRef.getServiceRefName());
81              GerServiceRefType serviceRefType = (GerServiceRefType) serviceRefMap.get(name);
82  //            Map credentialsNameMap = (Map) serviceRefCredentialsNameMap.get(name);
83              String serviceInterfaceName = getStringValue(serviceRef.getServiceInterface());
84              assureInterface(serviceInterfaceName, "javax.xml.rpc.Service", "[Web]Service", cl);
85              Class serviceInterface;
86              try {
87                  serviceInterface = cl.loadClass(serviceInterfaceName);
88              } catch (ClassNotFoundException e) {
89                  throw new DeploymentException("Could not load service interface class: " + serviceInterfaceName, e);
90              }
91              URI wsdlURI = null;
92              if (serviceRef.isSetWsdlFile()) {
93                  try {
94                      wsdlURI = new URI(serviceRef.getWsdlFile().getStringValue().trim());
95                  } catch (URISyntaxException e) {
96                      throw new DeploymentException("could not construct wsdl uri from " + serviceRef.getWsdlFile().getStringValue(), e);
97                  }
98              }
99              URI jaxrpcMappingURI = null;
100             if (serviceRef.isSetJaxrpcMappingFile()) {
101                 try {
102                     jaxrpcMappingURI = new URI(getStringValue(serviceRef.getJaxrpcMappingFile()));
103                 } catch (URISyntaxException e) {
104                     throw new DeploymentException("Could not construct jaxrpc mapping uri from " + serviceRef.getJaxrpcMappingFile(), e);
105                 }
106             }
107             QName serviceQName = null;
108             if (serviceRef.isSetServiceQname()) {
109                 serviceQName = serviceRef.getServiceQname().getQNameValue();
110             }
111             Map portComponentRefMap = new HashMap();
112             PortComponentRefType[] portComponentRefs = serviceRef.getPortComponentRefArray();
113             if (portComponentRefs != null) {
114                 for (int j = 0; j < portComponentRefs.length; j++) {
115                     PortComponentRefType portComponentRef = portComponentRefs[j];
116                     String portComponentLink = getStringValue(portComponentRef.getPortComponentLink());
117                     String serviceEndpointInterfaceType = getStringValue(portComponentRef.getServiceEndpointInterface());
118                     assureInterface(serviceEndpointInterfaceType, "java.rmi.Remote", "ServiceEndpoint", cl);
119                     Class serviceEndpointClass;
120                     try {
121                         serviceEndpointClass = cl.loadClass(serviceEndpointInterfaceType);
122                     } catch (ClassNotFoundException e) {
123                         throw new DeploymentException("could not load service endpoint class " + serviceEndpointInterfaceType, e);
124                     }
125                     portComponentRefMap.put(serviceEndpointClass, portComponentLink);
126                 }
127             }
128             ServiceRefHandlerType[] handlers = serviceRef.getHandlerArray();
129             List handlerInfos = buildHandlerInfoList(handlers, cl);
130 
131 //we could get a Reference or the actual serializable Service back.
132             Object ref = axisBuilder.createService(serviceInterface, wsdlURI, jaxrpcMappingURI, serviceQName, portComponentRefMap, handlerInfos, serviceRefType, module, cl);
133             getJndiContextMap(componentContext).put(ENV + name, ref);
134         }
135 
136     }
137 
138     private XmlObject[] getServiceRefs(XmlObject specDD) {
139         return convert(specDD.selectChildren(serviceRefQNameSet), J2EE_CONVERTER, ServiceRefType.type);
140     }
141 
142     public QNameSet getSpecQNameSet() {
143         return serviceRefQNameSet;
144     }
145 
146     public QNameSet getPlanQNameSet() {
147         return GER_SERVICE_REF_QNAME_SET;
148     }
149 
150 
151     private static List buildHandlerInfoList(ServiceRefHandlerType[] handlers, ClassLoader classLoader) throws DeploymentException {
152         List handlerInfos = new ArrayList();
153         for (int i = 0; i < handlers.length; i++) {
154             ServiceRefHandlerType handler = handlers[i];
155             org.apache.geronimo.xbeans.j2ee.String[] portNameArray = handler.getPortNameArray();
156             List portNames = new ArrayList();
157             for (int j = 0; j < portNameArray.length; j++) {
158                 portNames.add(portNameArray[j].getStringValue().trim());
159 
160             }
161 //            Set portNames = new HashSet(Arrays.asList(portNameArray));
162             String handlerClassName = handler.getHandlerClass().getStringValue().trim();
163             Class handlerClass;
164             try {
165                 handlerClass = ClassLoading.loadClass(handlerClassName, classLoader);
166             } catch (ClassNotFoundException e) {
167                 throw new DeploymentException("Could not load handler class", e);
168             }
169             Map config = new HashMap();
170             ParamValueType[] paramValues = handler.getInitParamArray();
171             for (int j = 0; j < paramValues.length; j++) {
172                 ParamValueType paramValue = paramValues[j];
173                 String paramName = paramValue.getParamName().getStringValue().trim();
174                 String paramStringValue = paramValue.getParamValue().getStringValue().trim();
175                 config.put(paramName, paramStringValue);
176             }
177             XsdQNameType[] soapHeaderQNames = handler.getSoapHeaderArray();
178             QName[] headerQNames = new QName[soapHeaderQNames.length];
179             for (int j = 0; j < soapHeaderQNames.length; j++) {
180                 XsdQNameType soapHeaderQName = soapHeaderQNames[j];
181                 headerQNames[j] = soapHeaderQName.getQNameValue();
182             }
183             Set soapRoles = new HashSet();
184             for (int j = 0; j < handler.getSoapRoleArray().length; j++) {
185                 String soapRole = handler.getSoapRoleArray(j).getStringValue().trim();
186                 soapRoles.add(soapRole);
187             }
188             HandlerInfoInfo handlerInfoInfo = new HandlerInfoInfo(new HashSet(portNames), handlerClass, config, headerQNames, soapRoles);
189             handlerInfos.add(handlerInfoInfo);
190         }
191         return handlerInfos;
192     }
193 
194 
195     private static Map mapServiceRefs(XmlObject[] refs) {
196         Map refMap = new HashMap();
197         if (refs != null) {
198             for (int i = 0; i < refs.length; i++) {
199                 GerServiceRefType ref = (GerServiceRefType) refs[i].copy().changeType(GerServiceRefType.type);
200                 String serviceRefName = ref.getServiceRefName().trim();
201                 refMap.put(serviceRefName, ref);
202             }
203         }
204         return refMap;
205     }
206 
207     public static final GBeanInfo GBEAN_INFO;
208 
209     static {
210         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(AxisServiceRefBuilder.class, NameFactory.MODULE_BUILDER);
211         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
212         infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
213         infoBuilder.addReference("AxisBuilder", AxisBuilder.class, NameFactory.MODULE_BUILDER);
214 
215         infoBuilder.setConstructor(new String[] {"defaultEnvironment", "eeNamespaces", "AxisBuilder"});
216 
217         GBEAN_INFO = infoBuilder.getBeanInfo();
218     }
219 
220     public static GBeanInfo getGBeanInfo() {
221         return GBEAN_INFO;
222     }
223 }