This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
/*
* 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;
/**
*
* @author Peter Rossbach
* @since 5.5.10
*/
public class Arg {
private String type;
private String value;
public void setType( String type) {
this.type=type;
}
public void setValue( String value ) {
this.value=value;
}
public String getValue() {
return value;
}
public String getType() {
return type;
}
}

View File

@@ -0,0 +1,229 @@
/*
* 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 org.apache.tools.ant.BuildException;
/**
* <b>Definition</b>:
* <pre>
* &lt;path id="catalina_ant"&gt;
* &lt;fileset dir="${catalina.home}/server/lib"&gt;
* &lt;include name="catalina-ant.jar"/&gt;
* &lt;/fileset&gt;
* &lt;/path&gt;
*
* &lt;typedef
* name="jmxCondition"
* classname="org.apache.catalina.ant.jmx.JMXAccessorCondition"
* classpathref="catalina_ant"/&gt;
* &lt;taskdef
* name="jmxOpen"
* classname="org.apache.catalina.ant.jmx.JMXAccessorTask"
* classpathref="catalina_ant"/&gt;
* </pre>
*
* <b>Usage</b>: Wait for start backup node
* <pre>
* &lt;target name="wait"&gt;
* &lt;jmxOpen
* host="${jmx.host}" port="${jmx.port}" username="${jmx.username}" password="${jmx.password}" /&gt;
* &lt;waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" &gt;
* &lt;and&gt;
* &lt;socket server="${server.name}" port="${server.port}"/&gt;
* &lt;http url="${url}"/&gt;
* &lt;jmxCondition
* name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
* operation="=="
* attribute="connected" value="true"
* /&gt;
* &lt;jmxCondition
* operation="&amp;lt;"
* name="Catalina:j2eeType=WebModule,name=//${tomcat.application.host}${tomcat.application.path},J2EEApplication=none,J2EEServer=none"
* attribute="startupTime" value="250"
* /&gt;
* &lt;/and&gt;
* &lt;/waitfor&gt;
* &lt;fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" /&gt;
* &lt;echo message="Server ${url} alive" /&gt;
* &lt;/target&gt;
*
* </pre>
* Allowed operation between jmx attribute and reference value:
* <ul>
* <li>== equals</li>
* <li>!= not equals</li>
* <li>&gt; greater than (&amp;gt;)</li>
* <li>&gt;= greater than or equals (&amp;gt;=)</li>
* <li>&lt; lesser than (&amp;lt;)</li>
* <li>&lt;= lesser than or equals (&amp;lt;=)</li>
* </ul>
* <b>NOTE</b>: For numeric expressions the type must be set and use xml entities as operations.<br>
* As type we currently support <em>long</em> and <em>double</em>.
* @author Peter Rossbach
* @since 5.5.10
*/
public class JMXAccessorCondition extends JMXAccessorConditionBase {
// ----------------------------------------------------- Instance Variables
private String operation = "==" ;
private String type = "long" ;
private String unlessCondition;
private String ifCondition;
// ----------------------------------------------------- Properties
/**
* @return Returns the operation.
*/
public String getOperation() {
return operation;
}
/**
* @param operation The operation to set.
*/
public void setOperation(String operation) {
this.operation = operation;
}
/**
* @return Returns the type.
*/
public String getType() {
return type;
}
/**
* @param type The type to set.
*/
public void setType(String type) {
this.type = type;
}
/**
* @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;
}
/**
* test the if condition
* @return true if there is no if condition, or the named property exists
*/
protected boolean testIfCondition() {
if (ifCondition == null || "".equals(ifCondition)) {
return true;
}
return getProject().getProperty(ifCondition) != null;
}
/**
* test the unless condition
* @return true if there is no unless condition,
* or there is a named property but it doesn't exist
*/
protected boolean testUnlessCondition() {
if (unlessCondition == null || "".equals(unlessCondition)) {
return true;
}
return getProject().getProperty(unlessCondition) == null;
}
/**
* This method evaluates the condition
* It support for operation "&gt;,&gt;=,&lt;,&lt;=" the types <code>long</code> and <code>double</code>.
* @return expression <em>jmxValue</em> <em>operation</em> <em>value</em>
*/
@Override
public boolean eval() {
String value = getValue();
if (operation == null) {
throw new BuildException("operation attribute is not set");
}
if (value == null) {
throw new BuildException("value attribute is not set");
}
if ((getName() == null || getAttribute() == null)) {
throw new BuildException(
"Must specify an MBean name and attribute for condition");
}
if (testIfCondition() && testUnlessCondition()) {
String jmxValue = accessJMXValue();
if (jmxValue != null) {
String op = getOperation();
if ("==".equals(op)) {
return jmxValue.equals(value);
} else if ("!=".equals(op)) {
return !jmxValue.equals(value);
} else {
if ("long".equals(type)) {
long jvalue = Long.parseLong(jmxValue);
long lvalue = Long.parseLong(value);
if (">".equals(op)) {
return jvalue > lvalue;
} else if (">=".equals(op)) {
return jvalue >= lvalue;
} else if ("<".equals(op)) {
return jvalue < lvalue;
} else if ("<=".equals(op)) {
return jvalue <= lvalue;
}
} else if ("double".equals(type)) {
double jvalue = Double.parseDouble(jmxValue);
double dvalue = Double.parseDouble(value);
if (">".equals(op)) {
return jvalue > dvalue;
} else if (">=".equals(op)) {
return jvalue >= dvalue;
} else if ("<".equals(op)) {
return jvalue < dvalue;
} else if ("<=".equals(op)) {
return jvalue <= dvalue;
}
}
}
}
return false;
}
return true;
}
}

