001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jaxws.builder;
017:
018: import java.net.URI;
019: import java.net.URISyntaxException;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.ws.Service;
026: import javax.xml.ws.handler.Handler;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.geronimo.common.DeploymentException;
031: import org.apache.geronimo.j2ee.deployment.Module;
032: import org.apache.geronimo.kernel.repository.Environment;
033: import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
034: import org.apache.geronimo.naming.deployment.ServiceRefBuilder;
035: import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefDocument;
036: import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
037: import org.apache.geronimo.xbeans.javaee.PortComponentRefType;
038: import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerChainType;
039: import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerChainsType;
040: import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerType;
041: import org.apache.geronimo.xbeans.javaee.ServiceRefType;
042:
043: import org.apache.xmlbeans.QNameSet;
044: import org.apache.xmlbeans.XmlObject;
045:
046: public abstract class JAXWSServiceRefBuilder extends
047: AbstractNamingBuilder implements ServiceRefBuilder {
048: private static final Log log = LogFactory
049: .getLog(JAXWSServiceRefBuilder.class);
050:
051: private static final QName GER_SERVICE_REF_QNAME = GerServiceRefDocument.type
052: .getDocumentElementName();
053:
054: private static final QNameSet GER_SERVICE_REF_QNAME_SET = QNameSet
055: .singleton(GER_SERVICE_REF_QNAME);
056:
057: private final QNameSet serviceRefQNameSet;
058:
059: public JAXWSServiceRefBuilder(Environment defaultEnvironment,
060: String[] eeNamespaces) {
061: super (defaultEnvironment);
062: serviceRefQNameSet = buildQNameSet(eeNamespaces, "service-ref");
063: }
064:
065: protected boolean willMergeEnvironment(XmlObject specDD,
066: XmlObject plan) {
067: return specDD.selectChildren(serviceRefQNameSet).length > 0;
068: }
069:
070: public void buildNaming(XmlObject specDD, XmlObject plan,
071: Module module, Map componentContext)
072: throws DeploymentException {
073: List<ServiceRefType> serviceRefsUntyped = convert(specDD
074: .selectChildren(serviceRefQNameSet), JEE_CONVERTER,
075: ServiceRefType.class, ServiceRefType.type);
076: XmlObject[] gerServiceRefsUntyped = plan == null ? NO_REFS
077: : plan.selectChildren(GER_SERVICE_REF_QNAME_SET);
078: Map serviceRefMap = mapServiceRefs(gerServiceRefsUntyped);
079:
080: for (ServiceRefType serviceRef : serviceRefsUntyped) {
081: String name = getStringValue(serviceRef.getServiceRefName());
082: addInjections(name, serviceRef.getInjectionTargetArray(),
083: componentContext);
084: GerServiceRefType serviceRefType = (GerServiceRefType) serviceRefMap
085: .get(name);
086: serviceRefMap.remove(name);
087: buildNaming(serviceRef, serviceRefType, module,
088: componentContext);
089: }
090:
091: if (serviceRefMap.size() > 0) {
092: log
093: .warn("Failed to build reference to service reference "
094: + serviceRefMap.keySet()
095: + " defined in plan file, reason - corresponding entry in deployment descriptor missing.");
096: }
097: }
098:
099: private Class loadClass(String className, ClassLoader cl,
100: String classDescription) throws DeploymentException {
101: try {
102: return cl.loadClass(className);
103: } catch (ClassNotFoundException e) {
104: throw new DeploymentException("Could not load "
105: + classDescription + " class " + className, e);
106: }
107: }
108:
109: public void buildNaming(XmlObject serviceRef,
110: GerServiceRefType gerServiceRefType, Module module,
111: Map componentContext) throws DeploymentException {
112: ServiceRefType serviceRefType = (ServiceRefType) convert(
113: serviceRef, JEE_CONVERTER, ServiceRefType.type);
114: buildNaming(serviceRefType, gerServiceRefType, module,
115: componentContext);
116: }
117:
118: public void buildNaming(ServiceRefType serviceRef,
119: GerServiceRefType gerServiceRef, Module module,
120: Map componentContext) throws DeploymentException {
121: ClassLoader cl = module.getEarContext().getClassLoader();
122: String name = getStringValue(serviceRef.getServiceRefName());
123:
124: String serviceInterfaceName = getStringValue(serviceRef
125: .getServiceInterface());
126: Class serviceInterfaceClass = loadClass(serviceInterfaceName,
127: cl, "service");
128: if (!Service.class.isAssignableFrom(serviceInterfaceClass)) {
129: throw new DeploymentException(serviceInterfaceName
130: + " service class does not extend "
131: + Service.class.getName());
132: }
133:
134: QName serviceQName = null;
135: if (serviceRef.isSetServiceQname()) {
136: serviceQName = serviceRef.getServiceQname().getQNameValue();
137: }
138:
139: URI wsdlURI = null;
140: if (serviceRef.isSetWsdlFile()) {
141: String wsdlLocation = serviceRef.getWsdlFile()
142: .getStringValue().trim();
143: try {
144: wsdlURI = new URI(wsdlLocation);
145: } catch (URISyntaxException e) {
146: throw new DeploymentException(
147: "Could not construct WSDL URI from "
148: + wsdlLocation, e);
149: }
150: }
151:
152: Class serviceReferenceType = null;
153: if (serviceRef.isSetServiceRefType()) {
154: String referenceClassName = getStringValue(serviceRef
155: .getServiceRefType());
156: serviceReferenceType = loadClass(referenceClassName, cl,
157: "service reference");
158: }
159:
160: if (serviceRef.isSetHandlerChains()) {
161: ServiceRefHandlerChainsType handlerChains = serviceRef
162: .getHandlerChains();
163: for (ServiceRefHandlerChainType handlerChain : handlerChains
164: .getHandlerChainArray()) {
165: for (ServiceRefHandlerType handler : handlerChain
166: .getHandlerArray()) {
167: String handlerClassName = getStringValue(handler
168: .getHandlerClass());
169: Class handlerClass = loadClass(handlerClassName,
170: cl, "handler");
171: if (!Handler.class.isAssignableFrom(handlerClass)) {
172: throw new DeploymentException(handlerClassName
173: + " handler class does not extend "
174: + Handler.class.getName());
175: }
176: }
177: }
178: }
179:
180: Map<Class, PortComponentRefType> portComponentRefMap = new HashMap<Class, PortComponentRefType>();
181: PortComponentRefType[] portComponentRefs = serviceRef
182: .getPortComponentRefArray();
183: if (portComponentRefs != null) {
184: for (int j = 0; j < portComponentRefs.length; j++) {
185: PortComponentRefType portComponentRef = portComponentRefs[j];
186: String serviceEndpointInterfaceType = getStringValue(portComponentRef
187: .getServiceEndpointInterface());
188: Class serviceEndpointClass = loadClass(
189: serviceEndpointInterfaceType, cl,
190: "service endpoint");
191:
192: // TODO: check if it is annotated?
193:
194: portComponentRefMap.put(serviceEndpointClass,
195: portComponentRef);
196: }
197: }
198:
199: Object ref = createService(serviceRef, gerServiceRef, module,
200: cl, serviceInterfaceClass, serviceQName, wsdlURI,
201: serviceReferenceType, portComponentRefMap);
202: getJndiContextMap(componentContext).put(ENV + name, ref);
203: }
204:
205: public abstract Object createService(ServiceRefType serviceRef,
206: GerServiceRefType gerServiceRef, Module module,
207: ClassLoader cl, Class serviceInterfaceClass,
208: QName serviceQName, URI wsdlURI,
209: Class serviceReferenceType,
210: Map<Class, PortComponentRefType> portComponentRefMap)
211: throws DeploymentException;
212:
213: private static Map mapServiceRefs(XmlObject[] refs) {
214: Map refMap = new HashMap();
215: if (refs != null) {
216: for (int i = 0; i < refs.length; i++) {
217: GerServiceRefType ref = (GerServiceRefType) refs[i]
218: .copy().changeType(GerServiceRefType.type);
219: String serviceRefName = ref.getServiceRefName().trim();
220: refMap.put(serviceRefName, ref);
221: }
222: }
223: return refMap;
224: }
225:
226: public QNameSet getSpecQNameSet() {
227: return serviceRefQNameSet;
228: }
229:
230: public QNameSet getPlanQNameSet() {
231: return GER_SERVICE_REF_QNAME_SET;
232: }
233:
234: }
|