KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > helper > RefHelper


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-core/mbeanapi/src/java/com/sun/appserv/management/helper/RefHelper.java,v 1.4 2005/12/25 03:50:38 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 03:50:38 $
28  */

29 package com.sun.appserv.management.helper;
30
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 import javax.management.ObjectName JavaDoc;
36
37 import com.sun.appserv.management.base.AMX;
38 import com.sun.appserv.management.base.AMXDebug;
39 import com.sun.appserv.management.base.XTypes;
40 import com.sun.appserv.management.base.QueryMgr;
41 import com.sun.appserv.management.base.Util;
42
43 import com.sun.appserv.management.util.misc.CollectionUtil;
44 import com.sun.appserv.management.util.misc.GSetUtil;
45 import com.sun.appserv.management.util.misc.Output;
46
47 import com.sun.appserv.management.config.RefConfigReferent;
48 import com.sun.appserv.management.config.DeployedItemRefConfigReferent;
49 import com.sun.appserv.management.config.ResourceRefConfigReferent;
50 import com.sun.appserv.management.config.ServerRefConfigReferent;
51 import com.sun.appserv.management.config.ClusterRefConfigReferent;
52 import com.sun.appserv.management.config.ClusterRefConfig;
53 import com.sun.appserv.management.config.ServerRefConfig;
54 import com.sun.appserv.management.config.ConfigRemover;
55
56 import com.sun.appserv.management.config.RefConfig;
57 import com.sun.appserv.management.config.ServerRefConfig;
58 import com.sun.appserv.management.config.ClusterRefConfig;
59 import com.sun.appserv.management.config.ResourceConfig;
60 import com.sun.appserv.management.config.ResourceRefConfig;
61 import com.sun.appserv.management.config.ResourceRefConfigCR;
62 import com.sun.appserv.management.config.ModuleConfig;
63 import com.sun.appserv.management.config.DeployedItemRefConfig;
64 import com.sun.appserv.management.config.DeployedItemRefConfigCR;
65 import com.sun.appserv.management.config.LifecycleModuleConfig;
66 import com.sun.appserv.management.config.CustomMBeanConfig;
67
68 /**
69     Helper routines for references. This class should only be used
70     in EE builds.
71     
72     @since AppServer 9.0
73  */

