KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > configuration > DirProvider


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.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.FileFilter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29
30 import org.apache.axis.AxisEngine;
31 import org.apache.axis.ConfigurationException;
32 import org.apache.axis.Handler;
33 import org.apache.axis.WSDDEngineConfiguration;
34 import org.apache.axis.deployment.wsdd.WSDDDeployment;
35 import org.apache.axis.deployment.wsdd.WSDDDocument;
36 import org.apache.axis.deployment.wsdd.WSDDGlobalConfiguration;
37 import org.apache.axis.encoding.TypeMappingRegistry;
38 import org.apache.axis.handlers.soap.SOAPService;
39 import org.apache.axis.utils.Messages;
40 import org.apache.axis.utils.XMLUtils;
41
42 import org.apache.commons.logging.LogFactory;
43 import org.apache.commons.logging.Log;
44
45 import org.w3c.dom.Document JavaDoc;
46
47 public class DirProvider implements WSDDEngineConfiguration {
48
49     protected static Log log =
50         LogFactory.getLog(DirProvider.class.getName());
51
52     private WSDDDeployment deployment = null;
53     private String JavaDoc configFile;
54     private File JavaDoc dir;
55
56     private static final String JavaDoc SERVER_CONFIG_FILE =
57         "server-config.wsdd";
58
59     public DirProvider(String JavaDoc basepath)
60         throws ConfigurationException {
61         this(basepath, SERVER_CONFIG_FILE);
62     }
63
64     public DirProvider(String JavaDoc basepath, String JavaDoc configFile)
65         throws ConfigurationException {
66         File JavaDoc dir = new File JavaDoc(basepath);
67
68         /*
69          * If the basepath is not a readable directory, throw an internal
70          * exception to make it easier to debug setup problems.
71          */

72         if (!dir.exists() || !dir.isDirectory() || !dir.canRead()) {
73             throw new ConfigurationException(Messages.getMessage
74                                              ("invalidConfigFilePath",
75                                               basepath));
76         }
77
78         this.dir = dir;
79         this.configFile = configFile;
80     }
81
82     public WSDDDeployment getDeployment() {
83         return this.deployment;
84     }
85
86     private static class DirFilter implements FileFilter JavaDoc {
87         public boolean accept(File JavaDoc path) {
88             return path.isDirectory();
89         }
90     }
91
92     public void configureEngine(AxisEngine engine)
93         throws ConfigurationException {
94         this.deployment = new WSDDDeployment();
95         WSDDGlobalConfiguration config = new WSDDGlobalConfiguration();
96         config.setOptionsHashtable(new Hashtable JavaDoc());
97         this.deployment.setGlobalConfiguration(config);
98         File JavaDoc [] dirs = this.dir.listFiles(new DirFilter());
99         for (int i = 0; i < dirs.length; i++) {
100             processWSDD(dirs[i]);
101         }
102         this.deployment.configureEngine(engine);
103         engine.refreshGlobalOptions();
104     }
105
106     private void processWSDD(File JavaDoc dir)
107         throws ConfigurationException {
108         File JavaDoc file = new File JavaDoc(dir, this.configFile);
109         if (!file.exists()) {
110             return;
111         }
112         log.debug("Loading service configuration from file: " + file);
113         InputStream JavaDoc in = null;
114         try {
115             in = new FileInputStream JavaDoc(file);
116             WSDDDocument doc = new WSDDDocument(XMLUtils.newDocument(in));
117             doc.deploy(this.deployment);
118         } catch (Exception JavaDoc e) {
119             throw new ConfigurationException(e);
120         } finally {
121             if (in != null) {
122                 try {
123                     in.close();
124                 } catch (IOException JavaDoc e) {}
125             }
126         }
127     }
128
129     /**
130      * Save the engine configuration. In case there's a problem, we
131      * write it to a string before saving it out to the actual file so
132      * we don't screw up the file.
133      */

134     public void writeEngineConfig(AxisEngine engine)
135         throws ConfigurationException {
136         // this is not implemented
137
}
138
139     /**
140      * retrieve an instance of the named handler
141      * @param qname XXX
142      * @return XXX
143      * @throws ConfigurationException XXX
144      */

145     public Handler getHandler(QName JavaDoc qname) throws ConfigurationException {
146         return this.deployment.getHandler(qname);
147     }
148
149     /**
150      * retrieve an instance of the named service
151      * @param qname XXX
152      * @return XXX
153      * @throws ConfigurationException XXX
154      */

155     public SOAPService getService(QName JavaDoc qname) throws ConfigurationException {
156         SOAPService service = this.deployment.getService(qname);
157         if (service == null) {
158             throw new ConfigurationException(Messages.getMessage("noService10",
159                                                            qname.toString()));
160         }
161         return service;
162     }
163
164     /**
165      * Get a service which has been mapped to a particular namespace
166      *
167      * @param namespace a namespace URI
168      * @return an instance of the appropriate Service, or null
169      */

170     public SOAPService getServiceByNamespaceURI(String JavaDoc namespace)
171             throws ConfigurationException {
172         return this.deployment.getServiceByNamespaceURI(namespace);
173     }
174
175     /**
176      * retrieve an instance of the named transport
177      * @param qname XXX
178      * @return XXX
179      * @throws ConfigurationException XXX
180      */

181     public Handler getTransport(QName JavaDoc qname) throws ConfigurationException {
182         return this.deployment.getTransport(qname);
183     }
184
185     public TypeMappingRegistry getTypeMappingRegistry()
186         throws ConfigurationException {
187         return this.deployment.getTypeMappingRegistry();
188     }
189
190     /**
191      * Returns a global request handler.
192      */

193     public Handler getGlobalRequest() throws ConfigurationException {
194         return this.deployment.getGlobalRequest();
195     }
196
197     /**
198      * Returns a global response handler.
199      */

200     public Handler getGlobalResponse() throws ConfigurationException {
201         return this.deployment.getGlobalResponse();
202     }
203
204     /**
205      * Returns the global configuration options.
206      */

207     public Hashtable JavaDoc getGlobalOptions() throws ConfigurationException {
208         WSDDGlobalConfiguration globalConfig
209             = this.deployment.getGlobalConfiguration();
210             
211         if (globalConfig != null)
212             return globalConfig.getParametersTable();
213
214         return null;
215     }
216
217     /**
218      * Get an enumeration of the services deployed to this engine
219      */

220     public Iterator JavaDoc getDeployedServices() throws ConfigurationException {
221         return this.deployment.getDeployedServices();
222     }
223
224     /**
225      * Get a list of roles that this engine plays globally. Services
226      * within the engine configuration may also add additional roles.
227      *
228      * @return a <code>List</code> of the roles for this engine
229      */

230     public List JavaDoc getRoles() {
231         return this.deployment.getRoles();
232     }
233 }
234
Popular Tags