KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > util > MBeanServerConnection_Hook


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/util/MBeanServerConnection_Hook.java,v 1.3 2005/12/25 03:45:55 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:55 $
28  */

29 package com.sun.cli.jmx.util;
30
31 import javax.management.*;
32 import java.io.IOException JavaDoc;
33 import java.util.Set JavaDoc;
34
35
36 /*
37     This class wraps an MBeanServerConnection and provides hooks on each method call
38     via its Hook interface.
39     
40     Typical used would be for a subclass to
41  */

42 public class MBeanServerConnection_Hook implements MBeanServerConnection
43 {
44     private final MBeanServerConnection mConn;
45     
46     /*
47         Prior to a method being called, preHook() is called; the 2 variants
48         correspond to either no arguments or 1 or more arguments.
49         
50         After a method is called, postHook() is called; the 3 variants
51         correspond to no-arguments-no-return-value, arguments-no-return-value, and
52         arguments-and-return-value methods.
53         
54         The callNameHook() is supplied to see just the ObjectName being used for
55         methods that accept a fully-qualifed ObjectName (but not methods that take
56         a pattern). The callNameHook() may return a different ObjectName which will
57         be used for the invocation.
58         
59         preHook() must return a unique call ID, which will be passed to nameHook() and
60         postHook(). The id identifies the particular calls (useful in a threaded
61         environment).
62      */

63     public interface Hook
64     {
65         long preHook( String JavaDoc methodName );
66         long preHook( String JavaDoc nmethodName, Object JavaDoc [] args );
67         void postHook( long id, String JavaDoc methodName );
68         void postHook( long id, String JavaDoc methodName, Object JavaDoc [] args );
69         void postHook( long id, String JavaDoc methodName, Object JavaDoc [] args, Object JavaDoc result );
70         ObjectName nameHook( long id, ObjectName methodName ) throws IOException JavaDoc;
71     }
72     
73     static public class HookImpl
74         implements Hook
75     {
76         public static final HookImpl HOOK = new HookImpl();
77         
78         long mID;
79             public
80         HookImpl()
81         {
82             mID = 0;
83         }
84         
85             synchronized long
86         getNewID()
87         {
88             return( mID++ );
89         }
90
91         public long preHook( String JavaDoc methodName ) { return( getNewID() ); }
92         public long preHook( String JavaDoc methodName, Object JavaDoc [] args ) { return( getNewID() ); }
93         public void postHook( long id, String JavaDoc methodName ) {}
94         public void postHook( long id, String JavaDoc methodName, Object JavaDoc [] args ) {}
95         public void postHook( long id, String JavaDoc methodName, Object JavaDoc [] args, Object JavaDoc result ) {}
96         public ObjectName nameHook( long id, ObjectName methodName ) throws IOException JavaDoc { return( methodName ); }
97     }
98     
99         Hook
100     getHook()
101     {
102         return( HookImpl.HOOK );
103     }
104
105         public
106     MBeanServerConnection_Hook(
107         MBeanServerConnection conn )
108     {
109         mConn = conn;
110         
111         assert( getConn() != null );
112         
113     }
114     
115         MBeanServerConnection
116     getConn()
117     {
118         return( mConn );
119     }
120     
121         long
122     callPreHook( String JavaDoc name, Object JavaDoc [] args )
123     {
124         return( getHook().preHook( name, args ) );
125     }
126     
127         long
128     callPreHook( String JavaDoc name )
129     {
130         return( getHook().preHook( name, null ) );
131     }
132     
133         void
134     callPostHook( long id, String JavaDoc name, Object JavaDoc [] args, Object JavaDoc result )
135     {
136         getHook().postHook( id, name, args, result );
137     }
138     
139         void
140     callPostHook( long id, String JavaDoc name, Object JavaDoc [] args )
141     {
142         getHook().postHook( id, name, args );
143     }
144     
145         void
146     callPostHook( long id, String JavaDoc name )
147     {
148         getHook().postHook( id, name );
149     }
150     
151         ObjectName
152     callNameHook( long id, ObjectName objectName )
153         throws IOException JavaDoc
154     {
155         return( getHook().nameHook( id, objectName ) );
156     }
157     
158     public ObjectInstance createMBean(String JavaDoc className, ObjectName name)
159         throws ReflectionException, InstanceAlreadyExistsException,
160            MBeanRegistrationException, MBeanException,
161            NotCompliantMBeanException, IOException JavaDoc
162     {
163         final Object JavaDoc [] args = new Object JavaDoc [] { className, name };
164         
165         final long id = callPreHook( "createMBean", args );
166         
167         final ObjectInstance result = getConn().createMBean( className, name );
168         
169         callPostHook( id, "createMBean", args, result );
170         
171         return( result );
172     }
173
174     public ObjectInstance createMBean(String JavaDoc className, ObjectName name, ObjectName loaderName)
175         throws ReflectionException, InstanceAlreadyExistsException,
176            MBeanRegistrationException, MBeanException,
177            NotCompliantMBeanException, InstanceNotFoundException,
178            IOException JavaDoc
179     {
180         final Object JavaDoc [] args = new Object JavaDoc [] { className, name, loaderName };
181         final long id = callPreHook( "createMBean", args );
182         
183         final ObjectInstance result = getConn().createMBean( className, name, loaderName );
184         
185         callPostHook( id, "createMBean", args, result );
186         
187         return( result );
188     }
189
190
191
192     public ObjectInstance createMBean(String JavaDoc className, ObjectName name,
193                       Object JavaDoc params[], String JavaDoc signature[])
194         throws ReflectionException, InstanceAlreadyExistsException,
195                MBeanRegistrationException, MBeanException,
196                NotCompliantMBeanException, IOException JavaDoc
197     {
198         final Object JavaDoc [] args = new Object JavaDoc [] { className, name, params, signature };
199         final long id = callPreHook( "createMBean", args );
200         
201         final ObjectInstance result = getConn().createMBean( className, name, params, signature );
202         
203         callPostHook( id, "createMBean", args, result );
204         
205         return( result );
206     }
207
208
209     public ObjectInstance createMBean(String JavaDoc className, ObjectName name,
210                       ObjectName loaderName, Object JavaDoc params[],
211                       String JavaDoc signature[])
212         throws ReflectionException, InstanceAlreadyExistsException,
213                MBeanRegistrationException, MBeanException,
214                NotCompliantMBeanException, InstanceNotFoundException,
215                IOException JavaDoc
216     {
217         final Object JavaDoc [] args = new Object JavaDoc [] { className, name, loaderName, params, signature };
218         final long id = callPreHook( "createMBean", args );
219         
220         final ObjectInstance result =
221             getConn().createMBean( className, name, loaderName, params, signature);
222         
223         callPostHook( id, "createMBean", args, result );
224         
225         return( result );
226     }
227
228
229     public void unregisterMBean(ObjectName name)
230         throws InstanceNotFoundException, MBeanRegistrationException,
231                IOException JavaDoc
232     {
233         final Object JavaDoc [] args = new Object JavaDoc [] { name };
234         final long id = callPreHook( "unregisterMBean", args );
235         
236         getConn().unregisterMBean( callNameHook( id, name ) );
237         
238         callPostHook( id, "unregisterMBean", args );
239     }
240
241
242     public ObjectInstance getObjectInstance(ObjectName name)
243         throws InstanceNotFoundException, IOException JavaDoc
244     {
245         final Object JavaDoc [] args = new Object JavaDoc [] { name };
246         final long id = callPreHook( "getObjectInstance", args );
247         
248         final ObjectInstance result = getConn().getObjectInstance( callNameHook( id, name ) );
249         
250         callPostHook( id, "getObjectInstance", args, result );
251         
252         return( result );
253     }
254
255
256     public Set JavaDoc queryMBeans(ObjectName name, QueryExp query)
257         throws IOException JavaDoc
258     {
259         final Object JavaDoc [] args = new Object JavaDoc [] { name, query };
260         final long id = callPreHook( "queryMBeans", args );
261         
262         final Set JavaDoc result = getConn().queryMBeans( name, query );
263         
264         callPostHook( id, "queryMBeans", args, result );
265         
266         return( result );
267     }
268
269
270     public Set JavaDoc queryNames(ObjectName name, QueryExp query)
271         throws IOException JavaDoc
272     {
273         final Object JavaDoc [] args = new Object JavaDoc [] { name, query };
274         final long id = callPreHook( "queryNames", args );
275         
276         final Set JavaDoc result = getConn().queryMBeans( name, query );
277         
278         callPostHook( id, "queryNames", args, result );
279         
280         return( result );
281     }
282
283
284
285
286     public boolean isRegistered(ObjectName name)
287         throws IOException JavaDoc
288     {
289         final Object JavaDoc [] args = new Object JavaDoc [] { name };
290         final long id = callPreHook( "isRegistered", args );
291         
292         boolean registered = false;
293         
294         registered = getConn().isRegistered( callNameHook( id, name ) );
295         
296         callPostHook( id, "isRegistered", args, registered ? Boolean.TRUE : Boolean.FALSE );
297         
298         return( registered );
299     }
300
301
302
303     public Integer JavaDoc getMBeanCount()
304         throws IOException JavaDoc
305     {
306         final long id = callPreHook( "getMBeanCount", null );
307         
308         final Integer JavaDoc result = getConn().getMBeanCount( );
309         
310         callPostHook( id, "getMBeanCount", null, result );
311         
312         return( result );
313     }
314
315
316     public Object JavaDoc getAttribute(ObjectName name, String JavaDoc attribute)
317         throws MBeanException, AttributeNotFoundException,
318                InstanceNotFoundException, ReflectionException,
319                IOException JavaDoc
320     {
321         final Object JavaDoc [] args = new Object JavaDoc [] { name, attribute };
322         final long id = callPreHook( "getAttribute", args );
323         
324         final Object JavaDoc result = getConn().getAttribute( callNameHook( id, name ), attribute );
325         
326         callPostHook( id, "getAttribute", args, result );
327         
328         return( result );
329     }
330
331
332
333     public AttributeList getAttributes(ObjectName name, String JavaDoc[] attributes)
334         throws InstanceNotFoundException, ReflectionException,
335            IOException JavaDoc
336     {
337         final Object JavaDoc [] args = new Object JavaDoc [] { name, attributes };
338         final long id = callPreHook( "getAttributes", args );
339         
340         final AttributeList result = getConn().getAttributes( callNameHook( id, name ), attributes );
341         
342         callPostHook( id, "getAttributes", args, result );
343         
344         return( result );
345     }
346
347
348     public void setAttribute(ObjectName name, Attribute attribute)
349         throws InstanceNotFoundException, AttributeNotFoundException,
350            InvalidAttributeValueException, MBeanException,
351            ReflectionException, IOException JavaDoc
352     {
353         final Object JavaDoc [] args = new Object JavaDoc [] { name, attribute };
354         final long id = callPreHook( "setAttribute", args );
355         
356         getConn().setAttribute( callNameHook( id, name ), attribute );
357         
358         callPostHook( id, "setAttribute", args );
359     }
360
361
362
363
364     public AttributeList setAttributes(ObjectName name, AttributeList attributes)
365         throws InstanceNotFoundException, ReflectionException, IOException JavaDoc
366     {
367         final Object JavaDoc [] args = new Object JavaDoc [] { name, attributes };
368         final long id = callPreHook( "setAttributes", args );
369         
370         final AttributeList result = getConn().setAttributes( callNameHook( id, name ), attributes );
371         
372         callPostHook( id, "setAttributes", args );
373         
374         return( result );
375     }
376
377
378     public Object JavaDoc invoke(ObjectName name, String JavaDoc operationName,
379              Object JavaDoc params[], String JavaDoc signature[])
380         throws InstanceNotFoundException, MBeanException,
381            ReflectionException, IOException JavaDoc
382     {
383         final Object JavaDoc [] args = new Object JavaDoc [] { name, operationName, params, signature };
384         final long id = callPreHook( "invoke", args );
385         
386         final Object JavaDoc result = getConn().invoke( callNameHook( id, name ), operationName, params, signature);
387     
388         callPostHook( id, "invoke", args );
389         return( result );
390     }
391
392  
393
394   
395     public String JavaDoc getDefaultDomain()
396         throws IOException JavaDoc
397     {
398         final long id = callPreHook( "getDefaultDomain" );
399         
400         final String JavaDoc result = getConn().getDefaultDomain();
401             
402         callPostHook( id, "getDefaultDomain" );
403         
404         return( result );
405     }
406
407
408     public String JavaDoc[] getDomains()
409         throws IOException JavaDoc
410     {
411         final long id = callPreHook( "getDomains" );
412         
413         final String JavaDoc [] result = getConn().getDomains( );
414             
415         callPostHook( id, "getDefaultDomain", result );
416         
417         return( result );
418     }
419
420
421     public void addNotificationListener(ObjectName name,
422                     NotificationListener listener,
423                     NotificationFilter filter,
424                     Object JavaDoc handback)
425         throws InstanceNotFoundException, IOException JavaDoc
426     {
427         final Object JavaDoc [] args = new Object JavaDoc [] { name, listener, filter, handback };
428         final long id = callPreHook( "addNotificationListener", args );
429         
430         getConn().addNotificationListener( callNameHook( id, name ), listener, filter, handback );
431         
432         callPostHook( id, "addNotificationListener", args );
433     }
434
435
436
437     public void addNotificationListener(ObjectName name,
438                     ObjectName listener,
439                     NotificationFilter filter,
440                     Object JavaDoc handback)
441         throws InstanceNotFoundException, IOException JavaDoc
442     {
443         final Object JavaDoc [] args = new Object JavaDoc [] { name, listener, filter, handback };
444         final long id = callPreHook( "addNotificationListener", args );
445         
446         getConn().addNotificationListener( callNameHook( id, name ), listener, filter, handback );
447         
448         callPostHook( id, "addNotificationListener", args );
449     }
450
451
452
453     public void removeNotificationListener(ObjectName name,
454                        ObjectName listener)
455     throws InstanceNotFoundException, ListenerNotFoundException,
456            IOException JavaDoc
457     {
458         final Object JavaDoc [] args = new Object JavaDoc [] { listener };
459         final long id = callPreHook( "removeNotificationListener", args );
460         
461         getConn().removeNotificationListener( callNameHook( id, name ), listener );
462         
463         callPostHook( id, "removeNotificationListener", args );
464     }
465
466
467     public void removeNotificationListener(ObjectName name,
468                        ObjectName listener,
469                        NotificationFilter filter,
470                        Object JavaDoc handback)
471         throws InstanceNotFoundException, ListenerNotFoundException,
472            IOException JavaDoc
473     {
474         final Object JavaDoc [] args = new Object JavaDoc [] { name, listener, filter, handback };
475         final long id = callPreHook( "removeNotificationListener", args );
476         
477         getConn().removeNotificationListener( callNameHook( id, name ), listener, filter, handback );
478         
479         callPostHook( id, "removeNotificationListener", args );
480     }
481
482
483
484     public void removeNotificationListener(ObjectName name,
485                        NotificationListener listener)
486         throws InstanceNotFoundException, ListenerNotFoundException,
487            IOException JavaDoc
488     {
489         final Object JavaDoc [] args = new Object JavaDoc [] { name, listener };
490         final long id = callPreHook( "removeNotificationListener", args );
491         
492         getConn().removeNotificationListener( callNameHook( id, name ), listener );
493         
494         callPostHook( id, "removeNotificationListener", args );
495     }
496
497
498     public void removeNotificationListener(ObjectName name,
499                        NotificationListener listener,
500                        NotificationFilter filter,
501                        Object JavaDoc handback)
502         throws InstanceNotFoundException, ListenerNotFoundException,
503            IOException JavaDoc
504     {
505         final Object JavaDoc [] args = new Object JavaDoc [] { name, listener, filter, handback };
506         final long id = callPreHook( "removeNotificationListener", args );
507         
508         getConn().removeNotificationListener( callNameHook( id, name ), listener, filter, handback );
509         
510         callPostHook( id, "removeNotificationListener", args );
511     }
512
513
514     public MBeanInfo getMBeanInfo(ObjectName name)
515         throws InstanceNotFoundException, IntrospectionException,
516                ReflectionException, IOException JavaDoc
517     {
518         final Object JavaDoc [] args = new Object JavaDoc [] { name };
519         final long id = callPreHook( "getMBeanInfo", args );
520         
521         final MBeanInfo result = getConn().getMBeanInfo( callNameHook( id, name ) );
522         
523         callPostHook( id, "getMBeanInfo", args );
524         
525         return( result );
526     }
527
528
529  
530     public boolean isInstanceOf(ObjectName name, String JavaDoc className)
531         throws InstanceNotFoundException, IOException JavaDoc
532     {
533         final Object JavaDoc [] args = new Object JavaDoc [] { name, className };
534         final long id = callPreHook( "isInstanceOf", args );
535         
536         final boolean isInstance = getConn().isInstanceOf( callNameHook( id, name ), className );
537         
538         callPostHook( id, "isInstanceOf", args, isInstance ? Boolean.TRUE : Boolean.FALSE );
539         
540         return( isInstance );
541     }
542
543 };
544
545
Popular Tags