Source code: org/apache/axis/configuration/SimpleProvider.java
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.axis.configuration;
18
19 import org.apache.axis.AxisEngine;
20 import org.apache.axis.ConfigurationException;
21 import org.apache.axis.EngineConfiguration;
22 import org.apache.axis.Handler;
23 import org.apache.axis.encoding.TypeMapping;
24 import org.apache.axis.encoding.TypeMappingRegistry;
25 import org.apache.axis.encoding.TypeMappingRegistryImpl;
26 import org.apache.axis.handlers.soap.SOAPService;
27
28 import javax.xml.namespace.QName;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Hashtable;
32 import java.util.Iterator;
33 import java.util.List;
34
35 /**
36 * A SimpleProvider is an EngineConfiguration which contains a simple
37 * HashMap-based registry of Handlers, Transports, and Services. This is
38 * for when you want to programatically deploy components which you create.
39 *
40 * SimpleProvider may also optionally contain a reference to a "default"
41 * EngineConfiguration, which will be scanned for components not found in
42 * the internal registry. This is handy when you want to start with a base
43 * configuration (like the default WSDD) and then quickly add stuff without
44 * changing the WSDD document.
45 *
46 * @author Glen Daniels (gdaniels@apache.org)
47 */
48 public class SimpleProvider implements EngineConfiguration
49 {
50 /** Handler registry */
51 HashMap handlers = new HashMap();
52 /** Transport registry */
53 HashMap transports = new HashMap();
54 /** Service registry */
55 HashMap services = new HashMap();
56
57 /** Global configuration stuff */
58 Hashtable globalOptions = null;
59 Handler globalRequest = null;
60 Handler globalResponse = null;
61 List roles = new ArrayList();
62
63 /** Our TypeMappingRegistry */
64 TypeMappingRegistry tmr = null;
65
66 /** An optional "default" EngineConfiguration */
67 EngineConfiguration defaultConfiguration = null;
68 private AxisEngine engine;
69
70 /**
71 * Default constructor.
72 */
73 public SimpleProvider() {
74 }
75
76 /**
77 * Constructor which takes an EngineConfiguration which will be used
78 * as the default.
79 */
80 public SimpleProvider(EngineConfiguration defaultConfiguration) {
81 this.defaultConfiguration = defaultConfiguration;
82 }
83
84 /**
85 * Construct a SimpleProvider using the supplied TypeMappingRegistry.
86 *
87 * @param typeMappingRegistry
88 */
89 public SimpleProvider(TypeMappingRegistry typeMappingRegistry) {
90 this.tmr = typeMappingRegistry;
91 }
92
93 /**
94 * Configure an AxisEngine. Right now just calls the default
95 * configuration if there is one, since we don't do anything special.
96 */
97 public void configureEngine(AxisEngine engine) throws ConfigurationException
98 {
99 this.engine = engine;
100
101 if (defaultConfiguration != null)
102 defaultConfiguration.configureEngine(engine);
103
104 for (Iterator i = services.values().iterator(); i.hasNext(); ) {
105 ((SOAPService)i.next()).setEngine(engine);
106 }
107 }
108
109 /**
110 * We don't write ourselves out, so this is a noop.
111 */
112 public void writeEngineConfig(AxisEngine engine) throws ConfigurationException
113 {
114 }
115
116 /**
117 * Returns the global configuration options.
118 */
119 public Hashtable getGlobalOptions() throws ConfigurationException {
120 if (globalOptions != null)
121 return globalOptions;
122
123 if (defaultConfiguration != null)
124 return defaultConfiguration.getGlobalOptions();
125
126 return null;
127 }
128
129 /**
130 * Set the global options Hashtable
131 *
132 * @param options
133 */
134 public void setGlobalOptions(Hashtable options) {
135 globalOptions = options;
136 }
137
138 /**
139 * Returns a global request handler.
140 */
141 public Handler getGlobalRequest() throws ConfigurationException {
142 if (globalRequest != null)
143 return globalRequest;
144
145 if (defaultConfiguration != null)
146 return defaultConfiguration.getGlobalRequest();
147
148 return null;
149 }
150
151 /**
152 * Set the global request Handler
153 *
154 * @param globalRequest
155 */
156 public void setGlobalRequest(Handler globalRequest) {
157 this.globalRequest = globalRequest;
158 }
159
160 /**
161 * Returns a global response handler.
162 */
163 public Handler getGlobalResponse() throws ConfigurationException {
164 if (globalResponse != null)
165 return globalResponse;
166
167 if (defaultConfiguration != null)
168 return defaultConfiguration.getGlobalResponse();
169
170 return null;
171 }
172
173 /**
174 * Set the global response Handler
175 *
176 * @param globalResponse
177 */
178 public void setGlobalResponse(Handler globalResponse) {
179 this.globalResponse = globalResponse;
180 }
181
182 /**
183 * Get our TypeMappingRegistry. Returns our specific one if we have
184 * one, otherwise the one from our defaultConfiguration. If we don't
185 * have one and also don't have a defaultConfiguration, we create one.
186 *
187 */
188 public TypeMappingRegistry getTypeMappingRegistry() throws ConfigurationException {
189 if (tmr != null)
190 return tmr;
191
192 if (defaultConfiguration != null)
193 return defaultConfiguration.getTypeMappingRegistry();
194
195 // No default config, but we need a TypeMappingRegistry...
196 // (perhaps the TMRs could just be chained?)
197 tmr = new TypeMappingRegistryImpl();
198 return tmr;
199 }
200
201 public TypeMapping getTypeMapping(String encodingStyle) throws ConfigurationException {
202 return (TypeMapping)getTypeMappingRegistry().getTypeMapping(encodingStyle);
203 }
204
205 public Handler getTransport(QName qname) throws ConfigurationException {
206 Handler transport = (Handler)transports.get(qname);
207 if ((defaultConfiguration != null) && (transport == null))
208 transport = defaultConfiguration.getTransport(qname);
209 return transport;
210 }
211
212 public SOAPService getService(QName qname) throws ConfigurationException {
213 SOAPService service = (SOAPService)services.get(qname);
214 if ((defaultConfiguration != null) && (service == null))
215 service = defaultConfiguration.getService(qname);
216 return service;
217 }
218
219 /**
220 * Get a service which has been mapped to a particular namespace
221 *
222 * @param namespace a namespace URI
223 * @return an instance of the appropriate Service, or null
224 */
225 public SOAPService getServiceByNamespaceURI(String namespace)
226 throws ConfigurationException {
227 SOAPService service = (SOAPService)services.get(new QName("",namespace));
228 if ((service == null) && (defaultConfiguration != null))
229 service = defaultConfiguration.getServiceByNamespaceURI(namespace);
230 return service;
231 }
232
233 public Handler getHandler(QName qname) throws ConfigurationException {
234 Handler handler = (Handler)handlers.get(qname);
235 if ((defaultConfiguration != null) && (handler == null))
236 handler = defaultConfiguration.getHandler(qname);
237 return handler;
238 }
239
240 public void deployService(QName qname, SOAPService service)
241 {
242 services.put(qname, service);
243 if (engine != null)
244 service.setEngine(engine);
245 }
246
247 public void deployService(String name, SOAPService service)
248 {
249 deployService(new QName(null, name), service);
250 }
251
252 public void deployTransport(QName qname, Handler transport)
253 {
254 transports.put(qname, transport);
255 }
256
257 public void deployTransport(String name, Handler transport)
258 {
259 deployTransport(new QName(null, name), transport);
260 }
261
262 /**
263 * Get an enumeration of the services deployed to this engine
264 */
265 public Iterator getDeployedServices() throws ConfigurationException {
266 ArrayList serviceDescs = new ArrayList();
267 Iterator i = services.values().iterator();
268 while (i.hasNext()) {
269 SOAPService service = (SOAPService)i.next();
270 serviceDescs.add(service.getServiceDescription());
271 }
272 return serviceDescs.iterator();
273 }
274
275 /**
276 * Set the global role list for this configuration. Note that we use
277 * the actual passed value, so if anyone else changes that collection,
278 * our role list will change. Be careful to pass this a cloned list if
279 * you want to change the list later without affecting the config.
280 *
281 * @param roles
282 */
283 public void setRoles(List roles) {
284 this.roles = roles;
285 }
286
287 /**
288 * Add a role to the configuration's global list
289 *
290 * @param role
291 */
292 public void addRole(String role) {
293 roles.add(role);
294 }
295
296 /**
297 * Remove a role from the configuration's global list
298 *
299 * @param role
300 */
301 public void removeRole(String role) {
302 roles.remove(role);
303 }
304
305 /**
306 * Get a list of roles that this engine plays globally. Services
307 * within the engine configuration may also add additional roles.
308 *
309 * @return a <code>List</code> of the roles for this engine
310 */
311 public List getRoles() {
312 return roles;
313 }
314 }