KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > MBeanServerConnection_Pattern


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 package com.sun.appserv.management.util.jmx;
24
25 import javax.management.*;
26 import java.io.IOException JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import com.sun.appserv.management.util.misc.GSetUtil;
30
31
32 /**
33     This class allows use of MBeanServerConnection methods with ObjectName patterns
34     that resolve to a single MBean. This is useful to avoid hard-coupling to specific
35     ObjectNames; instead an ObjectName pattern may be used which resolves to a
36     single MBean.
37     
38     For example, if you know the 'name' property is unique (at least for your MBean),
39     you could use the ObjectName "*:name=myname,*" instead of a possibly much longer
40     and complicated name (which potentially could change each time the MBean is registered).
41  */

42 public class MBeanServerConnection_Pattern extends MBeanServerConnection_Hook
43 {
44     final MBeanServerConnection_Hook.Hook mHook;
45     
46         public
47     MBeanServerConnection_Pattern( MBeanServerConnection impl )
48     {
49         super( impl );
50         
51         mHook = new NameResolverHook();
52     }
53     
54         Hook
55     getHook()
56     {
57         return( mHook );
58     }
59     
60     /**
61         Hook which resolves a name pattern to a single name
62     */

63     class NameResolverHook extends MBeanServerConnection_Hook.HookImpl
64     {
65             public
66         NameResolverHook( )
67         {
68         }
69         
70             public ObjectName
71         nameHook( long id, ObjectName name )
72             throws java.io.IOException JavaDoc
73         {
74             try
75             {
76                 ObjectName newName = resolve( name );
77                 
78                 return( newName );
79             }
80             catch( InstanceNotFoundException e )
81             {
82                 // we can't let InstanceNotFoundException propogate because
83
// the MBeanServerConnection interface does not declare it as a legal
84
// exception for some of the calls the nameHook() is called in.
85
throw new IllegalArgumentException JavaDoc();
86             }
87         }
88     }
89
90     
91         public ObjectName
92     resolve( ObjectName input )
93         throws java.io.IOException JavaDoc, InstanceNotFoundException
94     {
95         ObjectName resolvedName = input;
96         
97         if ( input.isPattern() )
98         {
99             final Set JavaDoc<ObjectName> resolvedNames = JMXUtil.queryNames( getConn(), input, null );
100             final int numNames = resolvedNames.size();
101             
102             if ( numNames == 1 )
103             {
104                 resolvedName = GSetUtil.getSingleton( resolvedNames );
105             }
106             else if ( numNames > 1 )
107             {
108                 throw new InstanceNotFoundException( input.toString() );
109             }
110             else
111             {
112                 throw new InstanceNotFoundException( input.toString() );
113             }
114         }
115         return( resolvedName );
116     }
117     
118
119
120 };
121
122
Popular Tags