KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jmx > kstat > kstatMgr


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/enterprise/jmx/kstat/kstatMgr.java,v 1.3 2005/12/25 03:46:10 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:46:10 $
28  */

29 package com.sun.enterprise.jmx.kstat;
30
31 import javax.management.*;
32
33 import java.lang.Runtime JavaDoc;
34 import java.lang.Process JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.BufferedInputStream JavaDoc;
37 import java.io.InputStreamReader JavaDoc;
38
39 import com.sun.cli.util.LineReaderImpl;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Arrays JavaDoc;
42 import java.util.ListIterator JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.HashSet JavaDoc;
46 import java.util.Set JavaDoc;
47
48 import com.sun.cli.util.stringifier.ArrayStringifier;
49
50 final class kstatModule
51 {
52     final String JavaDoc mName;
53     final HashMap JavaDoc mStats;
54     
55         public
56     kstatModule( String JavaDoc name )
57     {
58         mName = name;
59         mStats = new HashMap JavaDoc();
60     }
61     
62         public void
63     add( kstat stat )
64     {
65         mStats.put( stat.getName(), stat );
66     }
67     
68         public kstat
69     getkstat( String JavaDoc name )
70     {
71         return( (kstat)mStats.get( name ) );
72     }
73     
74         public String JavaDoc
75     getName()
76     {
77         return( mName );
78     }
79     
80         public Set JavaDoc
81     getNames()
82     {
83         return( mStats.keySet() );
84     }
85 }
86
87
88 class kstatCache implements kstatRepository
89 {
90     long mRefreshMillis;
91     long mLastRefreshMillis;
92     
93     final HashMap JavaDoc mkstats;
94     final HashMap JavaDoc mModules;
95     
96         private static void
97     dm( Object JavaDoc o )
98     {
99         System.out.println( o.toString() );
100     }
101
102         public
103     kstatCache()
104     {
105         mRefreshMillis = 30 * 1000;
106         mLastRefreshMillis = 0;
107         mkstats = new HashMap JavaDoc();
108         mModules = new HashMap JavaDoc();
109     }
110     
111         public void
112     clear()
113     {
114         mkstats.clear();
115         mModules.clear();
116     }
117     
118         private kstat
119     parsekstatAttr( final String JavaDoc line )
120     {
121         // parse first line for module name and instance number
122
String JavaDoc [] tok = line.split( ":" );
123         
124         // kstat -p spits out malformed lines occassionally, so check for the correct
125
// number of tokens
126
kstat stat = null;
127         if ( tok.length == 4 )
128         {
129             //dm( ArrayStringifier.stringify( tok, "," ) );
130
final String JavaDoc moduleName = tok[ 0 ];
131             final int instanceNumber = Integer.parseInt( tok[ 1 ] );
132             final String JavaDoc name = tok[ 2 ];
133             final String JavaDoc attrAll = tok[ 3 ];
134             
135             tok = attrAll.split( "[ \t]+" );
136             
137             if ( tok.length == 2 )
138             {
139                 final String JavaDoc attrName = tok[ 0 ].trim();
140                 final String JavaDoc attrValue = tok[ 1 ].trim();
141                 
142                 final kstat.kstatAttribute attr = new kstat.kstatAttribute( attrName, attrValue );
143                 
144                 final String JavaDoc scopedName =
145                                     kstat.getScopedName( moduleName, instanceNumber, name );
146                 stat = (kstat)mkstats.get( scopedName );
147                 if ( stat == null )
148                 {
149                     stat = new kstat( moduleName, instanceNumber, name );
150                 }
151                 stat.addAttribute( attr );
152             }
153         }
154         
155         return( stat );
156     }
157     
158         private void
159     add( final kstat stat )
160     {
161         mkstats.put( stat.getScopedName( ), stat );
162         
163         final String JavaDoc moduleName = stat.getModuleName();
164         
165         kstatModule module = (kstatModule)mModules.get( moduleName );
166         if ( module == null )
167         {
168             module = new kstatModule( moduleName );
169             mModules.put( moduleName, module );
170         }
171         
172         module.add( stat );
173     }
174
175         private void
176     processResults( String JavaDoc [] lines )
177         throws java.io.IOException JavaDoc
178     {
179         final ListIterator JavaDoc iter = Arrays.asList( lines ).listIterator( );
180         
181         while( iter.hasNext() )
182         {
183             final String JavaDoc line = ((String JavaDoc)iter.next()).trim();
184             
185             final kstat stat = parsekstatAttr( line );
186             if ( stat != null )
187             {
188                 add( stat );
189             }
190         }
191     }
192
193         private Process JavaDoc
194     invoke_kstat( String JavaDoc args )
195         throws java.io.IOException JavaDoc
196     {
197         final String JavaDoc execString = "kstat -p " + ((args == null) ? "" : args);
198         
199         dm( "invoking kstat as: " + execString );
200         // invoke kstat with -p option, which is machine parseable output
201
return( Runtime.getRuntime().exec( execString ) );
202     }
203     
204
205     
206         private String JavaDoc []
207     readResults( final InputStream JavaDoc resultsStream )
208         throws java.io.IOException JavaDoc
209     {
210         final InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc( resultsStream );
211         
212         final StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
213         int count;
214         final char [] cbuf = new char [ 256 * 1024 ];
215         while ( (count = reader.read( cbuf, 0, cbuf.length)) >= 0 )
216         {
217             sbuf.append( cbuf, 0, count );
218         }
219         
220         return( sbuf.toString().split( "\n" ) );
221     }
222     
223         public void
224     refresh( String JavaDoc scopedName )
225         throws java.io.IOException JavaDoc
226     {
227         if ( scopedName == null || scopedName.equals( "" ) )
228         {
229             clear();
230         }
231
232         final Process JavaDoc proc = invoke_kstat( scopedName );
233         
234         final InputStream JavaDoc resultsStream = proc.getInputStream();
235         
236         final String JavaDoc [] outputLines = readResults( resultsStream );
237         
238         processResults( outputLines );
239         
240         try
241         {
242             proc.waitFor();
243         }
244         catch( InterruptedException JavaDoc e )
245         {
246             System.err.println( "Interrupted: " + e.toString() );
247         }
248         
249         mLastRefreshMillis = System.currentTimeMillis();
250     }
251     
252         public void
253     refresh()
254         throws java.io.IOException JavaDoc
255     {
256         refresh( "" );
257     }
258     
259         void
260     maybeRefresh()
261     {
262         if ( ( System.currentTimeMillis() - mLastRefreshMillis ) > mRefreshMillis )
263         {
264             try
265             {
266                 refresh();
267             }
268             catch( java.io.IOException JavaDoc e )
269             {
270                 System.err.println("couldn't refresh kstat" );
271             }
272         }
273     }
274
275         public void
276     setRefreshMillis( long millis)
277     {
278         mRefreshMillis = millis;
279         maybeRefresh();
280     }
281     
282         public Set JavaDoc
283     getModuleNames()
284     {
285         maybeRefresh();
286         
287         return( mModules.keySet() );
288     }
289     
290         public Set JavaDoc
291     getNamesInModule( String JavaDoc moduleName )
292     {
293         maybeRefresh();
294             
295         Set JavaDoc names = null;
296         
297         final kstatModule module = (kstatModule)mModules.get( moduleName );
298         
299         if ( module != null )
300         {
301             names = module.getNames();
302         }
303         return( names );
304     }
305
306         public kstat
307     getkstat( String JavaDoc moduleName, String JavaDoc name )
308     {
309         maybeRefresh();
310         
311         final kstatModule module = (kstatModule)mModules.get( moduleName );
312         kstat stat = null;
313         if ( module != null )
314         {
315             stat = module.getkstat( name );
316         }
317         
318         return( stat );
319     }
320     
321         public String JavaDoc
322     query_kstatAttribute( String JavaDoc module, int instance, String JavaDoc name, String JavaDoc attributeName )
323     {
324         maybeRefresh();
325         return( null );
326     }
327 };
328
329
330 /*
331     Solaris specific MBean to support kstat
332  */

