KickJava   Java API By Example, From Geeks To Geeks.

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


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.utils ;
18
19 import org.apache.axis.AxisEngine;
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Constants;
22 import org.apache.axis.EngineConfiguration;
23 import org.apache.axis.Handler;
24 import org.apache.axis.MessageContext;
25 import org.apache.axis.WSDDEngineConfiguration;
26 import org.apache.axis.client.AxisClient;
27 import org.apache.axis.components.logger.LogFactory;
28 import org.apache.axis.deployment.wsdd.WSDDConstants;
29 import org.apache.axis.deployment.wsdd.WSDDDeployment;
30 import org.apache.axis.deployment.wsdd.WSDDDocument;
31 import org.apache.axis.encoding.SerializationContext;
32 import org.apache.axis.server.AxisServer;
33 import org.apache.commons.logging.Log;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37
38 import java.io.FileInputStream JavaDoc;
39 import java.io.StringReader JavaDoc;
40 import java.io.StringWriter JavaDoc;
41 import java.net.InetAddress JavaDoc;
42 import java.net.UnknownHostException JavaDoc;
43
44 /**
45  * Handy static utility functions for turning XML into
46  * Axis deployment operations.
47  *
48  * @author Doug Davis (dug@us.ibm.com)
49  * @author Glen Daniels (gdaniels@apache.org)
50  */

