jkstatustask.java

来自「以便Apache与其他服务进行整合 Mod_JK安装」· Java 代码 · 共 736 行 · 第 1/2 页

JAVA
736
字号
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jk.status;import java.util.Iterator;import java.util.List;import org.apache.catalina.ant.BaseRedirectorHelperTask;import org.apache.tomcat.util.IntrospectionUtils;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;/** * Ant task that implements the show <code>/jkstatus</code> command, supported * by the mod_jk status (1.2.13) application. *  * @author Peter Rossbach * @version $Revision: 550245 $ * @since 5.5.10 */public class JkStatusTask extends BaseRedirectorHelperTask {    /**     * The descriptive information about this implementation.     */    private static final String info = "org.apache.jk.status.JkStatusTask/1.2";    /**     * Store status as <code>resultProperty</code> prefix.     */    protected String resultproperty;    /**     * Echo status at ant console     */    protected boolean echo = false;    /**     * The login password for the <code>mod_jk status</code> page.     */    protected String password = null;    /**     * The URL of the <code>mod_jk status</code> page to be used.     */    protected String url = "http://localhost:80/jkstatus";    /**     * The login username for the <code>mod_jk status</code> page.     */    protected String username = null;    private String errorProperty;    private String worker;    private String loadbalancer;    /**     * Return descriptive information about this implementation and the     * corresponding version number, in the format     * <code>&lt;description&gt;/&lt;version&gt;</code>.     */    public String getInfo() {        return (info);    }    public String getPassword() {        return (this.password);    }    public void setPassword(String password) {        this.password = password;    }    public String getUrl() {        return (this.url);    }    public void setUrl(String url) {        this.url = url;    }    public String getUsername() {        return (this.username);    }    public void setUsername(String username) {        this.username = username;    }    /**     * @return Returns the echo.     */    public boolean isEcho() {        return echo;    }    /**     * @param echo     *            The echo to set.     */    public void setEcho(boolean echo) {        this.echo = echo;    }    /**     * @return Returns the resultproperty.     */    public String getResultproperty() {        return resultproperty;    }    /**     * @param resultproperty     *            The resultproperty to set.     */    public void setResultproperty(String resultproperty) {        this.resultproperty = resultproperty;    }    /**     * @return Returns the loadbalancer.     */    public String getLoadbalancer() {        return loadbalancer;    }    /**     * @param loadbalancer The loadbalancer to set.     */    public void setLoadbalancer(String loadbalancer) {        this.loadbalancer = loadbalancer;    }    /**     * @return Returns the worker.     */    public String getWorker() {        return worker;    }    /**     * @param worker The worker to set.     */    public void setWorker(String worker) {        this.worker = worker;    }    // --------------------------------------------------------- Public Methods    /**     * Get jkstatus from server.     *      * @exception BuildException     *                if a validation error occurs     */    public void execute() throws BuildException {        if (url == null) {            throw new BuildException("Must specify an 'url'");        }        boolean isWorkerOnly = worker != null && !"".equals(worker);        boolean isLoadbalancerOnly = loadbalancer != null                && !"".equals(loadbalancer);        StringBuffer error = new StringBuffer();        try {            JkStatusAccessor accessor = new JkStatusAccessor();            JkStatus status = accessor.status(url, username, password);            if (status.result != null && !"OK".equals(status.result.type) ) {                if (getErrorProperty() != null) {                    getProject().setNewProperty(errorProperty, status.result.message);                }                if (isFailOnError()) {                    throw new BuildException(status.result.message);                } else {                    handleErrorOutput(status.result.message);                    return;                }            }                        if (!isWorkerOnly && !isLoadbalancerOnly) {                JkServer server = status.getServer();                JkSoftware software = status.getSoftware();                JkResult result = status.getResult();                if (resultproperty != null) {                    createProperty(server, "server", "name");                    createProperty(server, "server", "port");                    createProperty(software, "web_server");                    createProperty(software, "jk_version");                    createProperty(result, "result", "type");                    createProperty(result, "result", "message");                }                if (isEcho()) {                    handleOutput("server name=" + server.getName() + ":"                            + server.getPort() + " - " + software.getWeb_server() + " - " + software.getJk_version());                }            }            List balancers = status.getBalancers();            for (Iterator iter = balancers.iterator(); iter.hasNext();) {                JkBalancer balancer = (JkBalancer) iter.next();                String balancerIndex = null;                if (isLoadbalancerOnly) {                    if (loadbalancer.equals(balancer.getName())) {                        if (resultproperty != null) {                            setPropertyBalancerOnly(balancer);                        }                        echoBalancer(balancer);                        return;                    }                } else {                    if (!isWorkerOnly) {                        if (resultproperty != null) {                         	if ( balancer.getId() >= 0)                        		balancerIndex = Integer.toString(balancer.getId());                        	else                        		balancerIndex = balancer.getName() ;                        	                        	setPropertyBalancer(balancer,balancerIndex);                        }                        echoBalancer(balancer);                    }                    List members = balancer.getBalancerMembers();                    for (Iterator iterator = members.iterator(); iterator                            .hasNext();) {                        JkBalancerMember member = (JkBalancerMember) iterator                                .next();                        if (isWorkerOnly) {                            if (worker.equals(member.getName())) {                                if (resultproperty != null) {                                    setPropertyWorkerOnly(balancer, member);                                }                                echoWorker(member);                                return;                            }                        } else {                            if (resultproperty != null) {                                setPropertyWorker(null, member);                            }                            echoWorker(member);                            if (member.getStatus() != null && !"OK".equals(member.getStatus())) {                                error.append(" worker name=" + member.getName()                                        + " status=" + member.getStatus()                                        + " host=" + member.getAddress());                            }                            if (member.getState() != null &&                                !("OK".equals(member.getState()) ||                                  "N/A".equals(member.getState()) ||                                  "OK/IDLE".equals(member.getState())) ){                                error.append(" worker name=" + member.getName()                                        + " state=" + member.getState()                                        + " host=" + member.getAddress());                            }                        }                    }                    if (!isWorkerOnly) {                        if (resultproperty != null && members.size() > 0) {                            getProject().setNewProperty(                                    resultproperty + "."                                            + balancer.getName() + ".length",                                    Integer.toString(members.size()));                        }                        List mappings = balancer.getBalancerMappings();                        int j = 0;                        String mapIndex ;                        if( balancerIndex != null )                        	mapIndex = balancerIndex + ".map" ;                        else                        	mapIndex = "map" ;                        for (Iterator iterator = mappings.iterator(); iterator                                .hasNext(); j++) {                            JkBalancerMapping mapping = (JkBalancerMapping) iterator                                    .next();                            if (resultproperty != null) {                            	String stringIndex2 ;                                if( mapping.getId() >= 0) {                                    stringIndex2 =  Integer.toString(mapping.getId()) ;                                } else {                                    stringIndex2 = Integer.toString(j);                                }                       	                                createProperty(mapping, mapIndex,                                        stringIndex2, "type");                                createProperty(mapping, mapIndex,                                        stringIndex2, "uri");                                createProperty(mapping, mapIndex,                                        stringIndex2, "context");                                createProperty(mapping, mapIndex,                                        stringIndex2, "source");                            }                            if (isEcho()) {                            	String mappingOut ;                            	                            	if(mapping.source != null) {                            		mappingOut =                            			"balancer name="                                        + balancer.getName() + " mappingtype="                                        + mapping.getType() + " uri="                                        + mapping.getUri() + " source="                                        + mapping.getSource() ;                            	} else {                            		mappingOut = "balancer name="                                        + balancer.getName() + " mappingtype="                                        + mapping.getType() + " uri="                                        + mapping.getUri() + " context="                                        + mapping.getContext() ;                            	}                            	handleOutput(mappingOut);                            }                        }                        if (resultproperty != null && mappings.size() > 0) {                            getProject().setNewProperty(                                    resultproperty + "."                                            + mapIndex + ".length",                                    Integer.toString(mappings.size()));                        }                    }                }            }            if (!isWorkerOnly && !isLoadbalancerOnly) {                if (resultproperty != null && balancers.size() > 0) {                    getProject().setNewProperty(                            resultproperty + ".length",                            Integer.toString(balancers.size()));                }            }        } catch (Throwable t) {            error.append(t.getMessage());            if (getErrorProperty() != null) {                getProject().setNewProperty(errorProperty, error.toString());            }            if (isFailOnError()) {                throw new BuildException(t);            } else {                handleErrorOutput(t.getMessage());                return;            }        }        if (error.length() != 0) {            if (getErrorProperty() != null) {                getProject().setNewProperty(errorProperty, error.toString());            }            if (isFailOnError()) {                // exception should be thrown only if failOnError == true                // or error line will be logged twice                throw new BuildException(error.toString());            }        }    }    /**     * @param member     */

⌨️ 快捷键说明

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