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.axis.builder;
017:
018: import java.net.URI;
019: import java.net.URISyntaxException;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.geronimo.common.DeploymentException;
032: import org.apache.geronimo.gbean.GBeanInfo;
033: import org.apache.geronimo.gbean.GBeanInfoBuilder;
034: import org.apache.geronimo.j2ee.deployment.HandlerInfoInfo;
035: import org.apache.geronimo.j2ee.deployment.Module;
036: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
037: import org.apache.geronimo.kernel.ClassLoading;
038: import org.apache.geronimo.kernel.repository.Environment;
039: import org.apache.geronimo.naming.deployment.AbstractNamingBuilder;
040: import org.apache.geronimo.naming.deployment.ServiceRefBuilder;
041: import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefDocument;
042: import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
043: import org.apache.geronimo.xbeans.javaee.ParamValueType;
044: import org.apache.geronimo.xbeans.javaee.PortComponentRefType;
045: import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerType;
046: import org.apache.geronimo.xbeans.javaee.ServiceRefType;
047: import org.apache.geronimo.xbeans.javaee.XsdQNameType;
048: import org.apache.xmlbeans.QNameSet;
049: import org.apache.xmlbeans.XmlObject;
050:
051: /**
052: * @version $Rev: 539548 $ $Date: 2007-05-18 11:29:40 -0700 (Fri, 18 May 2007) $
053: */
054: public class AxisServiceRefBuilder extends AbstractNamingBuilder
055: implements ServiceRefBuilder {
056: private static final Log log = LogFactory
057: .getLog(AxisServiceRefBuilder.class);
058: private final QNameSet serviceRefQNameSet;
059: private static final QName GER_SERVICE_REF_QNAME = GerServiceRefDocument.type
060: .getDocumentElementName();
061: private static final QNameSet GER_SERVICE_REF_QNAME_SET = QNameSet
062: .singleton(GER_SERVICE_REF_QNAME);
063:
064: private final AxisBuilder axisBuilder;
065:
066: public AxisServiceRefBuilder(Environment defaultEnvironment,
067: String[] eeNamespaces, AxisBuilder axisBuilder) {
068: super (defaultEnvironment);
069: this .axisBuilder = axisBuilder;
070: serviceRefQNameSet = buildQNameSet(eeNamespaces, "service-ref");
071: }
072:
073: protected boolean willMergeEnvironment(XmlObject specDD,
074: XmlObject plan) {
075: return specDD.selectChildren(serviceRefQNameSet).length > 0;
076: }
077:
078: public void buildNaming(XmlObject specDD, XmlObject plan,
079: Module module, Map componentContext)
080: throws DeploymentException {
081: List<ServiceRefType> serviceRefsUntyped = convert(specDD
082: .selectChildren(serviceRefQNameSet), JEE_CONVERTER,
083: ServiceRefType.class, ServiceRefType.type);
084: XmlObject[] gerServiceRefsUntyped = plan == null ? NO_REFS
085: : plan.selectChildren(GER_SERVICE_REF_QNAME_SET);
086: Map serviceRefMap = mapServiceRefs(gerServiceRefsUntyped);
087:
088: for (ServiceRefType serviceRef : serviceRefsUntyped) {
089: String name = getStringValue(serviceRef.getServiceRefName());
090: addInjections(name, serviceRef.getInjectionTargetArray(),
091: componentContext);
092: GerServiceRefType serviceRefType = (GerServiceRefType) serviceRefMap
093: .get(name);
094: serviceRefMap.remove(name);
095: buildNaming(serviceRef, serviceRefType, module,
096: componentContext);
097: }
098:
099: if (serviceRefMap.size() > 0) {
100: log
101: .warn("Failed to build reference to service reference "
102: + serviceRefMap.keySet()
103: + " defined in plan file, reason - corresponding entry in deployment descriptor missing.");
104: }
105: }
106:
107: public void buildNaming(XmlObject serviceRef,
108: GerServiceRefType gerServiceRefType, Module module,
109: Map componentContext) throws DeploymentException {
110: ServiceRefType serviceRefType = (ServiceRefType) convert(
111: serviceRef, JEE_CONVERTER, ServiceRefType.type);
112: buildNaming(serviceRefType, gerServiceRefType, module,
113: componentContext);
114: }
115:
116: private void buildNaming(ServiceRefType serviceRef,
117: GerServiceRefType serviceRefType, Module module,
118: Map componentContext) throws DeploymentException {
119: String name = getStringValue(serviceRef.getServiceRefName());
120: ClassLoader cl = module.getEarContext().getClassLoader();
121:
122: // Map credentialsNameMap = (Map) serviceRefCredentialsNameMap.get(name);
123: String serviceInterfaceName = getStringValue(serviceRef
124: .getServiceInterface());
125: assureInterface(serviceInterfaceName, "javax.xml.rpc.Service",
126: "[Web]Service", cl);
127: Class serviceInterface;
128: try {
129: serviceInterface = cl.loadClass(serviceInterfaceName);
130: } catch (ClassNotFoundException e) {
131: throw new DeploymentException(
132: "Could not load service interface class: "
133: + serviceInterfaceName, e);
134: }
135: URI wsdlURI = null;
136: if (serviceRef.isSetWsdlFile()) {
137: try {
138: wsdlURI = new URI(serviceRef.getWsdlFile()
139: .getStringValue().trim());
140: } catch (URISyntaxException e) {
141: throw new DeploymentException(
142: "could not construct wsdl uri from "
143: + serviceRef.getWsdlFile()
144: .getStringValue(), e);
145: }
146: }
147: URI jaxrpcMappingURI = null;
148: if (serviceRef.isSetJaxrpcMappingFile()) {
149: try {
150: jaxrpcMappingURI = new URI(getStringValue(serviceRef
151: .getJaxrpcMappingFile()));
152: } catch (URISyntaxException e) {
153: throw new DeploymentException(
154: "Could not construct jaxrpc mapping uri from "
155: + serviceRef.getJaxrpcMappingFile(), e);
156: }
157: }
158: QName serviceQName = null;
159: if (serviceRef.isSetServiceQname()) {
160: serviceQName = serviceRef.getServiceQname().getQNameValue();
161: }
162: Map portComponentRefMap = new HashMap();
163: PortComponentRefType[] portComponentRefs = serviceRef
164: .getPortComponentRefArray();
165: if (portComponentRefs != null) {
166: for (int j = 0; j < portComponentRefs.length; j++) {
167: PortComponentRefType portComponentRef = portComponentRefs[j];
168: String portComponentLink = getStringValue(portComponentRef
169: .getPortComponentLink());
170: String serviceEndpointInterfaceType = getStringValue(portComponentRef
171: .getServiceEndpointInterface());
172: assureInterface(serviceEndpointInterfaceType,
173: "java.rmi.Remote", "ServiceEndpoint", cl);
174: Class serviceEndpointClass;
175: try {
176: serviceEndpointClass = cl
177: .loadClass(serviceEndpointInterfaceType);
178: } catch (ClassNotFoundException e) {
179: throw new DeploymentException(
180: "could not load service endpoint class "
181: + serviceEndpointInterfaceType, e);
182: }
183: portComponentRefMap.put(serviceEndpointClass,
184: portComponentLink);
185: }
186: }
187: ServiceRefHandlerType[] handlers = serviceRef.getHandlerArray();
188: List handlerInfos = buildHandlerInfoList(handlers, cl);
189:
190: //we could get a Reference or the actual serializable Service back.
191: Object ref = axisBuilder.createService(serviceInterface,
192: wsdlURI, jaxrpcMappingURI, serviceQName,
193: portComponentRefMap, handlerInfos, serviceRefType,
194: module, cl);
195: getJndiContextMap(componentContext).put(ENV + name, ref);
196: }
197:
198: public QNameSet getSpecQNameSet() {
199: return serviceRefQNameSet;
200: }
201:
202: public QNameSet getPlanQNameSet() {
203: return GER_SERVICE_REF_QNAME_SET;
204: }
205:
206: private static List buildHandlerInfoList(
207: ServiceRefHandlerType[] handlers, ClassLoader classLoader)
208: throws DeploymentException {
209: List handlerInfos = new ArrayList();
210: for (int i = 0; i < handlers.length; i++) {
211: ServiceRefHandlerType handler = handlers[i];
212: org.apache.geronimo.xbeans.javaee.String[] portNameArray = handler
213: .getPortNameArray();
214: List portNames = new ArrayList();
215: for (int j = 0; j < portNameArray.length; j++) {
216: portNames.add(portNameArray[j].getStringValue().trim());
217:
218: }
219: // Set portNames = new HashSet(Arrays.asList(portNameArray));
220: String handlerClassName = handler.getHandlerClass()
221: .getStringValue().trim();
222: Class handlerClass;
223: try {
224: handlerClass = ClassLoading.loadClass(handlerClassName,
225: classLoader);
226: } catch (ClassNotFoundException e) {
227: throw new DeploymentException(
228: "Could not load handler class", e);
229: }
230: Map config = new HashMap();
231: ParamValueType[] paramValues = handler.getInitParamArray();
232: for (int j = 0; j < paramValues.length; j++) {
233: ParamValueType paramValue = paramValues[j];
234: String paramName = paramValue.getParamName()
235: .getStringValue().trim();
236: String paramStringValue = paramValue.getParamValue()
237: .getStringValue().trim();
238: config.put(paramName, paramStringValue);
239: }
240: XsdQNameType[] soapHeaderQNames = handler
241: .getSoapHeaderArray();
242: QName[] headerQNames = new QName[soapHeaderQNames.length];
243: for (int j = 0; j < soapHeaderQNames.length; j++) {
244: XsdQNameType soapHeaderQName = soapHeaderQNames[j];
245: headerQNames[j] = soapHeaderQName.getQNameValue();
246: }
247: Set soapRoles = new HashSet();
248: for (int j = 0; j < handler.getSoapRoleArray().length; j++) {
249: String soapRole = handler.getSoapRoleArray(j)
250: .getStringValue().trim();
251: soapRoles.add(soapRole);
252: }
253: HandlerInfoInfo handlerInfoInfo = new HandlerInfoInfo(
254: new HashSet(portNames), handlerClass, config,
255: headerQNames, soapRoles);
256: handlerInfos.add(handlerInfoInfo);
257: }
258: return handlerInfos;
259: }
260:
261: private static Map mapServiceRefs(XmlObject[] refs) {
262: Map refMap = new HashMap();
263: if (refs != null) {
264: for (int i = 0; i < refs.length; i++) {
265: GerServiceRefType ref = (GerServiceRefType) refs[i]
266: .copy().changeType(GerServiceRefType.type);
267: String serviceRefName = ref.getServiceRefName().trim();
268: refMap.put(serviceRefName, ref);
269: }
270: }
271: return refMap;
272: }
273:
274: // This is temporary
275: private static String getStringValue(
276: org.apache.geronimo.xbeans.j2ee.String string) {
277: if (string == null) {
278: return null;
279: }
280: String s = string.getStringValue();
281: return s == null ? null : s.trim();
282: }
283:
284: public static final GBeanInfo GBEAN_INFO;
285:
286: static {
287: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder
288: .createStatic(AxisServiceRefBuilder.class,
289: NameFactory.MODULE_BUILDER);
290: infoBuilder.addInterface(ServiceRefBuilder.class);
291: infoBuilder.addAttribute("defaultEnvironment",
292: Environment.class, true, true);
293: infoBuilder.addAttribute("eeNamespaces", String[].class, true,
294: true);
295: infoBuilder.addReference("AxisBuilder", AxisBuilder.class,
296: NameFactory.MODULE_BUILDER);
297:
298: infoBuilder.setConstructor(new String[] { "defaultEnvironment",
299: "eeNamespaces", "AxisBuilder" });
300:
301: GBEAN_INFO = infoBuilder.getBeanInfo();
302: }
303:
304: public static GBeanInfo getGBeanInfo() {
305: return GBEAN_INFO;
306: }
307: }
|