74 public class RefHelper
75 {
76     private RefHelper() {}
77
78
79         private static Output
80     getDebug()
81     {
82         return AMXDebug.getInstance().getOutput( "com.sun.appserv.management.helper.RefHelper" );
83     }
84     
85     /**
86         Find all ref configs of any j2EEType
87         @return Set of all {@link RefConfig} found.
88      */

89         public static <T extends RefConfig> Set JavaDoc<T>
90     findAllRefConfigs( final QueryMgr queryMgr )
91     {
92         final Set JavaDoc<T> refs =
93             queryMgr.queryInterfaceSet( RefConfig.class.getName(), null);
94         
95         return refs;
96     }
97     
98     /**
99         Find all ref configs of the specified j2EEType
100         @return Set of all {@link RefConfig} found.
101      */

102         public static <T extends RefConfig> Set JavaDoc<T>
103     findAllRefConfigsByJ2EEType(
104         final QueryMgr queryMgr,
105         final String JavaDoc refJ2EEType )
106     {
107         final Set JavaDoc<T> refs =
108             queryMgr.queryJ2EETypeSet( refJ2EEType );
109         
110         return refs;
111     }
112     
113     /**
114         Find all {@link ResourceRefConfig}.
115         @return Set of all {@link ResourceRefConfig} found.
116      */

117         public static Set JavaDoc<ResourceRefConfig>
118     findAllResourceRefConfigs( final QueryMgr queryMgr )
119     {
120         return
121             findAllRefConfigsByJ2EEType( queryMgr, XTypes.RESOURCE_REF_CONFIG );
122     }
123     
124     /**
125         Find all {@link DeployedItemRefConfig}.
126         @return Set of all {@link DeployedItemRefConfig} found.
127      */

128         public static Set JavaDoc<DeployedItemRefConfig>
129     findAllDeployedItemRefConfigs( final QueryMgr queryMgr )
130     {
131         return
132             findAllRefConfigsByJ2EEType( queryMgr, XTypes.DEPLOYED_ITEM_REF_CONFIG );
133     }
134     
135     /**
136         Find all {@link ServerRefConfig}.
137         @return Set of all {@link ServerRefConfig} found.
138      */

139         public static Set JavaDoc<ServerRefConfig>
140     findAllServerRefConfigs( final QueryMgr queryMgr )
141     {
142         return
143             findAllRefConfigsByJ2EEType( queryMgr, XTypes.SERVER_REF_CONFIG );
144     }
145     
146     
147     /**
148         Find all {@link ClusterRefConfig}.
149         @return Set of all {@link ClusterRefConfig} found.
150      */

151         public static Set JavaDoc<ClusterRefConfig>
152     findAllClusterRefConfigs( final QueryMgr queryMgr )
153     {
154         return
155             findAllRefConfigsByJ2EEType( queryMgr, XTypes.CLUSTER_REF_CONFIG );
156     }
157     
158     /**
159         Find all {@link RefConfig} of the specified j2EEType having
160         the specified name.
161         @return Set of all {@link RefConfig} found with the specified name.
162      */

163         public static <T extends RefConfig> Set JavaDoc<T>
164     findAllRefConfigsWithName(
165         final QueryMgr queryMgr,
166         final String JavaDoc refJ2EEType,
167         final String JavaDoc name )
168     {
169         final String JavaDoc props = Util.makeRequiredProps( refJ2EEType, name );
170         
171         final Set JavaDoc<T> refs = queryMgr.queryPatternSet( null, props );
172         
173         return refs;
174     }
175     
176     /**
177         @return the j2eeType of the config element which can <i>refer</i> to this item
178      */

179         public static String JavaDoc
180     getReferentRefJ2EEType( final RefConfigReferent referent )
181     {
182         String JavaDoc j2eeType = null;
183         
184         if ( referent instanceof ResourceRefConfigReferent )
185         {
186             j2eeType = XTypes.RESOURCE_REF_CONFIG;
187         }
188         else if ( referent instanceof DeployedItemRefConfigReferent )
189         {
190             j2eeType = XTypes.DEPLOYED_ITEM_REF_CONFIG;
191         }
192         else if ( referent instanceof ServerRefConfigReferent )
193         {
194             j2eeType = XTypes.SERVER_REF_CONFIG;
195         }
196         else if ( referent instanceof ClusterRefConfigReferent )
197         {
198             j2eeType = XTypes.CLUSTER_REF_CONFIG;
199         }
200         else
201         {
202             throw new IllegalArgumentException JavaDoc(
203                 "Unknown referent class: " + Util.asAMX( referent ).getJ2EEType() );
204         }
205         
206         return j2eeType;
207     }
208     
209    private static final Set JavaDoc<String JavaDoc> REFERENT_J2EE_TYPES =
210     Collections.unmodifiableSet( GSetUtil.newSet( new String JavaDoc[]
211         {
212             XTypes.RESOURCE_REF_CONFIG,
213             XTypes.DEPLOYED_ITEM_REF_CONFIG,
214             XTypes.SERVER_REF_CONFIG,
215             XTypes.CLUSTER_REF_CONFIG,
216         }));
217     
218         public static Set JavaDoc<String JavaDoc>
219     getReferentJ2EETypes()
220     {
221         return REFERENT_J2EE_TYPES;
222     }
223    
224     
225      /**
226         Find all {@link RefConfig} that reference the specified j2eeType/name
227         combo.
228      */

229         public static <T extends RefConfig> Set JavaDoc<T>
230     findAllRefConfigs( final RefConfigReferent referent )
231     {
232         final AMX amx = Util.asAMX( referent );
233         
234         return findAllRefConfigsWithName( getQueryMgr( amx ),
235                     getReferentRefJ2EEType( referent ),
236                     amx.getName() );
237     }
238     
239     /**
240         Find all {@link RefConfig} that reference the specified j2eeType/name
241         combo.
242      */

243         public static <T extends RefConfig> Set JavaDoc<T>
244     findAllRefConfigs( final RefConfigReferent referent, final String JavaDoc refJ2EEType )
245     {
246         final AMX amx = Util.asAMX( referent );
247         
248         return findAllRefConfigsWithName( getQueryMgr( amx ), refJ2EEType, amx.getName() );
249     }
250     
251     /**
252         Find all {@link DeployedItemRefConfig} that reference
253         the specified item.
254      */

255         public static Set JavaDoc<DeployedItemRefConfig>
256     findAllRefConfigs( final DeployedItemRefConfigReferent referent )
257     {
258         final AMX amx = Util.asAMX( referent );
259         
260         return
261             findAllRefConfigsWithName( getQueryMgr( amx ), XTypes.DEPLOYED_ITEM_REF_CONFIG, amx.getName() );
262     }
263     
264     /**
265         Find all {@link ResourceRefConfig} that reference
266         the specified item.
267      */

268         public static Set JavaDoc<ResourceRefConfig>
269     findAllRefConfigs( final ResourceRefConfigReferent referent )
270     {
271         final AMX amx = Util.asAMX( referent );
272         
273         return
274             findAllRefConfigsWithName( getQueryMgr( amx ), XTypes.RESOURCE_REF_CONFIG, amx.getName() );
275     }
276     
277     /**
278         Find all {@link ServerRefConfig} that reference
279         the specified item.
280      */

281         public static Set JavaDoc<ServerRefConfig>
282     findAllRefConfigs( final ServerRefConfigReferent referent )
283     {
284         final AMX amx = Util.asAMX( referent );
285         
286         return
287             findAllRefConfigsWithName( getQueryMgr( amx ), XTypes.SERVER_REF_CONFIG, amx.getName() );
288     }
289     
290         private static QueryMgr
291     getQueryMgr( final AMX amx )
292     {
293         return amx.getDomainRoot().getQueryMgr();
294     }
295     
296     /**
297         Find all {@link ClusterRefConfig} that reference
298         the specified item.
299      */

300         public static Set JavaDoc<ClusterRefConfig>
301     findAllRefConfigs( final ClusterRefConfigReferent referent )
302     {
303         final AMX amx = Util.asAMX( referent );
304         
305         return
306             findAllRefConfigsWithName( getQueryMgr( amx ), XTypes.CLUSTER_REF_CONFIG, amx.getName() );
307     }
308     
309         public static <T extends RefConfig> Set JavaDoc<T>
310     removeAllRefsTo(
311         final RefConfigReferent item,
312         final boolean allowSingleFailure )
313     {
314         final Set JavaDoc<T> refs = RefHelper.findAllRefConfigs( item );
315             
316         final Set JavaDoc<T> failures = RefHelper.removeRefConfigs( refs );
317         
318         if ( failures.size() > 1 || ! allowSingleFailure )
319         {
320             final Set JavaDoc<ObjectName JavaDoc> objectNames = Util.toObjectNames( failures );
321             
322             throw new IllegalArgumentException JavaDoc( "failure removing refererences:\n{" +
323                 CollectionUtil.toString( objectNames, "\n" ) + "\n}" );
324         }
325         
326         return failures;
327     }
328     
329     /**
330         Remove all specified references (that are possible).
331         @return any references that could not be removed
332      */

333         public static <T extends RefConfig> Set JavaDoc<T>
334     removeRefConfigs( final Set JavaDoc<T> refs )
335     {
336         final Set JavaDoc<T> failures = new HashSet JavaDoc<T>();
337         
338         for( final T ref : refs )
339         {
340             final ConfigRemover rrc = ConfigRemover.class.cast( ref.getContainer() );
341             
342             try
343             {
344                 // may fail as illegal in PE or EE with one ref
345
rrc.removeConfig( ref.getJ2EEType(), ref.getName() );
346             }
347             catch( Exception JavaDoc e )
348             {
349                 failures.add( ref );
350             }
351         }
352         
353         return failures;
354     }
355     
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
Popular Tags