KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > Admin


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.utils;
57
58 import org.jboss.axis.AxisEngine;
59 import org.jboss.axis.AxisFault;
60 import org.jboss.axis.Constants;
61 import org.jboss.axis.EngineConfiguration;
62 import org.jboss.axis.Handler;
63 import org.jboss.axis.MessageContext;
64 import org.jboss.axis.WSDDEngineConfiguration;
65 import org.jboss.axis.client.AxisClient;
66 import org.jboss.axis.deployment.wsdd.WSDDConstants;
67 import org.jboss.axis.deployment.wsdd.WSDDDeployment;
68 import org.jboss.axis.deployment.wsdd.WSDDDocument;
69 import org.jboss.axis.encoding.SerializationContext;
70 import org.jboss.axis.encoding.SerializationContextImpl;
71 import org.jboss.axis.server.AxisServer;
72 import org.jboss.logging.Logger;
73 import org.w3c.dom.Document JavaDoc;
74 import org.w3c.dom.Element JavaDoc;
75 import org.xml.sax.InputSource JavaDoc;
76
77 import java.io.FileInputStream JavaDoc;
78 import java.io.StringReader JavaDoc;
79 import java.io.StringWriter JavaDoc;
80 import java.net.InetAddress JavaDoc;
81 import java.net.UnknownHostException JavaDoc;
82
83 /**
84  * Handy static utility functions for turning XML into
85  * Axis deployment operations.
86  *
87  * @author Doug Davis (dug@us.ibm.com)
88  * @author Glen Daniels (gdaniels@macromedia.com)
89  */

