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

📄 mmservlet.java

📁 mobile to system control
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        Object[] params = new Object[mpi.length];                        String[] types = new String[mpi.length];                        String paramName = null;                        try {                            for (int j = 0; j < mpi.length; j++) {                                types[j] = mpi[j].getType();                                paramName = mpi[j].getName();                                params[j] = findAndSetParameter                                (paramName, types[j], request);                            }                            retVal = connectorClient.invoke                            (mBeanName, action, params, types);                        } catch (MMParameterException mpe) {                            return                            ((paramName + ": " + mpe.getMessage()).getBytes());                        }                    } else {                        retVal = connectorClient.invoke                        (mBeanName, action, null, null);                    }                    if (retVal instanceof Page) return retVal;                    return convertReturnValue(retVal, retType);                }            }            return("Operation " + action + " not found").getBytes();        } catch (Exception e) {            log(e.getClass().getName(), e);            return("Exception occured: " + e.getMessage()).getBytes();        }    }        private byte[] getDescription(String host, String mbean, String action) {        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 (action.equals("is" + mai[i].getName())                || action.equals("get" + mai[i].getName())                || action.equals("set" + mai[i].getName())) {                    sb.append(mai[i].getDescription());                    break;                }            }            MBeanOperationInfo[] moi = mbi.getOperations();            for (int i = 0; i < moi.length; i++) {                if (action.equals(moi[i].getName())) {                    sb.append(moi[i].getDescription()).append("\n");                    MBeanParameterInfo mpi[] = moi[i].getSignature();                    if (mpi.length > 0) {                        for (int j = 0; j < mpi.length; j++) {                            sb.append(mpi[j].getName()).append(": ")                            .append(mpi[j].getDescription()).append("\n");                        }                    }                    break;                }            }        } catch (Exception e) {            log(e.getClass().getName(), e);            sb.append("Exception occured: ").append(e.getMessage());        }        return sb.toString().getBytes();    }        private Object findAndSetParameter(String name, String type,    HttpServletRequest request) throws MMParameterException {        String value = request.getParameter(name);        if ((value == null) || (value.length() < 1))            throw new MMParameterException            ("No value supplied for this parameter");        return convertValue(value, type);    }        private byte[] convertReturnValue(Object o, String type) {        if (o == null) return "null".getBytes();        if (type.equals("void") || type.equals("java.lang.Void"))            return "No return value".getBytes();        if (o instanceof java.lang.String) {            return ((String)o).getBytes();        } else if (o instanceof java.lang.Integer) {            return ((Integer)o).toString().getBytes();        } else if (o instanceof java.lang.Byte) {            return ((Byte)o).toString().getBytes();        } else if (o instanceof java.lang.Short) {            return ((Short)o).toString().getBytes();        } else if (o instanceof java.lang.Long) {            return ((Long)o).toString().getBytes();        } else if (o instanceof java.lang.Boolean) {            return ((Boolean)o).toString().getBytes();        } else if (o instanceof java.lang.Character) {            return ((Character)o).toString().getBytes();        } else if (o instanceof java.lang.Float) {            return ((Float)o).toString().getBytes();        } else if (o instanceof java.lang.Double) {            return ((Double)o).toString().getBytes();        } else if (o instanceof byte[]) {            return (byte[])o;        } else if (o instanceof java.net.URL[]) {            URL[] urlarr = (URL[])o;            StringBuffer sb = new StringBuffer();            for (int i = 0; i < urlarr.length; i++) {                sb.append(urlarr[i].toString()).append("\n");            }            return sb.toString().getBytes();        } else if (o instanceof javax.management.ObjectName) {            return ((ObjectName)o).toString().getBytes();        } else {            return "Unsupported data type".getBytes();        }    }        private Object convertValue(String value, String type) throws MMParameterException {        try {            if (type.equals("java.lang.String")) {                return value;            } else if (type.equals("javax.management.ObjectName")) {                return new ObjectName(value);            } else if (type.equals("int")            || type.equals("java.lang.Integer")) {                return new Integer(Integer.parseInt(value));            } else if (type.equals("byte")            || type.equals("java.lang.Byte")) {                return new Byte(Byte.parseByte(value));            } else if (type.equals("short")            || type.equals("java.lang.Short")) {                return new Short(Short.parseShort(value));            } else if (type.equals("long")            || type.equals("java.lang.Long")) {                return new Long(Long.parseLong(value));            } else if (type.equals("boolean")            || type.equals("java.lang.Boolean")) {                return new Boolean(value);            } else if (type.equals("char")            || type.equals("java.lang.Character")) {                return new Character(value.charAt(0));            } else if (type.equals("float")            || type.equals("java.lang.Float")) {                return new Float(Float.parseFloat(value));            } else if (type.equals("double")            || type.equals("java.lang.Double")) {                return new Double(Double.parseDouble(value));            } else {                throw new MMParameterException("Unsupported type: " + type);            }        } catch (NumberFormatException nfe) {            throw new MMParameterException            ("Can't convert " + value + " to " + type);        } catch (MalformedObjectNameException mon) {            throw new MMParameterException            ("Can't convert " + value + " to " + type);        }    }        private byte[] getParameters(String host, String mbean, String action) {        StringBuffer sb = new StringBuffer();        boolean notFound = true;        RmiConnectorClient connectorClient = null;        try {            connectorClient = connect(host);            ObjectName mBeanName = new ObjectName(mbean);            MBeanInfo mbi = connectorClient.getMBeanInfo(mBeanName);            MBeanOperationInfo[] moi = mbi.getOperations();            for (int i = 0; i < moi.length; i++) {                if (action.equals(moi[i].getName())) {                    MBeanParameterInfo mpi[] = moi[i].getSignature();                    if (mpi.length > 0) {                        for (int j = 0; j < mpi.length; j++) {                            sb.append(mpi[j].getName()).append(":")                            .append(mpi[j].getType()).append("\n");                        }                    }                    notFound = false;                    break;                }            }            if (notFound && action.startsWith("set")) {                MBeanAttributeInfo[] mai = mbi.getAttributes();                for (int i = 0; i < mai.length; i++) {                    if (action.substring(3).equals(mai[i].getName())) {                        sb.append(mai[i].getName()).append(":")                        .append(mai[i].getType());                        break;                    }                }            }        } catch (Exception e) {            log(e.getClass().getName(), e);            sb.append("Exception occured: ").append(e.getMessage());        }        return sb.toString().getBytes();    }        private byte[] listDomains(String host) {        StringBuffer sb = new StringBuffer();        RmiConnectorClient connectorClient = null;        try {            connectorClient = connect(host);            HashSet hs = new HashSet();            Set s = connectorClient.queryNames(null, null);            Iterator it = s.iterator();            while (it.hasNext()) {                ObjectName on = (ObjectName)it.next();                hs.add(on.getDomain());            }            it = hs.iterator();            while (it.hasNext()) {                sb.append((String)it.next()).append("\n");            }        } catch (Exception e) {            log(e.getClass().getName(), e);            sb.append("Exception occured: ").append(e.getMessage());        }        return sb.toString().getBytes();    }        private RmiConnectorClient connect(String host)    throws NoSuchElementException {        Object o = hostConnections.get(host);        if (o == null) throw new NoSuchElementException("Connection to " + host + " not found");        while ((o != null) && (o instanceof String)        && ((String)o).equals("RETRYING")) {            try {                Thread.sleep(5000);            } catch (InterruptedException ie) {}            o = hostConnections.get(host);            if (o == null) throw new NoSuchElementException            ("Connection to " + host + " not found");        }        RmiConnectorClient rcc = (RmiConnectorClient)o;        return rcc;    }        private void readProperties() {        try {            servletResources = ResourceBundle.getBundle(SERVLET_RESOURCE_FILE);        } catch (MissingResourceException mre) {            log("Unable to read servlet properties");            servletResources = null;        }    }        public void handleNotification(Notification notification, Object handback) {        log("Notification received");        if (notification instanceof HeartBeatNotification) {            handleHeartBeatNotification(notification);        } else if (notification instanceof MonitorNotification) {            MonitorNotification mn = (MonitorNotification)notification;            log(mn.getObservedAttribute());            log(mn.getTrigger().toString());            log(mn.getDerivedGauge().toString());        }    }        private void handleHeartBeatNotification(Notification notification) {        ConnectorAddress notif_address =        ((HeartBeatNotification)notification).getConnectorAddress();        if (notif_address instanceof RmiConnectorAddress) {            RmiConnectorAddress rmi_address =            (RmiConnectorAddress)notif_address;            log("HOST  = " + rmi_address.getHost());            log("EVENT = " + notification.getType());            if (notification.getType()            .equals(HeartBeatNotification.CONNECTION_TERMINATED)) {                try {                    log("Trying to reconnect to " + rmi_address.getHost());                    reconnect(rmi_address.getHost());                } catch (ConnectException ce) {                    hostConnections.remove(rmi_address.getHost());                }            }        } else {            log("Unknown notification received from " + notif_address.toString());        }    }        private void subscribeToMonitorNotifications(RmiConnectorClient rcc) {        try {            Set s = rcc.queryNames(new ObjectName("Monitor:*"), null);            Iterator it = s.iterator();            while (it.hasNext()) {                ObjectName on = (ObjectName)it.next();                rcc.addNotificationListener(on, this, null, null);                log("Listening to notifications from " + on.getCanonicalName());            }        } catch (Exception e) {            log(e.getClass().getName(), e);        }    }        private void reconnect(String hostName) throws ConnectException {        RmiConnectorClient rcc = new RmiConnectorClient();        RmiConnectorAddress address = new RmiConnectorAddress        (hostName, ServiceName.RMI_CONNECTOR_PORT,        ServiceName.RMI_CONNECTOR_SERVER);        synchronized(lock) {            hostConnections.remove(hostName);            hostConnections.put(hostName, "RETRYING");            for (int i = 0; i < reconnectMaxRetries; i++) {                try {                    rcc.connect(address) ; // CONNECTION                    rcc.addHeartBeatNotificationListener(this, null, null);                    subscribeToMonitorNotifications(rcc);                    hostConnections.remove(hostName);                    hostConnections.put(hostName, rcc);                    log("Reconnected to " + hostName);                    break;                } catch (Exception e) {                    try {                        Thread.sleep(5000);                    } catch (InterruptedException ie) {}                }            }        }        if (!rcc.isConnected())            throw new ConnectException            ("Unable to connect to host " + hostName);    }        private Object invokePageOperation(RmiConnectorClient connectorClient,    ObjectName mBeanName, String action, int pageNo) {        String pageAction = action + "Page";        try {            MBeanInfo mbi = connectorClient.getMBeanInfo(mBeanName);            MBeanOperationInfo moi[] = mbi.getOperations();            for (int i = 0; i < moi.length; i++) {                if (moi[i].getName().equals(pageAction)) {                    String retType = moi[i].getReturnType();                    if (!retType.equals("mobimon.common.Page")) continue;                    Object retVal;                    MBeanParameterInfo mpi[] = moi[i].getSignature();                    if (mpi.length > 0) {                        if (mpi.length > 1) continue;                        Object[] params = new Object[mpi.length];                        String[] types = new String[mpi.length];                        types[0] = mpi[0].getType();                        if (!types[0].equals("int")) continue;                        params[0] = new Integer(pageNo);                        return connectorClient.invoke                        (mBeanName, pageAction, params, types);                    } else {                        continue;                    }                }            }            return getMBeanAttribute            (connectorClient, mBeanName, action.substring(3));        } catch (Exception e) {            log(e.getClass().getName(), e);            return("Exception occured: " + e.getMessage()).getBytes();        }    }    }

⌨️ 快捷键说明

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