| 1 17 18 19 package org.apache.catalina.manager; 20 21 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.Iterator ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.naming.Binding ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingEnumeration ; 35 import javax.naming.NamingException ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.ServletInputStream ; 38 import javax.servlet.UnavailableException ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 43 import org.apache.catalina.Container; 44 import org.apache.catalina.ContainerServlet; 45 import org.apache.catalina.Context; 46 import org.apache.catalina.Engine; 47 import org.apache.catalina.Globals; 48 import org.apache.catalina.Host; 49 import org.apache.catalina.Lifecycle; 50 import org.apache.catalina.Role; 51 import org.apache.catalina.Server; 52 import org.apache.catalina.ServerFactory; 53 import org.apache.catalina.Session; 54 import org.apache.catalina.UserDatabase; 55 import org.apache.catalina.Wrapper; 56 import org.apache.catalina.core.StandardServer; 57 import org.apache.catalina.util.RequestUtil; 58 import org.apache.catalina.util.ServerInfo; 59 import org.apache.catalina.util.StringManager; 60 import org.apache.tomcat.util.modeler.Registry; 61 62 63 157 158 public class ManagerServlet 159 extends HttpServlet implements ContainerServlet { 160 161 162 164 165 168 protected File configBase = null; 169 170 171 174 protected Context context = null; 175 176 177 180 protected int debug = 1; 181 182 183 188 protected File deployed = null; 189 190 191 194 protected File versioned = null; 195 196 197 200 protected File contextDescriptors = null; 201 202 203 206 protected Host host = null; 207 208 209 212 protected File appBase = null; 213 214 215 218 protected MBeanServer mBeanServer = null; 219 220 221 224 protected ObjectName oname = null; 225 226 227 231 protected javax.naming.Context global = null; 232 233 234 237 protected static StringManager sm = 238 StringManager.getManager(Constants.Package); 239 240 241 244 protected Wrapper wrapper = null; 245 246 247 249 250 253 public Wrapper getWrapper() { 254 255 return (this.wrapper); 256 257 } 258 259 260 265 public void setWrapper(Wrapper wrapper) { 266 267 this.wrapper = wrapper; 268 if (wrapper == null) { 269 context = null; 270 host = null; 271 oname = null; 272 } else { 273 context = (Context ) wrapper.getParent(); 274 host = (Host) context.getParent(); 275 Engine engine = (Engine) host.getParent(); 276 try { 277 oname = new ObjectName (engine.getName() 278 + ":type=Deployer,host=" + host.getName()); 279 } catch (Exception e) { 280 } 282 } 283 284 mBeanServer = Registry.getRegistry(null, null).getMBeanServer(); 286 287 } 288 289 290 292 293 296 public void destroy() { 297 298 ; 300 } 301 302 303 312 public void doGet(HttpServletRequest request, 313 HttpServletResponse response) 314 throws IOException , ServletException { 315 316 if (request.getAttribute(Globals.INVOKED_ATTR) != null) 318 throw new UnavailableException  319 (sm.getString("managerServlet.cannotInvoke")); 320 321 String command = request.getPathInfo(); 323 if (command == null) 324 command = request.getServletPath(); 325 String config = request.getParameter("config"); 326 String path = request.getParameter("path"); 327 String type = request.getParameter("type"); 328 String war = request.getParameter("war"); 329 String tag = request.getParameter("tag"); 330 boolean update = false; 331 if ((request.getParameter("update") != null) 332 && (request.getParameter("update").equals("true"))) { 333 update = true; 334 } 335 336 response.setContentType("text/plain; charset=" + Constants.CHARSET); 338 PrintWriter writer = response.getWriter(); 339 340 if (command == null) { 342 writer.println(sm.getString("managerServlet.noCommand")); 343 } else if (command.equals("/deploy")) { 344 if (war != null || config != null) { 345 deploy(writer, config, path, war, update); 346 } else { 347 deploy(writer, path, tag); 348 } 349 } else if (command.equals("/install")) { 350 deploy(writer, config, path, war, false); 352 } else if (command.equals("/list")) { 353 list(writer); 354 } else if (command.equals("/reload")) { 355 reload(writer, path); 356 } else if (command.equals("/remove")) { 357 undeploy(writer, path); 359 } else if (command.equals("/resources")) { 360 resources(writer, type); 361 } else if (command.equals("/roles")) { 362 roles(writer); 363 } else if (command.equals("/save")) { 364 save(writer, path); 365 } else if (command.equals("/serverinfo")) { 366 serverinfo(writer); 367 } else if (command.equals("/sessions")) { 368 sessions(writer, path); 369 } else if (command.equals("/start")) { 370 start(writer, path); 371 } else if (command.equals("/stop")) { 372 stop(writer, path); 373 } else if (command.equals("/undeploy")) { 374 undeploy(writer, path); 375 } else { 376 writer.println(sm.getString("managerServlet.unknownCommand", 377 command)); 378 } 379 380 writer.flush(); 382 writer.close(); 383 384 } 385 386 387 396 public void doPut(HttpServletRequest request, 397 HttpServletResponse response) 398 throws IOException , ServletException { 399 400 if (request.getAttribute(Globals.INVOKED_ATTR) != null) 402 throw new UnavailableException  403 (sm.getString("managerServlet.cannotInvoke")); 404 405 String command = request.getPathInfo(); 407 if (command == null) 408 command = request.getServletPath(); 409 String path = request.getParameter("path"); 410 String tag = request.getParameter("tag"); 411 boolean update = false; 412 if ((request.getParameter("update") != null) 413 && (request.getParameter("update").equals("true"))) { 414 update = true; 415 } 416 417 response.setContentType("text/plain;charset="+Constants.CHARSET); 419 PrintWriter writer = response.getWriter(); 420 421 if (command == null) { 423 writer.println(sm.getString("managerServlet.noCommand")); 424 } else if (command.equals("/deploy")) { 425 deploy(writer, path, tag, update, request); 426 } else { 427 writer.println(sm.getString("managerServlet.unknownCommand", 428 command)); 429 } 430 431 writer.flush(); 433 writer.close(); 434 435 } 436 437 438 441 public void init() throws ServletException { 442 443 if ((wrapper == null) || (context == null)) 445 throw new UnavailableException  446 (sm.getString("managerServlet.noWrapper")); 447 448 String servletName = getServletConfig().getServletName(); 450 if (servletName == null) 451 servletName = ""; 452 if (servletName.startsWith("org.apache.catalina.INVOKER.")) 453 throw new UnavailableException  454 (sm.getString("managerServlet.cannotInvoke")); 455 456 String value = null; 458 try { 459 value = getServletConfig().getInitParameter("debug"); 460 debug = Integer.parseInt(value); 461 } catch (Throwable t) { 462 ; 463 } 464 465 Server server = ServerFactory.getServer(); 467 if ((server != null) && (server instanceof StandardServer)) { 468 global = ((StandardServer) server).getGlobalNamingContext(); 469 } 470 471 versioned = (File ) getServletContext().getAttribute 473 ("javax.servlet.context.tempdir"); 474 475 String appBase = ((Host) context.getParent()).getAppBase(); 478 deployed = new File (appBase); 479 if (!deployed.isAbsolute()) { 480 deployed = new File (System.getProperty("catalina.base"), 481 appBase); 482 } 483 configBase = new File (System.getProperty("catalina.base"), "conf"); 484 Container container = context; 485 Container host = null; 486 Container engine = null; 487 while (container != null) { 488 if (container instanceof Host) 489 host = container; 490 if (container instanceof Engine) 491 engine = container; 492 container = container.getParent(); 493 } 494 if (engine != null) { 495 configBase = new File (configBase, engine.getName()); 496 } 497 if (host != null) { 498 configBase = new File (configBase, host.getName()); 499 } 500 502 if (debug >= 1) { 504 log("init: Associated with Deployer '" + 505 oname + "'"); 506 if (global != null) { 507 log("init: Global resources are available"); 508 } 509 } 510 511 } 512 513 514 515 517 518 523 protected synchronized void save(PrintWriter writer, String path) { 524 525 Server server = ServerFactory.getServer(); 526 527 if (!(server instanceof StandardServer)) { 528 writer.println(sm.getString("managerServlet.saveFail", server)); 529 return; 530 } 531 532 if ((path == null) || path.length() == 0 || !path.startsWith("/")) { 533 try { 534 ((StandardServer) server).storeConfig(); 535 writer.println(sm.getString("managerServlet.saved")); 536 } catch (Exception e) { 537 log("managerServlet.storeConfig", e); 538 writer.println(sm.getString("managerServlet.exception", 539 e.toString())); 540 return; 541 } 542 } else { 543 String contextPath = path; 544 if (path.equals("/")) { 545 contextPath = ""; 546 } 547 Context context = (Context ) host.findChild(contextPath); 548 if (context == null) { 549 writer.println(sm.getString("managerServlet.noContext", path)); 550 return; 551 } 552 try { 553 ((StandardServer) server).storeContext(context); 554 writer.println(sm.getString("managerServlet.savedContext", 555 path)); 556 } catch (Exception e) { 557 log("managerServlet.save[" + path + "]", e); 558 writer.println(sm.getString("managerServlet.exception", 559 e.toString())); 560 return; 561 } 562 } 563 564 } 565 566 567 576 protected synchronized void deploy 577 (PrintWriter writer, String path, 578 String tag, boolean update, HttpServletRequest request) { 579 580 if (debug >= 1) { 581 log("deploy: Deploying web application at '" + path + "'"); 582 } 583 584 if ((path == null) || path.length() == 0 || !path.startsWith("/")) { 586 writer.println(sm.getString("managerServlet.invalidPath", path)); 587 return; 588 } 589 String displayPath = path; 590 if( path.equals("/") ) 591 path = ""; 592 String basename = getDocBase(path); 593 594 Context context = (Context ) host.findChild(path); 596 if (update) { 597 if (context != null) { 598 undeploy(writer, displayPath); 599 } 600 context = (Context ) host.findChild(path); 601 } 602 if (context != null) { 603 writer.println 604 (sm.getString("managerServlet.alreadyContext", 605 displayPath)); 606 return; 607 } 608 609 File deployedPath = deployed; 611 if (tag != null) { 612 deployedPath = new File (versioned, tag); 613 deployedPath.mkdirs(); 614 } 615 616 File localWar = new File (deployedPath, basename + ".war"); 618 if (debug >= 2) { 619 log("Uploading WAR file to " + localWar); 620 } 621 622 try { 624 if (!isServiced(path)) { 625 addServiced(path); 626 try { 627 uploadWar(request, localWar); 629 if (tag != null) { 631 deployedPath = deployed; 632 File localWarCopy = new File (deployedPath, basename + ".war"); 633 copy(localWar, localWarCopy); 634 localWar = localWarCopy; 635 copy(localWar, new File (getAppBase(), basename + ".war")); 636 } 637 check(path); 639 } finally { 640 removeServiced(path); 641 } 642 } 643 } catch (Exception e) { 644 log("managerServlet.check[" + displayPath + "]", e); 645 writer.println(sm.getString("managerServlet.exception", 646 e.toString())); 647 return; 648 } 649 650 context = (Context ) host.findChild(path); 651 if (context != null && context.getConfigured()) { 652 writer.println(sm.getString("managerServlet.deployed", displayPath)); 653 } else { 654 writer.println(sm.getString("managerServlet.deployFailed", displayPath)); 656 } 657 658 } 659 660 661 669 protected void deploy(PrintWriter writer, String path, String tag) { 670 671 if ((path == null) || path.length() == 0 || !path.startsWith("/")) { 673 writer.println(sm.getString("managerServlet.invalidPath", path)); 674 return; 675 } 676 String displayPath = path; 677 if( path.equals("/") ) 678 path = ""; 679 680 File deployedPath = versioned; 682 if (tag != null) { 683 deployedPath = new File (deployedPath, tag); 684 } 685 686 File localWar = new File (deployedPath, getDocBase(path) + ".war"); 688 File localXml = new File (configBase, getConfigFile(path) + ".xml"); 690 691 Context context = (Context ) host.findChild(path); 693 if (context != null) { 694 undeploy(writer, displayPath); 695 } 696 697 try { 699 if (!isServiced(path)) { 700 addServiced(path); 701 try { 702 copy(localWar, new File (getAppBase(), getDocBase(path) + ".war")); 703 check(path); 705 } finally { 706 removeServiced(path); 707 } 708 } 709 } catch (Exception e) { 710 log("managerServlet.check[" + displayPath + "]", e); 711 writer.println(sm.getString("managerServlet.exception", 712 e.toString())); 713 return; 714 } 715 716 context = (Context ) host.findChild(path); 717 if (context != null && context.getConfigured()) { 718 writer.println(sm.getString("managerServlet.deployed", displayPath)); 719 } else { 720 writer.println(sm.getString("managerServlet.deployFailed", displayPath)); 722 } 723 724 } 725 726 727 737 protected void deploy(PrintWriter writer, String config, 738 String path, String war, boolean update) { 739 740 if (config != null && config.length() == 0) { 741 config = null; 742 } 743 if (war != null && war.length() == 0) { 744 war = null; 745 } 746 747 if (debug >= 1) { 748 if (config != null && config.length() > 0) { 749 if (war != null) { 750 log("install: Installing context configuration at '" + 751 config + "' from '" + war + "'"); 752 } else { 753 log("install: Installing context configuration at '" + 754 config + "'"); 755 } 756 } else { 757 if (path != null && path.length() > 0) { 758 log("install: Installing web application at '" + path + 759 "' from '" + war + "'"); 760 } else { 761 log("install: Installing web application from '" + war + "'"); 762 } 763 } 764 } 765 766 if (path == null || path.length() == 0 || !path.startsWith("/")) { 767 writer.println(sm.getString("managerServlet.invalidPath", 768 RequestUtil.filter(path))); 769 return; 770 } 771 String displayPath = path; 772 if("/".equals(path)) { 773 path = ""; 774 } 775 776 Context context = (Context ) host.findChild(path); 778 if (update) { 779 if (context != null) { 780 undeploy(writer, displayPath); 781 } 782 context = (Context ) host.findChild(path); 783 } 784 if (context != null) { 785 writer.println 786 (sm.getString("managerServlet.alreadyContext", 787 displayPath)); 788 return; 789 } 790 791 if (config != null && (config.startsWith("file:"))) { 792 config = config.substring("file:".length()); 793 } 794 if (war != null && (war.startsWith("file:"))) { 795 war = war.substring("file:".length()); 796 } 797 798 try { 799 if (!isServiced(path)) { 800 addServiced(path); 801 try { 802 if (config != null) { 803 copy(new File (config), 804 <
|