1 17 18 19 package org.apache.catalina.manager.host; 20 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.PrintWriter ; 28 import java.util.StringTokenizer ; 29 30 import javax.management.MBeanServer ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.UnavailableException ; 33 import javax.servlet.http.HttpServlet ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.apache.catalina.Container; 38 import org.apache.catalina.ContainerServlet; 39 import org.apache.catalina.Context; 40 import org.apache.catalina.Engine; 41 import org.apache.catalina.Globals; 42 import org.apache.catalina.Host; 43 import org.apache.catalina.Lifecycle; 44 import org.apache.catalina.Wrapper; 45 import org.apache.catalina.core.StandardHost; 46 import org.apache.catalina.startup.HostConfig; 47 import org.apache.catalina.util.StringManager; 48 import org.apache.tomcat.util.modeler.Registry; 49 import org.apache.catalina.core.ContainerBase; 50 51 52 99 100 public class HostManagerServlet 101 extends HttpServlet implements ContainerServlet { 102 103 104 106 107 110 protected File configBase = null; 111 112 113 116 protected Context context = null; 117 118 119 122 protected int debug = 1; 123 124 125 128 protected Host host = null; 129 130 131 134 protected Engine engine = null; 135 136 137 140 protected MBeanServer mBeanServer = null; 141 142 143 146 protected static StringManager sm = 147 StringManager.getManager(Constants.Package); 148 149 150 153 protected Wrapper wrapper = null; 154 155 156 158 159 162 public Wrapper getWrapper() { 163 164 return (this.wrapper); 165 166 } 167 168 169 174 public void setWrapper(Wrapper wrapper) { 175 176 this.wrapper = wrapper; 177 if (wrapper == null) { 178 context = null; 179 host = null; 180 engine = null; 181 } else { 182 context = (Context) wrapper.getParent(); 183 host = (Host) context.getParent(); 184 engine = (Engine) host.getParent(); 185 } 186 187 mBeanServer = Registry.getRegistry(null, null).getMBeanServer(); 189 190 } 191 192 193 195 196 199 public void destroy() { 200 201 ; 203 } 204 205 206 215 public void doGet(HttpServletRequest request, 216 HttpServletResponse response) 217 throws IOException , ServletException { 218 219 if (request.getAttribute(Globals.INVOKED_ATTR) != null) 221 throw new UnavailableException 222 (sm.getString("hostManagerServlet.cannotInvoke")); 223 224 String command = request.getPathInfo(); 226 if (command == null) 227 command = request.getServletPath(); 228 String name = request.getParameter("name"); 229 230 response.setContentType("text/plain; charset=" + Constants.CHARSET); 232 PrintWriter writer = response.getWriter(); 233 234 if (command == null) { 236 writer.println(sm.getString("hostManagerServlet.noCommand")); 237 } else if (command.equals("/add")) { 238 add(request, writer, name, false); 239 } else if (command.equals("/remove")) { 240 remove(writer, name); 241 } else if (command.equals("/list")) { 242 list(writer); 243 } else if (command.equals("/start")) { 244 start(writer, name); 245 } else if (command.equals("/stop")) { 246 stop(writer, name); 247 } else { 248 writer.println(sm.getString("hostManagerServlet.unknownCommand", 249 command)); 250 } 251 252 writer.flush(); 254 writer.close(); 255 256 } 257 258 259 267 protected void add(HttpServletRequest request, PrintWriter writer, String name, boolean htmlMode ) { 268 String aliases = request.getParameter("aliases"); 269 String appBase = request.getParameter("appBase"); 270 boolean manager = booleanParameter(request, "manager", false, htmlMode); 271 boolean autoDeploy = booleanParameter(request, "autoDeploy", true, htmlMode); 272 boolean deployOnStartup = booleanParameter(request, "deployOnStartup", true, htmlMode); 273 boolean deployXML = booleanParameter(request, "deployXML", true, htmlMode); 274 boolean unpackWARs = booleanParameter(request, "unpackWARs", true, htmlMode); 275 boolean xmlNamespaceAware = booleanParameter(request, "xmlNamespaceAware", false, htmlMode); 276 boolean xmlValidation = booleanParameter(request, "xmlValidation", false, htmlMode); 277 add(writer, name, aliases, appBase, manager, 278 autoDeploy, 279 deployOnStartup, 280 deployXML, 281 unpackWARs, 282 xmlNamespaceAware, 283 xmlValidation); 284 } 285 286 287 295 protected boolean booleanParameter(HttpServletRequest request, 296 String parameter, boolean theDefault, boolean htmlMode) { 297 String value = request.getParameter(parameter); 298 boolean booleanValue = theDefault; 299 if (value != null) { 300 if (htmlMode) { 301 if (value.equals("on")) { 302 booleanValue = true; 303 } 304 } else if (theDefault) { 305 if (value.equals("false")) { 306 booleanValue = false; 307 } 308 } else if (value.equals("true")) { 309 booleanValue = true; 310 } 311 } else if (htmlMode) 312 booleanValue = false; 313 return booleanValue; 314 } 315 316 317 320 public void init() throws ServletException { 321 322 if ((wrapper == null) || (context == null)) 324 throw new UnavailableException 325 (sm.getString("hostManagerServlet.noWrapper")); 326 327 String servletName = getServletConfig().getServletName(); 329 if (servletName == null) 330 servletName = ""; 331 if (servletName.startsWith("org.apache.catalina.INVOKER.")) 332 throw new UnavailableException 333 (sm.getString("hostManagerServlet.cannotInvoke")); 334 335 String value = null; 337 try { 338 value = getServletConfig().getInitParameter("debug"); 339 debug = Integer.parseInt(value); 340 } catch (Throwable t) { 341 ; 342 } 343 344 } 345 346 347 348 350 351 360 protected synchronized void add 361 (PrintWriter writer, String name, String aliases, String appBase, 362 boolean manager, 363 boolean autoDeploy, 364 boolean deployOnStartup, 365 boolean deployXML, 366 boolean unpackWARs, 367 boolean xmlNamespaceAware, 368 boolean xmlValidation) { 369 if (debug >= 1) { 370 log("add: Adding host '" + name + "'"); 371 } 372 373 if ((name == null) || name.length() == 0) { 375 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 376 return; 377 } 378 379 if (engine.findChild(name) != null) { 381 writer.println 382 (sm.getString("hostManagerServlet.alreadyHost", name)); 383 return; 384 } 385 386 File appBaseFile = null; 388 if (appBase == null || appBase.length() == 0) { 389 appBase = name; 390 } 391 File file = new File (appBase); 392 if (!file.isAbsolute()) 393 file = new File (System.getProperty("catalina.base"), appBase); 394 try { 395 appBaseFile = file.getCanonicalFile(); 396 } catch (IOException e) { 397 appBaseFile = file; 398 } 399 if (!appBaseFile.exists()) { 400 appBaseFile.mkdirs(); 401 } 402 403 File configBaseFile = getConfigBase(name); 405 406 if (manager) { 408 InputStream is = null; 409 OutputStream os = null; 410 try { 411 is = getServletContext().getResourceAsStream("/manager.xml"); 412 os = new FileOutputStream (new File (configBaseFile, "manager.xml")); 413 byte buffer[] = new byte[512]; 414 int len = buffer.length; 415 while (true) { 416 len = is.read(buffer); 417 if (len == -1) 418 break; 419 os.write(buffer, 0, len); 420 } 421 } catch (IOException e) { 422 writer.println 423 (sm.getString("hostManagerServlet.managerXml")); 424 return; 425 } finally { 426 if (is != null) { 427 try { 428 is.close(); 429 } catch (IOException e) { 430 } 431 } 432 if (os != null) { 433 try { 434 os.close(); 435 } catch (IOException e) { 436 } 437 } 438 } 439 } 440 441 StandardHost host = new StandardHost(); 442 host.setAppBase(appBase); 443 host.setName(name); 444 445 host.addLifecycleListener(new HostConfig()); 446 447 if ((aliases != null) && !("".equals(aliases))) { 449 StringTokenizer tok = new StringTokenizer (aliases, ", "); 450 while (tok.hasMoreTokens()) { 451 host.addAlias(tok.nextToken()); 452 } 453 } 454 host.setAutoDeploy(autoDeploy); 455 host.setDeployOnStartup(deployOnStartup); 456 host.setDeployXML(deployXML); 457 host.setUnpackWARs(unpackWARs); 458 host.setXmlNamespaceAware(xmlNamespaceAware); 459 host.setXmlValidation(xmlValidation); 460 461 try { 463 engine.addChild(host); 464 } catch (Exception e) { 465 writer.println(sm.getString("hostManagerServlet.exception", 466 e.toString())); 467 return; 468 } 469 470 host = (StandardHost) engine.findChild(name); 471 if (host != null) { 472 writer.println(sm.getString("hostManagerServlet.add", name)); 473 } else { 474 writer.println(sm.getString("hostManagerServlet.addFailed", name)); 476 } 477 478 } 479 480 481 487 protected synchronized void remove(PrintWriter writer, String name) { 488 489 if (debug >= 1) { 490 log("remove: Removing host '" + name + "'"); 491 } 492 493 if ((name == null) || name.length() == 0) { 495 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 496 return; 497 } 498 499 if (engine.findChild(name) == null) { 501 writer.println 502 (sm.getString("hostManagerServlet.noHost", name)); 503 return; 504 } 505 506 if (engine.findChild(name) == host) { 508 writer.println 509 (sm.getString("hostManagerServlet.cannotRemoveOwnHost", name)); 510 return; 511 } 512 513 try { 516 Container child = engine.findChild(name); 517 engine.removeChild(child); 518 if ( child instanceof ContainerBase ) ((ContainerBase)child).destroy(); 519 } catch (Exception e) { 520 writer.println(sm.getString("hostManagerServlet.exception", 521 e.toString())); 522 return; 523 } 524 525 Host host = (StandardHost) engine.findChild(name); 526 if (host == null) { 527 writer.println(sm.getString("hostManagerServlet.remove", name)); 528 } else { 529 writer.println(sm.getString("hostManagerServlet.removeFailed", name)); 531 } 532 533 } 534 535 536 541 protected void list(PrintWriter writer) { 542 543 if (debug >= 1) 544 log("list: Listing hosts for engine '" 545 + engine.getName() + "'"); 546 547 writer.println(sm.getString("hostManagerServlet.listed", 548 engine.getName())); 549 Container[] hosts = engine.findChildren(); 550 for (int i = 0; i < hosts.length; i++) { 551 Host host = (Host) hosts[i]; 552 String name = host.getName(); 553 String [] aliases = host.findAliases(); 554 StringBuffer buf = new StringBuffer (); 555 if (aliases.length > 0) { 556 buf.append(aliases[0]); 557 for (int j = 1; j < aliases.length; j++) { 558 buf.append(',').append(aliases[j]); 559 } 560 } 561 writer.println(sm.getString("hostManagerServlet.listitem", 562 name, buf.toString())); 563 } 564 } 565 566 567 573 protected void start(PrintWriter writer, String name) { 574 575 if (debug >= 1) 576 log("start: Starting host with name '" + name + "'"); 577 578 if ((name == null) || name.length() == 0) { 580 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 581 return; 582 } 583 584 if (engine.findChild(name) == null) { 586 writer.println 587 (sm.getString("hostManagerServlet.noHost", name)); 588 return; 589 } 590 591 if (engine.findChild(name) == host) { 593 writer.println 594 (sm.getString("hostManagerServlet.cannotStartOwnHost", name)); 595 return; 596 } 597 598 try { 600 ((Lifecycle) engine.findChild(name)).start(); 601 writer.println 602 (sm.getString("hostManagerServlet.started", name)); 603 } catch (Throwable t) { 604 getServletContext().log 605 (sm.getString("hostManagerServlet.startFailed", name), t); 606 writer.println 607 (sm.getString("hostManagerServlet.startFailed", name)); 608 writer.println(sm.getString("hostManagerServlet.exception", 609 t.toString())); 610 return; 611 } 612 613 } 614 615 616 622 protected void stop(PrintWriter writer, String name) { 623 624 if (debug >= 1) 625 log("stop: Stopping host with name '" + name + "'"); 626 627 if ((name == null) || name.length() == 0) { 629 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 630 return; 631 } 632 633 if (engine.findChild(name) == null) { 635 writer.println 636 (sm.getString("hostManagerServlet.noHost", name)); 637 return; 638 } 639 640 if (engine.findChild(name) == host) { 642 writer.println 643 (sm.getString("hostManagerServlet.cannotStopOwnHost", name)); 644 return; 645 } 646 647 try { 649 ((Lifecycle) engine.findChild(name)).stop(); 650 writer.println 651 (sm.getString("hostManagerServlet.stopped", name)); 652 } catch (Throwable t) { 653 getServletContext().log 654 (sm.getString("hostManagerServlet.stopFailed", name), t); 655 writer.println 656 (sm.getString("hostManagerServlet.stopFailed", name)); 657 writer.println(sm.getString("hostManagerServlet.exception", 658 t.toString())); 659 return; 660 } 661 662 } 663 664 665 667 668 671 protected File getConfigBase(String hostName) { 672 File configBase = 673 new File (System.getProperty("catalina.base"), "conf"); 674 if (!configBase.exists()) { 675 return null; 676 } 677 if (engine != null) { 678 configBase = new File (configBase, engine.getName()); 679 } 680 if (host != null) { 681 configBase = new File (configBase, hostName); 682 } 683 configBase.mkdirs(); 684 return configBase; 685 } 686 687 688 } 689 | Popular Tags |