View Javadoc

1   /*
2    * Copyright 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.jelly.tags.jmx;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.management.ObjectName;
23  
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.MissingAttributeException;
26  import org.apache.commons.jelly.TagSupport;
27  import org.apache.commons.jelly.XMLOutput;
28  import org.apache.commons.jelly.impl.CollectionTag;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * Registers a JavaBean or JMX MBean with a server..
34   *
35   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
36   * @version $Revision: 155420 $
37   */
38  public class OperationTag extends TagSupport implements CollectionTag {
39  
40      /*** The Log to which logging calls will be made. */
41      private static final Log log = LogFactory.getLog(OperationTag.class);
42  
43      private String name;
44      private Object arguments;
45      private List argList = null;
46      private String[] parameters;
47  
48      public OperationTag() {
49      }
50  
51  
52      // CollectionTag interface
53      //-------------------------------------------------------------------------
54      public void addItem(Object value) {
55          if (argList == null) {
56              argList = new ArrayList();
57          }
58          argList.add(value);
59      }
60  
61      // Tag interface
62      //-------------------------------------------------------------------------
63      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
64          if (name == null) {
65              throw new MissingAttributeException("name");
66          }
67  
68          RegisterTag registerTag = (RegisterTag) findAncestorWithClass(RegisterTag.class);
69          if (registerTag == null) {
70              throw new JellyTagException("This class must be nested inside a <register> tag");
71          }
72          Object bean = null;
73          try {
74              invokeBody(output);
75  
76              ObjectName objectName = registerTag.getName();
77              registerTag.getServer().invoke(objectName, getName(), getArgumentArray(), getParameters());
78          }
79          catch (JellyTagException e) {
80              throw e;
81          }
82          catch (Exception e) {
83              throw new JellyTagException("Failed to register bean: " + bean, e);
84          }
85          finally {
86              argList = null;
87          }
88      }
89  
90  
91      // Properties
92      //-------------------------------------------------------------------------
93  
94  
95  
96      /***
97       * @return Object
98       */
99      public Object getArguments() {
100         return arguments;
101     }
102 
103     /***
104      * @return String
105      */
106     public String getName() {
107         return name;
108     }
109 
110     /***
111      * @return String[]
112      */
113     public String[] getParameters() {
114         return parameters;
115     }
116 
117     /***
118      * Sets the arguments.
119      * @param arguments The arguments to set
120      */
121     public void setArguments(Object arguments) {
122         this.arguments = arguments;
123     }
124 
125     /***
126      * Sets the name.
127      * @param name The name to set
128      */
129     public void setName(String name) {
130         this.name = name;
131     }
132 
133     /***
134      * Sets the parameters.
135      * @param parameters The parameters to set
136      */
137     public void setParameters(String[] parameters) {
138         this.parameters = parameters;
139     }
140 
141     // Implementation methods
142     //-------------------------------------------------------------------------
143 
144     /***
145      * Converts the argument property into an Object[] or converts the list of
146      * added argument objects (added via child tags) to an Object[] or
147      * return an empty argument array.
148      */
149     protected Object[] getArgumentArray() {
150         Object arg = getArguments();
151         if (arg != null) {
152             if (arg instanceof Object[]) {
153                 return (Object[]) arg;
154             }
155             else {
156                 return new Object[] { arg };
157             }
158         }
159         else if (argList != null) {
160             return argList.toArray();
161         }
162         else {
163             return new Object[0];
164         }
165     }
166 }