KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

26
27 package com.sun.enterprise.management.support;
28
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import java.lang.reflect.Proxy JavaDoc;
38
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.MBeanServer JavaDoc;
41 import javax.management.Notification JavaDoc;
42 import javax.management.MBeanServerNotification JavaDoc;
43 import javax.management.InstanceNotFoundException JavaDoc;
44 import javax.management.MBeanRegistrationException JavaDoc;
45 import javax.management.InstanceAlreadyExistsException JavaDoc;
46 import javax.management.NotCompliantMBeanException JavaDoc;
47
48 import javax.management.relation.MBeanServerNotificationFilter JavaDoc;
49
50
51 import com.sun.appserv.management.base.XTypes;
52 import com.sun.appserv.management.base.Util;
53
54 import com.sun.appserv.management.util.jmx.stringifier.NotificationStringifier;
55 import com.sun.appserv.management.util.jmx.JMXUtil;
56 import com.sun.appserv.management.util.jmx.MBeanProxyHandler;
57 import com.sun.appserv.management.util.jmx.MBeanServerConnectionSource;
58 import com.sun.appserv.management.util.jmx.stringifier.StringifierRegistryIniter;
59
60 import com.sun.appserv.management.util.misc.ExceptionUtil;
61
62 import com.sun.appserv.management.util.stringifier.StringifierRegistryIniterImpl;
63 import com.sun.appserv.management.util.stringifier.StringifierRegistryImpl;
64
65 import com.sun.appserv.management.DomainRoot;
66 import com.sun.appserv.management.client.ProxyFactory;
67
68 import com.sun.enterprise.management.support.TypeInfos;
69
70 /**
71     
72  */

