KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > adaptor > http > SetAttributesCommandProcessor


1 /*
2  * Copyright (C) The MX4J Contributors.
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 package mx4j.tools.adaptor.http;
9
10 import java.io.IOException JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.SortedMap JavaDoc;
14 import java.util.TreeMap JavaDoc;
15 import javax.management.Attribute JavaDoc;
16 import javax.management.JMException JavaDoc;
17 import javax.management.MBeanAttributeInfo JavaDoc;
18 import javax.management.MBeanInfo JavaDoc;
19 import javax.management.MalformedObjectNameException JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24
25 /**
26  * SetAttributesCommandProcessor, processes a request for setting one or more attributes
27  * in one MBean. it uses th facility of havin multiple submit buttons in a web page
28  * if the set_all=Set variable is passed all attributes will be set, if a set_XXX varialbe
29  * is passed only the specific attribute will be set
30  *
31  * @version $Revision: 1.3 $
32  */

33 public class SetAttributesCommandProcessor extends HttpCommandProcessorAdaptor
34 {
35
36    public SetAttributesCommandProcessor()
37    {
38    }
39
40    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
41    {
42       Document JavaDoc document = builder.newDocument();
43
44       Element JavaDoc root = document.createElement("MBeanOperation");
45       document.appendChild(root);
46       Element JavaDoc operationElement = document.createElement("Operation");
47       operationElement.setAttribute("operation", "setattributes");
48       root.appendChild(operationElement);
49
50       String JavaDoc objectVariable = in.getVariable("objectname");
51       if (objectVariable == null || objectVariable.equals(""))
52       {
53          operationElement.setAttribute("result", "error");
54          operationElement.setAttribute("errorMsg", "Missing objectname in the request");
55          return document;
56       }
57       operationElement.setAttribute("objectname", objectVariable);
58       ObjectName JavaDoc name = null;
59       try
60       {
61          name = new ObjectName JavaDoc(objectVariable);
62       }
63       catch (MalformedObjectNameException JavaDoc e)
64       {
65          operationElement.setAttribute("result", "error");
66          operationElement.setAttribute("errorMsg", "Malformed object name");
67          return document;
68       }
69       if (server.isRegistered(name))
70       {
71          Map JavaDoc variables = in.getVariables();
72          if (variables.containsKey("setall"))
73          {
74             Iterator JavaDoc keys = variables.keySet().iterator();
75             SortedMap JavaDoc allAttributes = new TreeMap JavaDoc();
76             while (keys.hasNext())
77             {
78                String JavaDoc key = (String JavaDoc)keys.next();
79                if (key.startsWith("value_"))
80                {
81                   String JavaDoc attributeVariable = key.substring(6, key.length());
82                   String JavaDoc valueVariable = in.getVariable(key);
83                   Element JavaDoc attributeElement = setAttribute(document, attributeVariable, valueVariable, name);
84                   allAttributes.put(attributeVariable, attributeElement);
85                   operationElement.appendChild(attributeElement);
86                }
87             }
88             keys = allAttributes.keySet().iterator();
89             while (keys.hasNext())
90             {
91                Element JavaDoc attributeElement = (Element JavaDoc)allAttributes.get(keys.next());
92                operationElement.appendChild(attributeElement);
93             }
94          }
95          else
96          {
97             Iterator JavaDoc keys = variables.keySet().iterator();
98             SortedMap JavaDoc allAttributes = new TreeMap JavaDoc();
99             while (keys.hasNext())
100             {
101                String JavaDoc key = (String JavaDoc)keys.next();
102                if (key.startsWith("set_"))
103                {
104                   String JavaDoc attributeVariable = key.substring(4, key.length());
105                   String JavaDoc valueVariable = in.getVariable("value_" + attributeVariable);
106                   Element JavaDoc attributeElement = setAttribute(document, attributeVariable, valueVariable, name);
107                   allAttributes.put(attributeVariable, attributeElement);
108                }
109             }
110             keys = allAttributes.keySet().iterator();
111             while (keys.hasNext())
112             {
113                Element JavaDoc attributeElement = (Element JavaDoc)allAttributes.get(keys.next());
114                operationElement.appendChild(attributeElement);
115             }
116          }
117          //operationElement.setAttribute("result", "success");
118
}
119       else
120       {
121          if (name != null)
122          {
123             operationElement.setAttribute("result", "error");
124             operationElement.setAttribute("errorMsg", "MBean " + name + " not registered");
125          }
126       }
127       return document;
128    }
129
130    private Element JavaDoc setAttribute(Document JavaDoc document, String JavaDoc attributeVariable, String JavaDoc valueVariable, ObjectName JavaDoc name) throws JMException JavaDoc
131    {
132       Element JavaDoc attributeElement = document.createElement("Attribute");
133       attributeElement.setAttribute("attribute", attributeVariable);
134       MBeanInfo JavaDoc info = server.getMBeanInfo(name);
135       MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
136       MBeanAttributeInfo JavaDoc targetAttribute = null;
137       if (attributes != null)
138       {
139          for (int i = 0; i < attributes.length; i++)
140          {
141             if (attributes[i].getName().equals(attributeVariable))
142             {
143                targetAttribute = attributes[i];
144                break;
145             }
146          }
147       }
148       if (targetAttribute != null)
149       {
150          String JavaDoc type = targetAttribute.getType();
151          Object JavaDoc value = null;
152          if (valueVariable != null)
153          {
154             try
155             {
156                value = CommandProcessorUtil.createParameterValue(type, valueVariable);
157             }
158             catch (Exception JavaDoc e)
159             {
160                attributeElement.setAttribute("result", "error");
161                attributeElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type);
162             }
163             if (value != null)
164             {
165                try
166                {
167                   server.setAttribute(name, new Attribute JavaDoc(attributeVariable, value));
168                   attributeElement.setAttribute("result", "success");
169                   attributeElement.setAttribute("value", valueVariable);
170                }
171                catch (Exception JavaDoc e)
172                {
173                   attributeElement.setAttribute("result", "error");
174                   attributeElement.setAttribute("errorMsg", e.getMessage());
175                }
176             }
177          }
178       }
179       else
180       {
181          attributeElement.setAttribute("result", "error");
182          attributeElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found");
183       }
184       return attributeElement;
185    }
186
187 }
188
Popular Tags