333 public final class kstatMgr implements kstatMgrMBean, MBeanRegistration
334 {
335     MBeanServer mServer;
336     ObjectName mMyName;
337     final kstatCache mCache;
338     
339     public final static String JavaDoc KSTAT_DOMAIN = "kstat";
340     
341         private static void
342     dm( Object JavaDoc o )
343     {
344         System.out.println( o.toString() );
345     }
346     
347         public
348     kstatMgr()
349     {
350         mCache = new kstatCache();
351         mServer = null;
352         mMyName = null;
353         // we get this in preRegister()
354
}
355     
356     
357         public ObjectName
358     preRegister(MBeanServer server, ObjectName name)
359     {
360         mServer = server;
361         mMyName = name;
362         
363         return( mMyName );
364     }
365     
366         public void
367     postRegister( Boolean JavaDoc registrationDone )
368     {
369     }
370     
371         public void
372     preDeregister()
373     {
374         // nothing to do
375
}
376         public void
377     postDeregister( )
378     {
379         // nothing to do
380
}
381     
382     
383     private static final String JavaDoc TYPE_PROPERTY = "type=kstat";
384     private static final String JavaDoc NAME_PROPERTY= "name";
385     private static final String JavaDoc MODULE_PROPERTY = "kstat-module";
386     private static final String JavaDoc INSTANCE_PROPERTY = "kstat-instance";
387     private static final String JavaDoc KSTAT_NAME_PROPERTY = "kstat-name";
388     private static final String JavaDoc CLASS_PROPERTY = "kstat-class";
389     private static final char PROPERTY_DELIM = ',';
390     private static final char PROPERTY_VALUE_DELIM = '=';
391     
392     
393         private String JavaDoc
394     mapkstatName( final String JavaDoc name)
395     {
396         String JavaDoc result = name;
397         
398         if ( name.indexOf( ',' ) >= 0 )
399         {
400             final char [] chars = name.toCharArray();
401             
402             for( int i = 0; i < chars.length; ++i )
403             {
404                 if ( chars[ i ] == ',' )
405                 {
406                     chars[ i ] = '.';
407                 }
408             }
409             
410             result = new String JavaDoc( chars );
411             
412         }
413         return( result );
414     }
415
416         private String JavaDoc
417     createObjectNameString( String JavaDoc domain, final kstat stat )
418     {
419         // kstat names can contain the "," character, which we can't use in an ObjectName
420
final String JavaDoc kstatName = mapkstatName( stat.getName() );
421         
422         final String JavaDoc nameProperty = stat.getModuleName() + "." + kstatName;
423         
424         final String JavaDoc name = domain + ":" +
425             TYPE_PROPERTY + PROPERTY_DELIM +
426             NAME_PROPERTY + PROPERTY_VALUE_DELIM + nameProperty + PROPERTY_DELIM +
427             KSTAT_NAME_PROPERTY + PROPERTY_VALUE_DELIM + kstatName + PROPERTY_DELIM +
428             MODULE_PROPERTY + PROPERTY_VALUE_DELIM + stat.getModuleName();
429             
430             /* + PROPERTY_DELIM +
431             INSTANCE_PROPERTY + PROPERTY_VALUE_DELIM + stat.getInstanceNumber() + PROPERTY_DELIM +
432             KSTAT_NAME_PROPERTY + PROPERTY_VALUE_DELIM + kstatName + PROPERTY_DELIM +
433             CLASS_PROPERTY + PROPERTY_VALUE_DELIM + stat.getkstatClass(); */

434         
435         return( name );
436     }
437     
438         private void
439     addMBeanFor_kstat( final kstat stat )
440     {
441         String JavaDoc objectNameString = createObjectNameString( KSTAT_DOMAIN, stat );
442         
443         try
444         {
445             final kstatMBean mb = new kstatMBean( this, stat );
446             
447             final ObjectName objectName = new ObjectName( objectNameString );
448         
449             unregisterMBean( objectName );
450             mServer.registerMBean( mb, objectName );
451         }
452         catch( Exception JavaDoc e )
453         {
454             System.err.println( "Can't add object named: " + objectNameString );
455             
456             System.err.println( e.getMessage() );
457         }
458     }
459
460         private void
461     addMBeansForModule( final String JavaDoc moduleName )
462     {
463         final Set JavaDoc names = mCache.getNamesInModule( moduleName );
464         final Iterator JavaDoc iter = names.iterator();
465         
466         while ( iter.hasNext() )
467         {
468             addMBeanFor_kstat( mCache.getkstat( moduleName, (String JavaDoc)iter.next() ) );
469         }
470     }
471     
472         private void
473     addMBeansForModules( final Set JavaDoc moduleNames )
474         throws Exception JavaDoc
475     {
476         final Iterator JavaDoc iter = moduleNames.iterator();
477         
478         while ( iter.hasNext() )
479         {
480             addMBeansForModule( (String JavaDoc)iter.next() );
481         }
482     }
483     
484         private void
485     unregisterMBean( ObjectName name )
486     {
487         try
488         {
489             mServer.unregisterMBean( name );
490         }
491         catch( InstanceNotFoundException e )
492         {
493             // that's fine, we wanted it unregistered anyway
494
}
495         catch( MBeanRegistrationException e )
496         {
497             // that's fine, we wanted it unregistered anyway
498
}
499     }
500     
501         private void
502     unregisterAll( Iterator JavaDoc objectNames )
503         throws MBeanRegistrationException
504     {
505         while ( objectNames.hasNext() )
506         {
507             unregisterMBean( (ObjectName)objectNames.next() );
508         }
509     }
510     
511 //---------------------------------------------------
512

513         public synchronized void
514     initkstats()
515         throws Exception JavaDoc
516     {
517         refresh();
518     }
519     
520         public synchronized void
521     clearkstats()
522     {
523         mCache.clear();
524         
525         try
526         {
527             final ObjectName pattern = new ObjectName( KSTAT_DOMAIN + ":type=kstat,*" );
528             final Set JavaDoc allkstatMBeans = mServer.queryNames( pattern, null );
529             
530             unregisterAll( allkstatMBeans.iterator() );
531         }
532         catch( MalformedObjectNameException e )
533         {
534             // a bug
535
assert( false );
536         }
537         catch( MBeanRegistrationException e )
538         {
539             // a bug
540
assert( false );
541         }
542     }
543     
544     
545         public void
546     refresh( String JavaDoc scopedName )
547         throws Exception JavaDoc
548     {
549         mCache.refresh( scopedName );
550         
551         final Set JavaDoc moduleNames = mCache.getModuleNames();
552         addMBeansForModules( moduleNames );
553     }
554     
555         public void
556     refresh( )
557         throws Exception JavaDoc
558     {
559         clearkstats();
560         
561         refresh( null );
562     }
563 };
564
565
566
Popular Tags