KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > DottedNameGetSetMBeanImpl


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/mbeans/DottedNameGetSetMBeanImpl.java,v 1.3 2005/12/25 03:42:19 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:42:19 $
28  */

29  
30
31 package com.sun.enterprise.admin.mbeans;
32
33 import com.sun.enterprise.admin.dottedname.*;
34
35 import javax.management.*;
36 import java.lang.reflect.Array JavaDoc;
37
38
39 /*
40     MBean which supports get/set CLI commands using dotted names
41  */

42 public class DottedNameGetSetMBeanImpl
43     extends StandardMBean implements DottedNameGetSetMBean
44 {
45     final DottedNameGetSetForConfig mConfigImpl;
46     final DottedNameGetSetForMonitoring mMonitoringImpl;
47     final DottedNameServerInfoCache mServerInfo;
48     
49     long mTimeOfLastCall;
50     
51     /*
52         Instantiate with a reference to an MBeanServerConnection which will be used
53         as the server when searching for required objects (which is possibly different
54         than the MBeanServer in which this object will be registered).
55         
56         Due to a bug in the server startup sequence, this is the only allowed
57         constructor; avoiding the bug requires the SunoneInterceptor as 'conn'.
58         Ideally the only constructor would be one that takes no arguments, and obtains
59         its MBeanServerConnection from MBeanServerRegistration.preRegister().
60      */

61         public
62     DottedNameGetSetMBeanImpl(
63         final MBeanServerConnection conn,
64         final DottedNameRegistry registry,
65         final DottedNameRegistry monitoringRegistry )
66         throws NotCompliantMBeanException, MalformedObjectNameException
67     {
68
69
70         super( DottedNameGetSet.class );
71     
72         mServerInfo = new DottedNameServerInfoCache( createServerInfo( conn ) );
73         
74         mConfigImpl = new DottedNameGetSetForConfig( conn, registry, mServerInfo );
75         mMonitoringImpl = new DottedNameGetSetForMonitoring( conn, monitoringRegistry, mServerInfo );
76     }
77     
78     // clear the DottedNameFactory every 10 minutes
79
static private final long CLEAR_CACHE_INTERVAL_MILLIS = 10 * 60 * 1000;
80     
81         protected void
82     pre()
83     {
84         mServerInfo.refresh();
85         
86         final long now = System.currentTimeMillis();
87         if ( (now - mTimeOfLastCall) > CLEAR_CACHE_INTERVAL_MILLIS )
88         {
89             DottedNameFactory.getInstance().clear();
90         }
91         
92         mTimeOfLastCall = now;
93     }
94     
95     // this method exists so unit test code can override it
96
protected DottedNameServerInfo
97     createServerInfo( final MBeanServerConnection conn )
98     {
99         return( new DottedNameServerInfoImpl( conn ) );
100     }
101     
102         protected boolean
103     checkGetResults( final Object JavaDoc[] results )
104     {
105         boolean good = true;
106         
107         for( int i = 0; i < results.length; ++i )
108         {
109             final Object JavaDoc o = results[ i ];
110             
111             if ( ! (o instanceof Attribute ||
112                     o instanceof Attribute[] ||
113                     o instanceof Exception JavaDoc ) )
114             {
115                 //System.out.println( "checkGetResults: object has class: " + o.getClass().getName() );
116
good = false;
117                 break;
118             }
119         }
120         return( good );
121     }
122     
123         protected boolean
124     checkSetResults( final Object JavaDoc[] results )
125     {
126         boolean good = true;
127         
128         for( int i = 0; i < results.length; ++i )
129         {
130             final Object JavaDoc o = results[ i ];
131             
132             if ( ! (o instanceof Attribute ||
133                     o instanceof Exception JavaDoc ) )
134             {
135                 good = false;
136                 break;
137             }
138         }
139         return( good );
140     }
141     
142     /*
143         Return true if every element of the array has *exactly* the same class.
144      */

145         private boolean
146     hasIdenticalElementClasses( final Object JavaDoc [] a )
147     {
148         boolean isUniform = true;
149         
150         if ( a.length > 0 )
151         {
152             final Class JavaDoc matchType = a[ 0 ].getClass();
153             
154             for( int i = 1; i < a.length; ++i )
155             {
156                 if ( a[ i ].getClass() != matchType )
157                 {
158                     isUniform = false;
159                     break;
160                 }
161             }
162         }
163         
164         return( isUniform );
165     }
166     
167     /*
168         If every element of the array has the same class, return a new array whose type specifies that
169         class, otherwise return the original array.
170      */

171     protected Object JavaDoc []
172     convertArrayType( final Object JavaDoc [] input )
173     {
174         Object JavaDoc [] result = input;
175         
176         if ( hasIdenticalElementClasses( input ) && input.length != 0 )
177         {
178             result = (Object JavaDoc [])Array.newInstance( input[ 0 ].getClass(), input.length );
179             
180             for( int i = 0; i < input.length; ++i )
181             {
182                 result[ i ] = input[ i ];
183             }
184         }
185         
186         return( result );
187     }
188     
189         protected Object JavaDoc []
190     dottedNameAnyGet( DottedNameGetSetMBeanBase impl, final String JavaDoc [] names )
191     {
192         pre();
193         
194         final Object JavaDoc [] results = impl.dottedNameGet( names );
195         
196         assert( checkGetResults( results ) );
197         
198         assert( results.length == names.length );
199         return( convertArrayType( results ) );
200     }
201     
202         protected Object JavaDoc
203     dottedNameAnyGet( DottedNameGetSetMBeanBase impl, final String JavaDoc name )
204     {
205         final Object JavaDoc [] results = dottedNameAnyGet( impl, new String JavaDoc [] { name } );
206         
207         return( results[ 0 ] );
208     }
209     
210         public Object JavaDoc []
211     dottedNameGet( final String JavaDoc [] names )
212     {
213         return( dottedNameAnyGet( mConfigImpl, names ) );
214     }
215     
216         public Object JavaDoc
217     dottedNameGet( final String JavaDoc name )
218     {
219         final Object JavaDoc result = dottedNameAnyGet( mConfigImpl, name );
220         return( result );
221     }
222     
223         public Object JavaDoc []
224     dottedNameSet( final String JavaDoc [] nameValuePairs )
225     {
226         pre();
227         
228         Object JavaDoc [] results = mConfigImpl.dottedNameSet( nameValuePairs );
229         
230         assert( checkSetResults( results ) );
231         assert( results.length == nameValuePairs.length );
232         
233         return( convertArrayType( results ) );
234     }
235     
236         public Object JavaDoc
237     dottedNameSet( final String JavaDoc nameValuePair )
238     {
239         final Object JavaDoc [] results = dottedNameSet( new String JavaDoc [] { nameValuePair } );
240         
241         return( results[ 0 ] );
242     }
243     
244         public String JavaDoc []
245     dottedNameList( final String JavaDoc [] namePrefixes )
246     {
247         pre();
248         
249         return( mConfigImpl.dottedNameList( namePrefixes ) );
250     }
251     
252     //------------------------- monitoring --------------------------
253

254         public Object JavaDoc []
255     dottedNameMonitoringGet( final String JavaDoc [] names )
256     {
257         return( dottedNameAnyGet( mMonitoringImpl, names ) );
258     }
259     
260         public Object JavaDoc
261     dottedNameMonitoringGet( final String JavaDoc name )
262     {
263         return( dottedNameAnyGet( mMonitoringImpl, name ) );
264     }
265
266         public String JavaDoc []
267     dottedNameMonitoringList( final String JavaDoc [] namePrefixes )
268     {
269         pre();
270         
271         return( mMonitoringImpl.dottedNameList( namePrefixes ) );
272     }
273 }
274
275
276 /*
277     Implementing subclass for monitoring dotted names
278  */

279 final class DottedNameGetSetForMonitoring extends DottedNameGetSetMBeanBase
280 {
281     final DottedNameResolver mMonitoringResolver;
282     
283         public
284     DottedNameGetSetForMonitoring(
285         final MBeanServerConnection conn,
286         final DottedNameRegistry registry,
287         final DottedNameServerInfo serverInfo )
288         throws MalformedObjectNameException
289     {
290         super( conn, registry, serverInfo );
291         
292         // DottedNameResolver for monitoring dotted names does NOT need to account for aliases
293
mMonitoringResolver = new DottedNameResolverFromRegistry( registry );
294     }
295     
296     
297         DottedNameResolver
298     getResolver( )
299     {
300         return( mMonitoringResolver );
301     }
302     
303     
304         DottedNameQuery
305     createQuery( )
306     {
307         // no aliasing is done; the registry implements query directly
308
return( mRegistry );
309     }
310 }
311
Popular Tags