1 package org.apache.turbine.services.xmlrpc; 2 3 18 19 import java.io.InputStream ; 20 import java.net.InetAddress ; 21 import java.net.Socket ; 22 import java.net.URL ; 23 import java.net.UnknownHostException ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Vector ; 27 28 import javax.servlet.ServletConfig ; 29 30 import org.apache.commons.configuration.Configuration; 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.apache.xerces.parsers.SAXParser; 36 import org.apache.xmlrpc.WebServer; 37 import org.apache.xmlrpc.XmlRpc; 38 import org.apache.xmlrpc.XmlRpcClient; 39 import org.apache.xmlrpc.XmlRpcServer; 40 import org.apache.xmlrpc.secure.SecureWebServer; 41 42 import org.apache.turbine.services.InitializationException; 43 import org.apache.turbine.services.TurbineBaseService; 44 import org.apache.turbine.services.xmlrpc.util.FileTransfer; 45 import org.apache.turbine.util.TurbineException; 46 47 73 public class TurbineXmlRpcService 74 extends TurbineBaseService 75 implements XmlRpcService 76 { 77 78 private static Log log = LogFactory.getLog(TurbineXmlRpcService.class); 79 80 84 protected boolean isModernVersion = false; 85 86 87 protected WebServer webserver = null; 88 89 90 protected XmlRpcServer server = null; 91 92 96 private InetAddress address = null; 97 98 99 protected int port = 0; 100 101 109 public void init() 110 throws InitializationException 111 { 112 Configuration conf = getConfiguration(); 113 114 try 115 { 116 server = new XmlRpcServer(); 117 118 Configuration secureServerOptions = 120 conf.subset("secure.server.option"); 121 122 if (secureServerOptions != null) 123 { 124 setSystemPropertiesFromConfiguration(secureServerOptions); 125 } 126 127 String addr = conf.getString("address", "0.0.0.0"); 129 port = conf.getInt("port", 0); 130 131 if (port != 0) 132 { 133 if (addr != null && addr.length() > 0) 134 { 135 try 136 { 137 address = InetAddress.getByName(addr); 138 } 139 catch (UnknownHostException useDefault) 140 { 141 address = null; 142 } 143 } 144 145 log.debug("Port: " + port + ", Address: " + address); 146 147 if (conf.getBoolean("secure.server", false)) 148 { 149 webserver = new SecureWebServer(port, address); 150 } 151 else 152 { 153 webserver = new WebServer(port, address); 154 } 155 } 156 157 String saxParserClass = 159 conf.getString("parser", SAXParser.class.getName()); 160 161 XmlRpc.setDriver(saxParserClass); 162 163 for (Iterator keys = conf.getKeys("handler"); keys.hasNext();) 165 { 166 String handler = (String ) keys.next(); 167 String handlerName = handler.substring(handler.indexOf('.')+1); 168 String handlerClass = conf.getString(handler); 169 170 log.debug("Found Handler " + handler + " as " + handlerName + " / " + handlerClass); 171 172 registerHandler(handlerName, handlerClass); 173 } 174 175 boolean stateOfParanoia = 177 conf.getBoolean("paranoid", false); 178 179 if (stateOfParanoia) 180 { 181 webserver.setParanoid(stateOfParanoia); 182 log.info(XmlRpcService.SERVICE_NAME + 183 ": Operating in a state of paranoia"); 184 185 189 List acceptedClients = 193 conf.getList("acceptClient"); 194 195 for (int i = 0; i < acceptedClients.size(); i++) 196 { 197 String acceptClient = (String ) acceptedClients.get(i); 198 199 if (StringUtils.isNotEmpty(acceptClient)) 200 { 201 webserver.acceptClient(acceptClient); 202 log.info(XmlRpcService.SERVICE_NAME + 203 ": Accepting client -> " + acceptClient); 204 } 205 } 206 207 List deniedClients = conf.getList("denyClient"); 211 212 for (int i = 0; i < deniedClients.size(); i++) 213 { 214 String denyClient = (String ) deniedClients.get(i); 215 216 if (StringUtils.isNotEmpty(denyClient)) 217 { 218 webserver.denyClient(denyClient); 219 log.info(XmlRpcService.SERVICE_NAME + 220 ": Denying client -> " + denyClient); 221 } 222 } 223 } 224 try 227 { 228 Class.forName("org.apache.xmlrpc.XmlRpcRequest"); 229 isModernVersion = true; 230 webserver.start(); 231 } 232 catch (ClassNotFoundException ignored) 233 { 234 } 237 log.debug(XmlRpcService.SERVICE_NAME + ": Using " + 238 "Apache XML-RPC version " + 239 (isModernVersion ? 240 "greater than 1.1" : "1.1 or lower")); 241 } 242 catch (Exception e) 243 { 244 String errorMessage = "XMLRPCService failed to initialize"; 245 log.error(errorMessage, e); 246 throw new InitializationException(errorMessage, e); 247 } 248 249 setInit(true); 250 } 251 252 257 public void init(ServletConfig config) throws InitializationException 258 { 259 init(); 260 } 261 262 271 private void setSystemPropertiesFromConfiguration(Configuration configuration) 272 { 273 for (Iterator i = configuration.getKeys(); i.hasNext();) 274 { 275 String key = (String ) i.next(); 276 String value = configuration.getString(key); 277 278 log.debug("JSSE option: " + key + " => " + value); 279 280 System.setProperty(key, value); 281 } 282 } 283 284 289 public void registerHandler(Object handler) 290 { 291 registerHandler("$default", handler); 292 } 293 294 300 public void registerHandler(String handlerName, 301 Object handler) 302 { 303 if (webserver != null) 304 { 305 webserver.addHandler(handlerName, handler); 306 } 307 308 server.addHandler(handlerName, handler); 309 310 log.debug("Registered Handler " + handlerName + " as " 311 + handler.getClass().getName() 312 + ", Server: " + server 313 + ", Webserver: " + webserver); 314 } 315 316 326 public void registerHandler(String handlerName, String handlerClass) 327 throws TurbineException 328 { 329 try 330 { 331 Object handler = Class.forName(handlerClass).newInstance(); 332 333 if (webserver != null) 334 { 335 webserver.addHandler(handlerName, handler); 336 } 337 338 server.addHandler(handlerName, handler); 339 } 340 catch (ThreadDeath t) 342 { 343 throw t; 344 } 345 catch (OutOfMemoryError t) 346 { 347 throw t; 348 } 349 350 catch (Throwable t) 351 { 352 throw new TurbineException 353 ("Failed to instantiate " + handlerClass, t); 354 } 355 } 356 357 362 public void unregisterHandler(String handlerName) 363 { 364 if (webserver != null) 365 { 366 webserver.removeHandler(handlerName); 367 } 368 369 server.removeHandler(handlerName); 370 } 371 372 381 public byte[] handleRequest(InputStream is) 382 { 383 return server.execute(is); 384 } 385 386 401 public byte[] handleRequest(InputStream is, String user, String password) 402 { 403 return server.execute(is, user, password); 404 } 405 406 418 public Object executeRpc(URL url, 419 String methodName, 420 Vector params) 421 throws TurbineException 422 { 423 try 424 { 425 XmlRpcClient client = new XmlRpcClient(url); 426 return client.execute(methodName, params); 427 } 428 catch (Exception e) 429 { 430 throw new TurbineException("XML-RPC call failed", e); 431 } 432 } 433 434 448 public Object executeAuthenticatedRpc(URL url, 449 String username, 450 String password, 451 String methodName, 452 Vector params) 453 throws TurbineException 454 { 455 try 456 { 457 XmlRpcClient client = new XmlRpcClient(url); 458 client.setBasicAuthentication(username, password); 459 return client.execute(methodName, params); 460 } 461 catch (Exception e) 462 { 463 throw new TurbineException("XML-RPC call failed", e); 464 } 465 } 466 467 478 public void send(String serverURL, 479 String sourceLocationProperty, 480 String sourceFileName, 481 String destinationLocationProperty, 482 String destinationFileName) 483 throws TurbineException 484 { 485 FileTransfer.send(serverURL, 486 sourceLocationProperty, 487 sourceFileName, 488 destinationLocationProperty, 489 destinationFileName); 490 } 491 492 506 public void send(String serverURL, 507 String username, 508 String password, 509 String sourceLocationProperty, 510 String sourceFileName, 511 String destinationLocationProperty, 512 String destinationFileName) 513 throws TurbineException 514 { 515 FileTransfer.send(serverURL, 516 username, 517 password, 518 sourceLocationProperty, 519 sourceFileName, 520 destinationLocationProperty, 521 destinationFileName); 522 } 523 524 535 public void get(String serverURL, 536 String sourceLocationProperty, 537 String sourceFileName, 538 String destinationLocationProperty, 539 String destinationFileName) 540 throws TurbineException 541 { 542 FileTransfer.get(serverURL, 543 sourceLocationProperty, 544 sourceFileName, 545 destinationLocationProperty, 546 destinationFileName); 547 } 548 549 563 public void get(String serverURL, 564 String username, 565 String password, 566 String sourceLocationProperty, 567 String sourceFileName, 568 String destinationLocationProperty, 569 String destinationFileName) 570 throws TurbineException 571 { 572 FileTransfer.get(serverURL, 573 username, 574 password, 575 sourceLocationProperty, 576 sourceFileName, 577 destinationLocationProperty, 578 destinationFileName); 579 } 580 581 591 public void remove(String serverURL, 592 String sourceLocationProperty, 593 String sourceFileName) 594 throws TurbineException 595 { 596 FileTransfer.remove(serverURL, 597 sourceLocationProperty, 598 sourceFileName); 599 } 600 601 613 public void remove(String serverURL, 614 String username, 615 String password, 616 String sourceLocationProperty, 617 String sourceFileName) 618 throws TurbineException 619 { 620 FileTransfer.remove(serverURL, 621 username, 622 password, 623 sourceLocationProperty, 624 sourceFileName); 625 } 626 627 635 public void setParanoid(boolean state) 636 { 637 webserver.setParanoid(state); 638 } 639 640 651 public void acceptClient(String address) 652 { 653 webserver.acceptClient(address); 654 } 655 656 666 public void denyClient(String address) 667 { 668 webserver.denyClient(address); 669 } 670 671 674 public void shutdown() 675 { 676 webserver.shutdown(); 678 679 if (!isModernVersion) 680 { 681 try 684 { 685 Socket interrupt = new Socket (address, port); 686 interrupt.close(); 687 } 688 catch (Exception notShutdown) 689 { 690 log.warn(XmlRpcService.SERVICE_NAME + 693 "It's possible the xmlrpc server was not " + 694 "shutdown: " + notShutdown.getMessage()); 695 } 696 } 697 698 setInit(false); 699 } 700 } 701 | Popular Tags |