90 public class Admin
91 {
92    private static Logger log = Logger.getLogger(Admin.class.getName());
93
94    /**
95     * Process a given XML document - needs cleanup.
96     */

97    public Element JavaDoc[] AdminService(Element JavaDoc[] xml)
98            throws Exception JavaDoc
99    {
100       log.debug("Enter: Admin::AdminService");
101       MessageContext msgContext = MessageContext.getCurrentContext();
102       Document doc = process(msgContext, xml[0]);
103       Element JavaDoc[] result = new Element JavaDoc[1];
104       result[0] = doc.getDocumentElement();
105       log.debug("Exit: Admin::AdminService");
106       return result;
107    }
108
109    protected static Document processWSDD(MessageContext msgContext,
110                                          AxisEngine engine,
111                                          Element JavaDoc root)
112            throws Exception JavaDoc
113    {
114       Document doc = null;
115
116       String JavaDoc action = root.getLocalName();
117       if (action.equals("passwd"))
118       {
119          String JavaDoc newPassword = root.getFirstChild().getNodeValue();
120          engine.setAdminPassword(newPassword);
121          doc = XMLUtils.newDocument();
122          doc.appendChild(root = doc.createElementNS("", "Admin"));
123          root.appendChild(doc.createTextNode(Messages.getMessage("done00")));
124          return doc;
125       }
126
127       if (action.equals("quit"))
128       {
129          log.error(Messages.getMessage("quitRequest00"));
130          if (msgContext != null)
131          {
132             // put a flag into message context so listener will exit after
133
// sending response
134
msgContext.setProperty(MessageContext.QUIT_REQUESTED, "true");
135          }
136          doc = XMLUtils.newDocument();
137          doc.appendChild(root = doc.createElementNS("", "Admin"));
138          root.appendChild(doc.createTextNode(Messages.getMessage("quit00", "")));
139          return doc;
140       }
141
142       if (action.equals("list"))
143       {
144          return listConfig(engine);
145       }
146
147       if (action.equals("clientdeploy"))
148       {
149          // set engine to client engine
150
engine = engine.getClientEngine();
151       }
152
153       WSDDDocument wsddDoc = new WSDDDocument(root);
154       EngineConfiguration config = engine.getConfig();
155       if (config instanceof WSDDEngineConfiguration)
156       {
157          WSDDDeployment deployment =
158                  ((WSDDEngineConfiguration)config).getDeployment();
159          wsddDoc.deploy(deployment);
160       }
161       engine.refreshGlobalOptions();
162
163       engine.saveConfiguration();
164
165       doc = XMLUtils.newDocument();
166       doc.appendChild(root = doc.createElementNS("", "Admin"));
167       root.appendChild(doc.createTextNode(Messages.getMessage("done00")));
168
169       return doc;
170    }
171
172    protected void preProcessWSDD(MessageContext msgContext, AxisEngine engine, Element JavaDoc root)
173    {
174       // NoOp (for any extenders to implement any preprocessing desired
175
}
176
177    /**
178     * The meat of the Admin service. Process an xML document rooted with
179     * a "deploy", "undeploy", "list", or "quit" element.
180     *
181     * @param msgContext the MessageContext we're processing
182     * @param root the root Element of the XML
183     * @return an XML Document indicating the results.
184     */

185    public Document process(MessageContext msgContext, Element JavaDoc root)
186            throws Exception JavaDoc
187    {
188       // Check security FIRST.
189

190       /** Might do something like this once security is a little more
191        * integrated.
192        if (!engine.hasSafePassword() &&
193        !action.equals("passwd"))
194        throw new AxisFault("Server.MustSetPassword",
195        "You must change the admin password before administering Axis!",
196        null, null);
197        */

198
199       /** For now, though - make sure we can only admin from our own
200        * IP, unless the remoteAdmin option is set.
201        */

202       Handler serviceHandler = msgContext.getService();
203       if (serviceHandler != null &&
204               !JavaUtils.isTrueExplicitly(serviceHandler.getOption("enableRemoteAdmin")))
205       {
206
207          String JavaDoc remoteIP = msgContext.getStrProp(Constants.MC_REMOTE_ADDR);
208          if (remoteIP != null &&
209                  !remoteIP.equals("127.0.0.1"))
210          {
211
212             try
213             {
214                InetAddress JavaDoc myAddr = InetAddress.getLocalHost();
215                InetAddress JavaDoc remoteAddr =
216                        InetAddress.getByName(remoteIP);
217                if (log.isDebugEnabled())
218                {
219                   log.debug("Comparing remote caller " + remoteAddr + " to " + myAddr);
220                }
221
222
223                if (!myAddr.equals(remoteAddr))
224                {
225                   log.error(Messages.getMessage("noAdminAccess01",
226                           remoteAddr.toString()));
227                   throw new AxisFault("Server.Unauthorized",
228                           Messages.getMessage("noAdminAccess00"),
229                           null, null);
230                }
231             }
232             catch (UnknownHostException JavaDoc e)
233             {
234                throw new AxisFault("Server.UnknownHost",
235                        Messages.getMessage("unknownHost00"),
236                        null, null);
237             }
238          }
239       }
240
241       String JavaDoc rootNS = root.getNamespaceURI();
242       AxisEngine engine = msgContext.getAxisEngine();
243
244       // If this is WSDD, process it correctly.
245
if (rootNS != null && rootNS.equals(WSDDConstants.URI_WSDD))
246       {
247          preProcessWSDD(msgContext, engine, root);
248          return processWSDD(msgContext, engine, root);
249       }
250
251       // Else fault
252
// TODO: Better handling here
253
throw new Exception JavaDoc("FIXME");
254    }
255
256    /**
257     * Get an XML document representing this engine's configuration.
258     * <p/>
259     * This document is suitable for saving and reloading into the
260     * engine.
261     *
262     * @param engine the AxisEngine to work with
263     * @return an XML document holding the engine config
264     * @throws AxisFault
265     */

266    public static Document listConfig(AxisEngine engine)
267            throws AxisFault
268    {
269       StringWriter JavaDoc writer = new StringWriter JavaDoc();
270       SerializationContext context = new SerializationContextImpl(writer, null);
271       context.setPretty(true);
272       try
273       {
274          EngineConfiguration config = engine.getConfig();
275
276          if (config instanceof WSDDEngineConfiguration)
277          {
278             WSDDDeployment deployment =
279                     ((WSDDEngineConfiguration)config).getDeployment();
280             deployment.writeToContext(context);
281          }
282       }
283       catch (Exception JavaDoc e)
284       {
285          // If the engine config isn't a FileProvider, or we have no
286
// engine config for some odd reason, we'll end up here.
287

288          throw new AxisFault(Messages.getMessage("noEngineWSDD"));
289       }
290
291       try
292       {
293          writer.close();
294          return XMLUtils.newDocument(new InputSource JavaDoc(new StringReader JavaDoc(writer.getBuffer().toString())));
295       }
296       catch (Exception JavaDoc e)
297       {
298          log.error("exception00", e);
299          return null;
300       }
301    }
302
303    public static void main(String JavaDoc args[]) throws Exception JavaDoc
304    {
305       int i = 0;
306
307       if (args.length < 2 || !(args[0].equals("client") ||
308               args[0].equals("server")))
309       {
310          log.error(Messages.getMessage("usage00", "Admin client|server <xml-file>"));
311
312          log.error(Messages.getMessage("where00", "<xml-file>"));
313          log.error("<deploy>");
314          /*
315          log.error( " <transport name=a request=\"a,b,c\" sender=\"s\"");
316          log.error( " response=\"d,e\"/>" );
317          */

318          log.error(" <handler name=a class=className/>");
319          log.error(" <chain name=a flow=\"a,b,c\" />");
320          log.error(" <chain name=a request=\"a,b,c\" pivot=\"d\"");
321          log.error(" response=\"e,f,g\" />");
322          log.error(" <service name=a handler=b />");
323          log.error("</deploy>");
324          log.error("<undeploy>");
325          log.error(" <handler name=a/>");
326          log.error(" <chain name=a/>");
327          log.error(" <service name=a/>");
328          log.error("</undeploy>");
329          log.error("<list/>");
330
331
332          // throw an Exception which will go uncaught! this way, a test
333
// suite can invoke main() and detect the exception
334
throw new IllegalArgumentException JavaDoc(Messages.getMessage("usage00",
335                  "Admin client|server <xml-file>"));
336          // System.exit( 1 );
337
}
338
339       Admin admin = new Admin();
340
341       AxisEngine engine;
342       if (args[0].equals("client"))
343          engine = new AxisClient();
344       else
345          engine = new AxisServer();
346       engine.setShouldSaveConfig(true);
347       engine.init();
348       MessageContext msgContext = new MessageContext(engine);
349
350       try
351       {
352          for (i = 1; i < args.length; i++)
353          {
354             if (log.isDebugEnabled())
355                log.debug(Messages.getMessage("process00", args[i]));
356
357             Document doc = XMLUtils.newDocument(new FileInputStream JavaDoc(args[i]));
358             admin.process(msgContext, doc.getDocumentElement());
359          }
360       }
361       catch (Exception JavaDoc e)
362       {
363          log.error(Messages.getMessage("errorProcess00", args[i]), e);
364          //System.exit( 1 );
365
throw e;
366       }
367    }
368 }
369
Popular Tags