KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > LoaderRegThread


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  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28  
29 /*
30  */

31
32 package com.sun.enterprise.management.support;
33
34 import java.util.List JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41
42 import javax.management.ObjectName JavaDoc;
43 import javax.management.MBeanRegistration JavaDoc;
44 import javax.management.NotificationListener JavaDoc;
45
46 import com.sun.appserv.management.util.misc.ExceptionUtil;
47     
48 final class LoaderRegThread extends Thread JavaDoc
49 {
50     private boolean mQuit;
51     private List JavaDoc<ObjectName JavaDoc> mRegistrationQueue;
52     private List JavaDoc<ObjectName JavaDoc> mUnregistrationQueue;
53     private final Logger JavaDoc mLogger;
54     private final LoaderRegHandler mRegHandler;
55     
56         public
57     LoaderRegThread(
58         final LoaderRegHandler regHandler,
59         final Logger JavaDoc logger )
60     {
61         mRegHandler = regHandler;
62         mLogger = logger;
63         mQuit = false;
64
65         mRegistrationQueue = Collections.synchronizedList( new ArrayList JavaDoc<ObjectName JavaDoc>() );
66         mUnregistrationQueue = Collections.synchronizedList( new ArrayList JavaDoc<ObjectName JavaDoc>() );
67     }
68     
69         private final Logger JavaDoc
70     getLogger( )
71     {
72         return( mLogger );
73     }
74     
75         private final void
76     trace( final Object JavaDoc o )
77     {
78         if ( mLogger != null )
79         {
80             mLogger.finer( o.toString() );
81         }
82     }
83
84         public void
85     quit()
86     {
87         mQuit = true;
88         synchronized( this )
89         {
90             this.notify();
91         }
92     }
93
94         public void
95     enqueue(
96         final boolean register,
97         final List JavaDoc candidates )
98     {
99         final Iterator JavaDoc iter = candidates.iterator();
100         while ( iter.hasNext() )
101         {
102             enqueue( register, (ObjectName JavaDoc)iter.next() );
103         }
104     }
105     
106         public void
107     enqueue(
108         final boolean register,
109         final ObjectName JavaDoc theObject )
110     {
111         final List JavaDoc<ObjectName JavaDoc> theQueue;
112         
113         if ( register )
114         {
115             theQueue = mRegistrationQueue;
116         }
117         else
118         {
119             theQueue = mUnregistrationQueue;
120         }
121         
122         theQueue.add( theObject );
123         
124         synchronized( this )
125         {
126             this.notify();
127         }
128     }
129
130         private ObjectName JavaDoc
131     dequeue( final List JavaDoc<ObjectName JavaDoc> theQueue )
132     {
133         ObjectName JavaDoc theObject = null;
134         
135         synchronized( theQueue )
136         {
137             if ( theQueue.size() != 0 )
138             {
139                 theObject = (ObjectName JavaDoc)theQueue.remove( 0 );
140             }
141         }
142         return( theObject );
143     }
144
145
146         public void
147     run()
148     {
149         // never wake up unless interrupted
150
final int INTERVAL = 1024 * 1024 * 1024;
151         
152         mQuit = false;
153         
154         trace( "Loader.RegThread.run(): start" );
155         while ( ! mQuit )
156         {
157             try
158             {
159                 trace( "Loader.RegThread.run(): waiting" );
160                 synchronized( this )
161                 {
162                     wait();
163                 }
164             }
165             catch( InterruptedException JavaDoc e )
166             {
167                 trace( "Loader.RegThread.run(): interrupted from wait" );
168             }
169             
170             if ( mQuit )
171             {
172                 break;
173             }
174             
175             processRegistrations();
176             processUnregistrations();
177         }
178     }
179
180
181         private void
182     mySleep( final long millis )
183     {
184         try
185         {
186             Thread.sleep( millis );
187         }
188         catch( InterruptedException JavaDoc e )
189         {
190         }
191     }
192     
193         public void
194     waitEmpty()
195     {
196         int queueSize = 0;
197         
198         while ( (queueSize = mRegistrationQueue.size() ) != 0 )
199         {
200             mySleep( 100 );
201         }
202         trace( "RegThread.waitEmpty: queue is empty" );
203     }
204     
205         public int
206     getRegistrationQueueSize()
207     {
208         return( mRegistrationQueue.size() );
209     }
210
211     /** WORKAROUND_FOR_BUG_SRIDATTA_FOUND (should not be public)*/
212         public synchronized void
213     processRegistration( final ObjectName JavaDoc objectName )
214     {
215         try
216         {
217             mRegHandler.handleMBeanRegistered( objectName );
218             getLogger().finer( "LoaderRegThread.processRegistration: processed mbean: " + objectName );
219         }
220         catch( Throwable JavaDoc t )
221         {
222             getLogger().warning( "LoaderRegThread.processRegistration: " +
223                 "registration of MBean failed for: " +
224                 objectName + " = " + t.toString() + ", " + t.getMessage() + "\n" +
225                 ExceptionUtil.getStackTrace( t ) );
226         }
227     }
228     
229     /** WORKAROUND_FOR_BUG_SRIDATTA_FOUND (should not be public)*/
230         public synchronized void
231     processUnregistration( final ObjectName JavaDoc objectName )
232     {
233         try
234         {
235             mRegHandler.handleMBeanUnregistered( objectName );
236         }
237         catch( Throwable JavaDoc t )
238         {
239             getLogger().warning( "LoaderRegThread.processUnregistration: " +
240                 "unregistration of MBean failed for: " +
241                 objectName + " = " + t.toString() );
242         }
243     }
244     
245         private void
246     processRegistrations()
247     {
248         ObjectName JavaDoc objectName = null;
249          
250         int numDone = 0;
251         while ( (objectName = dequeue( mRegistrationQueue )) != null )
252         {
253             processRegistration( objectName );
254             ++numDone;
255         }
256         getLogger().fine( "LoaderRegThread: processed mbeans: " + numDone );
257     }
258     
259         private void
260     processUnregistrations()
261     {
262         ObjectName JavaDoc objectName = null;
263          
264         while ( (objectName = dequeue( mUnregistrationQueue )) != null )
265         {
266             processUnregistration( objectName );
267         }
268     }
269 }
270
271
272
273
274
Popular Tags