View File

@@ -0,0 +1,181 @@
/*
* 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.net.MalformedURLException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.condition.Condition;
public abstract class JMXAccessorConditionBase extends ProjectComponent implements Condition {
private String url = null;
private String host = "localhost";
private String port = "8050";
private String password = null;
private String username = null;
private String name = null;
private String attribute;
private String value;
private String ref = "jmx.server" ;
/**
* @return Returns the attribute.
*/
public String getAttribute() {
return attribute;
}
/**
* @param attribute The attribute to set.
*/
public void setAttribute(String attribute) {
this.attribute = attribute;
}
/**
* @return Returns the host.
*/
public String getHost() {
return host;
}
/**
* @param host The host to set.
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param objectName The name to set.
*/
public void setName(String objectName) {
this.name = objectName;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the port.
*/
public String getPort() {
return port;
}
/**
* @param port The port to set.
*/
public void setPort(String port) {
this.port = port;
}
/**
* @return Returns the url.
*/
public String getUrl() {
return url;
}
/**
* @param url The url to set.
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return Returns the value.
*/
public String getValue() {
return value;
}
// The setter for the "value" attribute
public void setValue(String value) {
this.value = value;
}
/**
* @return Returns the ref.
*/
public String getRef() {
return ref;
}
/**
* @param refId The ref to set.
*/
public void setRef(String refId) {
this.ref = refId;
}
/**
* Get JMXConnection (default look at <em>jmx.server</em> project reference
* from jmxOpen Task).
*
* @return active JMXConnection
* @throws MalformedURLException Invalid URL for JMX server
* @throws IOException Connection error
*/
protected MBeanServerConnection getJMXConnection()
throws MalformedURLException, IOException {
return JMXAccessorTask.accessJMXConnection(
getProject(),
getUrl(), getHost(),
getPort(), getUsername(), getPassword(), ref);
}
/**
* Get value from MBeans attribute.
*
* @return The value
*/
protected String accessJMXValue() {
try {
Object result = getJMXConnection().getAttribute(
new ObjectName(name), attribute);
if(result != null)
return result.toString();
} catch (Exception e) {
// ignore access or connection open errors
}
return null;
}
}

View File

