1 16 17 18 package org.apache.catalina.hostmanager; 19 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.PrintWriter ; 27 import java.util.StringTokenizer ; 28 29 import javax.management.MBeanServer ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.UnavailableException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.catalina.Container; 37 import org.apache.catalina.ContainerServlet; 38 import org.apache.catalina.Context; 39 import org.apache.catalina.Engine; 40 import org.apache.catalina.Globals; 41 import org.apache.catalina.Host; 42 import org.apache.catalina.Lifecycle; 43 import org.apache.catalina.Wrapper; 44 import org.apache.catalina.core.StandardHost; 45 import org.apache.catalina.startup.HostConfig; 46 import org.apache.catalina.util.StringManager; 47 import org.apache.commons.modeler.Registry; 48 49 50 97 98 public class HostManagerServlet 99 extends HttpServlet implements ContainerServlet { 100 101 102 104 105 108 protected File configBase = null; 109 110 111 114 protected Context context = null; 115 116 117 120 protected int debug = 1; 121 122 123 126 protected Host host = null; 127 128 129 132 protected Engine engine = null; 133 134 135 138 protected MBeanServer mBeanServer = null; 139 140 141 144 protected static StringManager sm = 145 StringManager.getManager(Constants.Package); 146 147 148 151 protected Wrapper wrapper = null; 152 153 154 156 157 160 public Wrapper getWrapper() { 161 162 return (this.wrapper); 163 164 } 165 166 167 172 public void setWrapper(Wrapper wrapper) { 173 174 this.wrapper = wrapper; 175 if (wrapper == null) { 176 context = null; 177 host = null; 178 engine = null; 179 } else { 180 context = (Context) wrapper.getParent(); 181 host = (Host) context.getParent(); 182 engine = (Engine) host.getParent(); 183 } 184 185 mBeanServer = Registry.getRegistry(null, null).getMBeanServer(); 187 188 } 189 190 191 193 194 197 public void destroy() { 198 199 ; 201 } 202 203 204 213 public void doGet(HttpServletRequest request, 214 HttpServletResponse response) 215 throws IOException , ServletException { 216 217 if (request.getAttribute(Globals.INVOKED_ATTR) != null) 219 throw new UnavailableException 220 (sm.getString("hostManagerServlet.cannotInvoke")); 221 222 String command = request.getPathInfo(); 224 if (command == null) 225 command = request.getServletPath(); 226 String name = request.getParameter("name"); 227 228 response.setContentType("text/plain; charset=" + Constants.CHARSET); 230 PrintWriter writer = response.getWriter(); 231 232 if (command == null) { 234 writer.println(sm.getString("hostManagerServlet.noCommand")); 235 } else if (command.equals("/add")) { 236 add(request, writer, name, false); 237 } else if (command.equals("/remove")) { 238 remove(writer, name); 239 } else if (command.equals("/list")) { 240 list(writer); 241 } else if (command.equals("/start")) { 242 start(writer, name); 243 } else if (command.equals("/stop")) { 244 stop(writer, name); 245 } else { 246 writer.println(sm.getString("hostManagerServlet.unknownCommand", 247 command)); 248 } 249 250 writer.flush(); 252 writer.close(); 253 254 } 255 256 257 263 protected void add(HttpServletRequest request, PrintWriter writer, String name, boolean htmlMode ) { 264 String aliases = request.getParameter("aliases"); 265 String appBase = request.getParameter("appBase"); 266 boolean manager = booleanParameter(request, "manager", true, htmlMode); 267 boolean autoDeploy = booleanParameter(request, "autoDeploy", true, htmlMode); 268 boolean deployOnStartup = booleanParameter(request, "deployOnStartup", true, htmlMode); 269 boolean deployXML = booleanParameter(request, "deployXML", true, htmlMode); 270 boolean unpackWARs = booleanParameter(request, "unpackWARs", true, htmlMode); 271 boolean xmlNamespaceAware = booleanParameter(request, "xmlNamespaceAware", false, htmlMode); 272 boolean xmlValidation = booleanParameter(request, "xmlValidation", false, htmlMode); 273 add(writer, name, aliases, appBase, manager, 274 autoDeploy, 275 deployOnStartup, 276 deployXML, 277 unpackWARs, 278 xmlNamespaceAware, 279 xmlValidation); 280 } 281 282 283 291 protected boolean booleanParameter(HttpServletRequest request, 292 String parameter, boolean theDefault, boolean htmlMode) { 293 String value = request.getParameter(parameter); 294 boolean booleanValue = theDefault; 295 if (value != null) { 296 if (htmlMode) { 297 if (value.equals("on")) { 298 booleanValue = true; 299 } 300 } else if (theDefault) { 301 if (value.equals("false")) { 302 booleanValue = false; 303 } 304 } else if (value.equals("true")) { 305 booleanValue = true; 306 } 307 } else if (htmlMode) 308 booleanValue = false; 309 return booleanValue; 310 } 311 312 313 316 public void init() throws ServletException { 317 318 if ((wrapper == null) || (context == null)) 320 throw new UnavailableException 321 (sm.getString("hostManagerServlet.noWrapper")); 322 323 String servletName = getServletConfig().getServletName(); 325 if (servletName == null) 326 servletName = ""; 327 if (servletName.startsWith("org.apache.catalina.INVOKER.")) 328 throw new UnavailableException 329 (sm.getString("hostManagerServlet.cannotInvoke")); 330 331 String value = null; 333 try { 334 value = getServletConfig().getInitParameter("debug"); 335 debug = Integer.parseInt(value); 336 } catch (Throwable t) { 337 ; 338 } 339 340 } 341 342 343 344 346 347 356 protected synchronized void add 357 (PrintWriter writer, String name, String aliases, String appBase, 358 boolean manager, 359 boolean autoDeploy, 360 boolean deployOnStartup, 361 boolean deployXML, 362 boolean unpackWARs, 363 boolean xmlNamespaceAware, 364 boolean xmlValidation) { 365 if (debug >= 1) { 366 log("add: Adding host '" + name + "'"); 367 } 368 369 if ((name == null) || name.length() == 0) { 371 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 372 return; 373 } 374 375 if (engine.findChild(name) != null) { 377 writer.println 378 (sm.getString("hostManagerServlet.alreadyHost", name)); 379 return; 380 } 381 382 File appBaseFile = null; 384 if (appBase == null || appBase.length() == 0) { 385 appBase = name; 386 } 387 File file = new File (appBase); 388 if (!file.isAbsolute()) 389 file = new File (System.getProperty("catalina.base"), appBase); 390 try { 391 appBaseFile = file.getCanonicalFile(); 392 } catch (IOException e) { 393 appBaseFile = file; 394 } 395 if (!appBaseFile.exists()) { 396 appBaseFile.mkdirs(); 397 } 398 399 File configBaseFile = getConfigBase(name); 401 402 if (manager) { 404 InputStream is = null; 405 OutputStream os = null; 406 try { 407 is = getServletContext().getResourceAsStream("/manager.xml"); 408 os = new FileOutputStream (new File (configBaseFile, "manager.xml")); 409 byte buffer[] = new byte[512]; 410 int len = buffer.length; 411 while (true) { 412 len = is.read(buffer); 413 if (len == -1) 414 break; 415 os.write(buffer, 0, len); 416 } 417 } catch (IOException e) { 418 writer.println 419 (sm.getString("hostManagerServlet.managerXml")); 420 return; 421 } finally { 422 if (is != null) { 423 try { 424 is.close(); 425 } catch (IOException e) { 426 } 427 } 428 if (os != null) { 429 try { 430 os.close(); 431 } catch (IOException e) { 432 } 433 } 434 } 435 } 436 437 StandardHost host = new StandardHost(); 438 host.setAppBase(appBase); 439 host.setName(name); 440 441 host.addLifecycleListener(new HostConfig()); 442 443 if ((aliases != null) && !("".equals(aliases))) { 445 StringTokenizer tok = new StringTokenizer (aliases, ","); 446 while (tok.hasMoreTokens()) { 447 host.addAlias(tok.nextToken()); 448 } 449 } 450 host.setAutoDeploy(autoDeploy); 451 host.setDeployOnStartup(deployOnStartup); 452 host.setDeployXML(deployXML); 453 host.setUnpackWARs(unpackWARs); 454 host.setXmlNamespaceAware(xmlNamespaceAware); 455 host.setXmlValidation(xmlValidation); 456 457 try { 459 engine.addChild(host); 460 } catch (Exception e) { 461 writer.println(sm.getString("hostManagerServlet.exception", 462 e.toString())); 463 return; 464 } 465 466 host = (StandardHost) engine.findChild(name); 467 if (host != null) { 468 writer.println(sm.getString("hostManagerServlet.add", name)); 469 } else { 470 writer.println(sm.getString("hostManagerServlet.addFailed", name)); 472 } 473 474 } 475 476 477 483 protected synchronized void remove(PrintWriter writer, String name) { 484 485 if (debug >= 1) { 486 log("remove: Removing host '" + name + "'"); 487 } 488 489 if ((name == null) || name.length() == 0) { 491 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 492 return; 493 } 494 495 if (engine.findChild(name) == null) { 497 writer.println 498 (sm.getString("hostManagerServlet.noHost", name)); 499 return; 500 } 501 502 if (engine.findChild(name) == host) { 504 writer.println 505 (sm.getString("hostManagerServlet.cannotRemoveOwnHost", name)); 506 return; 507 } 508 509 try { 512 engine.removeChild(engine.findChild(name)); 513 } catch (Exception e) { 514 writer.println(sm.getString("hostManagerServlet.exception", 515 e.toString())); 516 return; 517 } 518 519 Host host = (StandardHost) engine.findChild(name); 520 if (host == null) { 521 writer.println(sm.getString("hostManagerServlet.remove", name)); 522 } else { 523 writer.println(sm.getString("hostManagerServlet.removeFailed", name)); 525 } 526 527 } 528 529 530 535 protected void list(PrintWriter writer) { 536 537 if (debug >= 1) 538 log("list: Listing hosts for engine '" 539 + engine.getName() + "'"); 540 541 writer.println(sm.getString("hostManagerServlet.listed", 542 engine.getName())); 543 Container[] hosts = engine.findChildren(); 544 for (int i = 0; i < hosts.length; i++) { 545 Host host = (Host) hosts[i]; 546 String name = host.getName(); 547 String [] aliases = host.findAliases(); 548 StringBuffer buf = new StringBuffer (); 549 if (aliases.length > 0) { 550 buf.append(aliases[0]); 551 for (int j = 1; j < aliases.length; j++) { 552 buf.append(',').append(aliases[j]); 553 } 554 } 555 writer.println(sm.getString("hostManagerServlet.listitem", 556 name, buf.toString())); 557 } 558 } 559 560 561 567 protected void start(PrintWriter writer, String name) { 568 569 if (debug >= 1) 570 log("start: Starting host with name '" + name + "'"); 571 572 if ((name == null) || name.length() == 0) { 574 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 575 return; 576 } 577 578 if (engine.findChild(name) == null) { 580 writer.println 581 (sm.getString("hostManagerServlet.noHost", name)); 582 return; 583 } 584 585 if (engine.findChild(name) == host) { 587 writer.println 588 (sm.getString("hostManagerServlet.cannotStartOwnHost", name)); 589 return; 590 } 591 592 try { 594 ((Lifecycle) engine.findChild(name)).start(); 595 writer.println 596 (sm.getString("hostManagerServlet.started", name)); 597 } catch (Throwable t) { 598 getServletContext().log 599 (sm.getString("hostManagerServlet.startFailed", name), t); 600 writer.println 601 (sm.getString("hostManagerServlet.startFailed", name)); 602 writer.println(sm.getString("hostManagerServlet.exception", 603 t.toString())); 604 return; 605 } 606 607 } 608 609 610 616 protected void stop(PrintWriter writer, String name) { 617 618 if (debug >= 1) 619 log("stop: Stopping host with name '" + name + "'"); 620 621 if ((name == null) || name.length() == 0) { 623 writer.println(sm.getString("hostManagerServlet.invalidHostName", name)); 624 return; 625 } 626 627 if (engine.findChild(name) == null) { 629 writer.println 630 (sm.getString("hostManagerServlet.noHost", name)); 631 return; 632 } 633 634 if (engine.findChild(name) == host) { 636 writer.println 637 (sm.getString("hostManagerServlet.cannotStopOwnHost", name)); 638 return; 639 } 640 641 try { 643 ((Lifecycle) engine.findChild(name)).stop(); 644 writer.println 645 (sm.getString("hostManagerServlet.stopped", name)); 646 } catch (Throwable t) { 647 getServletContext().log 648 (sm.getString("hostManagerServlet.stopFailed", name), t); 649 writer.println 650 (sm.getString("hostManagerServlet.stopFailed", name)); 651 writer.println(sm.getString("hostManagerServlet.exception", 652 t.toString())); 653 return; 654 } 655 656 } 657 658 659 661 662 665 protected File getConfigBase(String hostName) { 666 File configBase = 667 new File (System.getProperty("catalina.base"), "conf"); 668 if (!configBase.exists()) { 669 return null; 670 } 671 if (engine != null) { 672 configBase = new File (configBase, engine.getName()); 673 } 674 if (host != null) { 675 configBase = new File (configBase, hostName); 676 } 677 configBase.mkdirs(); 678 return configBase; 679 } 680 681 682 } 683 | Popular Tags |