KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.management.JMException JavaDoc;
15 import javax.management.MBeanInfo JavaDoc;
16 import javax.management.MBeanOperationInfo JavaDoc;
17 import javax.management.MBeanParameterInfo JavaDoc;
18 import javax.management.MalformedObjectNameException JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23
24 /**
25  * InvokeOperationCommandProcessor, processes a request for unregistering an MBean
26  *
27  * @version $Revision: 1.3 $
28  */

29 public class InvokeOperationCommandProcessor extends HttpCommandProcessorAdaptor
30 {
31    public InvokeOperationCommandProcessor()
32    {
33    }
34
35    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
36    {
37       Document JavaDoc document = builder.newDocument();
38
39       Element JavaDoc root = document.createElement("MBeanOperation");
40       document.appendChild(root);
41       Element JavaDoc operationElement = document.createElement("Operation");
42       operationElement.setAttribute("operation", "invoke");
43       root.appendChild(operationElement);
44
45       String JavaDoc objectVariable = in.getVariable("objectname");
46       String JavaDoc operationVariable = in.getVariable("operation");
47       if (objectVariable == null || objectVariable.equals("")
48           || operationVariable == null || operationVariable.equals(""))
49       {
50          operationElement.setAttribute("result", "error");
51          operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
52          return document;
53       }
54       operationElement.setAttribute("objectname", objectVariable);
55       List JavaDoc types = new ArrayList JavaDoc();
56       List JavaDoc values = new ArrayList JavaDoc();
57       int i = 0;
58       boolean unmatchedParameters = false;
59       boolean valid = false;
60       do
61       {
62          String JavaDoc parameterType = in.getVariable("type" + i);
63          String JavaDoc parameterValue = in.getVariable("value" + i);
64          valid = (parameterType != null && parameterValue != null);
65          if (valid)
66          {
67             types.add(parameterType);
68             Object JavaDoc value = null;
69             try
70             {
71                value = CommandProcessorUtil.createParameterValue(parameterType, parameterValue);
72             }
73
74             catch (Exception JavaDoc e)
75             {
76                operationElement.setAttribute("result", "error");
77                operationElement.setAttribute("errorMsg", "Parameter " + i + ": " + parameterValue + " cannot be converted to type " + parameterType);
78                return document;
79             }
80             if (value != null)
81             {
82                values.add(value);
83             }
84          }
85          if (parameterType == null ^ parameterValue == null)
86          {
87             unmatchedParameters = true;
88             break;
89          }
90          i++;
91       }
92       while (valid);
93       if (objectVariable == null || objectVariable.equals("") ||
94           operationVariable == null || operationVariable.equals(""))
95       {
96          operationElement.setAttribute("result", "error");
97          operationElement.setAttribute("errorMsg", "Incorrect parameters in the request");
98          return document;
99       }
100       if (unmatchedParameters)
101       {
102          operationElement.setAttribute("result", "error");
103          operationElement.setAttribute("errorMsg", "count of parameter types doesn't match count of parameter values");
104          return document;
105       }
106       ObjectName JavaDoc name = null;
107       try
108       {
109          name = new ObjectName JavaDoc(objectVariable);
110       }
111       catch (MalformedObjectNameException JavaDoc e)
112       {
113          operationElement.setAttribute("result", "error");
114          operationElement.setAttribute("errorMsg", "Malformed object name");
115          return document;
116       }
117
118       if (server.isRegistered(name))
119       {
120          MBeanInfo JavaDoc info = server.getMBeanInfo(name);
121          MBeanOperationInfo JavaDoc[] operations = info.getOperations();
122          boolean match = false;
123          if (operations != null)
124          {
125             for (int j = 0; j < operations.length; j++)
126             {
127                if (operations[j].getName().equals(operationVariable))
128                {
129                   MBeanParameterInfo JavaDoc[] parameters = operations[j].getSignature();
130                   if (parameters.length != types.size())
131                   {
132                      continue;
133                   }
134                   Iterator JavaDoc k = types.iterator();
135                   boolean signatureMatch = true;
136                   for (int p = 0; p < types.size(); p++)
137                   {
138                      if (!parameters[p].getType().equals(k.next()))
139                      {
140                         signatureMatch = false;
141                         break;
142                      }
143                   }
144                   match = signatureMatch;
145                }
146                if (match)
147                {
148                   break;
149                }
150             }
151          }
152          if (!match)
153          {
154             operationElement.setAttribute("result", "error");
155             operationElement.setAttribute("errorMsg", "Operation singature has no match in the MBean");
156          }
157          else
158          {
159             try
160             {
161                Object JavaDoc[] params = values.toArray();
162                String JavaDoc[] signature = new String JavaDoc[types.size()];
163                types.toArray(signature);
164                Object JavaDoc returnValue = server.invoke(name, operationVariable, params, signature);
165                operationElement.setAttribute("result", "success");
166                if (returnValue != null)
167                {
168                   operationElement.setAttribute("returnclass", returnValue.getClass().getName());
169                   operationElement.setAttribute("return", returnValue.toString());
170                }
171                else
172                {
173                   operationElement.setAttribute("returnclass", null);
174                   operationElement.setAttribute("return", null);
175                }
176             }
177             catch (Exception JavaDoc e)
178             {
179                operationElement.setAttribute("result", "error");
180                operationElement.setAttribute("errorMsg", e.getMessage());
181             }
182          }
183       }
184       else
185       {
186          if (name != null)
187          {
188             operationElement.setAttribute("result", "error");
189             operationElement.setAttribute("errorMsg", new StringBuffer JavaDoc("MBean ").append(name).append(" not registered").toString());
190          }
191       }
192       return document;
193    }
194
195 }
196
Popular Tags