View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.modeler.ant;
19  
20  import javax.management.Attribute;
21  import javax.management.MBeanServer;
22  import javax.management.ObjectName;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.commons.modeler.Registry;
27  import org.apache.tools.ant.Task;
28  
29  /***
30   * Set mbean properties.
31   *
32   */
33  public class JmxSet extends Task {
34      private static Log log = LogFactory.getLog(JmxSet.class);
35  
36      String attribute;
37      String value;
38      String valueRef;
39      Object objValue;
40      String objectName;
41      ObjectName oname;
42      String type;
43  
44  
45      public JmxSet() {
46      }
47  
48      public void setAttribute(String attribute) {
49          this.attribute = attribute;
50      }
51  
52      public void setName( String name ) {
53          this.attribute=name;
54      }
55  
56      public String getName() {
57          return attribute;
58      }
59  
60      public void setValue(String value) {
61          this.value = value;
62      }
63  
64      public void addText( String value ) {
65          this.value=value;
66      }
67  
68      public void setValueRef(String valueRef) {
69          this.valueRef = valueRef;
70      }
71  
72      public void setType(String type) {
73          this.type = type;
74      }
75  
76      public void setObjValue(Object objValue) {
77          this.objValue = objValue;
78      }
79  
80      public void setObjectName(String name) {
81          this.objectName = name;
82      }
83  
84      public void setObjectName( ObjectName oname ) {
85          this.oname=oname;
86      }
87  
88      public void execute() {
89          try {
90              Registry registry=Registry.getRegistry();
91              MBeanServer server=registry.getMBeanServer();
92  
93              if( oname==null )
94                  oname=new ObjectName(objectName);
95              if( type==null ) {
96                  type=registry.getType(oname, attribute);
97                  if( log.isDebugEnabled())
98                      log.debug("Discovered type " + type);
99              }
100 
101             // XXX convert value, use meta data to find type
102              if( objValue==null && valueRef != null ) {
103                  objValue=project.getReference(valueRef);
104              }
105              if( objValue==null ) {
106                  objValue=registry.convertValue(type, value);
107 
108              }
109             if( log.isDebugEnabled())
110                 log.debug("Setting " + oname + " " + attribute + " " +
111                         objValue);
112             server.setAttribute(oname, new Attribute(attribute, objValue));
113 
114         } catch(Exception ex) {
115             ex.printStackTrace();
116         }
117     }
118 
119 }