KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
61 import java.io.*;
62 import javax.servlet.*;
63 import javax.xml.parsers.*;
64 import org.apache.soap.*;
65 import org.apache.soap.server.http.*;
66 import org.apache.soap.util.* ;
67 import org.apache.soap.util.xml.*;
68 import org.apache.soap.rpc.SOAPContext ;
69 import org.w3c.dom.*;
70 import org.xml.sax.*;
71
72 /**
73  * A <code>ServiceManager</code> manages services and their associated
74  * <code>DeploymentDescriptors</code>.
75  *
76  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
77  */

78 public class ServiceManager {
79   protected String JavaDoc configFilename = "soap.xml";
80   protected DeploymentDescriptor smsdd; // Svc Mgmnt Svc's Dep Descriptor
81
protected ConfigManager configMgr = null;
82   protected ServletContext context = null;
83   protected DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
84   protected boolean soapInterfaceEnabled = true;
85
86   public ServiceManager (ServletContext context, String JavaDoc configFilename) {
87     this.context = context;
88
89     if (configFilename != null && !configFilename.equals(""))
90     {
91       this.configFilename = configFilename;
92     }
93
94     readConfigFile ();
95
96     /*If the SOAP Interface for the ServiceManager has been disabled,
97       don't create the DeploymentDescriptor -WAN*/

98     if (soapInterfaceEnabled)
99     {
100       smsdd = new DeploymentDescriptor ();
101       smsdd.setID (ServerConstants.SERVICE_MANAGER_SERVICE_NAME);
102       String JavaDoc[] svcs = new String JavaDoc[] { "deploy", "undeploy", "list", "query" };
103       smsdd.setMethods (svcs);
104       smsdd.setScope (DeploymentDescriptor.SCOPE_APPLICATION);
105       smsdd.setProviderType (DeploymentDescriptor.PROVIDER_JAVA);
106       smsdd.setProviderClass ("org.apache.soap.server.ServiceManager");
107       smsdd.setIsStatic (false);
108       
109       // set up mappings to send/recv DeploymentDescriptor and TypeMapping
110
// objects
111
smsdd.setMappings (new TypeMapping[] {
112     new TypeMapping (Constants.NS_URI_SOAP_ENC,
113              new QName (Constants.NS_URI_XML_SOAP,
114                     "DeploymentDescriptor"),
115              "org.apache.soap.server.DeploymentDescriptor",
116              "org.apache.soap.encoding.soapenc.BeanSerializer",
117              "org.apache.soap.encoding.soapenc.BeanSerializer"),
118     new TypeMapping (Constants.NS_URI_SOAP_ENC,
119              new QName (Constants.NS_URI_XML_SOAP,
120                     "TypeMapping"),
121              "org.apache.soap.server.TypeMapping",
122              "org.apache.soap.server.TypeMappingSerializer",
123              "org.apache.soap.server.TypeMappingSerializer")});
124     }
125   }
126
127   public void setConfigFilename (String JavaDoc configFilename) {
128     if (configFilename == null || configFilename.equals(""))
129     {
130       return;
131     }
132     else
133     {
134       this.configFilename = configFilename;
135       readConfigFile();
136     }
137   }
138
139   private void readConfigFile() {
140     FileReader reader = null ;
141     Document doc = null ;
142     Element elem = null ;
143     NodeList list = null ;
144     int i, k ;
145     Hashtable options = null ;
146
147     try {
148       File configFile =
149         ServerHTTPUtils.getFileFromNameAndContext(configFilename, context);
150
151       reader = new FileReader(configFile);
152       doc = xdb.parse(new InputSource(reader));
153       elem = doc.getDocumentElement();
154       if ( !"soapServer".equals(elem.getTagName()) )
155         throw new Exception JavaDoc( "Root element must be 'soapServer'" );
156       
157       list = elem.getChildNodes();
158       for ( i = 0 ; list != null && i < list.getLength() ; i++ ) {
159         String JavaDoc name = null ;
160         Node n = list.item( i );
161
162         if ( n.getNodeType() != Node.ELEMENT_NODE ) continue ;
163         elem = (Element) n ;
164         name = elem.getTagName();
165         if ( name.equals( "configManager" ) ) {
166           String JavaDoc className = elem.getAttribute( "value" );
167
168           ClassLoader JavaDoc cl = null ;
169           Class JavaDoc c = null ;
170           
171           cl = ServerHTTPUtils.getServletClassLoaderFromContext(context);
172
173           if ( cl == null )
174             c = Class.forName( className );
175           else
176             c = Class.forName( className, true, cl );
177
178           if (!ConfigManager.class.isAssignableFrom(c)) {
179             throw new IllegalArgumentException JavaDoc("Class '" + className +
180                                                "' isn't a ConfigManager.");
181           }
182     
183           configMgr = (ConfigManager) c.newInstance();
184
185           // Set the servlet context.
186
configMgr.setContext(context);
187           // Now check for options
188
NodeList optList = elem.getElementsByTagName( "option" );
189           for ( k = 0 ; optList != null && k < optList.getLength() ; k++ ) {
190             elem = (Element) optList.item( k );
191             name = elem.getAttribute( "name" );
192             String JavaDoc value = elem.getAttribute( "value" ) ;
193             if ( options == null ) options = new Hashtable();
194             if ( name == null || value == null ) continue ;
195             options.put( name, value );
196           }
197           // Now set the options
198
if ( options != null )
199             configMgr.setOptions( options );
200         }
201     else
202       if (name.equals("serviceManager"))
203       {
204           // Now check for options
205
NodeList optList = elem.getElementsByTagName( "option" );
206         for ( k = 0 ; optList != null && k < optList.getLength() ; k++ ) {
207           elem = (Element) optList.item( k );
208           name = elem.getAttribute( "name" );
209           String JavaDoc value = elem.getAttribute( "value" ) ;
210           if ( name == null || value == null ) continue ;
211           if (name.equalsIgnoreCase("SOAPInterfaceEnabled") && value.equalsIgnoreCase("false"))
212         soapInterfaceEnabled = false;
213         }
214       }
215       }
216       reader.close();
217     }
218     catch( Throwable JavaDoc e ) {
219       // For backwards compatibility - if there was no reader then
220
// we probably failed because the file doesn't exists and in that
221
// case just skip the error message - it'll just annoy old users
222
if ( reader != null ) {
223         System.err.println( "Error processing configuration file (" +
224                             configFilename + ")" );
225         System.err.println( "Error was: " + e );
226         System.err.println( "Using DefaultConfigManager" );
227       }
228     }
229
230
231     // When all is said and done - if there still isn't a configMgr
232
// then use the default one.
233
if ( configMgr == null ) {
234       configMgr = new DefaultConfigManager( );
235
236       // Set the servlet context.
237
configMgr.setContext(context);
238     }
239
240     try {
241       configMgr.init();
242     }
243     catch( SOAPException e ) {
244       e.printStackTrace();
245     }
246   }
247
248   /**
249    * Deploy a service: add the descriptor to the persistent record of
250    * what has been deployed.
251    */

252   public void deploy (DeploymentDescriptor dd) throws SOAPException {
253     String JavaDoc id = dd.getID ();
254     if (id.equals (ServerConstants.SERVICE_MANAGER_SERVICE_NAME)) {
255       throw new SOAPException (Constants.FAULT_CODE_SERVER,
256                                "service management service '" +
257                                ServerConstants.SERVICE_MANAGER_SERVICE_NAME +
258                                "' cannot be user deployed");
259     }
260     configMgr.deploy( dd );
261   }
262
263   /**
264    * Undeploy a service: remove the descriptor from the persistent record
265    * of what has been deployed.
266    *
267    * @id the id of the service I'm undeploying
268    * @exception SOAPException if service is not found
269    */

270   public DeploymentDescriptor undeploy (String JavaDoc id) throws SOAPException {
271     return( configMgr.undeploy( id ) );
272   }
273
274   /**
275    * Return the deployment descriptor for a service. If the id identifies
276    * the service management service, then the deployment descriptor of
277    * the service management service is returned.
278    *
279    * @param id the id of the service I'm looking for
280    * @exception SOAPException if service is not found
281    */

282   public DeploymentDescriptor query(String JavaDoc id) throws SOAPException {
283     if (id == null)
284       return null;
285     else if (id.equals (ServerConstants.SERVICE_MANAGER_SERVICE_NAME))
286       return smsdd;
287     else {
288       DeploymentDescriptor dd = configMgr.query( id );
289       if (dd != null)
290         return dd;
291       else
292         // Should we really throw an exception ?
293
// Why not just return null ?
294
throw new SOAPException (Constants.FAULT_CODE_SERVER,
295                                  "service '" + id + "' unknown");
296     }
297   }
298
299   /**
300    * Return an array of all the deployed service names. Returns an array
301    * of length zero if there are no deployed services.
302    *
303    * @return array of all service names
304    */

305   public String JavaDoc[] list () throws SOAPException {
306     return( configMgr.list() );
307   }
308 }
309
Popular Tags