001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.ant.jmx;
019:
020: import javax.management.Attribute;
021: import javax.management.MBeanAttributeInfo;
022: import javax.management.MBeanInfo;
023: import javax.management.MBeanServerConnection;
024: import javax.management.ObjectName;
025:
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Access <em>JMX</em> JSR 160 MBeans Server.
030: * <ul>
031: * <li>Get Mbeans attributes</li>
032: * <li>Show Get result as Ant console log</li>
033: * <li>Bind Get result as Ant properties</li>
034: * </ul>
035: * <p>
036: * Examples:
037: * Set a Mbean Manager attribute maxActiveSessions.
038: * Set this attribute with fresh jmx connection without save reference
039: * <pre>
040: * <jmx:set
041: * host="127.0.0.1"
042: * port="9014"
043: * ref=""
044: * name="Catalina:type=Manager,path="/ClusterTest",host=localhost"
045: * attribute="maxActiveSessions"
046: * value="100"
047: * type="int"
048: * echo="false">
049: * />
050: * </pre>
051: * </p>
052: * <p>
053: * First call to a remote MBeanserver save the JMXConnection a referenz <em>jmx.server</em>
054: * </p>
055: * These tasks require Ant 1.6 or later interface.
056: *
057: * @author Peter Rossbach
058: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
059: * @since 5.5.10
060: */
061:
062: public class JMXAccessorSetTask extends JMXAccessorTask {
063:
064: // ----------------------------------------------------- Instance Variables
065:
066: private String attribute;
067: private String value;
068: private String type;
069: private boolean convert = false;
070:
071: // ----------------------------------------------------- Instance Info
072:
073: /**
074: * Descriptive information describing this implementation.
075: */
076: private static final String info = "org.apache.catalina.ant.JMXAccessorSetTask/1.0";
077:
078: /**
079: * Return descriptive information about this implementation and the
080: * corresponding version number, in the format
081: * <code><description>/<version></code>.
082: */
083: public String getInfo() {
084:
085: return (info);
086:
087: }
088:
089: // ------------------------------------------------------------- Properties
090:
091: /**
092: * @return Returns the attribute.
093: */
094: public String getAttribute() {
095: return attribute;
096: }
097:
098: /**
099: * @param attribute The attribute to set.
100: */
101: public void setAttribute(String attribute) {
102: this .attribute = attribute;
103: }
104:
105: /**
106: * @return Returns the value.
107: */
108: public String getValue() {
109: return value;
110: }
111:
112: /**
113: * @param value The value to set.
114: */
115: public void setValue(String value) {
116: this .value = value;
117: }
118:
119: /**
120: * @return Returns the type.
121: */
122: public String getType() {
123: return type;
124: }
125:
126: /**
127: * @param valueType The type to set.
128: */
129: public void setType(String valueType) {
130: this .type = valueType;
131: }
132:
133: /**
134: * @return Returns the convert.
135: */
136: public boolean isConvert() {
137: return convert;
138: }
139:
140: /**
141: * @param convert The convert to set.
142: */
143: public void setConvert(boolean convert) {
144: this .convert = convert;
145: }
146:
147: // ------------------------------------------------------ protected Methods
148:
149: /**
150: * Execute the specified command, based on the configured properties. The
151: * input stream will be closed upon completion of this task, whether it was
152: * executed successfully or not.
153: *
154: * @exception Exception
155: * if an error occurs
156: */
157: public String jmxExecute(MBeanServerConnection jmxServerConnection)
158: throws Exception {
159:
160: if (getName() == null) {
161: throw new BuildException("Must specify a 'name'");
162: }
163: if ((attribute == null || value == null)) {
164: throw new BuildException(
165: "Must specify a 'attribute' and 'value' for set");
166: }
167: return jmxSet(jmxServerConnection, getName());
168: }
169:
170: /**
171: * @param jmxServerConnection
172: * @param name
173: * @throws Exception
174: */
175: protected String jmxSet(MBeanServerConnection jmxServerConnection,
176: String name) throws Exception {
177: Object realValue;
178: if (type != null) {
179: realValue = convertStringToType(value, type);
180: } else {
181: if (isConvert()) {
182: String mType = getMBeanAttributeType(
183: jmxServerConnection, name, attribute);
184: realValue = convertStringToType(value, mType);
185: } else
186: realValue = value;
187: }
188: jmxServerConnection.setAttribute(new ObjectName(name),
189: new Attribute(attribute, realValue));
190: return null;
191: }
192:
193: /**
194: * Get MBean Attriute from Mbean Server
195: * @param jmxServerConnection
196: * @param name
197: * @param attribute
198: * @return The type
199: * @throws Exception
200: */
201: protected String getMBeanAttributeType(
202: MBeanServerConnection jmxServerConnection, String name,
203: String attribute) throws Exception {
204: ObjectName oname = new ObjectName(name);
205: String mattrType = null;
206: MBeanInfo minfo = jmxServerConnection.getMBeanInfo(oname);
207: MBeanAttributeInfo attrs[] = minfo.getAttributes();
208: if (attrs != null) {
209: for (int i = 0; mattrType == null && i < attrs.length; i++) {
210: if (attribute.equals(attrs[i].getName()))
211: mattrType = attrs[i].getType();
212: }
213: }
214: return mattrType;
215: }
216: }
|