@@ -0,0 +1,179 @@
/*
* 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.util.ArrayList;
import java.util.List;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
/**
* Create new MBean at <em>JMX</em> JSR 160 MBeans Server.
* <ul>
* <li>Create Mbeans</li>
* <li>Create Mbeans with parameter</li>
* <li>Create remote Mbeans with different classloader</li>
* </ul>
* <p>
* Examples:
* <br>
* create a new Mbean at jmx.server connection
* </p>
* <pre>
* &lt;jmx:create
* ref="jmx.server"
* name="Catalina:type=MBeanFactory"
* className="org.apache.catalina.mbeans.MBeanFactory"
* classLoader="Catalina:type=ServerClassLoader,name=server"&gt;
* &lt;Arg value="org.apache.catalina.mbeans.MBeanFactory" /&gt;
* &lt;/jmxCreate/&gt;
* </pre>
* <p>
* <b>WARNING</b>Not all Tomcat MBeans can create remotely and autoregister by its parents!
* Please, use the MBeanFactory operation to generate valves and realms.
* </p>
* <p>
* First call to a remote MBeanserver save the JMXConnection a reference <em>jmx.server</em>
* </p>
* These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @since 5.5.12
*/
public class JMXAccessorCreateTask extends JMXAccessorTask {
// ----------------------------------------------------- Instance Variables
private String className;
private String classLoader;
private List<Arg> args=new ArrayList<>();
// ------------------------------------------------------------- Properties
/**
* @return Returns the classLoader.
*/
public String getClassLoader() {
return classLoader;
}
/**
* @param classLoaderName The classLoader to set.
*/
public void setClassLoader(String classLoaderName) {
this.classLoader = classLoaderName;
}
/**
* @return Returns the className.
*/
public String getClassName() {
return className;
}
/**
* @param className The className to set.
*/
public void setClassName(String className) {
this.className = className;
}
public void addArg(Arg arg ) {
args.add(arg);
}
/**
* @return Returns the args.
*/
public List<Arg> getArgs() {
return args;
}
/**
* @param args The args to set.
*/
public void setArgs(List<Arg> args) {
this.args = args;
}
// ------------------------------------------------------ protected Methods
@Override
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if (getName() == null) {
throw new BuildException("Must specify a 'name'");
}
if ((className == null)) {
throw new BuildException(
"Must specify a 'className' for get");
}
jmxCreate(jmxServerConnection, getName());
return null;
}
/**
* Create new MBean from ClassLoader identified by an ObjectName.
*
* @param jmxServerConnection Connection to the JMX server
* @param name MBean name
* @throws Exception Error creating MBean
*/
protected void jmxCreate(MBeanServerConnection jmxServerConnection,
String name) throws Exception {
Object argsA[] = null;
String sigA[] = null;
if (args != null) {
argsA = new Object[ args.size()];
sigA = new String[args.size()];
for( int i=0; i<args.size(); i++ ) {
Arg arg=args.get(i);
if (arg.getType() == null) {
arg.setType("java.lang.String");
sigA[i]=arg.getType();
argsA[i]=arg.getValue();
} else {
sigA[i]=arg.getType();
argsA[i]=convertStringToType(arg.getValue(),arg.getType());
}
}
}
if (classLoader != null && !"".equals(classLoader)) {
if (isEcho()) {
handleOutput("create MBean " + name + " from class "
+ className + " with classLoader " + classLoader);
}
if(args == null)
jmxServerConnection.createMBean(className, new ObjectName(name), new ObjectName(classLoader));
else
jmxServerConnection.createMBean(className, new ObjectName(name), new ObjectName(classLoader),argsA,sigA);
} else {
if (isEcho()) {
handleOutput("create MBean " + name + " from class "
+ className);
}
if(args == null)
jmxServerConnection.createMBean(className, new ObjectName(name));
else
jmxServerConnection.createMBean(className, new ObjectName(name),argsA,sigA);
}
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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 org.apache.tools.ant.BuildException;
/**
*
* Definition
* <pre>
* &lt;path id="catalina_ant"&gt;
* &lt;fileset dir="${catalina.home}/server/lib"&gt;
* &lt;include name="catalina-ant.jar"/&gt;
* &lt;/fileset&gt;
* &lt;/path&gt;
*
* &lt;typedef
* name="jmxEquals"
* classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition"
* classpathref="catalina_ant"/&gt;
* </pre>
*
* usage: Wait for start backup node
* <pre>
* &lt;target name="wait"&gt;
* &lt;waitfor maxwait="${maxwait}" maxwaitunit="second" timeoutproperty="server.timeout" &gt;
* &lt;and&gt;
* &lt;socket server="${server.name}" port="${server.port}"/&gt;
* &lt;http url="${url}"/&gt;
* &lt;jmxEquals
* host="localhost" port="9014" username="controlRole" password="tomcat"
* name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.111.1,senderPort=9025"
* attribute="connected" value="true"
* /&gt;
* &lt;/and&gt;
* &lt;/waitfor&gt;
* &lt;fail if="server.timeout" message="Server ${url} don't answer inside ${maxwait} sec" /&gt;
* &lt;echo message="Server ${url} alive" /&gt;
* &lt;/target&gt;
*
* </pre>
*
* @author Peter Rossbach
* @since 5.5.10
*/
public class JMXAccessorEqualsCondition extends JMXAccessorConditionBase {
@Override
public boolean eval() {
String value = getValue();
if (value == null) {
throw new BuildException("value attribute is not set");
}
if (getName() == null || getAttribute() == null) {
throw new BuildException(
"Must specify an MBean name and attribute for equals condition");
}
//FIXME check url or host/parameter
String jmxValue = accessJMXValue();
if (jmxValue != null) {
return jmxValue.equals(value);
}
return false;
}
}

View File

@@ -0,0 +1,120 @@
/*
* 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 javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
/**
* Access <em>JMX</em> JSR 160 MBeans Server.
* <ul>
* <li>Get Mbeans attributes</li>
* <li>Show Get result as Ant console log</li>
* <li>Bind Get result as Ant properties</li>
* </ul>
* <p>
* Examples:
* <br>
* Get an Mbean IDataSender attribute nrOfRequests and create a new ant property <em>IDataSender.9025.nrOfRequests</em>
* </p>
* <pre>
* &lt;jmx:get
* ref="jmx.server"
* name="Catalina:type=IDataSender,host=localhost,senderAddress=192.168.1.2,senderPort=9025"
* attribute="nrOfRequests"
* resultproperty="IDataSender.9025.nrOfRequests"
* echo="false"&gt;
* /&gt;
* </pre>
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
* </p>
* These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @since 5.5.10
*/
public class JMXAccessorGetTask extends JMXAccessorTask {
// ----------------------------------------------------- Instance Variables
private String attribute;
// ------------------------------------------------------------- Properties
/**
* @return Returns the attribute.
*/
public String getAttribute() {
return attribute;
}
/**
* @param attribute The attribute to set.
*/
public void setAttribute(String attribute) {
this.attribute = attribute;
}
// ------------------------------------------------------ protected Methods
@Override
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if (getName() == null) {
throw new BuildException("Must specify a 'name'");
}
if ((attribute == null)) {
throw new BuildException(
"Must specify a 'attribute' for get");
}
return jmxGet(jmxServerConnection, getName());
}
/**
* Get property value.
*
* @param jmxServerConnection Connection to the JMX server
* @param name The MBean name
* @return The error message if any
* @throws Exception An error occurred
*/
protected String jmxGet(MBeanServerConnection jmxServerConnection, String name) throws Exception {
String error = null;
if(isEcho()) {
handleOutput("MBean " + name + " get attribute " + attribute );
}
Object result = jmxServerConnection.getAttribute(
new ObjectName(name), attribute);
if (result != null) {
echoResult(attribute,result);
createProperty(result);
} else
error = "Attribute " + attribute + " is empty";
return error;
}
}