51 public class Admin
52 {
53     protected static Log log =
54         LogFactory.getLog(Admin.class.getName());
55
56     /**
57      * Process a given XML document - needs cleanup.
58      */

59     public Element JavaDoc[] AdminService(Element JavaDoc [] xml)
60         throws Exception JavaDoc
61     {
62         log.debug("Enter: Admin::AdminService");
63         MessageContext msgContext = MessageContext.getCurrentContext();
64         Document doc = process( msgContext, xml[0] );
65         Element JavaDoc[] result = new Element JavaDoc[1];
66         result[0] = doc.getDocumentElement();
67         log.debug("Exit: Admin::AdminService");
68         return result;
69     }
70
71     protected static Document processWSDD(MessageContext msgContext,
72                                           AxisEngine engine,
73                                           Element JavaDoc root)
74         throws Exception JavaDoc
75     {
76         Document doc = null ;
77
78         String JavaDoc action = root.getLocalName();
79         if (action.equals("passwd")) {
80             String JavaDoc newPassword = root.getFirstChild().getNodeValue();
81             engine.setAdminPassword(newPassword);
82             doc = XMLUtils.newDocument();
83             doc.appendChild( root = doc.createElementNS("", "Admin" ) );
84             root.appendChild( doc.createTextNode( Messages.getMessage("done00") ) );
85             return doc;
86         }
87
88         if (action.equals("quit")) {
89             log.error(Messages.getMessage("quitRequest00"));
90             if (msgContext != null) {
91                 // put a flag into message context so listener will exit after
92
// sending response
93
msgContext.setProperty(MessageContext.QUIT_REQUESTED, "true");
94             }
95             doc = XMLUtils.newDocument();
96             doc.appendChild( root = doc.createElementNS("", "Admin" ) );
97             root.appendChild( doc.createTextNode( Messages.getMessage("quit00", "") ) );
98             return doc;
99         }
100
101         if ( action.equals("list") ) {
102             return listConfig(engine);
103         }
104
105         if (action.equals("clientdeploy")) {
106             // set engine to client engine
107
engine = engine.getClientEngine();
108         }
109
110         WSDDDocument wsddDoc = new WSDDDocument(root);
111         EngineConfiguration config = engine.getConfig();
112         if (config instanceof WSDDEngineConfiguration) {
113             WSDDDeployment deployment =
114                 ((WSDDEngineConfiguration)config).getDeployment();
115             wsddDoc.deploy(deployment);
116         }
117         engine.refreshGlobalOptions();
118
119         engine.saveConfiguration();
120
121         doc = XMLUtils.newDocument();
122         doc.appendChild( root = doc.createElementNS("", "Admin" ) );
123         root.appendChild( doc.createTextNode( Messages.getMessage("done00") ) );
124
125         return doc;
126     }
127
128     /**
129      * The meat of the Admin service. Process an xML document rooted with
130      * a "deploy", "undeploy", "list", or "quit" element.
131      *
132      * @param msgContext the MessageContext we're processing
133      * @param root the root Element of the XML
134      * @return an XML Document indicating the results.
135      */

136     public Document process(MessageContext msgContext, Element JavaDoc root)
137         throws Exception JavaDoc
138     {
139         // Check security FIRST.
140

141         /** Might do something like this once security is a little more
142          * integrated.
143         if (!engine.hasSafePassword() &&
144             !action.equals("passwd"))
145             throw new AxisFault("Server.MustSetPassword",
146           "You must change the admin password before administering Axis!",
147                                  null, null);
148          */

149
150         verifyHostAllowed(msgContext);
151
152         String JavaDoc rootNS = root.getNamespaceURI();
153         AxisEngine engine = msgContext.getAxisEngine();
154
155         // If this is WSDD, process it correctly.
156
if (rootNS != null && rootNS.equals(WSDDConstants.URI_WSDD)) {
157             return processWSDD(msgContext, engine, root);
158         }
159
160         // Else fault
161
// TODO: Better handling here
162
throw new Exception JavaDoc(Messages.getMessage("adminServiceNoWSDD"));
163     }
164
165     /**
166      * host validation logic goes here
167      * @param msgContext
168      * @throws AxisFault
169      */

170     private void verifyHostAllowed(MessageContext msgContext) throws AxisFault {
171         /** For now, though - make sure we can only admin from our own
172          * IP, unless the remoteAdmin option is set.
173          */

174         Handler serviceHandler = msgContext.getService();
175         if (serviceHandler != null &&
176             !JavaUtils.isTrueExplicitly(serviceHandler.getOption("enableRemoteAdmin"))) {
177
178             String JavaDoc remoteIP = msgContext.getStrProp(Constants.MC_REMOTE_ADDR);
179             if (remoteIP != null &&
180                 !remoteIP.equals("127.0.0.1")) {
181
182                 try {
183                     InetAddress JavaDoc myAddr = InetAddress.getLocalHost();
184                     InetAddress JavaDoc remoteAddr =
185                             InetAddress.getByName(remoteIP);
186                     if(log.isDebugEnabled()) {
187                         log.debug("Comparing remote caller " + remoteAddr +" to "+ myAddr);
188                     }
189
190
191                     if (!myAddr.equals(remoteAddr)) {
192                         log.error(Messages.getMessage("noAdminAccess01",
193                                 remoteAddr.toString()));
194                         throw new AxisFault("Server.Unauthorized",
195                            Messages.getMessage("noAdminAccess00"),
196                            null, null);
197                     }
198                 } catch (UnknownHostException JavaDoc e) {
199                     throw new AxisFault("Server.UnknownHost",
200                         Messages.getMessage("unknownHost00"),
201                         null, null);
202                 }
203             }
204         }
205     }
206
207     /** Get an XML document representing this engine's configuration.
208      *
209      * This document is suitable for saving and reloading into the
210      * engine.
211      *
212      * @param engine the AxisEngine to work with
213      * @return an XML document holding the engine config
214      * @exception AxisFault
215      */

216     public static Document listConfig(AxisEngine engine)
217         throws AxisFault
218     {
219         StringWriter JavaDoc writer = new StringWriter JavaDoc();
220         SerializationContext context = new SerializationContext(writer, null);
221         context.setPretty(true);
222         try {
223             EngineConfiguration config = engine.getConfig();
224
225             if (config instanceof WSDDEngineConfiguration) {
226                 WSDDDeployment deployment =
227                     ((WSDDEngineConfiguration)config).getDeployment();
228                 deployment.writeToContext(context);
229             }
230         } catch (Exception JavaDoc e) {
231             // If the engine config isn't a FileProvider, or we have no
232
// engine config for some odd reason, we'll end up here.
233

234             throw new AxisFault(Messages.getMessage("noEngineWSDD"));
235         }
236
237         try {
238             writer.close();
239             return XMLUtils.newDocument(new InputSource JavaDoc(new StringReader JavaDoc(writer.getBuffer().toString())));
240         } catch (Exception JavaDoc e) {
241             log.error("exception00", e);
242             return null;
243         }
244     }
245
246     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
247         int i = 0 ;
248
249         if ( args.length < 2 || !(args[0].equals("client") ||
250                                   args[0].equals("server")) ) {
251             log.error( Messages.getMessage("usage00", "Admin client|server <xml-file>") );
252
253             log.error( Messages.getMessage("where00", "<xml-file>") );
254             log.error( "<deploy>" );
255             /*
256             log.error( " <transport name=a request=\"a,b,c\" sender=\"s\"");
257             log.error( " response=\"d,e\"/>" );
258             */

259             log.error( " <handler name=a class=className/>" );
260             log.error( " <chain name=a flow=\"a,b,c\" />" );
261             log.error( " <chain name=a request=\"a,b,c\" pivot=\"d\"" );
262             log.error( " response=\"e,f,g\" />" );
263             log.error( " <service name=a handler=b />" );
264             log.error( "</deploy>" );
265             log.error( "<undeploy>" );
266             log.error( " <handler name=a/>" );
267             log.error( " <chain name=a/>" );
268             log.error( " <service name=a/>" );
269             log.error( "</undeploy>" );
270             log.error( "<list/>" );
271
272
273             // throw an Exception which will go uncaught! this way, a test
274
// suite can invoke main() and detect the exception
275
throw new IllegalArgumentException JavaDoc(
276                     Messages.getMessage("usage00",
277                                          "Admin client|server <xml-file>"));
278             // System.exit( 1 );
279
}
280
281         Admin admin = new Admin();
282
283         AxisEngine engine;
284         if ( args[0].equals("client") )
285             engine = new AxisClient();
286         else
287             engine = new AxisServer();
288         engine.setShouldSaveConfig(true);
289         engine.init();
290         MessageContext msgContext = new MessageContext(engine);
291
292         try {
293             for ( i = 1 ; i < args.length ; i++ ) {
294                 if (log.isDebugEnabled())
295                     log.debug( Messages.getMessage("process00", args[i]) );
296
297                 Document doc = XMLUtils.newDocument( new FileInputStream JavaDoc( args[i] ) );
298                 Document result = admin.process(msgContext, doc.getDocumentElement());
299                 if (result != null) {
300                     System.out.println(XMLUtils.DocumentToString(result));
301                 }
302             }
303         }
304         catch( Exception JavaDoc e ) {
305             log.error( Messages.getMessage("errorProcess00", args[i]), e );
306             throw e;
307         }
308     }
309 }
310
Popular Tags