KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > builder > AxisServiceRefBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.axis.builder;
19
20 import java.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.apache.geronimo.common.DeploymentException;
32 import org.apache.geronimo.gbean.GBeanInfo;
33 import org.apache.geronimo.gbean.GBeanInfoBuilder;
34 import org.apache.geronimo.j2ee.deployment.HandlerInfoInfo;
35 import org.apache.geronimo.j2ee.deployment.Module;
36 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
37 import org.apache.geronimo.kernel.ClassLoading;
38 import org.apache.geronimo.kernel.config.Configuration;
39 import org.apache.geronimo.kernel.repository.Environment;
40 import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
41 import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefDocument;
42 import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
43 import org.apache.geronimo.xbeans.j2ee.ParamValueType;
44 import org.apache.geronimo.xbeans.j2ee.PortComponentRefType;
45 import org.apache.geronimo.xbeans.j2ee.ServiceRefHandlerType;
46 import org.apache.geronimo.xbeans.j2ee.ServiceRefType;
47 import org.apache.geronimo.xbeans.j2ee.XsdQNameType;
48 import org.apache.xmlbeans.QNameSet;
49 import org.apache.xmlbeans.XmlObject;
50
51 /**
52  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
53  */

54 public class AxisServiceRefBuilder extends AbstractNamingBuilder {
55     private final QNameSet serviceRefQNameSet;
56     private static final QName JavaDoc GER_SERVICE_REF_QNAME = GerServiceRefDocument.type.getDocumentElementName();
57     private static final QNameSet GER_SERVICE_REF_QNAME_SET = QNameSet.singleton(GER_SERVICE_REF_QNAME);
58
59     private final AxisBuilder axisBuilder;
60
61     public AxisServiceRefBuilder(Environment defaultEnvironment, String JavaDoc[] eeNamespaces, AxisBuilder axisBuilder) {
62         super(defaultEnvironment);
63         this.axisBuilder = axisBuilder;
64         serviceRefQNameSet = buildQNameSet(eeNamespaces, "service-ref");
65     }
66
67     protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) {
68         return specDD.selectChildren(serviceRefQNameSet).length > 0;
69     }
70     
71     public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map JavaDoc componentContext) throws DeploymentException {
72         XmlObject[] serviceRefsUntyped = convert(specDD.selectChildren(serviceRefQNameSet), J2EE_CONVERTER, ServiceRefType.type);
73         XmlObject[] gerServiceRefsUntyped = plan == null? NO_REFS: plan.selectChildren(GER_SERVICE_REF_QNAME_SET);
74         Map JavaDoc serviceRefMap = mapServiceRefs(gerServiceRefsUntyped);
75         ClassLoader JavaDoc cl = module.getEarContext().getClassLoader();
76
77         for (int i = 0; i < serviceRefsUntyped.length; i++) {
78             ServiceRefType serviceRef = (ServiceRefType) serviceRefsUntyped[i];
79             String JavaDoc name = getStringValue(serviceRef.getServiceRefName());
80             GerServiceRefType serviceRefType = (GerServiceRefType) serviceRefMap.get(name);
81 // Map credentialsNameMap = (Map) serviceRefCredentialsNameMap.get(name);
82
String JavaDoc serviceInterfaceName = getStringValue(serviceRef.getServiceInterface());
83             assureInterface(serviceInterfaceName, "javax.xml.rpc.Service", "[Web]Service", cl);
84             Class JavaDoc serviceInterface;
85             try {
86                 serviceInterface = cl.loadClass(serviceInterfaceName);
87             } catch (ClassNotFoundException JavaDoc e) {
88                 throw new DeploymentException("Could not load service interface class: " + serviceInterfaceName, e);
89             }
90             URI JavaDoc wsdlURI = null;
91             if (serviceRef.isSetWsdlFile()) {
92                 try {
93                     wsdlURI = new URI JavaDoc(serviceRef.getWsdlFile().getStringValue().trim());
94                 } catch (URISyntaxException JavaDoc e) {
95                     throw new DeploymentException("could not construct wsdl uri from " + serviceRef.getWsdlFile().getStringValue(), e);
96                 }
97             }
98             URI JavaDoc jaxrpcMappingURI = null;
99             if (serviceRef.isSetJaxrpcMappingFile()) {
100                 try {
101                     jaxrpcMappingURI = new URI JavaDoc(getStringValue(serviceRef.getJaxrpcMappingFile()));
102                 } catch (URISyntaxException JavaDoc e) {
103                     throw new DeploymentException("Could not construct jaxrpc mapping uri from " + serviceRef.getJaxrpcMappingFile(), e);
104                 }
105             }
106             QName JavaDoc serviceQName = null;
107             if (serviceRef.isSetServiceQname()) {
108                 serviceQName = serviceRef.getServiceQname().getQNameValue();
109             }
110             Map JavaDoc portComponentRefMap = new HashMap JavaDoc();
111             PortComponentRefType[] portComponentRefs = serviceRef.getPortComponentRefArray();
112             if (portComponentRefs != null) {
113                 for (int j = 0; j < portComponentRefs.length; j++) {
114                     PortComponentRefType portComponentRef = portComponentRefs[j];
115                     String JavaDoc portComponentLink = getStringValue(portComponentRef.getPortComponentLink());
116                     String JavaDoc serviceEndpointInterfaceType = getStringValue(portComponentRef.getServiceEndpointInterface());
117                     assureInterface(serviceEndpointInterfaceType, "java.rmi.Remote", "ServiceEndpoint", cl);
118                     Class JavaDoc serviceEndpointClass;
119                     try {
120                         serviceEndpointClass = cl.loadClass(serviceEndpointInterfaceType);
121                     } catch (ClassNotFoundException JavaDoc e) {
122                         throw new DeploymentException("could not load service endpoint class " + serviceEndpointInterfaceType, e);
123                     }
124                     portComponentRefMap.put(serviceEndpointClass, portComponentLink);
125                 }
126             }
127             ServiceRefHandlerType[] handlers = serviceRef.getHandlerArray();
128             List JavaDoc handlerInfos = buildHandlerInfoList(handlers, cl);
129
130 //we could get a Reference or the actual serializable Service back.
131
Object JavaDoc ref = axisBuilder.createService(serviceInterface, wsdlURI, jaxrpcMappingURI, serviceQName, portComponentRefMap, handlerInfos, serviceRefType, module, cl);
132             getJndiContextMap(componentContext).put(ENV + name, ref);
133         }
134
135     }
136
137     public QNameSet getSpecQNameSet() {
138         return serviceRefQNameSet;
139     }
140
141     public QNameSet getPlanQNameSet() {
142         return GER_SERVICE_REF_QNAME_SET;
143     }
144
145
146     private static List JavaDoc buildHandlerInfoList(ServiceRefHandlerType[] handlers, ClassLoader JavaDoc classLoader) throws DeploymentException {
147         List JavaDoc handlerInfos = new ArrayList JavaDoc();
148         for (int i = 0; i < handlers.length; i++) {
149             ServiceRefHandlerType handler = handlers[i];
150             org.apache.geronimo.xbeans.j2ee.String[] portNameArray = handler.getPortNameArray();
151             List JavaDoc portNames = new ArrayList JavaDoc();
152             for (int j = 0; j < portNameArray.length; j++) {
153                 portNames.add(portNameArray[j].getStringValue().trim());
154
155             }
156 // Set portNames = new HashSet(Arrays.asList(portNameArray));
157
String JavaDoc handlerClassName = handler.getHandlerClass().getStringValue().trim();
158             Class JavaDoc handlerClass;
159             try {
160                 handlerClass = ClassLoading.loadClass(handlerClassName, classLoader);
161             } catch (ClassNotFoundException JavaDoc e) {
162                 throw new DeploymentException("Could not load handler class", e);
163             }
164             Map JavaDoc config = new HashMap JavaDoc();
165             ParamValueType[] paramValues = handler.getInitParamArray();
166             for (int j = 0; j < paramValues.length; j++) {
167                 ParamValueType paramValue = paramValues[j];
168                 String JavaDoc paramName = paramValue.getParamName().getStringValue().trim();
169                 String JavaDoc paramStringValue = paramValue.getParamValue().getStringValue().trim();
170                 config.put(paramName, paramStringValue);
171             }
172             XsdQNameType[] soapHeaderQNames = handler.getSoapHeaderArray();
173             QName JavaDoc[] headerQNames = new QName JavaDoc[soapHeaderQNames.length];
174             for (int j = 0; j < soapHeaderQNames.length; j++) {
175                 XsdQNameType soapHeaderQName = soapHeaderQNames[j];
176                 headerQNames[j] = soapHeaderQName.getQNameValue();
177             }
178             Set JavaDoc soapRoles = new HashSet JavaDoc();
179             for (int j = 0; j < handler.getSoapRoleArray().length; j++) {
180                 String JavaDoc soapRole = handler.getSoapRoleArray(j).getStringValue().trim();
181                 soapRoles.add(soapRole);
182             }
183             HandlerInfoInfo handlerInfoInfo = new HandlerInfoInfo(new HashSet JavaDoc(portNames), handlerClass, config, headerQNames, soapRoles);
184             handlerInfos.add(handlerInfoInfo);
185         }
186         return handlerInfos;
187     }
188
189
190     private static Map JavaDoc mapServiceRefs(XmlObject[] refs) {
191         Map JavaDoc refMap = new HashMap JavaDoc();
192         if (refs != null) {
193             for (int i = 0; i < refs.length; i++) {
194                 GerServiceRefType ref = (GerServiceRefType) refs[i].copy().changeType(GerServiceRefType.type);
195                 String JavaDoc serviceRefName = ref.getServiceRefName().trim();
196                 refMap.put(serviceRefName, ref);
197             }
198         }
199         return refMap;
200     }
201
202     public static final GBeanInfo GBEAN_INFO;
203
204     static {
205         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(AxisServiceRefBuilder.class, NameFactory.MODULE_BUILDER);
206         infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
207         infoBuilder.addAttribute("eeNamespaces", String JavaDoc[].class, true, true);
208         infoBuilder.addReference("AxisBuilder", AxisBuilder.class, NameFactory.MODULE_BUILDER);
209
210         infoBuilder.setConstructor(new String JavaDoc[] {"defaultEnvironment", "eeNamespaces", "AxisBuilder"});
211
212         GBEAN_INFO = infoBuilder.getBeanInfo();
213     }
214
215     public static GBeanInfo getGBeanInfo() {
216         return GBEAN_INFO;
217     }
218 }
219
Popular Tags