KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.management.support;
29
30 import java.util.Set JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 import java.io.IOException JavaDoc;
36
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.MBeanRegistrationException JavaDoc;
39 import javax.management.InstanceAlreadyExistsException JavaDoc;
40 import javax.management.InstanceNotFoundException JavaDoc;
41
42 import com.sun.appserv.management.base.Util;
43 import com.sun.appserv.management.base.XTypes;
44 import com.sun.appserv.management.base.AMX;
45 import com.sun.appserv.management.base.AMXDebug;
46 import com.sun.appserv.management.base.SystemInfo;
47
48 import com.sun.appserv.management.client.ProxyFactory;
49
50 import com.sun.appserv.management.util.jmx.JMXUtil;
51 import com.sun.appserv.management.util.misc.GSetUtil;
52 import com.sun.appserv.management.util.misc.CollectionUtil;
53 import com.sun.appserv.management.util.misc.StringUtil;
54
55 import com.sun.appserv.management.config.ClusterConfig;
56 import com.sun.appserv.management.config.DomainConfig;
57 import com.sun.appserv.management.config.ServerConfig;
58 import com.sun.appserv.management.config.ClusteredServerConfig;
59 import com.sun.appserv.management.config.StandaloneServerConfig;
60 import com.sun.appserv.management.config.ServerRefConfig;
61 import com.sun.appserv.management.config.RefConfig;
62
63 import com.sun.appserv.management.util.stringifier.SmartStringifier;
64
65 import com.sun.enterprise.management.support.oldconfig.OldServersMBean;
66 import com.sun.enterprise.management.support.oldconfig.OldConfigProxies;
67 import com.sun.enterprise.management.support.oldconfig.OldProps;
68
69 import com.sun.appserv.management.util.jmx.MBeanRegistrationListener;
70 import com.sun.appserv.management.util.jmx.JMXUtil;
71
72
73 /**
74     Loads MBeans.
75  */