View File

@@ -0,0 +1,184 @@
/*
* 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.util.ArrayList;
import java.util.List;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
/**
* 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:
* <ul>
* <li>
* Get a session attribute hello from session with ref <em>${sessionid.0}</em> form
* app <em>Catalina:type=Manager,context=/ClusterTest,host=localhost</em>
* <pre>
* &lt;jmx:invoke
* name="Catalina:type=Manager,context=/ClusterTest,host=localhost"
* operation="getSessionAttribute"
* resultproperty="hello"&gt;
* &lt;arg value="${sessionid.0}"/&gt;
* &lt;arg value="Hello"/&gt;
* &lt;/jmx:invoke&gt;
* </pre>
* </li>
* <li>
* Create new AccessLogger at localhost
* <code>
* &lt;jmx:invoke
* name="Catalina:type=MBeanFactory"
* operation="createAccessLoggerValve"
* resultproperty="accessLoggerObjectName"
* &gt;
* &lt;arg value="Catalina:type=Host,host=localhost"/&gt;
* &lt;/jmx:invoke&gt;
*
* </code>
* </li>
* <li>
* Remove existing AccessLogger at localhost
* <code>
* &lt;jmx:invoke
* name="Catalina:type=MBeanFactory"
* operation="removeValve"
* &gt;
* &lt;arg value="Catalina:type=Valve,name=AccessLogValve,host=localhost"/&gt;
* &lt;/jmx:invoke&gt;
*
* </code>
* </li>
* </ul>
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
* </p>
* These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @since 5.5.10
*/
public class JMXAccessorInvokeTask extends JMXAccessorTask {
// ----------------------------------------------------- Instance Variables
private String operation ;
private List<Arg> args=new ArrayList<>();
// ------------------------------------------------------------- Properties
/**
* @return Returns the operation.
*/
public String getOperation() {
return operation;
}
/**
* @param operation The operation to set.
*/
public void setOperation(String operation) {
this.operation = operation;
}
public void addArg(Arg arg ) {
args.add(arg);
}
/**
* @return Returns the args.
*/
public List<Arg> getArgs() {
return args;
}
/**
* @param args The args to set.
*/
public void setArgs(List<Arg> args) {
this.args = args;
}
// ------------------------------------------------------ protected Methods
@Override
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if (getName() == null) {
throw new BuildException("Must specify a 'name'");
}
if ((operation == null)) {
throw new BuildException(
"Must specify a 'operation' for call");
}
return jmxInvoke(jmxServerConnection, getName());
}
/**
* Invoke specified operation.
*
* @param jmxServerConnection Connection to the JMX server
* @param name The MBean name
* @return null (no error message to report other than exception)
* @throws Exception An error occurred
*/
protected String jmxInvoke(MBeanServerConnection jmxServerConnection, String name) throws Exception {
Object result ;
if (args == null) {
result = jmxServerConnection.invoke(new ObjectName(name),
operation, null, null);
} else {
Object argsA[]=new Object[ args.size()];
String sigA[]=new String[args.size()];
for( int i=0; i<args.size(); i++ ) {
Arg arg=args.get(i);
if (arg.getType() == null) {
arg.setType("java.lang.String");
sigA[i]=arg.getType();
argsA[i]=arg.getValue();
} else {
sigA[i]=arg.getType();
argsA[i]=convertStringToType(arg.getValue(),arg.getType());
}
}
result = jmxServerConnection.invoke(new ObjectName(name), operation, argsA, sigA);
}
if(result != null) {
echoResult(operation,result);
createProperty(result);
}
return null;
}
}

