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.commons.modeler.ant;
019:
020: import javax.management.Attribute;
021: import javax.management.MBeanServer;
022: import javax.management.ObjectName;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.commons.modeler.Registry;
027: import org.apache.tools.ant.Task;
028:
029: /**
030: * Set mbean properties.
031: *
032: */
033: public class JmxSet extends Task {
034: private static Log log = LogFactory.getLog(JmxSet.class);
035:
036: String attribute;
037: String value;
038: String valueRef;
039: Object objValue;
040: String objectName;
041: ObjectName oname;
042: String type;
043:
044: public JmxSet() {
045: }
046:
047: public void setAttribute(String attribute) {
048: this .attribute = attribute;
049: }
050:
051: public void setName(String name) {
052: this .attribute = name;
053: }
054:
055: public String getName() {
056: return attribute;
057: }
058:
059: public void setValue(String value) {
060: this .value = value;
061: }
062:
063: public void addText(String value) {
064: this .value = value;
065: }
066:
067: public void setValueRef(String valueRef) {
068: this .valueRef = valueRef;
069: }
070:
071: public void setType(String type) {
072: this .type = type;
073: }
074:
075: public void setObjValue(Object objValue) {
076: this .objValue = objValue;
077: }
078:
079: public void setObjectName(String name) {
080: this .objectName = name;
081: }
082:
083: public void setObjectName(ObjectName oname) {
084: this .oname = oname;
085: }
086:
087: public void execute() {
088: try {
089: Registry registry = Registry.getRegistry();
090: MBeanServer server = registry.getMBeanServer();
091:
092: if (oname == null)
093: oname = new ObjectName(objectName);
094: if (type == null) {
095: type = registry.getType(oname, attribute);
096: if (log.isDebugEnabled())
097: log.debug("Discovered type " + type);
098: }
099:
100: // XXX convert value, use meta data to find type
101: if (objValue == null && valueRef != null) {
102: objValue = project.getReference(valueRef);
103: }
104: if (objValue == null) {
105: objValue = registry.convertValue(type, value);
106:
107: }
108: if (log.isDebugEnabled())
109: log.debug("Setting " + oname + " " + attribute + " "
110: + objValue);
111: server.setAttribute(oname, new Attribute(attribute,
112: objValue));
113:
114: } catch (Exception ex) {
115: ex.printStackTrace();
116: }
117: }
118:
119: }
|