73 abstract class LoaderBase extends MBeanImplBase
74     implements LoaderMBean, LoaderRegHandler
75 {
76     protected boolean mStarted;
77     protected ObjectNames mObjectNames;
78     
79         public
80     LoaderBase()
81     {
82         debug( "LoaderBase" );
83
84         final boolean offline = false;
85         BootUtil.init( false );
86         
87         mServer = null;
88         
89         new StringifierRegistryIniterImpl( StringifierRegistryImpl.DEFAULT );
90         new StringifierRegistryIniter( StringifierRegistryImpl.DEFAULT );
91         
92         mStarted = false;
93         mObjectNames = null;
94     }
95     
96         public String JavaDoc
97     getAMXJMXDomainName()
98     {
99         return( BootUtil.getInstance().getAMXJMXDomainName() );
100     }
101     
102     
103         public String JavaDoc
104     getAdministrativeDomainName()
105     {
106         return( BootUtil.getInstance().getAppserverDomainName() );
107     }
108     
109     
110         public void
111     handleNotification(
112         final Notification JavaDoc notifIn,
113         final Object JavaDoc handback)
114     {
115         /* nothing by default */
116     }
117     
118         public void
119     handleMBeanRegistered( final ObjectName JavaDoc oldObjectName )
120         throws InstanceNotFoundException JavaDoc
121     {
122     }
123     
124         public void
125     handleMBeanUnregistered( final ObjectName JavaDoc oldObjectName )
126         throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
127     {
128     }
129     
130         protected static long
131     now()
132     {
133         return( System.currentTimeMillis() );
134     }
135     
136         public ObjectName JavaDoc
137     loadSystemInfo( final MBeanServer JavaDoc server )
138         throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
139         InstanceAlreadyExistsException JavaDoc
140     {
141         debug( "loadSystemInfo" );
142         
143         final BootUtil bootUtil = BootUtil.getInstance();
144         
145         final SystemInfoImpl systemInfo = new SystemInfoImpl( server, bootUtil );
146         
147         final ObjectName JavaDoc tempName = mObjectNames.getSingletonObjectName( systemInfo.J2EE_TYPE );
148         
149         final ObjectName JavaDoc objectName = mServer.registerMBean( systemInfo, tempName ).getObjectName();
150         
151         debug( "loaded SystemInfo" );
152         return( objectName );
153     }
154         
155         protected void
156     preRegisterHook()
157     {
158     }
159     
160         public final ObjectName JavaDoc
161     preRegister(
162         final MBeanServer JavaDoc server,
163         final ObjectName JavaDoc objectNameIn)
164         throws Exception JavaDoc
165     {
166         debug( "preRegister" );
167         final ObjectName JavaDoc superObjectName = super.preRegister( server, objectNameIn );
168         
169         mObjectNames = ObjectNames.getInstance( getAMXJMXDomainName() );
170         
171         final String JavaDoc domain = BootUtil.getInstance().getAMXSupportJMXDomain();
172         mSelfObjectName = Util.newObjectName( domain, LOADER_NAME_PROPS );
173             
174         preRegisterHook();
175         
176         try
177         {
178             loadSystemInfo( server );
179             
180             final MBeanServerNotificationFilter JavaDoc filter =
181                 new MBeanServerNotificationFilter JavaDoc();
182
183             filter.enableAllObjectNames();
184         
185             JMXUtil.listenToMBeanServerDelegate( mServer, this, filter, null );
186             
187         }
188         catch( Exception JavaDoc e )
189         {
190             throw new RuntimeException JavaDoc( e );
191         }
192         
193         debug( "preRegister done: " + mSelfObjectName );
194         return( mSelfObjectName );
195     }
196     
197         public void
198     postRegister( Boolean JavaDoc registrationDone )
199     {
200         super.postRegister( registrationDone );
201         
202         if ( registrationDone.booleanValue() )
203         {
204             initLOADER( getMBeanServer(), getObjectName() );
205         }
206         
207         start();
208     }
209
210         protected boolean
211     mySleep( final long millis )
212     {
213         boolean interrupted = false;
214         
215         try
216         {
217             Thread.sleep( millis );
218         }
219         catch( InterruptedException JavaDoc e )
220         {
221             Thread.interrupted();
222             interrupted = true;
223         }
224         
225         return interrupted;
226     }
227     
228         protected void
229     startHook()
230     {
231         // nothing
232
}
233     
234     
235     private final class CheckStartedThread extends Thread JavaDoc
236     {
237         public void CheckStartedThread() {}
238         
239             public void
240         run()
241         {
242             final long AMX_READY_SLEEP_DURATION = 100;
243             
244             while ( ! isStarted() )
245             {
246                 debug( "Waiting " + AMX_READY_SLEEP_DURATION + "ms for AMX to start");
247                 sleepMillis( AMX_READY_SLEEP_DURATION );
248             }
249
250             BootUtil.getInstance().setAMXReady( true );
251         }
252     }
253     
254
255         public boolean
256     isStarted( )
257     {
258         return( mStarted );
259     }
260
261     protected abstract Object JavaDoc createDomainRoot();
262     
263     public abstract boolean isDAS();
264     
265         private final void
266     loadDomainRoot()
267     {
268         debug( "loadDomainRoot ENTER" );
269         final Object JavaDoc domainRoot = createDomainRoot();
270         if ( domainRoot != null )
271         {
272             ObjectName JavaDoc objectName = mObjectNames.getDomainRootObjectName( );
273             
274             debug( "Registering DomainRoot, impl class = " + domainRoot.getClass().getName() );
275             try
276             {
277                 objectName = mServer.registerMBean( domainRoot, objectName ).getObjectName();
278                 debug( "Registered DomainRoot: " + objectName );
279             }
280             catch( final Exception JavaDoc e )
281             {
282                 final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause(e);
283                 debug( "Exception loading DomainRoot: " +
284                      rootCause + ", msg=" + rootCause.getMessage() );
285                 throw new RuntimeException JavaDoc( rootCause );
286             }
287         }
288         else
289         {
290             debug( "loadDomainRoot: NULL" );
291         }
292         
293         debug( "loadDomainRoot DONE" );
294     }
295
296         public final DomainRoot
297     getDomainRoot()
298     {
299         final ProxyFactory factory =
300                 ProxyFactory.getInstance( getMBeanServer() );
301         
302         return( factory.getDomainRoot() );
303     }
304     
305     private static LoaderMBean LOADER = null;
306     
307         private static void
308     initLOADER( final MBeanServer JavaDoc server, final ObjectName JavaDoc loaderObjectName )
309     {
310         assert( LOADER == null );
311         
312         try
313         {
314             final MBeanServerConnectionSource conn = new MBeanServerConnectionSource( server );
315             final MBeanProxyHandler handler = new MBeanProxyHandler( conn, loaderObjectName );
316             
317             LOADER = (LoaderMBean)
318                     Proxy.newProxyInstance( LoaderMBean.class.getClassLoader(), new Class JavaDoc[] { LoaderMBean.class }, handler);
319         }
320         catch( IOException JavaDoc e )
321         {
322             assert( false ) : "can't happen";
323             throw new RuntimeException JavaDoc( e );
324         }
325     }
326     
327     
328         public static LoaderMBean
329     getLoader( final MBeanServer JavaDoc server )
330     {
331         return( LOADER );
332     }
333     
334     
335         protected void
336     start()
337     {
338         synchronized( this )
339         {
340             if ( mStarted )
341             {
342                 throw new IllegalArgumentException JavaDoc( "Can't start Loader twice" );
343             }
344             mStarted = true;
345         }
346         
347         new AMXDebugSupport( mServer );
348         loadDomainRoot();
349         
350         startHook();
351         
352         (new CheckStartedThread()).start();
353     }
354     
355     
356         public ObjectName JavaDoc
357     sync( final ObjectName JavaDoc name)
358     {
359         throw new UnsupportedOperationException JavaDoc( "sync" );
360     }
361     
362
363     /**
364         See com.sun.enterprise.admin.server.core.notifyAMXThatAdminServiceIsReady()
365         Make a [synchronous] call to AMX. Semantics are that this should
366         be a quick call, not a long-running one.
367      */

368         public void
369     adminServiceReady()
370     {
371         // nothing by default
372
}
373     
374         public ObjectName JavaDoc
375     resyncAMXMBean( final ObjectName JavaDoc amx )
376         throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
377     {
378         // do nothing
379
return amx;
380     }
381 }
382
383
384
385
386
387
388
389
390
Popular Tags