KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.management.support;
24
25 import java.util.Arrays JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import java.lang.reflect.Constructor JavaDoc;
35
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.MBeanRegistrationException JavaDoc;
39 import javax.management.InstanceAlreadyExistsException JavaDoc;
40 import javax.management.NotCompliantMBeanException JavaDoc;
41
42 import com.sun.appserv.management.base.AMX;
43 import com.sun.appserv.management.util.misc.ExceptionUtil;
44 import com.sun.appserv.management.util.misc.Output;
45 import com.sun.appserv.management.util.misc.GSetUtil;
46 import com.sun.appserv.management.util.misc.ListUtil;
47 import com.sun.appserv.management.util.misc.StringUtil;
48 import com.sun.appserv.management.util.jmx.JMXUtil;
49 import com.sun.appserv.management.base.Util;
50 import com.sun.appserv.management.base.AMXDebug;
51 import com.sun.appserv.management.DomainRoot;
52
53 /**
54     Loads MBeans.
55  */

56 abstract class LoaderOfOld
57 {
58     protected final Loader mLoader;
59     private final Output mDebug;
60     
61     LoaderOfOld( final Loader loader )
62     {
63         mLoader = loader;
64         
65         mDebug = AMXDebug.getInstance().getOutput( this.getClass().getName() );
66     }
67     
68         protected final void
69     debug(final Object JavaDoc o)
70     {
71         mDebug.println( o.toString() );
72     }
73     
74     
75     protected abstract Set JavaDoc<ObjectName JavaDoc> findAllOldCandidates();
76     
77     
78         protected DomainRoot
79     getDomainRoot()
80     {
81         return( mLoader.getDomainRoot() );
82     }
83     
84     protected abstract Set JavaDoc getNeedsSupport();
85     protected abstract Set JavaDoc getIgnoreTypes();
86     protected abstract boolean isOldMBean( final ObjectName JavaDoc objectName );
87     
88     
89         public final boolean
90     shouldSync( final ObjectName JavaDoc o )
91     {
92         boolean shouldSync = isOldMBean( o );
93         
94         if ( shouldSync )
95         {
96             final String JavaDoc type = o.getKeyProperty( "type" );
97             
98             if ( getNeedsSupport().contains( type ) )
99             {
100                 shouldSync = false;
101                 getLogger().warning(
102                     "com.sun.appserv MBean not yet supported: " +
103                     StringUtil.quote(o) );
104             }
105             else if ( isDefectiveIgnore( o ) )
106             {
107                 shouldSync = false;
108                 debug(
109                     "com.sun.appserv MBean was last determined to be defective " +
110                     "and will not be represented in AMX: " +
111                     StringUtil.quote(o) );
112             }
113         }
114
115         return( shouldSync );
116     }
117     
118     
119     /**
120         Is the com.sun.appserv MBean defective in some way such that it
121         cannot be properly represented in AMX?
122      */

123         protected boolean
124     isDefectiveIgnore( final ObjectName JavaDoc objectName )
125     {
126         return false;
127     }
128     
129     
130     /**
131         Create a list of all old MBeans and return a list in the order in which
132         they must be registered.
133      */

134         public final List JavaDoc
135     findAllOld()
136     {
137         final Set JavaDoc<ObjectName JavaDoc> all = findAllOldCandidates();
138         final Set JavaDoc<ObjectName JavaDoc> results = new HashSet JavaDoc<ObjectName JavaDoc>();
139         
140         for( final ObjectName JavaDoc objectName : all)
141         {
142             if ( shouldSync( objectName ) )
143             {
144                 results.add( objectName );
145             }
146         }
147         
148         return( ListUtil.newListFromCollection( results ) );
149     }
150     
151     protected abstract ObjectName JavaDoc oldToNewObjectName( final ObjectName JavaDoc o );
152     
153     
154     
155     /**
156         Create a Map of Set keyed by the ObjectName key value specified.
157         Each Set contains all the ObjectNames with that key.
158      */

159         protected final Map JavaDoc<String JavaDoc,Set JavaDoc<ObjectName JavaDoc>>
160     candidatesToMap(
161         final Set JavaDoc<ObjectName JavaDoc> candidates,
162         final String JavaDoc key)
163     {
164         final Map JavaDoc<String JavaDoc,Set JavaDoc<ObjectName JavaDoc>> setMap = new HashMap JavaDoc<String JavaDoc,Set JavaDoc<ObjectName JavaDoc>>();
165         for( final ObjectName JavaDoc candidate : candidates )
166         {
167             final String JavaDoc keyValue = candidate.getKeyProperty( key );
168             
169             Set JavaDoc<ObjectName JavaDoc> typeSet = setMap.get( keyValue );
170             if ( typeSet == null )
171             {
172                 typeSet = new HashSet JavaDoc<ObjectName JavaDoc>();
173                 setMap.put( keyValue, typeSet );
174             }
175             typeSet.add( candidate );
176         }
177         
178         return( setMap );
179     }
180     
181     
182     
183
184         protected final ObjectName JavaDoc
185     registerNew(
186         final Object JavaDoc impl,
187         final ObjectName JavaDoc implObjectName,
188         final ObjectName JavaDoc oldObjectName )
189         throws MBeanRegistrationException JavaDoc,
190             InstanceAlreadyExistsException JavaDoc, NotCompliantMBeanException JavaDoc
191     {
192         if ( impl == null )
193         {
194             final String JavaDoc msg = "unable to create new impl for old: " + oldObjectName;
195             debug( msg );
196             throw new IllegalArgumentException JavaDoc( msg );
197         }
198
199         return( mLoader.registerNew( impl, implObjectName, oldObjectName ) );
200     }
201     
202         protected final void
203     trace( final Object JavaDoc o )
204     {
205         debug( o );
206     }
207             protected String JavaDoc
208     toString( final Object JavaDoc o )
209     {
210         return( com.sun.appserv.management.util.stringifier.SmartStringifier.toString( o ) );
211     }
212     
213         protected Logger JavaDoc
214     getLogger()
215     {
216         return( mLoader.getMBeanLogger() );
217     }
218     
219         public String JavaDoc
220     getAMXJMXDomainName()
221     {
222         return( mLoader.getAMXJMXDomainName() );
223     }
224     
225         public MBeanServer JavaDoc
226     getMBeanServer()
227     {
228         return( mLoader.getMBeanServer() );
229     }
230     
231         
232
233     private static final Class JavaDoc[] EMPTY_SIG = new Class JavaDoc[0];
234     private static final Class JavaDoc[] DELEGATE_SIG =
235         new Class JavaDoc[] { Delegate.class };
236     
237     
238
239         private Constructor JavaDoc
240     findConstructor( final Constructor JavaDoc[] constructors, final Class JavaDoc[] sig )
241     {
242         Constructor JavaDoc constructor = null;
243         
244         for( int i = 0; i < constructors.length; ++i )
245         {
246             final Class JavaDoc<?>[] csig = constructors[ i ].getParameterTypes();
247             
248             if ( csig.length == sig.length )
249             {
250                 constructor = constructors[ i ];
251             
252                 for( int c = 0; c < sig.length; ++c )
253                 {
254                     if ( ! csig[ i ].isAssignableFrom( sig[ i ] ) )
255                     {
256                         constructor = null;
257                         break;
258                     }
259                 }
260                 
261                 if ( constructor != null )
262                 {
263                     break;
264                 }
265             }
266         }
267         
268         return( constructor );
269     }
270     
271         private Constructor JavaDoc
272     getDelegateConstructor( final Constructor JavaDoc[] constructors )
273     {
274         return( findConstructor( constructors, DELEGATE_SIG ) );
275     }
276     
277     
278         private Constructor JavaDoc
279     getEmptyConstructor( final Constructor JavaDoc[] constructors )
280     {
281         return( findConstructor( constructors, EMPTY_SIG ) );
282     }
283     
284             
285         protected Class JavaDoc
286     getImplClass(
287         final ObjectName JavaDoc newObjectName,
288         final ObjectName JavaDoc oldObjectName)
289     {
290         final String JavaDoc newType = Util.getJ2EEType( newObjectName );
291         
292         final TypeInfo info = TypeInfos.getInstance().getInfo( newType );
293         assert( info != null );
294         final Class JavaDoc implClass = info.getImplClass();
295         
296         return( implClass );
297     }
298             
299         protected Object JavaDoc
300     newImpl(
301         final ObjectName JavaDoc newObjectName,
302         final ObjectName JavaDoc oldObjectName )
303         throws Exception JavaDoc
304     {
305         Object JavaDoc impl = null;
306         
307         final Class JavaDoc implClass = getImplClass( newObjectName, oldObjectName );
308         
309         try
310         {
311             final Constructor JavaDoc[] constructors = implClass.getConstructors();
312             Constructor JavaDoc constructor = null;
313             
314             if ( (constructor = getDelegateConstructor( constructors )) != null )
315             {
316                 final DelegateToMBeanDelegate delegate =
317                     new DelegateToMBeanDelegate( mLoader.getMBeanServer(), oldObjectName );
318                 assert( delegate != null );
319                 debug( "created Delegate with target of " + oldObjectName +
320                     " for " + newObjectName );
321                     
322                 impl = constructor.newInstance( new Object JavaDoc[] { delegate } );
323             }
324             else if ( getEmptyConstructor( constructors ) != null )
325             {
326                 impl = implClass.newInstance();
327             }
328             else
329             {
330                 assert( false );
331                 throw new Error JavaDoc( "Delegate has no constructor" );
332             }
333         }
334         catch( Exception JavaDoc e )
335         {
336             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
337             debug( "Loader.newImpl: exception creating new impl: " + e + "\n" +
338                 ExceptionUtil.getStackTrace( rootCause ) );
339             throw e;
340         }
341         
342         return( impl );
343     }
344
345
346         protected ObjectName JavaDoc
347     findExisting(
348         final Set JavaDoc<ObjectName JavaDoc> newObjectNames,
349         final ObjectName JavaDoc oldObjectName )
350     {
351         ObjectName JavaDoc resultName = null;
352         
353         if ( newObjectNames.size() == 1 )
354         {
355             // already registered
356
resultName = GSetUtil.getSingleton( newObjectNames );
357         }
358
359         return( resultName );
360     }
361     
362         private final synchronized ObjectName JavaDoc
363     ensureNew(
364         final ObjectName JavaDoc newObjectName,
365         final ObjectName JavaDoc oldObjectName )
366         throws MBeanRegistrationException JavaDoc,
367             InstanceAlreadyExistsException JavaDoc, NotCompliantMBeanException JavaDoc, Exception JavaDoc
368     {
369         // don't assume this ObjectName is the entire name; query for it
370
final ObjectName JavaDoc pattern = Util.newObjectNamePattern( newObjectName );
371         
372         // don't assume this ObjectName is the entire name; query for it
373
final Set JavaDoc<ObjectName JavaDoc> objectNames = JMXUtil.queryNames( getMBeanServer(), pattern, null );
374         
375         final ObjectName JavaDoc existingObjectName = findExisting( objectNames, oldObjectName );
376         
377         ObjectName JavaDoc resultName = null;
378         
379         if ( existingObjectName == null )
380         {
381             // not yet registered, create it
382
final Object JavaDoc impl = newImpl( newObjectName, oldObjectName );
383             
384             resultName = registerNew( impl, newObjectName, oldObjectName );
385         }
386         else
387         {
388             resultName = existingObjectName;
389         }
390         
391         assert( resultName != null );
392         
393         return( resultName );
394     }
395     
396     
397         protected ObjectName JavaDoc
398     syncWithOld( final ObjectName JavaDoc oldObjectName )
399         throws MBeanRegistrationException JavaDoc,
400             InstanceAlreadyExistsException JavaDoc, NotCompliantMBeanException JavaDoc, Exception JavaDoc
401     {
402         final ObjectName JavaDoc newObjectName = oldToNewObjectName( oldObjectName );
403         debug( "\nsyncWithOld: \n" + oldObjectName + "\n=>\n" + newObjectName );
404         
405         final ObjectName JavaDoc resultName = ensureNew( newObjectName, oldObjectName );
406         
407         return( resultName );
408     }
409 }
410
411
412
413
414
415
416
417
418
Popular Tags