KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > dottedname > DottedNameServerInfoImpl


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/mbeans/src/java/com/sun/enterprise/admin/dottedname/DottedNameServerInfoImpl.java,v 1.3 2005/12/25 03:42:05 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:42:05 $
28  */

29
30
31 package com.sun.enterprise.admin.dottedname;
32
33 import java.util.HashSet JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Collections JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.AttributeNotFoundException JavaDoc;
40 import javax.management.Attribute JavaDoc;
41 import javax.management.MBeanServerConnection JavaDoc;
42 import javax.management.ReflectionException JavaDoc;
43 import javax.management.InstanceNotFoundException JavaDoc;
44 import javax.management.MBeanException JavaDoc;
45
46 import com.sun.enterprise.admin.util.ArrayConversion;
47
48 import com.sun.enterprise.admin.common.ObjectNames;
49
50
51 import javax.management.MBeanServerInvocationHandler JavaDoc;
52
53
54 /*
55     This is the 'glue' that knows how to get server names, and how to get
56     config names from server names, etc. It isolates the DottedNameResolverForAliases
57     code from direct knowledge of the server structure.
58  */

59 public class DottedNameServerInfoImpl implements DottedNameServerInfo
60 {
61     final MBeanServerConnection JavaDoc mConn;
62     
63     
64         public
65     DottedNameServerInfoImpl( MBeanServerConnection JavaDoc conn )
66     {
67         mConn = conn;
68     }
69     
70         ObjectName JavaDoc
71     getControllerObjectName()
72     {
73         return( ObjectNames.getControllerObjectName() );
74     }
75     
76         ObjectName JavaDoc
77     getConfigsObjectName()
78         throws MalformedObjectNameException JavaDoc
79     {
80         return( new ObjectName JavaDoc( "com.sun.appserv:type=configs,category=config" ) );
81     }
82     
83         ObjectName JavaDoc
84     getServerObjectName( final String JavaDoc serverName )
85     {
86         return( ObjectNames.getServerObjectName( serverName ) );
87     }
88     
89     
90         Set JavaDoc
91     _getConfigNames()
92         throws ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc, java.io.IOException JavaDoc,
93         MalformedObjectNameException JavaDoc, AttributeNotFoundException JavaDoc
94     {
95         // we can't use a proxy; it won't work when the method name starts with "get", apparently
96
// thinking it's an Attribute
97
final ObjectName JavaDoc [] configObjectNames =
98             (ObjectName JavaDoc [])mConn.invoke( getConfigsObjectName(), "getConfig", null, null );
99         
100         final HashSet JavaDoc configNames = new HashSet JavaDoc();
101         for( int i = 0; i < configObjectNames.length; ++i )
102         {
103             final String JavaDoc name = (String JavaDoc)mConn.getAttribute( configObjectNames[ i ], "name" );
104             
105             configNames.add( name );
106         }
107         
108         return( configNames );
109     }
110     
111         public Set JavaDoc
112     getConfigNames()
113         throws DottedNameServerInfo.UnavailableException
114     {
115         Set JavaDoc namesSet = null;
116         
117         try
118         {
119             namesSet = _getConfigNames();
120         }
121         catch( Exception JavaDoc e )
122         {
123             throw new DottedNameServerInfo.UnavailableException( e );
124         }
125         
126         return( namesSet );
127     }
128     
129     // used to create a proxy to controller mbean
130
private interface MyController
131     {
132         String JavaDoc [] listServerInstances();
133     };
134     
135         protected Set JavaDoc
136     _getServerNames()
137         throws ReflectionException JavaDoc, InstanceNotFoundException JavaDoc, MBeanException JavaDoc, java.io.IOException JavaDoc
138     {
139         final MyController controller = (MyController)
140             MBeanServerInvocationHandler.newProxyInstance( mConn, getControllerObjectName(), MyController.class, false );
141         
142         final String JavaDoc [] names = controller.listServerInstances();
143         
144         return( ArrayConversion.toSet( names ) );
145     }
146     
147         public Set JavaDoc
148     getServerNames()
149         throws DottedNameServerInfo.UnavailableException
150     {
151         Set JavaDoc namesSet = null;
152         
153         try
154         {
155             namesSet = _getServerNames();
156         }
157         catch( Exception JavaDoc e )
158         {
159             throw new DottedNameServerInfo.UnavailableException( e );
160         }
161         
162         return( namesSet );
163     }
164     
165         public String JavaDoc
166     getConfigNameForServer( String JavaDoc serverName )
167         throws DottedNameServerInfo.UnavailableException
168     {
169         final ObjectName JavaDoc serverObjectName = getServerObjectName( serverName );
170         
171         if ( serverObjectName == null )
172         {
173             throw new DottedNameServerInfo.UnavailableException( serverObjectName.toString() );
174         }
175         
176         String JavaDoc configName = null;
177         try
178         {
179             configName = (String JavaDoc)mConn.getAttribute( serverObjectName, "config_ref" );
180         }
181         catch( Exception JavaDoc e )
182         {
183             throw new DottedNameServerInfo.UnavailableException( e );
184         }
185         
186         return( configName );
187     }
188     
189         public String JavaDoc []
190     getServerNamesForConfig( String JavaDoc configName )
191         throws DottedNameServerInfo.UnavailableException
192     {
193         final java.util.Iterator JavaDoc iter = getServerNames().iterator();
194         final java.util.ArrayList JavaDoc namesOut = new java.util.ArrayList JavaDoc();
195         
196         while ( iter.hasNext() )
197         {
198             final String JavaDoc serverName = (String JavaDoc)iter.next();
199             
200             if ( configName.equals( getConfigNameForServer( serverName ) ) )
201             {
202                 namesOut.add( serverName );
203             }
204         }
205         
206         final String JavaDoc [] namesOutArray = new String JavaDoc [ namesOut.size() ];
207         namesOut.toArray( namesOutArray );
208         
209         return( namesOutArray );
210     }
211 }
212
213
214
215
216
217
218
219
220
221
Popular Tags