📄 jmxaccessortask.java
字号:
/*
* 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.catalina.ant.jmx;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.catalina.ant.BaseRedirectorHelperTask;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
/**
* Access <em>JMX</em> JSR 160 MBeans Server.
* <ul>
* <li>open more then one JSR 160 rmi connection</li>
* <li>Get/Set Mbeans attributes</li>
* <li>Call Mbean Operation with arguments</li>
* <li>Argument values can be converted from string to
* int,long,float,double,boolean,ObjectName or InetAddress</li>
* <li>Query Mbeans</li>
* <li>Show Get, Call, Query result at Ant console log</li>
* <li>Bind Get, Call, Query result at Ant properties</li>
* </ul>
*
* Examples: open server with reference and autorisation
*
* <pre>
*
* <jmxOpen
* host="127.0.0.1"
* port="9014"
* username="monitorRole"
* password="mysecret"
* ref="jmx.myserver"
* />
*
* </pre>
*
* All calls after opening with same refid reuse the connection.
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz
* <em>jmx.server</em>
* </p>
* All JMXAccessorXXXTask support the attribute <em>if</em> and
* <em>unless</em>. With <em>if</em> the task is only execute when property
* exist and with <em>unless</em> when property not exists. <br/><b>NOTE
* </b>: These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
* @since 5.5.10
*/
public class JMXAccessorTask extends BaseRedirectorHelperTask {
// ----------------------------------------------------- Instance Variables
public static String JMX_SERVICE_PREFIX = "service:jmx:rmi:///jndi/rmi://";
public static String JMX_SERVICE_SUFFIX = "/jmxrmi";
private String name = null;
private String resultproperty;
private String url = null;
private String host = "localhost";
private String port = "8050";
private String password = null;
private String username = null;
private String ref = "jmx.server";
private boolean echo = false;
private boolean separatearrayresults = true;
private String delimiter;
private String unlessCondition;
private String ifCondition;
private Properties properties = new Properties();
// ----------------------------------------------------- Instance Info
/**
* Descriptive information describing this implementation.
*/
private static final String info = "org.apache.catalina.ant.JMXAccessorTask/1.1";
/**
* Return descriptive information about this implementation and the
* corresponding version number, in the format
* <code><description>/<version></code>.
*/
public String getInfo() {
return (info);
}
// ------------------------------------------------------------- Properties
/**
* The name used at remote MbeanServer
*/
public String getName() {
return (this.name);
}
public void setName(String objectName) {
this.name = objectName;
}
/**
* @return Returns the resultproperty.
*/
public String getResultproperty() {
return resultproperty;
}
/**
* @param propertyName The resultproperty to set.
*/
public void setResultproperty(String propertyName) {
this.resultproperty = propertyName;
}
/**
* @return Returns the delimiter.
*/
public String getDelimiter() {
return delimiter;
}
/**
* @param separator The delimiter to set.
*/
public void setDelimiter(String separator) {
this.delimiter = separator;
}
/**
* @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 separatearrayresults.
*/
public boolean isSeparatearrayresults() {
return separatearrayresults;
}
/**
* @param separateArrayResults
* The separatearrayresults to set.
*/
public void setSeparatearrayresults(boolean separateArrayResults) {
this.separatearrayresults = separateArrayResults;
}
/**
* The login password for the <code>Manager</code> application.
*/
public String getPassword() {
return (this.password);
}
public void setPassword(String password) {
this.password = password;
}
/**
* The login username for the <code>JMX</code> MBeanServer.
*/
public String getUsername() {
return (this.username);
}
public void setUsername(String username) {
this.username = username;
}
/**
* The URL of the <code>JMX JSR 160</code> MBeanServer to be used.
*/
public String getUrl() {
return (this.url);
}
public void setUrl(String url) {
this.url = url;
}
/**
* The Host of the <code>JMX JSR 160</code> MBeanServer to be used.
*/
public String getHost() {
return (this.host);
}
public void setHost(String host) {
this.host = host;
}
/**
* The Port of the <code>JMX JSR 160</code> MBeanServer to be used.
*/
public String getPort() {
return (this.port);
}
public void setPort(String port) {
this.port = port;
}
/**
* @return Returns the useRef.
*/
public boolean isUseRef() {
return ref != null && !"".equals(ref);
}
/**
* @return Returns the ref.
*/
public String getRef() {
return ref;
}
/**
* @param refId The ref to set.
*/
public void setRef(String refId) {
this.ref = refId;
}
/**
* @return Returns the ifCondition.
*/
public String getIf() {
return ifCondition;
}
/**
* Only execute if a property of the given name exists in the current
* project.
*
* @param c property name
*/
public void setIf(String c) {
ifCondition = c;
}
/**
* @return Returns the unlessCondition.
*/
public String getUnless() {
return unlessCondition;
}
/**
* Only execute if a property of the given name does not exist in the
* current project.
*
* @param c property name
*/
public void setUnless(String c) {
unlessCondition = c;
}
// --------------------------------------------------------- Public Methods
/**
* Execute the specified command. This logic only performs the common
* attribute validation required by all subclasses; it does not perform any
* functional logic directly.
*
* @exception BuildException
* if a validation error occurs
*/
public void execute() throws BuildException {
if (testIfCondition() && testUnlessCondition()) {
try {
String error = null;
MBeanServerConnection jmxServerConnection = getJMXConnection();
error = jmxExecute(jmxServerConnection);
if (error != null && isFailOnError()) {
// exception should be thrown only if failOnError == true
// or error line will be logged twice
throw new BuildException(error);
}
} catch (Throwable t) {
if (isFailOnError()) {
throw new BuildException(t);
} else {
handleErrorOutput(t.getMessage());
}
} finally {
closeRedirector();
}
}
}
/**
* create a new JMX Connection with auth when username and password is set.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -