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.axis2.builder;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.geronimo.jaxws.builder.EndpointInfoBuilder;
021: import org.apache.geronimo.jaxws.builder.JAXWSServiceRefBuilder;
022: import org.apache.geronimo.jaxws.client.EndpointInfo;
023: import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
024: import org.apache.geronimo.kernel.GBeanNotFoundException;
025: import org.apache.geronimo.kernel.repository.Environment;
026: import org.apache.geronimo.xbeans.javaee.PortComponentRefType;
027: import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerChainsType;
028: import org.apache.geronimo.xbeans.javaee.ServiceRefType;
029: import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
030: import org.apache.geronimo.j2ee.deployment.EARContext;
031: import org.apache.geronimo.j2ee.deployment.Module;
032: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
033: import org.apache.geronimo.axis2.client.Axis2ConfigGBean;
034: import org.apache.geronimo.axis2.client.Axis2ServiceReference;
035: import org.apache.geronimo.common.DeploymentException;
036: import org.apache.geronimo.gbean.AbstractName;
037: import org.apache.geronimo.gbean.GBeanData;
038: import org.apache.geronimo.gbean.GBeanInfo;
039: import org.apache.geronimo.gbean.GBeanInfoBuilder;
040: import org.apache.geronimo.naming.deployment.ServiceRefBuilder;
041: import org.apache.xmlbeans.XmlOptions;
042:
043: import javax.xml.namespace.QName;
044:
045: import java.io.IOException;
046: import java.io.StringWriter;
047: import java.net.URI;
048: import java.util.Map;
049:
050: public class Axis2ServiceRefBuilder extends JAXWSServiceRefBuilder {
051:
052: private static final Log log = LogFactory
053: .getLog(Axis2ServiceRefBuilder.class);
054:
055: public Axis2ServiceRefBuilder(Environment defaultEnvironment,
056: String[] eeNamespaces) {
057: super (defaultEnvironment, eeNamespaces);
058: }
059:
060: public Object createService(ServiceRefType serviceRef,
061: GerServiceRefType gerServiceRef, Module module,
062: ClassLoader cl, Class serviceInterfaceClass,
063: QName serviceQName, URI wsdlURI,
064: Class serviceReferenceType,
065: Map<Class, PortComponentRefType> portComponentRefMap)
066: throws DeploymentException {
067: registerConfigGBean(module);
068: EndpointInfoBuilder builder = new EndpointInfoBuilder(
069: serviceInterfaceClass, gerServiceRef,
070: portComponentRefMap, module.getModuleFile(), wsdlURI,
071: serviceQName);
072: builder.build();
073:
074: wsdlURI = builder.getWsdlURI();
075: serviceQName = builder.getServiceQName();
076: Map<Object, EndpointInfo> seiInfoMap = builder
077: .getEndpointInfo();
078:
079: String handlerChainsXML = null;
080: try {
081: handlerChainsXML = getHandlerChainAsString(serviceRef
082: .getHandlerChains());
083: } catch (IOException e) {
084: // this should not happen
085: log.warn("Failed to serialize handler chains", e);
086: }
087:
088: String serviceReferenceName = (serviceReferenceType == null) ? null
089: : serviceReferenceType.getName();
090: return new Axis2ServiceReference(serviceInterfaceClass
091: .getName(), serviceReferenceName, wsdlURI,
092: serviceQName, module.getModuleName(), handlerChainsXML,
093: seiInfoMap);
094: }
095:
096: private static String getHandlerChainAsString(
097: ServiceRefHandlerChainsType handlerChains)
098: throws IOException {
099: String xml = null;
100: if (handlerChains != null) {
101: StringWriter w = new StringWriter();
102: XmlOptions options = new XmlOptions();
103: options.setSaveSyntheticDocumentElement(new QName(
104: "http://java.sun.com/xml/ns/javaee",
105: "handler-chains"));
106: handlerChains.save(w, options);
107: xml = w.toString();
108: }
109: return xml;
110: }
111:
112: private void registerConfigGBean(Module module)
113: throws DeploymentException {
114: EARContext context = module.getEarContext();
115: AbstractName containerFactoryName = context.getNaming()
116: .createChildName(module.getModuleName(),
117: Axis2ConfigGBean.GBEAN_INFO.getName(),
118: NameFactory.GERONIMO_SERVICE);
119:
120: try {
121: context.getGBeanInstance(containerFactoryName);
122: } catch (GBeanNotFoundException e1) {
123: GBeanData configGBeanData = new GBeanData(
124: containerFactoryName, Axis2ConfigGBean.GBEAN_INFO);
125: configGBeanData.setAttribute("moduleName", module
126: .getModuleName());
127:
128: try {
129: context.addGBean(configGBeanData);
130: } catch (GBeanAlreadyExistsException e) {
131: throw new DeploymentException(
132: "Could not add config gbean", e);
133: }
134: }
135: }
136:
137: public static final GBeanInfo GBEAN_INFO;
138:
139: static {
140: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
141: Axis2ServiceRefBuilder.class,
142: NameFactory.MODULE_BUILDER);
143: infoBuilder.addInterface(ServiceRefBuilder.class);
144: infoBuilder.addAttribute("defaultEnvironment",
145: Environment.class, true, true);
146: infoBuilder.addAttribute("eeNamespaces", String[].class, true,
147: true);
148:
149: infoBuilder.setConstructor(new String[] { "defaultEnvironment",
150: "eeNamespaces" });
151:
152: GBEAN_INFO = infoBuilder.getBeanInfo();
153: }
154:
155: public static GBeanInfo getGBeanInfo() {
156: return Axis2ServiceRefBuilder.GBEAN_INFO;
157: }
158: }
|