View File

@@ -0,0 +1,173 @@
/*
* 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.util.Iterator;
import java.util.Set;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
/**
* Query for Mbeans.
* <ul>
* <li>open no existing JSR 160 rmi jmx connection</li>
* <li>Get all Mbeans attributes</li>
* <li>Get only the Query Mbeans ObjectNames</li>
* <li>Show query result as Ant console log</li>
* <li>Bind query result as Ant properties</li>
* </ul>
* <br>
* Query a list of Mbeans.
* <pre>
* &lt;jmxQuery
* host="127.0.0.1"
* port="9014"
* name="Catalina:type=Manager,*
* resultproperty="manager" /&gt;
* </pre>
* with attribute <em>attributebinding="true"</em> you can get
* all attributes also from result objects.<br>
* The property manager.length show the size of the result
* and with manager.[0..length].name the
* resulted ObjectNames are saved.
* These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @since 5.5.10
*/
public class JMXAccessorQueryTask extends JMXAccessorTask {
// ----------------------------------------------------- Instance Variables
private boolean attributebinding = false;
// ------------------------------------------------------------- Properties
/**
* @return Returns the attributebinding.
*/
public boolean isAttributebinding() {
return attributebinding;
}
/**
* @param attributeBinding The attributebinding to set.
*/
public void setAttributebinding(boolean attributeBinding) {
this.attributebinding = attributeBinding;
}
// ------------------------------------------------------ protected Methods
@Override
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if (getName() == null) {
throw new BuildException("Must specify a 'name'");
}
return jmxQuery(jmxServerConnection, getName());
}
/**
* Call Mbean server for some mbeans with same domain, attributes.
* with <em>attributebinding=true</em> you can save all attributes from all found objects
*
* @param jmxServerConnection Connection to the JMX server
* @param qry The query
* @return null (no error message to report other than exception)
*/
protected String jmxQuery(MBeanServerConnection jmxServerConnection,
String qry) {
String isError = null;
Set<ObjectName> names = null;
String resultproperty = getResultproperty();
try {
names = jmxServerConnection.queryNames(new ObjectName(qry), null);
if (resultproperty != null) {
setProperty(resultproperty + ".Length",Integer.toString(names.size()));
}
} catch (Exception e) {
if (isEcho())
handleErrorOutput(e.getMessage());
return "Can't query mbeans " + qry;
}
if (resultproperty != null) {
Iterator<ObjectName> it = names.iterator();
int oindex = 0;
String pname = null;
while (it.hasNext()) {
ObjectName oname = it.next();
pname = resultproperty + "." + Integer.toString(oindex) + ".";
oindex++;
setProperty(pname + "Name", oname.toString());
if (isAttributebinding()) {
bindAttributes(jmxServerConnection, pname, oname);
}
}
}
return isError;
}
protected void bindAttributes(MBeanServerConnection jmxServerConnection, String pname, ObjectName oname) {
try {
MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname);
MBeanAttributeInfo attrs[] = minfo.getAttributes();
Object value = null;
for (int i = 0; i < attrs.length; i++) {
if (!attrs[i].isReadable())
continue;
String attName = attrs[i].getName();
if (attName.indexOf('=') >= 0 || attName.indexOf(':') >= 0
|| attName.indexOf(' ') >= 0) {
continue;
}
try {
value = jmxServerConnection
.getAttribute(oname, attName);
} catch (Exception e) {
if (isEcho())
handleErrorOutput("Error getting attribute "
+ oname + " " + pname + attName + " "
+ e.toString());
continue;
}
if (value == null)
continue;
if ("modelerType".equals(attName))
continue;
createProperty(pname + attName, value);
}
} catch (Exception e) {
// Ignore
}
}
}

