KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > resin > MBean


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Sam
28  */

29
30
31 package com.caucho.quercus.lib.resin;
32
33 import com.caucho.quercus.env.Value;
34
35 import javax.management.Attribute JavaDoc;
36 import javax.management.MBeanInfo JavaDoc;
37 import javax.management.MBeanOperationInfo JavaDoc;
38 import javax.management.MBeanParameterInfo JavaDoc;
39 import javax.management.MBeanServerConnection JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 public class MBean {
46   private static final Logger JavaDoc log = Logger.getLogger(MBean.class.getName());
47
48   private static final HashMap JavaDoc<String JavaDoc,Marshall> _marshallMap
49     = new HashMap JavaDoc<String JavaDoc,Marshall>();
50
51   private MBeanServerConnection JavaDoc _server;
52   private ObjectName JavaDoc _name;
53   private MBeanInfo JavaDoc _info;
54
55   MBean(MBeanServerConnection JavaDoc server, ObjectName JavaDoc name)
56   {
57     _server = server;
58     _name = name;
59   }
60
61   /**
62    * Returns the mbean's canonical name.
63    */

64   public String JavaDoc getMbean_name()
65   {
66     return _name.getCanonicalName();
67   }
68
69   /**
70    * Returns the MBeanInfo for the mbean.
71    */

72   public MBeanInfo JavaDoc getMbean_info()
73   {
74     try {
75       if (_info == null)
76         _info = _server.getMBeanInfo(_name);
77
78       return _info;
79     } catch (Exception JavaDoc e) {
80       log.log(Level.FINE, e.toString(), e);
81
82       return null;
83     }
84   }
85
86   /**
87    * Returns an attribute.
88    */

89   public Object JavaDoc __getField(String JavaDoc attrName)
90   {
91     try {
92       return unmarshall(_server.getAttribute(_name, attrName));
93     } catch (Exception JavaDoc e) {
94       log.log(Level.FINE, e.toString(), e);
95
96       return null;
97     }
98   }
99
100   /**
101    * Sets an attribute.
102    */

103   public boolean __setField(String JavaDoc attrName, Object JavaDoc value)
104   {
105     try {
106       _server.setAttribute(_name, new Attribute JavaDoc(attrName, value));
107
108       return true;
109     } catch (Exception JavaDoc e) {
110       log.log(Level.FINE, e.toString(), e);
111
112       return false;
113     }
114   }
115
116   /**
117    * Calls a method.
118    */

119   public Object JavaDoc __call(String JavaDoc name, Value []values)
120   {
121     try {
122       Object JavaDoc []args = new Object JavaDoc[values.length];
123       String JavaDoc []sig = new String JavaDoc[values.length];
124
125       for (int i = 0; i < values.length; i++) {
126     args[i] = values[i].toJavaObject();
127
128     if (args[i] != null)
129       sig[i] = args[i].getClass().getName();
130     else
131       sig[i] = "java.lang.Object";
132       }
133
134       String JavaDoc []mbeanSig = findClosestOperation(name, sig);
135
136       if (mbeanSig != null) {
137     marshall(args, mbeanSig);
138     
139     return unmarshall(_server.invoke(_name, name, args, mbeanSig));
140       }
141       else
142     return null;
143     } catch (Exception JavaDoc e) {
144       log.log(Level.FINE, e.toString(), e);
145
146       return null;
147     }
148   }
149
150   protected String JavaDoc []findClosestOperation(String JavaDoc name, String JavaDoc []sig)
151     throws Exception JavaDoc
152   {
153     MBeanInfo JavaDoc info = getMbean_info();
154
155     MBeanOperationInfo JavaDoc []ops = info.getOperations();
156     
157     MBeanOperationInfo JavaDoc bestOp = null;
158     long bestCost = Long.MAX_VALUE;
159
160     for (int i = 0; i < ops.length; i++) {
161       MBeanOperationInfo JavaDoc op = ops[i];
162       
163       if (! name.equals(op.getName()))
164     continue;
165
166       if (op.getSignature().length == sig.length) {
167     long cost = calculateCost(op.getSignature(), sig);
168
169     if (cost < bestCost) {
170       bestCost = cost;
171       bestOp = op;
172     }
173       }
174     }
175
176     if (bestOp != null) {
177       String JavaDoc []mbeanSig = new String JavaDoc[sig.length];
178
179       MBeanParameterInfo JavaDoc []params = bestOp.getSignature();
180
181       for (int j = 0; j < params.length; j++) {
182     mbeanSig[j] = params[j].getType();
183       }
184
185       return mbeanSig;
186     }
187       
188     return null;
189   }
190
191   private static long calculateCost(MBeanParameterInfo JavaDoc []paramInfo,
192                     String JavaDoc []args)
193   {
194     long cost = 0;
195     
196     for (int i = 0; i < paramInfo.length; i++) {
197       String JavaDoc param = paramInfo[i].getType();
198       String JavaDoc arg = args[i];
199       
200       if (param.equals(arg)) {
201       }
202       else if ((param.indexOf('[') >= 0) != (arg.indexOf('[') >= 0)) {
203     cost += 100;
204       }
205       else
206     cost += 1;
207     }
208
209     return cost;
210   }
211
212   private void marshall(Object JavaDoc []args, String JavaDoc []sig)
213   {
214     for (int i = 0; i < sig.length; i++) {
215       args[i] = findMarshall(sig[i]).marshall(args[i]);
216     }
217   }
218
219   private Object JavaDoc unmarshall(Object JavaDoc value)
220   {
221     if (value instanceof ObjectName JavaDoc) {
222       ObjectName JavaDoc name = (ObjectName JavaDoc) value;
223
224       return new MBean(_server, name);
225     }
226     else if (value instanceof ObjectName JavaDoc[]) {
227       ObjectName JavaDoc []names = (ObjectName JavaDoc []) value;
228
229       MBean []mbeans = new MBean[names.length];
230
231       for (int i = 0; i < names.length; i++)
232     mbeans[i] = new MBean(_server, names[i]);
233
234       return mbeans;
235     }
236     else
237       return value;
238   }
239
240   private Marshall findMarshall(String JavaDoc sig)
241   {
242     Marshall marshall = _marshallMap.get(sig);
243
244     if (marshall != null)
245       return marshall;
246     else
247       return Marshall.MARSHALL;
248   }
249
250   public String JavaDoc toString()
251   {
252     if (_name == null)
253       return "MBean[]";
254     else
255       return "MBean[" + _name.getCanonicalName() + "]";
256   }
257
258   static class Marshall {
259     static final Marshall MARSHALL = new Marshall();
260     
261     public Object JavaDoc marshall(Object JavaDoc value)
262     {
263       return value;
264     }
265   }
266
267   static class IntMarshall extends Marshall {
268     static final Marshall MARSHALL = new IntMarshall();
269     
270     public Object JavaDoc marshall(Object JavaDoc value)
271     {
272       if (value instanceof Integer JavaDoc)
273     return value;
274       else if (value instanceof Number JavaDoc)
275     return new Integer JavaDoc(((Number JavaDoc) value).intValue());
276       else if (value == null)
277     return new Integer JavaDoc(0);
278       else {
279     try {
280       return new Integer JavaDoc(Integer.parseInt(String.valueOf(value)));
281     } catch (Exception JavaDoc e) {
282       return new Integer JavaDoc(0);
283     }
284       }
285     }
286   }
287
288   static class LongMarshall extends Marshall {
289     static final Marshall MARSHALL = new LongMarshall();
290     
291     public Object JavaDoc marshall(Object JavaDoc value)
292     {
293       if (value instanceof Long JavaDoc)
294     return value;
295       else if (value instanceof Number JavaDoc)
296     return new Long JavaDoc(((Number JavaDoc) value).longValue());
297       else if (value == null)
298     return new Long JavaDoc(0);
299       else {
300     try {
301       return new Long JavaDoc(Long.parseLong(String.valueOf(value)));
302     } catch (Exception JavaDoc e) {
303       return new Long JavaDoc(0);
304     }
305       }
306     }
307   }
308
309   static class StringMarshall extends Marshall {
310     static final Marshall MARSHALL = new StringMarshall();
311     
312     public Object JavaDoc marshall(Object JavaDoc value)
313     {
314       if (value == null)
315     return null;
316       else
317     return value.toString();
318     }
319   }
320
321   static {
322     _marshallMap.put("int", IntMarshall.MARSHALL);
323     _marshallMap.put("java.lang.Integer", IntMarshall.MARSHALL);
324     
325     _marshallMap.put("long", LongMarshall.MARSHALL);
326     _marshallMap.put("java.lang.Long", LongMarshall.MARSHALL);
327     
328     _marshallMap.put("java.lang.String", StringMarshall.MARSHALL);
329   }
330 }
331
Popular Tags