blob: 86242cf60c1eef69e58591f9b4eaa6e0cb6e2f18 [file] [log] [blame]
Stephane Frenotcb0356f2006-07-20 09:36:47 +00001/*
2 * Copyright (C) MX4J.
3 * All rights reserved.
4 *
5 * This software is distributed under the terms of the MX4J License version 1.0.
6 * See the terms of the MX4J License in the documentation provided with this software.
7 */
8/*
9 * Copyright 2005 The Apache Software Foundation
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 *
23 */
24package org.apache.felix.mosgi.jmx.httpconnector.mx4j.tools.adaptor.http;
25
26import java.io.IOException;
27import java.util.Iterator;
28import java.util.Map;
29import java.util.SortedMap;
30import java.util.TreeMap;
31import javax.management.Attribute;
32import javax.management.JMException;
33import javax.management.MBeanAttributeInfo;
34import javax.management.MBeanInfo;
35import javax.management.MalformedObjectNameException;
36import javax.management.ObjectName;
37import org.w3c.dom.Document;
38import org.w3c.dom.Element;
39
40/**
41 * SetAttributesCommandProcessor, processes a request for setting one or more attributes
42 * in one MBean. it uses th facility of havin multiple submit buttons in a web page
43 * if the set_all=Set variable is passed all attributes will be set, if a set_XXX varialbe
44 * is passed only the specific attribute will be set
45 *
46 * @author <a href="mailto:tibu@users.sourceforge.net">Carlos Quiroz</a>
47 * @version $Revision: 1.1.1.1 $
48 */
49public class SetAttributesCommandProcessor extends HttpCommandProcessorAdaptor
50{
51
52 public SetAttributesCommandProcessor()
53 {
54 }
55
56 public Document executeRequest(HttpInputStream in) throws IOException, JMException
57 {
58 Document document = builder.newDocument();
59
60 Element root = document.createElement("MBeanOperation");
61 document.appendChild(root);
62 Element operationElement = document.createElement("Operation");
63 operationElement.setAttribute("operation", "setattributes");
64 root.appendChild(operationElement);
65
66 String objectVariable = in.getVariable("objectname");
67 if (objectVariable == null || objectVariable.equals(""))
68 {
69 operationElement.setAttribute("result", "error");
70 operationElement.setAttribute("errorMsg", "Missing objectname in the request");
71 return document;
72 }
73 operationElement.setAttribute("objectname", objectVariable);
74 ObjectName name = null;
75 try
76 {
77 name = new ObjectName(objectVariable);
78 }
79 catch (MalformedObjectNameException e)
80 {
81 operationElement.setAttribute("result", "error");
82 operationElement.setAttribute("errorMsg", "Malformed object name");
83 return document;
84 }
85 if (server.isRegistered(name))
86 {
87 Map variables = in.getVariables();
88 if (variables.containsKey("setall"))
89 {
90 Iterator keys = variables.keySet().iterator();
91 SortedMap allAttributes = new TreeMap();
92 while (keys.hasNext()) {
93 String key = (String)keys.next();
94 if (key.startsWith("value_"))
95 {
96 String attributeVariable = key.substring(6, key.length());
97 String valueVariable = in.getVariable(key);
98 Element attributeElement = setAttribute(document, attributeVariable, valueVariable, name);
99 allAttributes.put(attributeVariable, attributeElement);
100 operationElement.appendChild(attributeElement);
101 }
102 }
103 keys = allAttributes.keySet().iterator();
104 while (keys.hasNext()) {
105 Element attributeElement = (Element)allAttributes.get(keys.next());
106 operationElement.appendChild(attributeElement);
107 }
108 }
109 else
110 {
111 Iterator keys = variables.keySet().iterator();
112 SortedMap allAttributes = new TreeMap();
113 while (keys.hasNext())
114 {
115 String key = (String)keys.next();
116 if (key.startsWith("set_"))
117 {
118 String attributeVariable = key.substring(4, key.length());
119 String valueVariable = in.getVariable("value_" + attributeVariable);
120 Element attributeElement = setAttribute(document, attributeVariable, valueVariable, name);
121 allAttributes.put(attributeVariable, attributeElement);
122 }
123 }
124 keys = allAttributes.keySet().iterator();
125 while (keys.hasNext()) {
126 Element attributeElement = (Element)allAttributes.get(keys.next());
127 operationElement.appendChild(attributeElement);
128 }
129 }
130 //operationElement.setAttribute("result", "success");
131 }
132 else
133 {
134 if (name != null)
135 {
136 operationElement.setAttribute("result", "error");
137 operationElement.setAttribute("errorMsg", "MBean " + name + " not registered");
138 }
139 }
140 return document;
141 }
142
143 private Element setAttribute(Document document, String attributeVariable, String valueVariable, ObjectName name) throws JMException
144 {
145 Element attributeElement = document.createElement("Attribute");
146 attributeElement.setAttribute("attribute", attributeVariable);
147 MBeanInfo info = server.getMBeanInfo(name);
148 MBeanAttributeInfo[] attributes = info.getAttributes();
149 MBeanAttributeInfo targetAttribute = null;
150 if (attributes != null)
151 {
152 for (int i=0;i<attributes.length;i++)
153 {
154 if (attributes[i].getName().equals(attributeVariable))
155 {
156 targetAttribute = attributes[i];
157 break;
158 }
159 }
160 }
161 if (targetAttribute != null)
162 {
163 String type = targetAttribute.getType();
164 Object value = null;
165 if (valueVariable != null)
166 {
167 try
168 {
169 value = CommandProcessorUtil.createParameterValue(type, valueVariable);
170 }
171 catch (Exception e)
172 {
173 attributeElement.setAttribute("result", "error");
174 attributeElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type);
175 }
176 if (value != null)
177 {
178 try
179 {
180 server.setAttribute(name, new Attribute(attributeVariable, value));
181 attributeElement.setAttribute("result", "success");
182 attributeElement.setAttribute("value", valueVariable);
183 }
184 catch (Exception e)
185 {
186 attributeElement.setAttribute("result", "error");
187 attributeElement.setAttribute("errorMsg", e.getMessage());
188 }
189 }
190 }
191 }
192 else
193 {
194 attributeElement.setAttribute("result", "error");
195 attributeElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found");
196 }
197 return attributeElement;
198 }
199
200}