View File

@@ -0,0 +1,195 @@
/*
* 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 javax.management.Attribute;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
/**
* Access <em>JMX</em> JSR 160 MBeans Server.
* <ul>
* <li>Get Mbeans attributes</li>
* <li>Show Get result as Ant console log</li>
* <li>Bind Get result as Ant properties</li>
* </ul>
* <p>
* Examples:
* Set an Mbean Manager attribute maxActiveSessions.
* Set this attribute with fresh jmx connection without save reference
* </p>
* <pre>
* &lt;jmx:set
* host="127.0.0.1"
* port="9014"
* ref=""
* name="Catalina:type=Manager,context="/ClusterTest",host=localhost"
* attribute="maxActiveSessions"
* value="100"
* type="int"
* echo="false"&gt;
* /&gt;
* </pre>
* <p>
* First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
* </p>
* These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @since 5.5.10
*/
public class JMXAccessorSetTask extends JMXAccessorTask {
// ----------------------------------------------------- Instance Variables
private String attribute;
private String value;
private String type;
private boolean convert = false ;
// ------------------------------------------------------------- Properties
/**
* @return Returns the attribute.
*/
public String getAttribute() {
return attribute;
}
/**
* @param attribute The attribute to set.
*/
public void setAttribute(String attribute) {
this.attribute = attribute;
}
/**
* @return Returns the value.
*/
public String getValue() {
return value;
}
/**
* @param value The value to set.
*/
public void setValue(String value) {
this.value = value;
}
/**
* @return Returns the type.
*/
public String getType() {
return type;
}
/**
* @param valueType The type to set.
*/
public void setType(String valueType) {
this.type = valueType;
}
/**
* @return Returns the convert.
*/
public boolean isConvert() {
return convert;
}
/**
* @param convert The convert to set.
*/
public void setConvert(boolean convert) {
this.convert = convert;
}
// ------------------------------------------------------ protected Methods
@Override
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if (getName() == null) {
throw new BuildException("Must specify a 'name'");
}
if ((attribute == null || value == null)) {
throw new BuildException(
"Must specify a 'attribute' and 'value' for set");
}
return jmxSet(jmxServerConnection, getName());
}
/**
* Set property value.
*
* @param jmxServerConnection Connection to the JMX server
* @param name The MBean name
* @return null (no error message to report other than exception)
* @throws Exception An error occurred
*/
protected String jmxSet(MBeanServerConnection jmxServerConnection,
String name) throws Exception {
Object realValue;
if (type != null) {
realValue = convertStringToType(value, type);
} else {
if (isConvert()) {
String mType = getMBeanAttributeType(jmxServerConnection, name,
attribute);
realValue = convertStringToType(value, mType);
} else
realValue = value;
}
jmxServerConnection.setAttribute(new ObjectName(name), new Attribute(
attribute, realValue));
return null;
}
/**
* Get MBean Attribute from Mbean Server
*
* @param jmxServerConnection The JMX connection name
* @param name The MBean name
* @param attribute The attribute name
* @return The type of the attribute
* @throws Exception An error occurred
*/
protected String getMBeanAttributeType(
MBeanServerConnection jmxServerConnection,
String name,
String attribute) throws Exception {
ObjectName oname = new ObjectName(name);
String mattrType = null;
MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname);
MBeanAttributeInfo attrs[] = minfo.getAttributes();
for (int i = 0; mattrType == null && i < attrs.length; i++) {
if (attribute.equals(attrs[i].getName()))
mattrType = attrs[i].getType();
}
return mattrType;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,85 @@
/*
* 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 javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import org.apache.tools.ant.BuildException;
/**
* unregister an MBean at <em>JMX</em> JSR 160 MBeans Server.
* <ul>
* <li>unregister Mbeans</li>
* </ul>
* <p>
* Examples:
* <br>
* unregister an existing Mbean at jmx.server connection
* </p>
* <pre>
* &lt;jmx:unregister
* ref="jmx.server"
* name="Catalina:type=MBeanFactory" /&gt;
* </pre>
* <p>
* <b>WARNING</b>Not all Tomcat MBeans can successfully unregister remotely. The mbean
* unregistration don't remove valves, realm, .. from parent class.
* Please, use the MBeanFactory operation to remove valves and realms.
* </p>
* <p>
* First call to a remote MBeanserver save the JMXConnection a reference <em>jmx.server</em>
* </p>
* These tasks require Ant 1.6 or later interface.
*
* @author Peter Rossbach
* @since 5.5.12
*/
public class JMXAccessorUnregisterTask extends JMXAccessorTask {
// ------------------------------------------------------ protected Methods
@Override
public String jmxExecute(MBeanServerConnection jmxServerConnection)
throws Exception {
if (getName() == null) {
throw new BuildException("Must specify a 'name'");
}
return jmxUuregister(jmxServerConnection, getName());
}
/**
* Unregister MBean.
*
* @param jmxServerConnection Connection to the JMX server
* @param name The MBean name
* @return null (no error message to report other than exception)
* @throws Exception An error occurred
*/
protected String jmxUuregister(MBeanServerConnection jmxServerConnection,String name) throws Exception {
String error = null;
if(isEcho()) {
handleOutput("Unregister MBean " + name );
}
jmxServerConnection.unregisterMBean(
new ObjectName(name));
return error;
}
}

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<antlib>
<typedef
name="open"
classname="org.apache.catalina.ant.jmx.JMXAccessorTask" />
<typedef
name="set"
classname="org.apache.catalina.ant.jmx.JMXAccessorSetTask" />
<typedef
name="get"
classname="org.apache.catalina.ant.jmx.JMXAccessorGetTask" />
<typedef
name="invoke"
classname="org.apache.catalina.ant.jmx.JMXAccessorInvokeTask" />
<typedef
name="query"
classname="org.apache.catalina.ant.jmx.JMXAccessorQueryTask" />
<typedef
name="create"
classname="org.apache.catalina.ant.jmx.JMXAccessorCreateTask" />
<typedef
name="unregister"
classname="org.apache.catalina.ant.jmx.JMXAccessorUnregisterTask" />
<typedef
name="equals"
classname="org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition" />
<typedef
name="condition"
classname="org.apache.catalina.ant.jmx.JMXAccessorCondition" />
</antlib>

View File

@@ -0,0 +1,25 @@
# 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.
# JMX
jmxOpen=org.apache.catalina.ant.jmx.JMXAccessorTask
jmxSet=org.apache.catalina.ant.jmx.JMXAccessorSetTask
jmxGet=org.apache.catalina.ant.jmx.JMXAccessorGetTask
jmxInvoke=org.apache.catalina.ant.jmx.JMXAccessorInvokeTask
jmxQuery=org.apache.catalina.ant.jmx.JMXAccessorQueryTask
jmxCreate=org.apache.catalina.ant.jmx.JMXAccessorCreateTask
jmxUnregister=org.apache.catalina.ant.jmx.JMXAccessorUnregisterTask
jmxEquals=org.apache.catalina.ant.jmx.JMXAccessorEqualsCondition
jmxCondition=org.apache.catalina.ant.jmx.JMXAccessorCondition

View File

@@ -0,0 +1,77 @@
<!--
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.
-->
<body>
<p>This package contains a set of <code>JMX Task</code> implementations for
<em>Ant (version 1.6 or later)</em> that can be used to interact with the
Remote JMX JSR 160 RMI Adaptor to get/set attributes, invoke MBean operations
and query for Mbeans inside a running instance of Tomcat. For more information, see
<a href="https://tomcat.apache.org/tomcat-8.5-doc/monitoring.html">
https://tomcat.apache.org/tomcat-8.5-doc/monitoring.html</a>.</p>
<p>Each task element can open a new jmx connection or reference an
existing one. The following attribute are exists in every tasks:</p>
<table>
<caption>Common task attributes</caption>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>url</td>
<td>
The JMX Connection URL of the remote Tomcat MBeansServer.
</td>
</tr>
<tr>
<td>username</td>
<td>
The username of an MBeanServer auth, when configured.
</td>
</tr>
<tr>
<td>password</td>
<td>
The password of an MBeanServer auth, when configured.
</td>
</tr>
<tr>
<td>host</td>
<td>
The JMX Connection host.
</td>
</tr>
<tr>
<td>port</td>
<td>
The JMX Connection port.
</td>
</tr>
<tr>
<td>ref</td>
<td>
The name of the ant internal reference for a jmx connection.
</td>
</tr>
</table>
<p><strong>NOTE</strong> - This Tasks only work,
when JSR 160 MBean Adaptor as remote jvm is configured.</p>
</body>