76 class LoaderOfOldConfig extends LoaderOfOld
77 {
78     private final boolean mSupportsClusters;
79     private ServerRefListener mServerRefListener;
80     
81     LoaderOfOldConfig( final Loader loader )
82     {
83         super( loader );
84         
85         final SystemInfo systemInfo = getDomainRoot().getSystemInfo();
86         mSupportsClusters = systemInfo.supportsFeature( systemInfo.CLUSTERS_FEATURE );
87         
88         if ( NEEDS_SUPPORT.size() != 0 )
89         {
90             getLogger().warning(
91                 "Support for the following old config mbean types is not yet implemented: {" +
92                 SmartStringifier.toString( NEEDS_SUPPORT ) + "}" );
93         }
94         
95         try
96         {
97             mServerRefListener = new ServerRefListener();
98         }
99         catch( Exception JavaDoc e )
100         {
101             debug( "" + e );
102             throw new Error JavaDoc( e );
103         }
104     }
105     
106     
107         private ObjectName JavaDoc
108     getListenToServerRefConfigPattern()
109     {
110         final String JavaDoc jmxDomain = Util.getObjectName( getDomainRoot() ).getDomain();
111         final String JavaDoc props = Util.makeJ2EETypeProp( XTypes.SERVER_REF_CONFIG );
112         return Util.newObjectNamePattern( jmxDomain, props );
113     }
114     
115     /**
116         Listen for creation or removal of AMX ServerRefConfig MBeans.
117      */

118     private final class ServerRefListener
119         extends MBeanRegistrationListener
120     {
121         public ServerRefListener( )
122             throws InstanceNotFoundException JavaDoc, IOException JavaDoc
123         {
124             super( getMBeanServer(), getListenToServerRefConfigPattern() );
125         }
126         
127         /**
128             A ServerRefConfig has been created. It may require converting a
129             StandaloneServerConfig to a ClusteredServerConfig.
130          */

131             protected void
132         mbeanRegistered( final ObjectName JavaDoc objectName )
133         {
134             debug( "mbeanRegistered: " + objectName );
135             
136             final ServerRefConfig ref =
137                 ProxyFactory.getInstance( getMBeanServer() ).getProxy( objectName, ServerRefConfig.class);
138             if ( ref.getContainer() instanceof ClusterConfig )
139             {
140                 final String JavaDoc serverName = ref.getRef();
141                 debug( "mbeanRegistered: serverName = " + serverName );
142                 
143                 final DomainConfig domainConfig = getDomainRoot().getDomainConfig();
144                     
145                 if ( domainConfig.getClusteredServerConfigMap().get( serverName ) == null )
146                 {
147                     final ServerConfig server =
148                         domainConfig.getServerConfigMap().get( serverName );
149
150                     // it's referenced by a cluster, but not a ClusteredServerConfig. Fix it.
151
final ObjectName JavaDoc serverObjectName = Util.getObjectName( server );
152                     debug( "mbeanRegistered: serverConfig = " + serverObjectName );
153                     try
154                     {
155                         final Set JavaDoc<RefConfig> containees = new HashSet JavaDoc<RefConfig>();
156                         
157                         containees.addAll( server.getResourceRefConfigMap().values() );
158                         containees.addAll( server.getDeployedItemRefConfigMap().values() );
159                         
160                         mLoader.resyncAMXMBean( serverObjectName );
161                         
162                         for( final RefConfig containee : containees )
163                         {
164                             mLoader.resyncAMXMBean( Util.getObjectName( containee ) );
165                         }
166                     }
167                     catch( Throwable JavaDoc t )
168                     {
169                         getLogger().warning(
170                             "mbeanRegistered: can't resync with: " + serverObjectName +
171                             ": " + t);
172                     }
173                 }
174                 else
175                 {
176                     debug( "mbeanRegistered: server is already Clustered (ignoring)" );
177                 }
178             }
179             else
180             {
181                 debug( "mbeanRegistered: ref not from a cluster (ignoring)" );
182             }
183         }
184         
185         /**
186             A ServerRefConfig has been removed. It may require converting a
187             ClusteredServerConfig to a StandaloneServerConfig.
188             <p>
189             NOTE: not implemented: we don't support converting clustered
190             instances to standalone ones.
191          */

192             protected void
193         mbeanUnregistered( final ObjectName JavaDoc objectName )
194         {
195             debug( "mbeanUnregistered: " + objectName );
196         }
197     }
198     
199         protected Set JavaDoc<String JavaDoc>
200     getIgnoreTypes()
201     {
202         return( OldConfigTypes.getIgnoreTypes() );
203     }
204     
205         private Set JavaDoc<ObjectName JavaDoc>
206     getOldServerConfigObjectNames()
207     {
208         // 8.0 put the type=server MBeans into ias:
209
// see if they are present under com.sun.appserv
210
//
211
ObjectName JavaDoc pattern =
212             Util.newObjectNamePattern( "com.sun.appserv", "type=server" );
213         Set JavaDoc<ObjectName JavaDoc> objectNames = JMXUtil.queryNames( getMBeanServer(), pattern, null );
214         
215         if ( objectNames.size() == 0 )
216         {
217             pattern = Util.newObjectNamePattern( "ias", "type=server" );
218             
219             objectNames = JMXUtil.queryNames( getMBeanServer(), pattern, null );
220         }
221         return( objectNames );
222     }
223     
224         protected Set JavaDoc<ObjectName JavaDoc>
225     findAllOldCandidates()
226     {
227         ObjectName JavaDoc pattern = null;
228         Set JavaDoc<ObjectName JavaDoc> oldNames = null;
229         
230         pattern = Util.newObjectNamePattern( "com.sun.appserv", "category=config" );
231         oldNames = JMXUtil.queryNames( getMBeanServer(), pattern, null );
232         
233         oldNames.addAll( getOldServerConfigObjectNames() );
234         
235         debug( "LoaderOfOldConfig: found the following:" );
236         debug( CollectionUtil.toString( oldNames, "\n" ) );
237         return( oldNames );
238     }
239     
240
241     
242     /**
243         These types need to be supported.
244      */

245     private static final Set JavaDoc<String JavaDoc> NEEDS_SUPPORT = GSetUtil.newUnmodifiableStringSet();
246     
247         protected Set JavaDoc<String JavaDoc>
248     getNeedsSupport()
249     {
250         return( NEEDS_SUPPORT );
251     }
252     
253     
254         private boolean
255     isOldConfigMBean( final ObjectName JavaDoc objectName )
256     {
257         boolean isOldConfigMBean = false;
258         
259         if ( objectName.getDomain().equals( "com.sun.appserv" ) &&
260                     "config".equals( objectName.getKeyProperty( "category" ) ) )
261         {
262             final String JavaDoc type = objectName.getKeyProperty( "type" );
263             
264             isOldConfigMBean = ! OldConfigTypes.getIgnoreTypes().contains( type );
265         }
266         return( isOldConfigMBean );
267     }
268     
269         protected boolean
270     isOldMBean( final ObjectName JavaDoc objectName )
271     {
272         return( isOldConfigMBean( objectName ) );
273     }
274         
275     private final static String JavaDoc OLD_SERVER_TYPE = "server";
276     
277     private final class MyOldTypes
278         implements OldTypeToJ2EETypeMapper
279     {
280         private final OldConfigTypes mOldTypes;
281         private final OldServersMBean mOldServers;
282         
283             public
284         MyOldTypes()
285         {
286             mOldTypes = OldConfigTypes.getInstance();
287             mOldServers = OldConfigProxies.getInstance( getMBeanServer() ).getOldServersMBean();
288         }
289         
290             public String JavaDoc
291         j2eeTypeToOldType( final String JavaDoc j2eeType )
292         {
293             return( mOldTypes.j2eeTypeToOldType( j2eeType ) );
294         }
295         
296             private boolean
297         isStandaloneServer( final String JavaDoc name )
298         {
299             boolean isStandalone = true;
300             
301             if ( mSupportsClusters )
302             {
303                 final ObjectName JavaDoc[] standaloneServerObjectNames =
304                     mOldServers.listUnclusteredServerInstances( false );
305             
306                 isStandalone = false;
307                 for( int i = 0; i < standaloneServerObjectNames.length; ++i )
308                 {
309                     if ( name.equals( Util.getName( standaloneServerObjectNames[ i ] ) ) )
310                     {
311                         isStandalone = true;
312                         break;
313                     }
314                 }
315             }
316             
317             debug( "isStandalone: " + name + " = " + isStandalone );
318             
319             return( isStandalone );
320         }
321         
322             private String JavaDoc
323         getServerJ2EEType( final String JavaDoc name )
324         {
325             assert( name != null );
326             
327             final String JavaDoc j2eeType = isStandaloneServer( name ) ?
328                 XTypes.STANDALONE_SERVER_CONFIG :
329                 XTypes.CLUSTERED_SERVER_CONFIG;
330         
331             debug( "Server " + name + " is of type " + j2eeType );
332             return( j2eeType );
333         }
334         
335             public String JavaDoc
336         oldTypeToJ2EEType( final String JavaDoc oldType, final ObjectName JavaDoc objectName )
337         {
338             String JavaDoc j2eeType = null;
339             
340             if ( oldType.equals( objectName.getKeyProperty( "type" ) ) )
341             {
342                 j2eeType = oldObjectNameToJ2EEType( objectName );
343             }
344             else if ( oldType.equals( OLD_SERVER_TYPE ) )
345             {
346                 // the oldType is a property other than "type=server"
347
final String JavaDoc name = objectName.getKeyProperty( "server" );
348                 assert( name != null ) : "no name in: " + objectName;
349                 j2eeType = getServerJ2EEType( name );
350             }
351             else
352             {
353                 j2eeType = mOldTypes.oldTypeToJ2EEType( oldType, objectName );
354             }
355             return( j2eeType );
356         }
357     
358             public String JavaDoc
359         oldObjectNameToJ2EEType(
360             final ObjectName JavaDoc objectName )
361         {
362             String JavaDoc j2eeType = null;
363             
364             final String JavaDoc oldType = objectName.getKeyProperty( "type" );
365             // need to special-case "server"
366
if ( oldType.equals( OLD_SERVER_TYPE ) )
367             {
368                 final String JavaDoc name = objectName.getKeyProperty( "name" );
369                 j2eeType = getServerJ2EEType( name );
370             }
371             else
372             {
373                 j2eeType = mOldTypes.oldTypeToJ2EEType( oldType );
374             }
375             if ( j2eeType == null )
376             {
377                 getLogger().warning(
378                     "can't find j2eeType for com.sun.appserv type=" + oldType +
379                     StringUtil.NEWLINE() +
380                     "This usually means that a new domain.xml element has been added which " +
381                     "is not yet supported by AMX. File a bug requesting support, being sure to " +
382                     "specify the ObjectName " + StringUtil.quote( objectName ) );
383             }
384             
385             return( j2eeType );
386         }
387     }
388     
389     /**
390      */

391         protected ObjectName JavaDoc
392     oldToNewObjectName( final ObjectName JavaDoc oldObjectName )
393     {
394         trace( "oldToNewObjectName: " + oldObjectName );
395         final OldTypeToJ2EETypeMapper mapper = new MyOldTypes( );
396         final OldProps oldProps = new OldProps( oldObjectName, mapper );
397         final String JavaDoc domainName = mLoader.getAMXJMXDomainName();
398
399         String JavaDoc props = oldProps.getNewProps();
400         
401         // may be modified down below
402
final ObjectName JavaDoc newObjectName = Util.newObjectName( domainName, props );
403         
404         return( newObjectName );
405     }
406     
407     
408     
409 }
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
Popular Tags