KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > configuration > FileProvider


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.configuration;
57
58 import org.jboss.axis.AxisEngine;
59 import org.jboss.axis.ConfigurationException;
60 import org.jboss.axis.Handler;
61 import org.jboss.axis.WSDDEngineConfiguration;
62 import org.jboss.axis.deployment.wsdd.WSDDDeployment;
63 import org.jboss.axis.deployment.wsdd.WSDDDocument;
64 import org.jboss.axis.deployment.wsdd.WSDDGlobalConfiguration;
65 import org.jboss.axis.encoding.TypeMappingRegistry;
66 import org.jboss.axis.handlers.soap.SOAPService;
67 import org.jboss.axis.utils.Admin;
68 import org.jboss.axis.utils.ClassUtils;
69 import org.jboss.axis.utils.Messages;
70 import org.jboss.axis.utils.XMLUtils;
71 import org.jboss.logging.Logger;
72 import org.w3c.dom.Document JavaDoc;
73
74 import javax.xml.namespace.QName JavaDoc;
75 import java.io.File JavaDoc;
76 import java.io.FileInputStream JavaDoc;
77 import java.io.FileOutputStream JavaDoc;
78 import java.io.InputStream JavaDoc;
79 import java.io.StringWriter JavaDoc;
80 import java.util.Hashtable JavaDoc;
81 import java.util.Iterator JavaDoc;
82
83 /**
84  * A simple ConfigurationProvider that uses the Admin class to read +
85  * write XML files.
86  *
87  * @author Glen Daniels (gdaniels@macromedia.com)
88  * @author Glyn Normington (glyn@apache.org)
89  */

