1 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 ; 35 import org.w3c.dom.Element ; 36 import org.xml.sax.InputSource ; 37 38 import java.io.FileInputStream ; 39 import java.io.StringReader ; 40 import java.io.StringWriter ; 41 import java.net.InetAddress ; 42 import java.net.UnknownHostException ; 43 44 51 public class Admin 52 { 53 protected static Log log = 54 LogFactory.getLog(Admin.class.getName()); 55 56 59 public Element [] AdminService(Element [] xml) 60 throws Exception 61 { 62 log.debug("Enter: Admin::AdminService"); 63 MessageContext msgContext = MessageContext.getCurrentContext(); 64 Document doc = process( msgContext, xml[0] ); 65 Element [] result = new Element [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 root) 74 throws Exception 75 { 76 Document doc = null ; 77 78 String action = root.getLocalName(); 79 if (action.equals("passwd")) { 80 String 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 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 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 136 public Document process(MessageContext msgContext, Element root) 137 throws Exception 138 { 139 141 149 150 verifyHostAllowed(msgContext); 151 152 String rootNS = root.getNamespaceURI(); 153 AxisEngine engine = msgContext.getAxisEngine(); 154 155 if (rootNS != null && rootNS.equals(WSDDConstants.URI_WSDD)) { 157 return processWSDD(msgContext, engine, root); 158 } 159 160 throw new Exception (Messages.getMessage("adminServiceNoWSDD")); 163 } 164 165 170 private void verifyHostAllowed(MessageContext msgContext) throws AxisFault { 171 174 Handler serviceHandler = msgContext.getService(); 175 if (serviceHandler != null && 176 !JavaUtils.isTrueExplicitly(serviceHandler.getOption("enableRemoteAdmin"))) { 177 178 String remoteIP = msgContext.getStrProp(Constants.MC_REMOTE_ADDR); 179 if (remoteIP != null && 180 !remoteIP.equals("127.0.0.1")) { 181 182 try { 183 InetAddress myAddr = InetAddress.getLocalHost(); 184 InetAddress 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 e) { 199 throw new AxisFault("Server.UnknownHost", 200 Messages.getMessage("unknownHost00"), 201 null, null); 202 } 203 } 204 } 205 } 206 207 216 public static Document listConfig(AxisEngine engine) 217 throws AxisFault 218 { 219 StringWriter writer = new StringWriter (); 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 e) { 231 234 throw new AxisFault(Messages.getMessage("noEngineWSDD")); 235 } 236 237 try { 238 writer.close(); 239 return XMLUtils.newDocument(new InputSource (new StringReader (writer.getBuffer().toString()))); 240 } catch (Exception e) { 241 log.error("exception00", e); 242 return null; 243 } 244 } 245 246 public static void main(String args[]) throws Exception { 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 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 new IllegalArgumentException ( 276 Messages.getMessage("usage00", 277 "Admin client|server <xml-file>")); 278 } 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 ( 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 e ) { 305 log.error( Messages.getMessage("errorProcess00", args[i]), e ); 306 throw e; 307 } 308 } 309 } 310 | Popular Tags |