Source code: org/apache/axis/configuration/FileProvider.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 java.io.BufferedWriter;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.PrintWriter;
26 import java.io.Writer;
27 import java.util.Hashtable;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import javax.xml.namespace.QName;
32
33 import org.apache.axis.AxisEngine;
34 import org.apache.axis.ConfigurationException;
35 import org.apache.axis.Handler;
36 import org.apache.axis.WSDDEngineConfiguration;
37 import org.apache.axis.components.logger.LogFactory;
38 import org.apache.axis.deployment.wsdd.WSDDDeployment;
39 import org.apache.axis.deployment.wsdd.WSDDDocument;
40 import org.apache.axis.deployment.wsdd.WSDDGlobalConfiguration;
41 import org.apache.axis.encoding.TypeMappingRegistry;
42 import org.apache.axis.handlers.soap.SOAPService;
43 import org.apache.axis.utils.Admin;
44 import org.apache.axis.utils.ClassUtils;
45 import org.apache.axis.utils.Messages;
46 import org.apache.axis.utils.XMLUtils;
47 import org.apache.commons.logging.Log;
48 import org.w3c.dom.Document;
49
50 /**
51 * A simple ConfigurationProvider that uses the Admin class to read +
52 * write XML files.
53 *
54 * @author Glen Daniels (gdaniels@apache.org)
55 * @author Glyn Normington (glyn@apache.org)
56 */
57 public class FileProvider implements WSDDEngineConfiguration {
58 protected static Log log =
59 LogFactory.getLog(FileProvider.class.getName());
60
61 private WSDDDeployment deployment = null;
62
63 private String filename;
64 private File configFile = null;
65
66 private InputStream myInputStream = null;
67
68 private boolean readOnly = true;
69
70 // Should we search the classpath for the file if we don't find it in
71 // the specified location?
72 private boolean searchClasspath = true;
73
74 /**
75 * Constructor which accesses a file in the current directory of the
76 * engine or at an absolute path.
77 */
78 public FileProvider(String filename) {
79 this.filename = filename;
80 configFile = new File(filename);
81 check();
82 }
83
84 /**
85 * Constructor which accesses a file relative to a specific base
86 * path.
87 */
88 public FileProvider(String basepath, String filename)
89 throws ConfigurationException {
90 this.filename = filename;
91
92 File dir = new File(basepath);
93
94 /*
95 * If the basepath is not a readable directory, throw an internal
96 * exception to make it easier to debug setup problems.
97 */
98 if (!dir.exists() || !dir.isDirectory() || !dir.canRead()) {
99 throw new ConfigurationException(Messages.getMessage
100 ("invalidConfigFilePath",
101 basepath));
102 }
103
104 configFile = new File(basepath, filename);
105 check();
106 }
107
108 /**
109 * Check the configuration file attributes and remember whether
110 * or not the file is read-only.
111 */
112 private void check() {
113 try {
114 readOnly = configFile.canRead() & !configFile.canWrite();
115 } catch (SecurityException se){
116 readOnly = true;
117 }
118
119 /*
120 * If file is read-only, log informational message
121 * as configuration changes will not persist.
122 */
123 if (readOnly) {
124 log.info(Messages.getMessage("readOnlyConfigFile"));
125 }
126 }
127
128 /**
129 * Constructor which takes an input stream directly.
130 * Note: The configuration will be read-only in this case!
131 */
132 public FileProvider(InputStream is) {
133 setInputStream(is);
134 }
135
136 public void setInputStream(InputStream is) {
137 myInputStream = is;
138 }
139
140 private InputStream getInputStream() {
141 return myInputStream;
142 }
143
144 public WSDDDeployment getDeployment() {
145 return deployment;
146 }
147
148 public void setDeployment(WSDDDeployment deployment) {
149 this.deployment = deployment;
150 }
151
152 /**
153 * Determine whether or not we will look for a "*-config.wsdd" file
154 * on the classpath if we don't find it in the specified location.
155 *
156 * @param searchClasspath true if we should search the classpath
157 */
158 public void setSearchClasspath(boolean searchClasspath) {
159 this.searchClasspath = searchClasspath;
160 }
161
162 public void configureEngine(AxisEngine engine)
163 throws ConfigurationException {
164 try {
165 if (getInputStream() == null) {
166 try {
167 setInputStream(new FileInputStream(configFile));
168 } catch (Exception e) {
169 if (searchClasspath)
170 setInputStream(ClassUtils.getResourceAsStream(engine.getClass(), filename, true));
171 }
172 }
173
174 if (getInputStream() == null) {
175 throw new ConfigurationException(
176 Messages.getMessage("noConfigFile"));
177 }
178
179 WSDDDocument doc = new WSDDDocument(XMLUtils.
180 newDocument(getInputStream()));
181 deployment = doc.getDeployment();
182
183 deployment.configureEngine(engine);
184 engine.refreshGlobalOptions();
185
186 setInputStream(null);
187 } catch (Exception e) {
188 throw new ConfigurationException(e);
189 }
190 }
191
192 /**
193 * Save the engine configuration. In case there's a problem, we
194 * write it to a string before saving it out to the actual file so
195 * we don't screw up the file.
196 */
197 public void writeEngineConfig(AxisEngine engine)
198 throws ConfigurationException {
199 if (!readOnly) {
200 try {
201 Document doc = Admin.listConfig(engine);
202 Writer osWriter = new OutputStreamWriter(
203 new FileOutputStream(configFile),XMLUtils.getEncoding());
204 PrintWriter writer = new PrintWriter(new BufferedWriter(osWriter));
205 XMLUtils.DocumentToWriter(doc, writer);
206 writer.println();
207 writer.close();
208 } catch (Exception e) {
209 throw new ConfigurationException(e);
210 }
211 }
212 }
213
214 /**
215 * retrieve an instance of the named handler
216 * @param qname XXX
217 * @return XXX
218 * @throws ConfigurationException XXX
219 */
220 public Handler getHandler(QName qname) throws ConfigurationException {
221 return deployment.getHandler(qname);
222 }
223
224 /**
225 * retrieve an instance of the named service
226 * @param qname XXX
227 * @return XXX
228 * @throws ConfigurationException XXX
229 */
230 public SOAPService getService(QName qname) throws ConfigurationException {
231 SOAPService service = deployment.getService(qname);
232 if (service == null) {
233 throw new ConfigurationException(Messages.getMessage("noService10",
234 qname.toString()));
235 }
236 return service;
237 }
238
239 /**
240 * Get a service which has been mapped to a particular namespace
241 *
242 * @param namespace a namespace URI
243 * @return an instance of the appropriate Service, or null
244 */
245 public SOAPService getServiceByNamespaceURI(String namespace)
246 throws ConfigurationException {
247 return deployment.getServiceByNamespaceURI(namespace);
248 }
249
250 /**
251 * retrieve an instance of the named transport
252 * @param qname XXX
253 * @return XXX
254 * @throws ConfigurationException XXX
255 */
256 public Handler getTransport(QName qname) throws ConfigurationException {
257 return deployment.getTransport(qname);
258 }
259
260 public TypeMappingRegistry getTypeMappingRegistry()
261 throws ConfigurationException {
262 return deployment.getTypeMappingRegistry();
263 }
264
265 /**
266 * Returns a global request handler.
267 */
268 public Handler getGlobalRequest() throws ConfigurationException {
269 return deployment.getGlobalRequest();
270 }
271
272 /**
273 * Returns a global response handler.
274 */
275 public Handler getGlobalResponse() throws ConfigurationException {
276 return deployment.getGlobalResponse();
277 }
278
279 /**
280 * Returns the global configuration options.
281 */
282 public Hashtable getGlobalOptions() throws ConfigurationException {
283 WSDDGlobalConfiguration globalConfig
284 = deployment.getGlobalConfiguration();
285
286 if (globalConfig != null)
287 return globalConfig.getParametersTable();
288
289 return null;
290 }
291
292 /**
293 * Get an enumeration of the services deployed to this engine
294 */
295 public Iterator getDeployedServices() throws ConfigurationException {
296 return deployment.getDeployedServices();
297 }
298
299 /**
300 * Get a list of roles that this engine plays globally. Services
301 * within the engine configuration may also add additional roles.
302 *
303 * @return a <code>List</code> of the roles for this engine
304 */
305 public List getRoles() {
306 return deployment.getRoles();
307 }
308 }