• 深入刨析tomcat 之---第15篇 对应20章, myAdmin案例代码


    writedby 张艳涛

    有没有和我一样做到第20章的?今天基本结束了本书的阅读.最后一个案例没有demo,那我写了一回,如果有需要的可以在本地试试

    路径 D:wksp_studydesignbookserverwebappsmyadminWEB-INFclassesMyAdminServlet.class

    D:wksp_studydesignbookserverwebappsmyadminWEB-INFweb.xml

    以上是俩个文件,第三个文件D:wksp_studydesignbookwebappsmyadmin.xml

    第一文件内容

    //MyAdminServlet.java
    
    import com.zyt.tomcat.ex02.Response;
    import org.apache.commons.modeler.ManagedBean;
    import org.apache.commons.modeler.Registry;
    import org.apache.naming.ResourceRef;
    
    import javax.management.MBeanServer;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectInstance;
    import javax.management.ObjectName;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Iterator;
    import java.util.Set;
    
    public class MyAdminServlet extends HttpServlet {
        private Registry registry;
        private MBeanServer mBeanServer;
        @Override
        public void init() throws ServletException {
            registry = (Registry) getServletContext().getAttribute("org.apache.catalina.Registry");
            if (registry == null) {
                System.out.println("Register not available");
                return;
            }
            mBeanServer = ((MBeanServer) getServletContext().getAttribute("org.apache.catalina.MBeanServer"));
            if (mBeanServer == null) {
                System.out.println("MBeanServer not avaiale");
                return;
            }
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();
            if (registry == null || mBeanServer==null) {
                out.println("registry or MbeanServer not found");
                return;
            }
            out.println("<html><head></head><body>");
            String action = req.getParameter("action");
            if ("listBeans".equals(action)) {
                listAllManagedBeans(out);
            }else  if("listContexts".equals(action)){
                listAllContexts(out);
            }else if("removeContext".equals("contextObjectName")){
                String contextObjectName = req.getParameter("contextObjectName");
                removeContext(contextObjectName, out);
            }else {
                out.println("Invalid command");
            }
            out.println("</body></html>");
        }
        private void listAllManagedBeans(PrintWriter out){
            String[] managedBeans = registry.findManagedBeans();
            for (int i = 0; i < managedBeans.length; i++) {
                out.println(managedBeans[i]+"<br/>");
            }
    
        }
        private void listAllContexts(PrintWriter out){
            try {
                ObjectName objectName = new ObjectName("Catalina:type=Context,*");
                Set<ObjectName> set = mBeanServer.queryNames(objectName, null);
                Iterator<ObjectName> iterator = set.iterator();
                while (iterator.hasNext()) {
                    ObjectName obj = (ObjectName) iterator.next();
                    out.println(obj+"<br/>");
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        private  void removeContext(String contextObjectName,PrintWriter out){
            try {
                ObjectName mBeanFactoryObjectName = new ObjectName("Catalina:type=MBeanFactory");
                if (mBeanFactoryObjectName != null) {
                    String operation = "removeContext";
                    String[] params = new String[1];
                    params[0] =contextObjectName;
                    String signature[] = {"java.lang.String"};
                    mBeanServer.invoke(mBeanFactoryObjectName, operation, params, signature);
                    out.println("context removed");
    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    第二个文件

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
      <servlet>
        <servlet-name>myAdmin</servlet-name>
        <servlet-class>MyAdminServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>myAdmin</servlet-name>
        <url-pattern>/myAdmin</url-pattern>
      </servlet-mapping>
    </web-app>

    第三个文件

    <Context path="/myadmin" docBase="../server/webapps/myadmin" debug="8" privileged="true" reloadable="true">
    </Context>

    访问第一个api

    http://localhost:8080/myadmin/myAdmin?action=listBeans

    http://localhost:8080/myadmin/myAdmin?action=listContexts

    随书代码下载地址:https://www.cnblogs.com/caophoenix/p/11295681.html

  • 相关阅读:
    函数式编程中的基本概念
    【VS】Visual Studio 就可以反编译查看源码了,再见了 Reflector
    【C#】CsvHelper 使用手册
    【C#】使用 System.Globalization.DateTimeFormatInfo 屏蔽系统时间格式对代码的影响
    【swagger】C# 中 swagger 的使用及避坑
    【C#】Newtonsoft.Json 中 JArray 添加数组报错:Could not determine JSON object type for type 'xxx'
    【C#】比较 Random 与 RandomNumberGenerator 生成随机字符串
    【C#】正则进阶
    【C#】写文件时如何去掉编码前缀
    【C#】定时器保活机制引起的内存泄露问题
  • 原文地址:https://www.cnblogs.com/zytcomeon/p/15021273.html
Copyright © 2020-2023  润新知