90 public class FileProvider implements WSDDEngineConfiguration
91 {
92    private static Logger log = Logger.getLogger(FileProvider.class.getName());
93
94    private WSDDDeployment deployment = null;
95
96    private String JavaDoc filename;
97    private File JavaDoc configFile = null;
98
99    private InputStream JavaDoc myInputStream = null;
100
101    private boolean readOnly = true;
102
103    // Should we search the classpath for the file if we don't find it in
104
// the specified location?
105
private boolean searchClasspath = true;
106
107    /**
108     * Constructor which accesses a file in the current directory of the
109     * engine or at an absolute path.
110     */

111    public FileProvider(String JavaDoc filename)
112    {
113       this.filename = filename;
114       configFile = new File JavaDoc(filename);
115       check();
116    }
117
118    /**
119     * Constructor which accesses a file relative to a specific base
120     * path.
121     */

122    public FileProvider(String JavaDoc basepath, String JavaDoc filename)
123            throws ConfigurationException
124    {
125       this.filename = filename;
126
127       File JavaDoc dir = new File JavaDoc(basepath);
128
129       /*
130        * If the basepath is not a readable directory, throw an internal
131        * exception to make it easier to debug setup problems.
132        */

133       if (!dir.isDirectory() || !dir.canRead())
134       {
135          throw new ConfigurationException(Messages.getMessage
136                  ("invalidConfigFilePath",
137                          basepath));
138       }
139
140       configFile = new File JavaDoc(basepath, filename);
141       check();
142    }
143
144    /**
145     * Check the configuration file attributes and remember whether
146     * or not the file is read-only.
147     */

148    private void check()
149    {
150       readOnly = configFile.canRead() & !configFile.canWrite();
151
152       /*
153        * If file is read-only, log informational message
154        * as configuration changes will not persist.
155        */

156       if (readOnly)
157       {
158          log.info(Messages.getMessage("readOnlyConfigFile"));
159       }
160    }
161
162    /**
163     * Constructor which takes an input stream directly.
164     * Note: The configuration will be read-only in this case!
165     */

166    public FileProvider(InputStream JavaDoc is)
167    {
168       setInputStream(is);
169    }
170
171    public void setInputStream(InputStream JavaDoc is)
172    {
173       myInputStream = is;
174    }
175
176    private InputStream JavaDoc getInputStream()
177    {
178       return myInputStream;
179    }
180
181    public WSDDDeployment getDeployment()
182    {
183       return deployment;
184    }
185
186    public void setDeployment(WSDDDeployment deployment)
187    {
188       this.deployment = deployment;
189    }
190
191    /**
192     * Determine whether or not we will look for a "*-config.wsdd" file
193     * on the classpath if we don't find it in the specified location.
194     *
195     * @param searchClasspath true if we should search the classpath
196     */

197    public void setSearchClasspath(boolean searchClasspath)
198    {
199       this.searchClasspath = searchClasspath;
200    }
201
202    public void configureEngine(AxisEngine engine)
203            throws ConfigurationException
204    {
205       try
206       {
207          if (getInputStream() == null)
208          {
209             try
210             {
211                setInputStream(new FileInputStream JavaDoc(configFile));
212             }
213             catch (Exception JavaDoc e)
214             {
215                if (searchClasspath)
216                   setInputStream(ClassUtils.getResourceAsStream(engine.getClass(), filename));
217             }
218          }
219
220          if (getInputStream() == null)
221          {
222             throw new ConfigurationException(Messages.getMessage("noConfigFile"));
223          }
224
225          WSDDDocument doc = new WSDDDocument(XMLUtils.
226                  newDocument(getInputStream()));
227          deployment = doc.getDeployment();
228
229          deployment.configureEngine(engine);
230          engine.refreshGlobalOptions();
231
232          setInputStream(null);
233       }
234       catch (Exception JavaDoc e)
235       {
236          throw new ConfigurationException(e);
237       }
238    }
239
240    /**
241     * Save the engine configuration. In case there's a problem, we
242     * write it to a string before saving it out to the actual file so
243     * we don't screw up the file.
244     */

245    public void writeEngineConfig(AxisEngine engine)
246            throws ConfigurationException
247    {
248       if (!readOnly)
249       {
250          try
251          {
252             Document doc = Admin.listConfig(engine);
253             StringWriter JavaDoc writer = new StringWriter JavaDoc();
254             XMLUtils.DocumentToWriter(doc, writer);
255             writer.close();
256             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(configFile);
257             fos.write(writer.getBuffer().toString().getBytes());
258             fos.close();
259          }
260          catch (Exception JavaDoc e)
261          {
262             throw new ConfigurationException(e);
263          }
264       }
265    }
266
267    /**
268     * retrieve an instance of the named handler
269     *
270     * @param qname XXX
271     * @return XXX
272     * @throws ConfigurationException XXX
273     */

274    public Handler getHandler(QName JavaDoc qname) throws ConfigurationException
275    {
276       return deployment.getHandler(qname);
277    }
278
279    /**
280     * retrieve an instance of the named service
281     *
282     * @param qname XXX
283     * @return XXX
284     * @throws ConfigurationException XXX
285     */

286    public SOAPService getService(QName JavaDoc qname) throws ConfigurationException
287    {
288       return deployment.getService(qname);
289    }
290
291    /**
292     * Get a service which has been mapped to a particular namespace
293     *
294     * @param namespace a namespace URI
295     * @return an instance of the appropriate Service, or null
296     */

297    public SOAPService getServiceByNamespaceURI(String JavaDoc namespace)
298            throws ConfigurationException
299    {
300       return deployment.getServiceByNamespaceURI(namespace);
301    }
302
303    /**
304     * retrieve an instance of the named transport
305     *
306     * @param qname XXX
307     * @return XXX
308     * @throws ConfigurationException XXX
309     */

310    public Handler getTransport(QName JavaDoc qname) throws ConfigurationException
311    {
312       return deployment.getTransport(qname);
313    }
314
315    public TypeMappingRegistry getTypeMappingRegistry()
316            throws ConfigurationException
317    {
318       return deployment.getTypeMappingRegistry();
319    }
320
321    /**
322     * Returns a global request handler.
323     */

324    public Handler getGlobalRequest() throws ConfigurationException
325    {
326       return deployment.getGlobalRequest();
327    }
328
329    /**
330     * Returns a global response handler.
331     */

332    public Handler getGlobalResponse() throws ConfigurationException
333    {
334       return deployment.getGlobalResponse();
335    }
336
337    /**
338     * Returns the global configuration options.
339     */

340    public Hashtable JavaDoc getGlobalOptions() throws ConfigurationException
341    {
342       WSDDGlobalConfiguration globalConfig
343               = deployment.getGlobalConfiguration();
344
345       if (globalConfig != null)
346          return globalConfig.getParametersTable();
347
348       return null;
349    }
350
351    /**
352     * Get an enumeration of the services deployed to this engine
353     */

354    public Iterator JavaDoc getDeployedServices() throws ConfigurationException
355    {
356       return deployment.getDeployedServices();
357    }
358 }
359
Popular Tags