KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import javax.management.JMException JavaDoc;
14 import javax.management.MalformedObjectNameException JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19
20 /**
21  * CreateMBeanCommandProcessor, processes a request for creating and registering
22  * an MBean
23  *
24  * @version $Revision: 1.3 $
25  */

26 public class CreateMBeanCommandProcessor extends HttpCommandProcessorAdaptor
27 {
28
29    /**
30     * Constructs a new CreateMBeanCommandProcessor
31     */

32    public CreateMBeanCommandProcessor()
33    {
34    }
35
36
37    public Document JavaDoc executeRequest(HttpInputStream in)
38            throws IOException JavaDoc, JMException JavaDoc
39    {
40       Document JavaDoc document = builder.newDocument();
41
42       Element JavaDoc root = document.createElement("MBeanOperation");
43       document.appendChild(root);
44       Element JavaDoc operationElement = document.createElement("Operation");
45       operationElement.setAttribute("name", "create");
46       root.appendChild(operationElement);
47
48       String JavaDoc objectVariable = in.getVariable("objectname");
49       String JavaDoc classVariable = in.getVariable("class");
50       if (objectVariable == null || objectVariable.equals("")
51           || classVariable == null || classVariable.equals(""))
52       {
53          operationElement.setAttribute("result", "error");
54          operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
55          return document;
56       }
57       operationElement.setAttribute("objectname", objectVariable);
58       List JavaDoc types = new ArrayList JavaDoc();
59       List JavaDoc values = new ArrayList JavaDoc();
60       int i = 0;
61       boolean unmatchedParameters = false;
62       boolean valid = false;
63       do
64       {
65          String JavaDoc parameterType = in.getVariable("type" + i);
66          String JavaDoc parameterValue = in.getVariable("value" + i);
67          valid = (parameterType != null && parameterValue != null);
68          if (valid)
69          {
70             types.add(parameterType);
71             Object JavaDoc value = null;
72             try
73             {
74                value = CommandProcessorUtil.createParameterValue(parameterType, parameterValue);
75             }
76             catch (Exception JavaDoc e)
77             {
78                operationElement.setAttribute("result", "error");
79                operationElement.setAttribute("errorMsg", "Parameter " + i + ": " + parameterValue + " cannot be converted to type " + parameterType);
80                return document;
81             }
82             if (value != null)
83             {
84                values.add(value);
85             }
86          }
87          if (parameterType == null ^ parameterValue == null)
88          {
89             unmatchedParameters = true;
90             break;
91          }
92          i++;
93       }
94       while (valid);
95       if (objectVariable == null || objectVariable.equals(""))
96       {
97          operationElement.setAttribute("result", "error");
98          operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
99          return document;
100       }
101       if (unmatchedParameters)
102       {
103          operationElement.setAttribute("result", "error");
104          operationElement.setAttribute("errorMsg", "count of parameter types doesn't match count of parameter values");
105          return document;
106       }
107       ObjectName JavaDoc name = null;
108       try
109       {
110          name = new ObjectName JavaDoc(objectVariable);
111       }
112       catch (MalformedObjectNameException JavaDoc e)
113       {
114          operationElement.setAttribute("result", "error");
115          operationElement.setAttribute("errorMsg", "Malformed object name");
116          return document;
117       }
118
119       if (server.isRegistered(name))
120       {
121          operationElement.setAttribute("result", "error");
122          operationElement.setAttribute("errorMsg", "A MBean with name " + name + " is already registered");
123          return document;
124       }
125       else
126       {
127          try
128          {
129             if (types.size() > 0)
130             {
131                Object JavaDoc[] params = values.toArray();
132                String JavaDoc[] signature = new String JavaDoc[types.size()];
133                types.toArray(signature);
134                server.createMBean(classVariable, name, null, params, signature);
135             }
136             else
137             {
138                server.createMBean(classVariable, name, null);
139             }
140             operationElement.setAttribute("result", "success");
141          }
142          catch (Exception JavaDoc e)
143          {
144             operationElement.setAttribute("result", "error");
145             operationElement.setAttribute("errorMsg", e.getMessage());
146          }
147       }
148       return document;
149    }
150
151 }
152
153
Popular Tags