KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.Attribute JavaDoc;
12 import javax.management.JMException JavaDoc;
13 import javax.management.MBeanAttributeInfo JavaDoc;
14 import javax.management.MBeanInfo JavaDoc;
15 import javax.management.MalformedObjectNameException JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20
21 /**
22  * SetAttributeCommandProcessor, processes a request for setting one attribute
23  * in one MBean
24  *
25  * @version $Revision: 1.3 $
26  */

27 public class SetAttributeCommandProcessor extends HttpCommandProcessorAdaptor
28 {
29
30    public SetAttributeCommandProcessor()
31    {
32    }
33
34    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
35    {
36       Document JavaDoc document = builder.newDocument();
37
38       Element JavaDoc root = document.createElement("MBeanOperation");
39       document.appendChild(root);
40       Element JavaDoc operationElement = document.createElement("Operation");
41       operationElement.setAttribute("operation", "setattribute");
42       root.appendChild(operationElement);
43
44       String JavaDoc objectVariable = in.getVariable("objectname");
45       String JavaDoc attributeVariable = in.getVariable("attribute");
46       String JavaDoc valueVariable = in.getVariable("value");
47       if (objectVariable == null || objectVariable.equals("") ||
48           attributeVariable == null || attributeVariable.equals("") ||
49           valueVariable == null)
50       {
51          operationElement.setAttribute("result", "error");
52          operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
53          return document;
54       }
55       operationElement.setAttribute("objectname", objectVariable);
56       ObjectName JavaDoc name = null;
57       try
58       {
59          name = new ObjectName JavaDoc(objectVariable);
60       }
61       catch (MalformedObjectNameException JavaDoc e)
62       {
63          operationElement.setAttribute("result", "error");
64          operationElement.setAttribute("errorMsg", "Malformed object name");
65          return document;
66       }
67
68       if (server.isRegistered(name))
69       {
70          MBeanInfo JavaDoc info = server.getMBeanInfo(name);
71          MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
72          MBeanAttributeInfo JavaDoc targetAttribute = null;
73          if (attributes != null)
74          {
75             for (int i = 0; i < attributes.length; i++)
76             {
77                if (attributes[i].getName().equals(attributeVariable))
78                {
79                   targetAttribute = attributes[i];
80                   break;
81                }
82             }
83          }
84          if (targetAttribute != null)
85          {
86             String JavaDoc type = targetAttribute.getType();
87             Object JavaDoc value = null;
88             if (valueVariable != null)
89             {
90                try
91                {
92                   value = CommandProcessorUtil.createParameterValue(type, valueVariable);
93                }
94                catch (Exception JavaDoc e)
95                {
96                   operationElement.setAttribute("result", "error");
97                   operationElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type);
98                }
99                if (value != null)
100                {
101                   try
102                   {
103                      server.setAttribute(name, new Attribute JavaDoc(attributeVariable, value));
104                      operationElement.setAttribute("result", "success");
105                   }
106                   catch (Exception JavaDoc e)
107                   {
108                      operationElement.setAttribute("result", "error");
109                      operationElement.setAttribute("errorMsg", e.getMessage());
110                   }
111                }
112             }
113          }
114          else
115          {
116             operationElement.setAttribute("result", "error");
117             operationElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found");
118          }
119       }
120       else
121       {
122          if (name != null)
123          {
124             operationElement.setAttribute("result", "error");
125             operationElement.setAttribute("errorMsg", "MBean " + name + " not registered");
126          }
127       }
128       return document;
129    }
130
131 }
132
Popular Tags