⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmservlet.java

📁 mobile to system control
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *   This file is part of MobiMon. * *   MobiMon is free software; you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   MobiMon is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU General Public License for more details. *   You should have received a copy of the GNU General Public License *   along with MobiMon; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *//* * MMServlet.java * * Created on February 1, 2003, 5:53 PM */package mobimon.servlet;import java.net.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import javax.management.*;import javax.management.monitor.*;import com.sun.jdmk.comm.ConnectorAddress;import com.sun.jdmk.comm.HeartBeatNotification;import com.sun.jdmk.comm.RmiConnectorAddress;import com.sun.jdmk.comm.RmiConnectorClient;import com.sun.jdmk.ServiceName;import mobimon.common.*;/** * * @author  jan * @version */public class MMServlet extends HttpServlet implements NotificationListener {        private ResourceBundle servletResources;        private Hashtable hostConnections = new Hashtable();        private static String SERVLET_RESOURCE_FILE = "mobimon.servlet.MMServlet";        private int reconnectMaxRetries = 5;        private Object lock;        /** Initializes the servlet.     */    public void init(ServletConfig config) throws ServletException {        super.init(config);        lock = new Object();        readProperties();        if (servletResources != null) {            log("Properties read");            String hosts = servletResources.getString("mobimon.hosts");            StringTokenizer st = new StringTokenizer(hosts);            while (st.hasMoreTokens()) {                String host = st.nextToken();                RmiConnectorClient rcc = new RmiConnectorClient();                RmiConnectorAddress address = new RmiConnectorAddress                (host, ServiceName.RMI_CONNECTOR_PORT,                ServiceName.RMI_CONNECTOR_SERVER);                try {                    log("Trying connection to " + host);                    rcc.connect(address) ; // CONNECTION                    rcc.addHeartBeatNotificationListener(this, null, null);                    subscribeToMonitorNotifications(rcc);                    hostConnections.put(host, rcc);                    log("Connected to " + host);                } catch (Exception e) {                    log("Unable to connect to host " + host);                }            }        }    }        /** Destroys the servlet.     */    public void destroy() {        for (Enumeration e = hostConnections.elements(); e.hasMoreElements();) {            RmiConnectorClient rcc = (RmiConnectorClient)e.nextElement();            try {                rcc.disconnect();            } catch (Exception ex) {}        }    }        /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.     * @param request servlet request     * @param response servlet response     */    protected void processRequest(HttpServletRequest request,    HttpServletResponse response) throws ServletException, java.io.IOException {        response.setContentType("text/plain");        ServletOutputStream sos = response.getOutputStream();        byte[] barr = null;        int pageNo = 0;                String whatNext = request.getParameter("nodes");        if (whatNext != null) {            barr = listNodes();        } else {            whatNext = request.getParameter("host");            if (whatNext != null) {                String hostName = whatNext;                whatNext = request.getParameter("domain");                if (whatNext != null) {                    String domain = whatNext;                    barr = listMBeans(hostName, domain);                } else {                    whatNext = request.getParameter("mbean");                    if (whatNext != null) {                        String mBeanName = whatNext;                        whatNext = request.getParameter("action");                        if (whatNext != null) {                            String action = whatNext;                            whatNext = request.getParameter("desc");                            if (whatNext != null) {                                if (whatNext.equals("a")) {                                    barr =                                    getDescription(hostName, mBeanName, action);                                } else if (whatNext.equals("p")) {                                    barr =                                    getParameters(hostName, mBeanName, action);                                }                            } else {                                Object o = performAction                                (hostName, mBeanName, action, request);                                if (o instanceof Page) {                                    Page p = (Page)o;                                    barr = p.getPageContents();                                    pageNo = p.getPageNo();                                } else {                                    barr = (byte[])o;                                }                            }                        } else {                            barr = listActions(hostName, mBeanName);                        }                    } else {                        barr = listDomains(hostName);                    }                }            } else {                barr = "Unrecognized request".getBytes();            }        }        if (barr != null) {            response.setContentLength(barr.length);            response.setIntHeader("Page", pageNo);            sos.write(barr);        }                sos.close();    }        /** Handles the HTTP <code>GET</code> method.     * @param request servlet request     * @param response servlet response     */    protected void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, java.io.IOException {        processRequest(request, response);    }        /** Handles the HTTP <code>POST</code> method.     * @param request servlet request     * @param response servlet response     */    protected void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, java.io.IOException {        processRequest(request, response);    }        /** Returns a short description of the servlet.     */    public String getServletInfo() {        return "MobiMon Servlet";    }        private byte[] listNodes() {        StringBuffer sb = new StringBuffer();        for (Enumeration e = hostConnections.keys(); e.hasMoreElements();) {            sb.append((String)e.nextElement()).append("\n");        }        return sb.toString().getBytes();    }        private byte[] listMBeans(String host, String domain) {        StringBuffer sb = new StringBuffer();        RmiConnectorClient connectorClient = null;        try {            connectorClient = connect(host);            Set s = connectorClient.queryNames(new ObjectName(domain + ":*"),            null);            Iterator it = s.iterator();            while (it.hasNext()) {                ObjectName mBeanName = (ObjectName)it.next();                String name = mBeanName.getKeyPropertyListString();                sb.append(name).append("\n");            }        } catch (Exception e) {            log(e.getClass().getName(), e);            sb.append("Exception occured: ").append(e.getMessage());        }        return sb.toString().getBytes();    }        private byte[] listActions(String host, String mbean) {        StringBuffer sb = new StringBuffer();        RmiConnectorClient connectorClient = null;        try {            connectorClient = connect(host);            ObjectName mBeanName = new ObjectName(mbean);            MBeanInfo mbi = connectorClient.getMBeanInfo(mBeanName);            MBeanAttributeInfo[] mai = mbi.getAttributes();            for (int i = 0; i < mai.length; i++) {                if (mai[i].isReadable()) {                    if (mai[i].getType().equals("boolean")) {                        sb.append(mai[i].isIs() ? "is" : "get");                    } else {                        sb.append("get");                    }                }                sb.append(mai[i].getName()).append("\n");                if (mai[i].isWritable()) {                    sb.append("set").append(mai[i].getName()).append("\n");                }            }            MBeanOperationInfo[] moi = mbi.getOperations();            for (int i = 0; i < moi.length; i++) {                sb.append(moi[i].getName()).append("\n");            }        } catch (Exception e) {            log(e.getClass().getName(), e);            sb.append("Exception occured: ").append(e.getMessage());        }        return sb.toString().getBytes();    }        private Object performAction(String host, String mbean, String action,    HttpServletRequest request) {        Object o = null;        byte[] barr = null;        RmiConnectorClient connectorClient = null;        try {            connectorClient = connect(host);            ObjectName mBeanName = new ObjectName(mbean);            log("Performing action " + action);            boolean getterAction = false;            boolean setterAction = false;            String attrName = null;            if (action.startsWith("get") || action.startsWith("set")            || action.startsWith("is")) {                attrName = action.startsWith("is")                ? action.substring(2) : action.substring(3);                MBeanInfo mbi = connectorClient.getMBeanInfo(mBeanName);                MBeanAttributeInfo[] mai = mbi.getAttributes();                for (int i = 0; i < mai.length; i++) {                    if (mai[i].getName().equals(attrName)) {                        getterAction = (action.startsWith("get")                        || action.startsWith("is"));                        setterAction = action.startsWith("set");                        break;                    }                }            }            if (getterAction) {                if (request.getHeader("Page") != null) {                    int pageNo = request.getIntHeader("Page");                    if (pageNo != 0) {                        o = invokePageOperation                        (connectorClient, mBeanName, action, pageNo);                    } else {                        o = getMBeanAttribute                        (connectorClient, mBeanName, attrName);                    }                } else {                    o = getMBeanAttribute(connectorClient, mBeanName, attrName);                }            } else if (setterAction) {                String value = request.getParameter(attrName);                if ((value == null) || (value.length() < 1)) {                    barr = ("No attribute value specified for action "                    + action).getBytes();                } else {                    barr = setMBeanAttribute                    (connectorClient, mBeanName, attrName, value);                }            } else {                o = invokeMBeanOperation                (connectorClient, mBeanName, action, request);            }        } catch (Exception e) {            log(e.getClass().getName(), e);            barr = ("Exception occured: " + e.getMessage()).getBytes();        }        if (barr != null) o = barr;        return o;    }        private Object getMBeanAttribute(RmiConnectorClient connectorClient,    ObjectName mBeanName, String attrName) {        try {            Object o = connectorClient.getAttribute(mBeanName, attrName);            if (o instanceof Page) return o;            return convertReturnValue(o, "");        } catch (Exception e) {            log(e.getClass().getName(), e);            return("Exception occured: " + e.getMessage()).getBytes();        }    }        private byte[] setMBeanAttribute(RmiConnectorClient connectorClient,    ObjectName mBeanName, String attrName, String value) {        try {            MBeanInfo mbi = connectorClient.getMBeanInfo(mBeanName);            MBeanAttributeInfo[] mai = mbi.getAttributes();            for (int i = 0; i < mai.length; i++) {                if (mai[i].getName().equals(attrName)) {                    String type = mai[i].getType();                    try {                        Attribute a =                        new Attribute(attrName, convertValue(value, type));                        connectorClient.setAttribute(mBeanName, a);                        return ("Attribute " + attrName + " set").getBytes();                    } catch (MMParameterException mpe) {                        return                        ((attrName + ": " + mpe.getMessage()).getBytes());                    }                }            }            return("Attribute " + attrName + " not found").getBytes();        } catch (Exception e) {            log(e.getClass().getName(), e);            return("Exception occured: " + e.getMessage()).getBytes();        }    }        private Object invokeMBeanOperation(RmiConnectorClient connectorClient,    ObjectName mBeanName, String action, HttpServletRequest request) {        try {            MBeanInfo mbi = connectorClient.getMBeanInfo(mBeanName);            MBeanOperationInfo moi[] = mbi.getOperations();            for (int i = 0; i < moi.length; i++) {                if (moi[i].getName().equals(action)) {                    String retType = moi[i].getReturnType();                    Object retVal;                    MBeanParameterInfo mpi[] = moi[i].getSignature();                    if (mpi.length > 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -