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     */
017    package org.apache.geronimo.axis.client;
018    
019    import java.io.IOException;
020    import java.io.ObjectInputStream;
021    import java.io.Serializable;
022    import java.net.URL;
023    import java.rmi.Remote;
024    import java.util.Iterator;
025    import java.util.Map;
026    import javax.xml.namespace.QName;
027    import javax.xml.rpc.Call;
028    import javax.xml.rpc.ServiceException;
029    import javax.xml.rpc.encoding.TypeMappingRegistry;
030    import javax.xml.rpc.handler.HandlerRegistry;
031    
032    import org.apache.axis.SimpleTargetedChain;
033    import org.apache.axis.client.Service;
034    import org.apache.axis.configuration.SimpleProvider;
035    import org.apache.axis.encoding.TypeMappingRegistryImpl;
036    import org.apache.axis.transport.http.HTTPSender;
037    
038    
039    /**
040     * @version $Revision: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
041     */
042    public class ServiceImpl implements javax.xml.rpc.Service, Serializable {
043        private static final long serialVersionUID = 8657993237680414470L;
044    
045        private transient Service delegate;
046        private final Map seiClassNameToFactoryMap;
047        private final Map portToImplementationMap;
048    
049        public ServiceImpl(Map portToImplementationMap, Map seiClassNameToFactoryMap) {
050            this.portToImplementationMap = portToImplementationMap;
051            this.seiClassNameToFactoryMap = seiClassNameToFactoryMap;
052            buildDelegateService();
053        }
054    
055        private void buildDelegateService() {
056            TypeMappingRegistryImpl typeMappingRegistry = new TypeMappingRegistryImpl();
057            typeMappingRegistry.doRegisterFromVersion("1.3");
058    
059            SimpleProvider engineConfiguration = new SimpleProvider(typeMappingRegistry);
060            engineConfiguration.deployTransport("http", new SimpleTargetedChain(new HTTPSender()));
061    
062            GeronimoAxisClient engine = new GeronimoAxisClient(engineConfiguration, portToImplementationMap);
063    
064            delegate = new Service(engineConfiguration, engine);
065        }
066    
067        public Remote getPort(QName qName, Class portClass) throws ServiceException {
068            if (qName != null) {
069                String portName = qName.getLocalPart();
070                Remote port = internalGetPort(portName);
071                return port;
072            }
073            return getPort(portClass);
074        }
075    
076        public Remote getPort(Class portClass) throws ServiceException {
077            String fqcn = portClass.getName();
078            Remote port = internalGetPortFromClassName(fqcn);
079            return port;
080        }
081    
082        public Call[] getCalls(QName portName) throws ServiceException {
083    
084            if (portName == null)
085                throw new ServiceException("Portname cannot be null");
086    
087            SEIFactory factory = (SEIFactory) portToImplementationMap.get(portName.getLocalPart());
088            if( factory == null )
089                throw new ServiceException("No port for portname: " + portName);
090    
091            OperationInfo[] operationInfos = factory.getOperationInfos();
092            javax.xml.rpc.Call[] array = new javax.xml.rpc.Call[operationInfos.length];
093            for (int i = 0; i < operationInfos.length; i++) {
094                OperationInfo operation = operationInfos[i];
095                array[i] = delegate.createCall(factory.getPortQName(), operation.getOperationName());
096            }
097            return array;
098        }
099    
100        public Call createCall(QName qName) throws ServiceException {
101            return delegate.createCall(qName);
102        }
103    
104        public Call createCall(QName qName, QName qName1) throws ServiceException {
105            return delegate.createCall(qName, qName1);
106        }
107    
108        public Call createCall(QName qName, String s) throws ServiceException {
109            return delegate.createCall(qName, s);
110        }
111    
112        public Call createCall() throws ServiceException {
113            return delegate.createCall();
114        }
115    
116        public QName getServiceName() {
117            Iterator iterator = portToImplementationMap.values().iterator();
118            if( !iterator.hasNext() )
119                return null;
120            SEIFactory factory = (SEIFactory)iterator.next();
121            return factory.getServiceName();
122        }
123    
124        public Iterator getPorts() throws ServiceException {
125            return portToImplementationMap.values().iterator();
126        }
127    
128        public URL getWSDLDocumentLocation() {
129            Iterator iterator = portToImplementationMap.values().iterator();
130            if( !iterator.hasNext() )
131                return null;
132            SEIFactory factory = (SEIFactory)iterator.next();
133            return factory.getWSDLDocumentLocation();
134        }
135    
136        public TypeMappingRegistry getTypeMappingRegistry() {
137            throw new UnsupportedOperationException();
138            //return delegate.getTypeMappingRegistry();
139        }
140    
141        public HandlerRegistry getHandlerRegistry() {
142            throw new UnsupportedOperationException();
143        }
144    
145        Remote internalGetPort(String portName) throws ServiceException {
146            if (portToImplementationMap.containsKey(portName)) {
147                SEIFactory seiFactory = (SEIFactory) portToImplementationMap.get(portName);
148                Remote port = seiFactory.createServiceEndpoint();
149                return port;
150            }
151            throw new ServiceException("No port for portname: " + portName);
152        }
153    
154        Remote internalGetPortFromClassName(String className) throws ServiceException {
155            if (seiClassNameToFactoryMap.containsKey(className)) {
156                SEIFactory seiFactory = (SEIFactory) seiClassNameToFactoryMap.get(className);
157                Remote port = seiFactory.createServiceEndpoint();
158                return port;
159            }
160            throw new ServiceException("no port for class " + className);
161        }
162    
163        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
164            in.defaultReadObject();
165            buildDelegateService();
166        }
167    
168        Service getService() {
169            return delegate;
170        }
171    }