1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 package org.apache.geronimo.axis.client;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.Serializable;
22 import java.net.URL;
23 import java.rmi.Remote;
24 import java.util.Iterator;
25 import java.util.Map;
26 import javax.xml.namespace.QName;
27 import javax.xml.rpc.Call;
28 import javax.xml.rpc.ServiceException;
29 import javax.xml.rpc.encoding.TypeMappingRegistry;
30 import javax.xml.rpc.handler.HandlerRegistry;
31
32 import org.apache.axis.SimpleTargetedChain;
33 import org.apache.axis.client.Service;
34 import org.apache.axis.configuration.SimpleProvider;
35 import org.apache.axis.encoding.TypeMappingRegistryImpl;
36 import org.apache.axis.transport.http.HTTPSender;
37
38
39 /**
40 * @version $Revision: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
41 */
42 public class ServiceImpl implements javax.xml.rpc.Service, Serializable {
43 private static final long serialVersionUID = 8657993237680414470L;
44
45 private transient Service delegate;
46 private final Map seiClassNameToFactoryMap;
47 private final Map portToImplementationMap;
48
49 public ServiceImpl(Map portToImplementationMap, Map seiClassNameToFactoryMap) {
50 this.portToImplementationMap = portToImplementationMap;
51 this.seiClassNameToFactoryMap = seiClassNameToFactoryMap;
52 buildDelegateService();
53 }
54
55 private void buildDelegateService() {
56 TypeMappingRegistryImpl typeMappingRegistry = new TypeMappingRegistryImpl();
57 typeMappingRegistry.doRegisterFromVersion("1.3");
58
59 SimpleProvider engineConfiguration = new SimpleProvider(typeMappingRegistry);
60 engineConfiguration.deployTransport("http", new SimpleTargetedChain(new HTTPSender()));
61
62 GeronimoAxisClient engine = new GeronimoAxisClient(engineConfiguration, portToImplementationMap);
63
64 delegate = new Service(engineConfiguration, engine);
65 }
66
67 public Remote getPort(QName qName, Class portClass) throws ServiceException {
68 if (qName != null) {
69 String portName = qName.getLocalPart();
70 Remote port = internalGetPort(portName);
71 return port;
72 }
73 return getPort(portClass);
74 }
75
76 public Remote getPort(Class portClass) throws ServiceException {
77 String fqcn = portClass.getName();
78 Remote port = internalGetPortFromClassName(fqcn);
79 return port;
80 }
81
82 public Call[] getCalls(QName portName) throws ServiceException {
83
84 if (portName == null)
85 throw new ServiceException("Portname cannot be null");
86
87 SEIFactory factory = (SEIFactory) portToImplementationMap.get(portName.getLocalPart());
88 if( factory == null )
89 throw new ServiceException("No port for portname: " + portName);
90
91 OperationInfo[] operationInfos = factory.getOperationInfos();
92 javax.xml.rpc.Call[] array = new javax.xml.rpc.Call[operationInfos.length];
93 for (int i = 0; i < operationInfos.length; i++) {
94 OperationInfo operation = operationInfos[i];
95 array[i] = delegate.createCall(factory.getPortQName(), operation.getOperationName());
96 }
97 return array;
98 }
99
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
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 }