KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > server > XMLConfigManager


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 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 "SOAP" 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 and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.server ;
59
60 // Core java classes
61
import java.util.* ;
62 import java.io.* ;
63
64 // Java Servlet Classes
65
import javax.servlet.* ;
66 import javax.servlet.http.* ;
67
68 // XML Classes
69
import javax.xml.parsers.* ;
70 import org.w3c.dom.* ;
71 import org.xml.sax.* ;
72
73 // SOAP Classes
74
import org.apache.soap.* ;
75 import org.apache.soap.rpc.* ;
76 import org.apache.soap.server.* ;
77 import org.apache.soap.server.http.* ;
78 import org.apache.soap.util.* ;
79 import org.apache.soap.util.xml.* ;
80
81 /**
82  * An <code>XMLConfigManager</code> ...
83  *
84  * @author <a HREF="mailto:magnus@handpoint.com">Magnus Thor Torfason</a>
85  *
86  * This class should be almost identical in function to the
87  * DefaultConfigManager.
88  *
89  * <p>
90  *
91  * The only notable difference is that while
92  * the DefaultConfigManager uses java readObject() and writeObject()
93  * methods, this class uses the XML routines of DeploymentDescriptor
94  * to save and load the DeploymentDescriptors from the underlying
95  * registry file (typically <tt>DeployedServices.xml</tt>).
96  *
97  */

98 public class XMLConfigManager extends BaseConfigManager
99           implements ConfigManager {
100   protected DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
101
102   /** The name of the deployment file. */
103   protected String JavaDoc filename = "DeployedServices.xml";
104
105   /**
106    * This method sets the configuration options (usually
107    * read from the config file).
108    */

109   public void setOptions( Hashtable options ) {
110     if ( options == null ) return ;
111
112     String JavaDoc value = (String JavaDoc) options.get( "filename" );
113     if ( value != null && !"".equals(value) ) {
114       filename = value ;
115     }
116   }
117
118   /**
119    * Loads the descriptors from the underlying registry file, which
120    * should be represented as a list of deployment descriptor elements.
121    */

122   public void loadRegistry() throws SOAPException {
123     dds = null ;
124     try {
125       File file = ServerHTTPUtils.getFileFromNameAndContext(filename,
126         context);
127       FileReader rd = new FileReader (file);
128       Document doc = null;
129       Element root = null;
130
131       try {
132         doc = xdb.parse(new InputSource(rd));
133         root = doc.getDocumentElement();
134       } catch (Exception JavaDoc e) {
135         e.printStackTrace();
136         throw new SOAPException(Constants.FAULT_CODE_SERVER,e.getMessage());
137       }
138
139       NodeList deploymentElements = root.getElementsByTagNameNS(
140               Constants.NS_URI_XML_SOAP_DEPLOYMENT, "service");
141
142       int count = deploymentElements.getLength();
143       dds = new Hashtable();
144       for( int i=0; i<count; i++ ) {
145         Element deploymentElement = (Element)deploymentElements.item(i);
146         DeploymentDescriptor dd = DeploymentDescriptor.fromXML(
147                 deploymentElement);
148         String JavaDoc id = dd.getID();
149         dds.put( id, dd );
150       }
151     } catch(Exception JavaDoc e) {
152       dds = new Hashtable ();
153       System.err.println ("SOAP Service Manager: Unable to read '" +
154         filename + "': assuming fresh start");
155     }
156   }
157
158   /**
159    * Saves currently deployed descriptors to the underlying registry file,
160    * in XML format.
161    */

162   public void saveRegistry() throws SOAPException {
163     try {
164       File file = ServerHTTPUtils.getFileFromNameAndContext(filename,
165         context);
166       PrintWriter pw = new PrintWriter(new FileWriter (file));
167       Enumeration e = dds.elements();
168
169       pw.println("<deployedServices>");
170       pw.println();
171       while ( e.hasMoreElements() ) {
172         DeploymentDescriptor dd = (DeploymentDescriptor)e.nextElement();
173         dd.toXML(pw);
174         pw.println();
175       }
176       pw.println("</deployedServices>");
177
178       pw.close ();
179     } catch (Exception JavaDoc e) {
180       throw new SOAPException (Constants.FAULT_CODE_SERVER,
181         "Error saving services registry: " +
182         e.getMessage ());
183     }
184   }
185 }
186
187
Popular Tags