%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.jmx.OperationTag |
|
|
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: 1.4 $ |
|
37 | */ |
|
38 | 0 | public class OperationTag extends TagSupport implements CollectionTag { |
39 | ||
40 | /** The Log to which logging calls will be made. */ |
|
41 | 0 | private static final Log log = LogFactory.getLog(OperationTag.class); |
42 | ||
43 | private String name; |
|
44 | private Object arguments; |
|
45 | 0 | private List argList = null; |
46 | private String[] parameters; |
|
47 | ||
48 | 0 | public OperationTag() { |
49 | 0 | } |
50 | ||
51 | ||
52 | // CollectionTag interface |
|
53 | //------------------------------------------------------------------------- |
|
54 | public void addItem(Object value) { |
|
55 | 0 | if (argList == null) { |
56 | 0 | argList = new ArrayList(); |
57 | } |
|
58 | 0 | argList.add(value); |
59 | 0 | } |
60 | ||
61 | // Tag interface |
|
62 | //------------------------------------------------------------------------- |
|
63 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
64 | 0 | if (name == null) { |
65 | 0 | throw new MissingAttributeException("name"); |
66 | } |
|
67 | ||
68 | 0 | RegisterTag registerTag = (RegisterTag) findAncestorWithClass(RegisterTag.class); |
69 | 0 | if (registerTag == null) { |
70 | 0 | throw new JellyTagException("This class must be nested inside a <register> tag"); |
71 | } |
|
72 | 0 | Object bean = null; |
73 | try { |
|
74 | 0 | invokeBody(output); |
75 | ||
76 | 0 | ObjectName objectName = registerTag.getName(); |
77 | 0 | registerTag.getServer().invoke(objectName, getName(), getArgumentArray(), getParameters()); |
78 | 0 | } |
79 | catch (JellyTagException e) { |
|
80 | 0 | throw e; |
81 | } |
|
82 | catch (Exception e) { |
|
83 | 0 | throw new JellyTagException("Failed to register bean: " + bean, e); |
84 | } |
|
85 | finally { |
|
86 | 0 | argList = null; |
87 | } |
|
88 | 0 | } |
89 | ||
90 | ||
91 | // Properties |
|
92 | //------------------------------------------------------------------------- |
|
93 | ||
94 | ||
95 | ||
96 | /** |
|
97 | * @return Object |
|
98 | */ |
|
99 | public Object getArguments() { |
|
100 | 0 | return arguments; |
101 | } |
|
102 | ||
103 | /** |
|
104 | * @return String |
|
105 | */ |
|
106 | public String getName() { |
|
107 | 0 | return name; |
108 | } |
|
109 | ||
110 | /** |
|
111 | * @return String[] |
|
112 | */ |
|
113 | public String[] getParameters() { |
|
114 | 0 | return parameters; |
115 | } |
|
116 | ||
117 | /** |
|
118 | * Sets the arguments. |
|
119 | * @param arguments The arguments to set |
|
120 | */ |
|
121 | public void setArguments(Object arguments) { |
|
122 | 0 | this.arguments = arguments; |
123 | 0 | } |
124 | ||
125 | /** |
|
126 | * Sets the name. |
|
127 | * @param name The name to set |
|
128 | */ |
|
129 | public void setName(String name) { |
|
130 | 0 | this.name = name; |
131 | 0 | } |
132 | ||
133 | /** |
|
134 | * Sets the parameters. |
|
135 | * @param parameters The parameters to set |
|
136 | */ |
|
137 | public void setParameters(String[] parameters) { |
|
138 | 0 | this.parameters = parameters; |
139 | 0 | } |
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 | 0 | Object arg = getArguments(); |
151 | 0 | if (arg != null) { |
152 | 0 | if (arg instanceof Object[]) { |
153 | 0 | return (Object[]) arg; |
154 | } |
|
155 | else { |
|
156 | 0 | return new Object[] { arg }; |
157 | } |
|
158 | } |
|
159 | 0 | else if (argList != null) { |
160 | 0 | return argList.toArray(); |
161 | } |
|
162 | else { |
|
163 | 0 | return new Object[0]; |
164 | } |
|
165 | } |
|
166 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |