KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > ant > JmxSet


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.modeler.ant;
18
19 import javax.management.Attribute JavaDoc;
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.modeler.Registry;
26 import org.apache.tools.ant.Task;
27
28 /**
29  * Set mbean properties.
30  *
31  */

32 public class JmxSet extends Task {
33     private static Log log = LogFactory.getLog(JmxSet.class);
34
35     String JavaDoc attribute;
36     String JavaDoc value;
37     String JavaDoc valueRef;
38     Object JavaDoc objValue;
39     String JavaDoc objectName;
40     ObjectName JavaDoc oname;
41     String JavaDoc type;
42
43
44     public JmxSet() {
45     }
46
47     public void setAttribute(String JavaDoc attribute) {
48         this.attribute = attribute;
49     }
50
51     public void setName( String JavaDoc name ) {
52         this.attribute=name;
53     }
54
55     public String JavaDoc getName() {
56         return attribute;
57     }
58
59     public void setValue(String JavaDoc value) {
60         this.value = value;
61     }
62
63     public void addText( String JavaDoc value ) {
64         this.value=value;
65     }
66
67     public void setValueRef(String JavaDoc valueRef) {
68         this.valueRef = valueRef;
69     }
70
71     public void setType(String JavaDoc type) {
72         this.type = type;
73     }
74
75     public void setObjValue(Object JavaDoc objValue) {
76         this.objValue = objValue;
77     }
78
79     public void setObjectName(String JavaDoc name) {
80         this.objectName = name;
81     }
82
83     public void setObjectName( ObjectName JavaDoc oname ) {
84         this.oname=oname;
85     }
86
87     public void execute() {
88         try {
89             Registry registry=Registry.getRegistry();
90             MBeanServer JavaDoc server=registry.getMBeanServer();
91
92             if( oname==null )
93                 oname=new ObjectName JavaDoc(objectName);
94             if( type==null ) {
95                 type=registry.getType(oname, attribute);
96                 if( log.isDebugEnabled())
97                     log.debug("Discovered type " + type);
98             }
99
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 JavaDoc(attribute, objValue));
112
113         } catch(Exception JavaDoc ex) {
114             ex.printStackTrace();
115         }
116     }
117
118 }
119
Popular Tags