blob: 12285f2142dc026de4e6b2b98dede1167e57fa62 [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 javax.management.ObjectName;
27import javax.management.JMException;
28import javax.management.MalformedObjectNameException;
29import javax.management.MBeanInfo;
30import javax.management.Attribute;
31import javax.management.MBeanAttributeInfo;
32import java.io.IOException;
33import org.w3c.dom.Document;
34import org.w3c.dom.Element;
35
36/**
37 * SetAttributeCommandProcessor, processes a request for setting one attribute
38 * in one MBean
39 *
40 * @author <a href="mailto:tibu@users.sourceforge.net">Carlos Quiroz</a>
41 * @version $Revision: 1.1.1.1 $
42 */
43public class SetAttributeCommandProcessor extends HttpCommandProcessorAdaptor
44{
45
46 public SetAttributeCommandProcessor()
47 {
48 }
49
50 public Document executeRequest(HttpInputStream in) throws IOException, JMException
51 {
52 Document document = builder.newDocument();
53
54 Element root = document.createElement("MBeanOperation");
55 document.appendChild(root);
56 Element operationElement = document.createElement("Operation");
57 operationElement.setAttribute("operation", "setattribute");
58 root.appendChild(operationElement);
59
60 String objectVariable = in.getVariable("objectname");
61 String attributeVariable = in.getVariable("attribute");
62 String valueVariable = in.getVariable("value");
63 if (objectVariable == null || objectVariable.equals("") ||
64 attributeVariable == null || attributeVariable.equals("") ||
65 valueVariable == null)
66 {
67 operationElement.setAttribute("result", "error");
68 operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
69 return document;
70 }
71 operationElement.setAttribute("objectname", objectVariable);
72 ObjectName name = null;
73 try
74 {
75 name = new ObjectName(objectVariable);
76 }
77 catch (MalformedObjectNameException e)
78 {
79 operationElement.setAttribute("result", "error");
80 operationElement.setAttribute("errorMsg", "Malformed object name");
81 return document;
82 }
83
84 if (server.isRegistered(name))
85 {
86 MBeanInfo info = server.getMBeanInfo(name);
87 MBeanAttributeInfo[] attributes = info.getAttributes();
88 MBeanAttributeInfo targetAttribute = null;
89 if (attributes != null)
90 {
91 for (int i=0;i<attributes.length;i++)
92 {
93 if (attributes[i].getName().equals(attributeVariable))
94 {
95 targetAttribute = attributes[i];
96 break;
97 }
98 }
99 }
100 if (targetAttribute != null)
101 {
102 String type = targetAttribute.getType();
103 Object value = null;
104 if (valueVariable != null)
105 {
106 try
107 {
108 value = CommandProcessorUtil.createParameterValue(type, valueVariable);
109 }
110 catch (Exception e)
111 {
112 operationElement.setAttribute("result", "error");
113 operationElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type);
114 }
115 if (value != null)
116 {
117 try
118 {
119 server.setAttribute(name, new Attribute(attributeVariable, value));
120 operationElement.setAttribute("result", "success");
121 }
122 catch (Exception e)
123 {
124 operationElement.setAttribute("result", "error");
125 operationElement.setAttribute("errorMsg", e.getMessage());
126 }
127 }
128 }
129 }
130 else
131 {
132 operationElement.setAttribute("result", "error");
133 operationElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found");
134 }
135 }
136 else
137 {
138 if (name != null)
139 {
140 operationElement.setAttribute("result", "error");
141 operationElement.setAttribute("errorMsg", "MBean " + name + " not registered");
142 }
143 }
144 return document;
145 }
146
147}