KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > util > 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  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/util/MBeanServerConnection_Pattern.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 /*
38     This class allows use of MBeanServerConnection methods with ObjectName patterns
39     that resolve to a single MBean. This is useful to avoid hard-coupling to specific
40     ObjectNames; instead an ObjectName pattern may be used which resolves to a
41     single MBean.
42     
43     For example, if you know the 'name' property is unique (at least for your MBean),
44     you could use the ObjectName "*:name=myname,*" instead of a possibly much longer
45     and complicated name (which potentially could change each time the MBean is registered).
46  */

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

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