KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > http > AutoRegisterServlet


1 /*
2  * Copyright 2003,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.transport.http;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.axis.components.logger.LogFactory;
21 import org.apache.axis.deployment.wsdd.WSDDDocument;
22 import org.apache.axis.deployment.wsdd.WSDDDeployment;
23 import org.apache.axis.utils.XMLUtils;
24 import org.apache.axis.AxisEngine;
25 import org.apache.axis.AxisFault;
26 import org.apache.axis.ConfigurationException;
27 import org.apache.axis.EngineConfiguration;
28 import org.apache.axis.WSDDEngineConfiguration;
29 import org.apache.axis.i18n.Messages;
30 import org.w3c.dom.Document JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 import javax.xml.parsers.ParserConfigurationException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.BufferedInputStream JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40
41 /**
42  * Servlet that autoregisters
43  * @author Steve Loughran
44  * xdoclet tags are not active yet; keep web.xml in sync
45  * @web.servlet name="AutoRegisterServlet" display-name="Axis Autoregister Servlet" load-on-startup="30"
46  */

47 public class AutoRegisterServlet extends AxisServletBase {
48
49     private static Log log =
50             LogFactory.getLog(AutoRegisterServlet.class.getName());
51
52     /**
53      * init by registering
54      */

55     public void init() throws javax.servlet.ServletException JavaDoc {
56         log.debug(Messages.getMessage("autoRegServletInit00"));
57         autoRegister();
58     }
59
60     /**
61      * register an open stream, which we close afterwards
62      * @param instream
63      * @throws SAXException
64      * @throws ParserConfigurationException
65      * @throws IOException
66      */

67     public void registerStream(InputStream JavaDoc instream) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
68         try {
69             Document doc=XMLUtils.newDocument(instream);
70             WSDDDocument wsddDoc = new WSDDDocument(doc);
71             WSDDDeployment deployment;
72             deployment = getDeployment();
73             if(deployment!=null) {
74                 wsddDoc.deploy(deployment);
75             }
76         } finally {
77             instream.close();
78         }
79     }
80
81     /**
82      * register a resource
83      * @param resourcename
84      * @throws SAXException
85      * @throws ParserConfigurationException
86      * @throws IOException
87      */

88     public void registerResource(String JavaDoc resourcename)
89             throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
90         InputStream JavaDoc in=getServletContext().getResourceAsStream(resourcename);
91         if(in==null) {
92             throw new FileNotFoundException JavaDoc(resourcename);
93         }
94         registerStream(in);
95     }
96
97     /**
98      * register a file
99      * @param file
100      * @throws IOException
101      * @throws SAXException
102      * @throws ParserConfigurationException
103      */

104     public void registerFile(File JavaDoc file) throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
105         InputStream JavaDoc in=new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
106         registerStream(in);
107     }
108
109     /**
110      * subclass this to return an array of resource names.
111      * @return array of resource names of wsdd files, or null
112      */

113     public String JavaDoc[] getResourcesToRegister() {
114         return null;
115     }
116
117
118
119     /**
120      * get deployment
121      * @return
122      * @throws AxisFault
123      */

124     private WSDDDeployment getDeployment() throws AxisFault {
125         WSDDDeployment deployment;
126         AxisEngine engine = getEngine();
127         EngineConfiguration config = engine.getConfig();
128         if (config instanceof WSDDEngineConfiguration) {
129             deployment = ((WSDDEngineConfiguration) config).getDeployment();
130         } else {
131             deployment=null;
132         }
133         return deployment;
134     }
135
136     /**
137      * handler for logging success, defaults to handing off to logging
138      * at debug level
139      * @param item what were we loading?
140      */

141     protected void logSuccess(String JavaDoc item) {
142         log.debug(Messages.getMessage("autoRegServletLoaded01",item));
143     }
144
145     /**
146      * register classes, log exceptions
147      */

148     protected void autoRegister() {
149         String JavaDoc[] resources=getResourcesToRegister();
150         if(resources==null || resources.length==0) {
151             return;
152         }
153         for(int i=0;i<resources.length;i++) {
154             final String JavaDoc resource = resources[i];
155             registerAndLogResource(resource);
156         }
157         registerAnythingElse();
158         try {
159             applyAndSaveSettings();
160         } catch (Exception JavaDoc e) {
161             log.error(Messages.getMessage("autoRegServletApplyAndSaveSettings00"), e);
162         }
163     }
164
165     /**
166      * override point for subclasses to add other registration stuff
167      */

168     protected void registerAnythingElse() {
169     }
170
171     /**
172      * register a single resource; log trouble and success.
173      * @param resource
174      */

175     public void registerAndLogResource(final String JavaDoc resource) {
176         try {
177             registerResource(resource);
178             logSuccess(resource);
179         } catch (Exception JavaDoc e) {
180             log.error(Messages.getMessage("autoRegServletLoadFailed01",resource),e);
181         }
182     }
183
184     /**
185      * actually update the engine and save the settings
186      * @throws AxisFault
187      * @throws ConfigurationException
188      */

189     protected void applyAndSaveSettings()
190             throws AxisFault, ConfigurationException {
191         AxisEngine engine = getEngine();
192         engine.refreshGlobalOptions();
193         engine.saveConfiguration();
194     }
195 }
196
Popular Tags