KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > ServerBeansFactory


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  * ServerBeansFactory.java
26  *
27  * Created on February 26, 2003, 1:44 PM
28  */

29
30 package com.sun.enterprise.config.serverbeans;
31
32 import java.io.*;
33 import java.util.*;
34 import com.sun.enterprise.config.ConfigException;
35 import com.sun.enterprise.config.ConfigContext;
36 import com.sun.enterprise.config.ConfigBean;
37 import com.sun.enterprise.config.ConfigBeansFactory;
38 import java.lang.reflect.Method JavaDoc;
39 import java.text.CharacterIterator JavaDoc;
40 import java.text.StringCharacterIterator JavaDoc;
41
42 import com.sun.enterprise.util.i18n.StringManager;
43 import com.sun.enterprise.admin.util.IAdminConstants;
44
45 /**
46  * Factory for retrieving a particular config. bean given
47  * the ConfigContext object.
48  * This is the central place where a server resolves the references.
49  * There should be no place in the code that accesses beans using context directly.
50  *
51  * @author Sridatta
52  * @author ruyak
53  */

54 public class ServerBeansFactory {
55
56     /** Server configuration **/
57     private static Config configModel;
58
59     /** local string manager for i18n */
60     private static StringManager localStrings =
61     StringManager.getManager("com.sun.enterprise.server");
62
63     /** Creates a new instance of ServerBeansFactory */
64     public ServerBeansFactory() {
65     }
66
67     /**
68      * Get the config model for this server instance. This is the config
69      * bean for the server that is defined by the config-ref child element.
70      *
71      * @return the config bean for this config ref model.
72      */

73     public static Config getConfigModel(ConfigContext configContext) throws ConfigException {
74
75         //create the config bean for this instance
76
if (configContext != null) {
77             Server server = ServerBeansFactory.getServerBean(configContext);
78             String JavaDoc configRef = server.getConfigRef();
79             String JavaDoc configXpath = ServerXPathHelper.getConfigIdXpath(configRef);
80             configModel = (Config) ConfigBeansFactory.getConfigBeanByXPath(configContext, configXpath);
81         } else {
82             String JavaDoc msg = localStrings.getString(
83             "serverContext.config_context_is_null");
84             throw new ConfigException(msg);
85         }
86         return configModel;
87     }
88
89     /**
90     *
91     */

92     public static Domain getDomainBean(ConfigContext ctx) throws ConfigException {
93        return (Domain)ctx.getRootConfigBean();
94     }
95
96     /**
97      * Returns server config bean.
98      *
99      * @returns the server config bean
100      */

101     public static Server getServerBean(ConfigContext ctx) throws ConfigException {
102        final String JavaDoc serverName = java.lang.System.getProperty("com.sun.aas.instanceName");
103        String JavaDoc xpath = ServerXPathHelper.getServerIdXpath(serverName);
104        final Server server =
105             (Server) ConfigBeansFactory.getConfigBeanByXPath(ctx, xpath);
106        return server;
107     }
108
109     /**
110     * Returns the config bean for a particular instance.
111     *
112     * @returns the config bean for the instance
113     */

114     public static Config getConfigBean(ConfigContext ctx) throws ConfigException {
115         return ServerBeansFactory.getConfigModel(ctx);
116     }
117
118     /**
119      *
120      * @ctx config context of the server.
121      * @type type of the resource. maps to element name in dtd
122      */

123     public static ConfigBean[] getResources(ConfigContext ctx, String JavaDoc type) throws ConfigException {
124        Resources r = getDomainBean(ctx).getResources();
125
126        //ResourceRefs refs = getServerBean(ctx).getResourceRefs();
127

128        // Implement the logic of getting only the resources referenced by the server
129

130        // TBD
131
//FIXME: validate the code.
132
return r.getChildBeansByName(type);
133     }
134
135         /**
136          * Get the connector service object from the given config
137          * context
138          * @param ctx the ConfigContext from which the connector
139          * service should be obtained
140          * @return the connector service object (might be null)
141          */

142     public static ConnectorService getConnectorServiceBean(ConfigContext ctx) throws ConfigException{
143         return getConfigBean(ctx).getConnectorService();
144     }
145
146
147     /**
148      *
149      */

150     public static SecurityService getSecurityServiceBean(ConfigContext ctx) throws ConfigException {
151        Config cfg = ServerBeansFactory.getConfigBean(ctx);
152        return cfg.getSecurityService();
153     }
154
155     /**
156      *
157      */

158     public static TransactionService getTransactionServiceBean(ConfigContext ctx) throws ConfigException {
159        Config cfg = ServerBeansFactory.getConfigBean(ctx);
160        return cfg.getTransactionService();
161     }
162
163     /**
164      *
165      */

166     public static HttpService getHttpServiceBean(ConfigContext ctx) throws ConfigException {
167        Config cfg = ServerBeansFactory.getConfigBean(ctx);
168        HttpService httpService = cfg.getHttpService();
169        return httpService;
170     }
171
172     /**
173      *
174      */

175     public static JmsService getJmsServiceBean(ConfigContext ctx) throws ConfigException {
176        Config cfg = ServerBeansFactory.getConfigBean(ctx);
177        JmsService jmsService = cfg.getJmsService();
178        return jmsService;
179     }
180
181     /**
182      *
183      */

184     public static JmsHost getJmsHostBean(ConfigContext ctx) throws ConfigException {
185        JmsService jmsService = ServerBeansFactory.getJmsServiceBean(ctx);
186        JmsHost[] hosts = jmsService.getJmsHost();
187        return hosts[0];
188     }
189
190     /**
191      *
192      */

193     public static IiopService getIiopServiceBean(ConfigContext ctx) throws ConfigException {
194        Config cfg = ServerBeansFactory.getConfigBean(ctx);
195        IiopService iiopService = cfg.getIiopService();
196        return iiopService;
197     }
198
199     /**
200      *
201      */

202     public static JavaConfig getJavaConfigBean(ConfigContext ctx) throws ConfigException {
203        Config cfg = ServerBeansFactory.getConfigBean(ctx);
204        JavaConfig javaConfig = cfg.getJavaConfig();
205        return javaConfig;
206     }
207
208     /**
209      *
210      */

211     public static String JavaDoc getAdminServiceLogLevel(ConfigContext ctx) throws ConfigException {
212        Config cfg = ServerBeansFactory.getConfigBean(ctx);
213        LogService logService = cfg.getLogService();
214        return logService.getModuleLogLevels().getAdmin();
215     }
216
217     /**
218      *
219      */

220     public static String JavaDoc getEjbContainerLogLevel(ConfigContext ctx) throws ConfigException {
221        Config cfg = ServerBeansFactory.getConfigBean(ctx);
222        LogService logService = cfg.getLogService();
223        return logService.getModuleLogLevels().getEjbContainer();
224     }
225
226
227     /**
228      *
229      */

230     public static String JavaDoc getWebContainerLogLevel(ConfigContext ctx) throws ConfigException {
231        Config cfg = ServerBeansFactory.getConfigBean(ctx);
232        LogService logService = cfg.getLogService();
233        return logService.getModuleLogLevels().getWebContainer();
234     }
235
236
237     /**
238      *
239      */

240     public static String JavaDoc getMdbContainerLogLevel(ConfigContext ctx) throws ConfigException {
241        Config cfg = ServerBeansFactory.getConfigBean(ctx);
242        LogService logService = cfg.getLogService();
243        return logService.getModuleLogLevels().getMdbContainer();
244     }
245
246     /**
247      *
248      */

249     public static String JavaDoc getSecurityServiceLogLevel(ConfigContext ctx) throws ConfigException {
250        Config cfg = ServerBeansFactory.getConfigBean(ctx);
251        /*SecurityService securityService = cfg.getSecurityService();
252        return securityService.getLogLevel();
253         */

254        LogService logService = cfg.getLogService();
255        return logService.getModuleLogLevels().getSecurity();
256     }
257
258     /**
259      *
260      */

261     public static String JavaDoc getTransactionServiceLogLevel(ConfigContext ctx) throws ConfigException {
262        Config cfg = ServerBeansFactory.getConfigBean(ctx);
263        LogService logService = cfg.getLogService();
264        return logService.getModuleLogLevels().getJts();
265
266     }
267
268     /**
269      *
270      */

271     public static String JavaDoc getCorbaLogLevel(ConfigContext ctx) throws ConfigException {
272        Config cfg = ServerBeansFactory.getConfigBean(ctx);
273        LogService logService = cfg.getLogService();
274        return logService.getModuleLogLevels().getCorba();
275     }
276
277     /**
278      *
279      */

280     public static String JavaDoc getRootLogLevel(ConfigContext ctx) throws ConfigException {
281        Config cfg = ServerBeansFactory.getConfigBean(ctx);
282        LogService logService = cfg.getLogService();
283        return logService.getModuleLogLevels().getRoot();
284     }
285
286
287     /**
288      *
289      */

290     public static MdbContainer getMdbContainerBean(ConfigContext ctx) throws ConfigException {
291        Config cfg = ServerBeansFactory.getConfigBean(ctx);
292        MdbContainer container = cfg.getMdbContainer();
293        return container;
294     }
295
296     /**
297      *
298      */

299     public static DasConfig getDasConfigBean(ConfigContext ctx)
300                                                 throws ConfigException {
301        Config cfg = ServerBeansFactory.getConfigBean(ctx);
302        AdminService as = cfg.getAdminService();
303        DasConfig ds = null;
304        if(as != null) ds = as.getDasConfig();
305        return ds; // can be null;
306
}
307
308
309     /**
310      *
311      */

312     /*
313     public static ApplicationConfig getApplicationConfigBean(ConfigContext ctx)
314                                                 throws ConfigException {
315        Config cfg = ServerBeansFactory.getConfigBean(ctx);
316        ApplicationConfig appConfig = cfg.getApplicationConfig();
317        return appConfig;
318     }
319      */

320
321     /**
322      *
323      */

324     public static Applications getApplicationsBean(ConfigContext ctx)
325                                                 throws ConfigException {
326        return ServerBeansFactory.getDomainBean(ctx).getApplications();
327     }
328
329     /**
330      *
331      */

332     public static WebContainer getWebContainerBean(ConfigContext ctx)
333                                                 throws ConfigException {
334        Config cfg = ServerBeansFactory.getConfigBean(ctx);
335        WebContainer webContainer = cfg.getWebContainer();
336        return webContainer;
337     }
338
339     /**
340      * Returns the appropriate Server bean for an instance given an array
341      * of Server beans from the domain.xml config.
342      *
343      * @returns the Server bean for the instance
344      */

345     public static Server getServerBeanFromArray(Server[] serverArray,String JavaDoc serverName) {
346         for (int i=0;i<serverArray.length;i++) {
347             if (serverArray[i].getName().equals(serverName)) {
348                 return serverArray[i];
349             }
350         }
351         //if no match is made, then return first server bean in array
352
return serverArray[0];
353     }
354
355         /**
356            Indicate if the given {@link AdminObjectResource} resource is
357            referenced by one or more other elements within the given
358            {@link ConfigContext}.
359            @param res the {@link AdminObjectResource} (possibly) referenced resource
360            @param ctx the {@link ConfigContext}
361            @throws ConfigException if there's a problem in accessing
362            the config context.
363            @return true iff the resource is referenced within
364            the context
365         */

366     public static boolean isReferenced(final AdminObjectResource res, final ConfigContext ctx) throws ConfigException {
367         return isReferencedByResourceRef(res.getJndiName(), ctx);
368     }
369     
370         /**
371            Return a set containing all the elements that reference the
372            given {@link AdminObjectResource} resource within the given {@link
373            ConfigContext}
374            @param res the {@link AdminObjectResource}
375            @param ctx the {@link ConfigContext}
376            @throws ConfigException if there's a problem in accessing
377            teh config context.
378            @return a {@link java.util.Set} (never null, possibly
379            empty)
380         */

381     public static Set getReferers(final AdminObjectResource res, final ConfigContext ctx) throws ConfigException{
382         return getResourceRefsReferencing(res.getJndiName(), ctx);
383     }
384
385         /**
386            Indicate if the given {@link AppclientModule} element is
387            referenced by one or more other elements within the given
388            {@link ConfigContext}.
389            @param cm the {@link AppclientModule}
390            @param ctx the {@link ConfigContext}
391            @throws ConfigException if there's a problem in accessing
392            the config context.
393            @return true iff the appclient module is referenced within
394            the context
395         */

396     public static boolean isReferenced(final AppclientModule cm, final ConfigContext ctx) throws ConfigException {
397         return isReferencedByApplicationRef(cm.getName(), ctx);
398     }
399     
400         /**
401            Return a set containing all the elements that reference the
402            given {@link AppclientModule} within the given {@link
403            ConfigContext}
404            @param cm the {@link AppclientModule}
405            @param ctx the {@link ConfigContext}
406            @throws ConfigException if there's a problem in accessing
407            teh config context.
408            @return a {@link java.util.Set} (never null, possibly
409            empty)
410         */

411     public static Set getReferers(final AppclientModule cm, final ConfigContext ctx) throws ConfigException{
412         return getApplicationRefsReferencing(cm.getName(), ctx);
413     }
414
415         /**
416            Indicate if the given {@link Config} resource is
417            referenced by one or more other elements within the given
418            {@link ConfigContext}.
419            @param res the {@link Config} (possibly) referenced resource
420            @param ctx the {@link ConfigContext}
421            @throws ConfigException if there's a problem in accessing
422            the config context.
423            @return true iff the resource is referenced within
424            the context
425         */

426     public static boolean isReferenced(final Config res, final ConfigContext ctx) throws ConfigException {
427         if (null == res || null == ctx) { return false; }
428         for (final Iterator JavaDoc it = getServers(ctx).iterator(); it.hasNext();){
429             if (res.getName().equals(((Server) it.next()).getConfigRef())){
430                 return true;
431             }
432         }
433
434         for (final Iterator JavaDoc it = getClusters(ctx).iterator(); it.hasNext();){
435             if (res.getName().equals(((Cluster) it.next()).getConfigRef())){
436                 return true;
437             }
438         }
439
440         return false;
441     }
442     
443         /**
444            Return a set containing all the elements that reference the
445            given {@link Config} resource within the given {@link
446            ConfigContext}
447            @param res the {@link Config}
448            @param ctx the {@link ConfigContext}
449            @throws ConfigException if there's a problem in accessing
450            teh config context.
451            @return a {@link java.util.Set} (never null, possibly
452            empty) containing either {@link Server} or {@link Cluster} objects.
453         */

454     public static Set getReferers(final Config res, final ConfigContext ctx) throws ConfigException{
455         if (null == res || null == ctx) { return Collections.EMPTY_SET; }
456         final Set result = new HashSet();
457         for (final Iterator JavaDoc it = getServers(ctx).iterator(); it.hasNext();){
458             final Server el = (Server) it.next();
459             if (res.getName().equals(el.getConfigRef())){
460                 result.add(el);
461             }
462         }
463
464         for (final Iterator JavaDoc it = getClusters(ctx).iterator(); it.hasNext();){
465             final Cluster el = (Cluster) it.next();
466             if (res.getName().equals(el.getConfigRef())){
467                 result.add(el);
468             }
469         }
470
471         return result;
472     }
473
474         /**
475            Indicate if the given {@link NodeAgent} resource is
476            referenced by one or more other elements within the given
477            {@link ConfigContext}.
478            @param res the {@link NodeAgent} (possibly) referenced resource
479            @param ctx the {@link ConfigContext}
480            @throws ConfigException if there's a problem in accessing
481            the config context.
482            @return true iff the resource is referenced within
483            the context
484         */

485     public static boolean isReferenced(final NodeAgent res, final ConfigContext ctx) throws ConfigException {
486         if (null == res || null == ctx) { return false; }
487         for (final Iterator JavaDoc it = getServers(ctx).iterator(); it.hasNext();){
488             if (res.getName().equals(((Server) it.next()).getNodeAgentRef())){
489                 return true;
490             }
491         }
492
493         return false;
494     }
495     
496         /**
497            Return a set containing all the elements that reference the
498            given {@link NodeAgent} resource within the given {@link
499            ConfigContext}
500            @param res the {@link NodeAgent}
501            @param ctx the {@link ConfigContext}
502            @throws ConfigException if there's a problem in accessing
503            teh config context.
504            @return a {@link java.util.Set} (never null, possibly
505            empty) containing either {@link Server} or {@link Cluster} objects.
506         */

507     public static Set getReferers(final NodeAgent res, final ConfigContext ctx) throws ConfigException{
508         if (null == res || null == ctx) { return Collections.EMPTY_SET; }
509         final Set result = new HashSet();
510         for (final Iterator JavaDoc it = getServers(ctx).iterator(); it.hasNext();){
511             final Server el = (Server) it.next();
512             if (res.getName().equals(el.getNodeAgentRef())){
513                 result.add(el);
514             }
515         }
516         return result;
517     }
518
519         /**
520            Indicate if the given {@link ConnectorResource} resource is
521            referenced by one or more other elements within the given
522            {@link ConfigContext}.
523            @param res the {@link ConnectorResource} (possibly) referenced resource
524            @param ctx the {@link ConfigContext}
525            @throws ConfigException if there's a problem in accessing
526            the config context.
527            @return true iff the resource is referenced within
528            the context
529         */

530     public static boolean isReferenced(final ConnectorResource res, final ConfigContext ctx) throws ConfigException {
531         return isReferencedByResourceRef(res.getJndiName(), ctx);
532     }
533     
534         /**
535            Return a set containing all the elements that reference the
536            given {@link ConnectorResource} resource within the given {@link
537            ConfigContext}
538            @param res the {@link ConnectorResource}
539            @param ctx the {@link ConfigContext}
540            @throws ConfigException if there's a problem in accessing
541            teh config context.
542            @return a {@link java.util.Set} (never null, possibly
543            empty)
544         */

545     public static Set getReferers(final ConnectorResource res, final ConfigContext ctx) throws ConfigException{
546         return getResourceRefsReferencing(res.getJndiName(), ctx);
547     }
548
549         /**
550            Indicate if the given {@link Cluster} element is
551            referenced by one or more other elements within the given
552            {@link ConfigContext}.
553            @param cm the {@link Cluster}
554            @param ctx the {@link ConfigContext}
555            @throws ConfigException if there's a problem in accessing
556            the config context.
557            @return true iff the cluster is referenced within
558            the context
559         */

560     public static boolean isReferenced(final Cluster cm, final ConfigContext ctx) throws ConfigException {
561         if (null == cm || null == ctx) {return false;}
562
563         final Set refs = getClusterRefs(ctx);
564         if (refs.isEmpty()) { return false; }
565         for (final Iterator JavaDoc it = refs.iterator(); it.hasNext(); ){
566             if (cm.getName().equals(((ClusterRef) it.next()).getRef())){
567                 return true;
568             }
569         }
570         return false;
571     }
572     
573         /**
574            Return a set containing all the elements that reference the
575            given {@link Cluster} within the given {@link
576            ConfigContext}
577            @param cm the {@link Cluster}
578            @param ctx the {@link ConfigContext}
579            @throws ConfigException if there's a problem in accessing
580            teh config context.
581            @return a {@link java.util.Set} (never null, possibly
582            empty)
583         */

584     public static Set getReferers(final Cluster cm, final ConfigContext ctx) throws ConfigException{
585         if (null == cm || null == ctx) {return Collections.EMPTY_SET;}
586
587         final Set refs = getClusterRefs(ctx);
588         if (refs.isEmpty()) { return Collections.EMPTY_SET; }
589
590         final Set result = new HashSet();
591         for (final Iterator JavaDoc it = refs.iterator(); it.hasNext(); ){
592             final ClusterRef ref = (ClusterRef) it.next();
593             if (cm.getName().equals(ref.getRef())){
594                 result.add(ref);
595             }
596         }
597         return result;
598     }
599
600         /**
601            Indicate if the given {@link ConnectorConnectionPool} element is
602            referenced by one or more other elements within the given
603            {@link ConfigContext}.
604            @param cm the {@link ConnectorConnectionPool}
605            @param ctx the {@link ConfigContext}
606            @throws ConfigException if there's a problem in accessing
607            the config context.
608            @return true iff the cluster is referenced within
609            the context
610         */

611     public static boolean isReferenced(final ConnectorConnectionPool cm, final ConfigContext ctx) throws ConfigException {
612         if (null == cm || null == ctx) {return false;}
613
614         final Set crs = getConnectorResources(ctx);
615         if (crs.isEmpty()) { return false; }
616         for (final Iterator JavaDoc it = crs.iterator(); it.hasNext(); ){
617             if (cm.getName().equals(((ConnectorResource) it.next()).getPoolName())){
618                 return true;
619             }
620         }
621         return false;
622     }
623     
624         /**
625            Return a set containing all the elements that reference the
626            given {@link ConnectorConnectionPool} within the given {@link
627            ConfigContext}
628            @param cm the {@link ConnectorConnectionPool}
629            @param ctx the {@link ConfigContext}
630            @throws ConfigException if there's a problem in accessing
631            teh config context.
632            @return a {@link java.util.Set} (never null, possibly
633            empty)
634         */

635     public static Set getReferers(final ConnectorConnectionPool cm, final ConfigContext ctx) throws ConfigException{
636         if (null == cm || null == ctx) {return Collections.EMPTY_SET;}
637
638         final Set crs = getConnectorResources(ctx);
639         if (crs.isEmpty()) { return Collections.EMPTY_SET; }
640
641         final Set result = new HashSet();
642         for (final Iterator JavaDoc it = crs.iterator(); it.hasNext(); ){
643             final ConnectorResource cr = (ConnectorResource) it.next();
644             if (cm.getName().equals(cr.getPoolName())){
645                 result.add(cr);
646             }
647         }
648         return result;
649     }
650
651         /**
652            Indicate if the given {@link ConnectorModule} element is
653            referenced by one or more other elements within the given
654            {@link ConfigContext}.
655            @param cm the {@link ConnectorModule}
656            @param ctx the {@link ConfigContext}
657            @throws ConfigException if there's a problem in accessing
658            the config context.
659            @return true iff the connector module is referenced within
660            the context
661         */

662     public static boolean isReferenced(final ConnectorModule cm, final ConfigContext ctx) throws ConfigException {
663         return isReferencedByApplicationRef(cm.getName(), ctx);
664     }
665     
666         /**
667            Return a set containing all the elements that reference the
668            given {@link ConnectorModule} within the given {@link
669            ConfigContext}
670            @param cm the {@link ConnectorModule}
671            @param ctx the {@link ConfigContext}
672            @throws ConfigException if there's a problem in accessing
673            teh config context.
674            @return a {@link java.util.Set} (never null, possibly
675            empty)
676         */

677     public static Set getReferers(final ConnectorModule cm, final ConfigContext ctx) throws ConfigException{
678         return getApplicationRefsReferencing(cm.getName(), ctx);
679     }
680
681         /**
682            Indicate if the given {@link CustomResource} resource is
683            referenced by one or more other elements within the given
684            {@link ConfigContext}.
685            @param res the {@link CustomResource} (possibly) referenced resource
686            @param ctx the {@link ConfigContext}
687            @throws ConfigException if there's a problem in accessing
688            the config context.
689            @return true iff the resource is referenced within
690            the context
691         */

692     public static boolean isReferenced(final CustomResource res, final ConfigContext ctx) throws ConfigException {
693         return isReferencedByResourceRef(res.getJndiName(), ctx);
694     }
695     
696         /**
697            Return a set containing all the elements that reference the
698            given {@link CustomResource} resource within the given {@link
699            ConfigContext}
700            @param res the {@link CustomResource}
701            @param ctx the {@link ConfigContext}
702            @throws ConfigException if there's a problem in accessing
703            teh config context.
704            @return a {@link java.util.Set} (never null, possibly
705            empty)
706         */

707     public static Set getReferers(final CustomResource res, final ConfigContext ctx) throws ConfigException{
708         return getResourceRefsReferencing(res.getJndiName(), ctx);
709     }
710         /**
711            Indicate if the given {@link EjbModule} element is
712            referenced by one or more other elements within the given
713            {@link ConfigContext}.
714            @param cm the {@link EjbModule}
715            @param ctx the {@link ConfigContext}
716            @throws ConfigException if there's a problem in accessing
717            the config context.
718            @return true iff the ejb module is referenced within
719            the context
720         */

721     public static boolean isReferenced(final EjbModule cm, final ConfigContext ctx) throws ConfigException {
722         return isReferencedByApplicationRef(cm.getName(), ctx);
723     }
724     
725         /**
726            Return a set containing all the elements that reference the
727            given {@link EjbModule} within the given {@link
728            ConfigContext}
729            @param cm the {@link EjbModule}
730            @param ctx the {@link ConfigContext}
731            @throws ConfigException if there's a problem in accessing
732            teh config context.
733            @return a {@link java.util.Set} (never null, possibly
734            empty)
735         */

736     public static Set getReferers(final EjbModule cm, final ConfigContext ctx) throws ConfigException{
737         return getApplicationRefsReferencing(cm.getName(), ctx);
738     }
739
740         /**
741            Indicate if the given {@link ExternalJndiResource} resource is
742            referenced by one or more other elements within the given
743            {@link ConfigContext}.
744            @param res the {@link ExternalJndiResource} (possibly) referenced resource
745            @param ctx the {@link ConfigContext}
746            @throws ConfigException if there's a problem in accessing
747            the config context.
748            @return true iff the resource is referenced within
749            the context
750         */

751     public static boolean isReferenced(final ExternalJndiResource res, final ConfigContext ctx) throws ConfigException {
752         return isReferencedByResourceRef(res.getJndiName(), ctx);
753     }
754     
755         /**
756            Return a set containing all the elements that reference the
757            given {@link ExternalJndiResource} resource within the given {@link
758            ConfigContext}
759            @param res the {@link ExternalJndiResource}
760            @param ctx the {@link ConfigContext}
761            @throws ConfigException if there's a problem in accessing
762            teh config context.
763            @return a {@link java.util.Set} (never null, possibly
764            empty)
765         */

766     public static Set getReferers(final ExternalJndiResource res, final ConfigContext ctx) throws ConfigException{
767         return getResourceRefsReferencing(res.getJndiName(), ctx);
768     }
769         /**
770            Indicate if the given {@link J2eeApplication} element is
771            referenced by one or more other elements within the given
772            {@link ConfigContext}.
773            @param cm the {@link J2eeApplication}
774            @param ctx the {@link ConfigContext}
775            @throws ConfigException if there's a problem in accessing
776            the config context.
777            @return true iff the j2ee application is referenced within
778            the context
779         */

780     public static boolean isReferenced(final J2eeApplication cm, final ConfigContext ctx) throws ConfigException {
781         return isReferencedByApplicationRef(cm.getName(), ctx);
782     }
783     
784         /**
785            Return a set containing all the elements that reference the
786            given {@link J2eeApplication} within the given {@link
787            ConfigContext}
788            @param cm the {@link J2eeApplication}
789            @param ctx the {@link ConfigContext}
790            @throws ConfigException if there's a problem in accessing
791            teh config context.
792            @return a {@link java.util.Set} (never null, possibly
793            empty)
794         */

795     public static Set getReferers(final J2eeApplication cm, final ConfigContext ctx) throws ConfigException{
796         return getApplicationRefsReferencing(cm.getName(), ctx);
797     }
798
799         /**
800            Indicate if the given {@link JdbcResource} resource is
801            referenced by one or more other elements within the given
802            {@link ConfigContext}.
803            @param res the {@link JdbcResource} (possibly) referenced resource
804            @param ctx the {@link ConfigContext}
805            @throws ConfigException if there's a problem in accessing
806            the config context.
807            @return true iff the resource is referenced within
808            the context
809         */

810     public static boolean isReferenced(final JdbcResource res, final ConfigContext ctx) throws ConfigException {
811         return isReferencedByResourceRef(res.getJndiName(), ctx);
812     }
813     
814         /**
815            Return a set containing all the elements that reference the
816            given {@link JdbcResource} resource within the given {@link
817            ConfigContext}
818            @param res the {@link JdbcResource}
819            @param ctx the {@link ConfigContext}
820            @throws ConfigException if there's a problem in accessing
821            teh config context.
822            @return a {@link java.util.Set} (never null, possibly
823            empty)
824         */

825     public static Set getReferers(final JdbcResource res, final ConfigContext ctx) throws ConfigException{
826         return getResourceRefsReferencing(res.getJndiName(), ctx);
827     }
828         /**
829            Indicate if the given {@link LifecycleModule} element is
830            referenced by one or more other elements within the given
831            {@link ConfigContext}.
832            @param cm the {@link LifecycleModule}
833            @param ctx the {@link ConfigContext}
834            @throws ConfigException if there's a problem in accessing
835            the config context.
836            @return true iff the lifecycle module is referenced within
837            the context
838         */

839     public static boolean isReferenced(final LifecycleModule cm, final ConfigContext ctx) throws ConfigException {
840         return isReferencedByApplicationRef(cm.getName(), ctx);
841     }
842     
843         /**
844            Return a set containing all the elements that reference the
845            given {@link LifecycleModule} within the given {@link
846            ConfigContext}
847            @param cm the {@link LifecycleModule}
848            @param ctx the {@link ConfigContext}
849            @throws ConfigException if there's a problem in accessing
850            teh config context.
851            @return a {@link java.util.Set} (never null, possibly
852            empty)
853         */

854     public static Set getReferers(final LifecycleModule cm, final ConfigContext ctx) throws ConfigException{
855         return getApplicationRefsReferencing(cm.getName(), ctx);
856     }
857         /**
858            Indicate if the given {@link MailResource} resource is
859            referenced by one or more other elements within the given
860            {@link ConfigContext}.
861            @param res the {@link MailResource} (possibly) referenced resource
862            @param ctx the {@link ConfigContext}
863            @throws ConfigException if there's a problem in accessing
864            the config context.
865            @return true iff the resource is referenced within
866            the context
867         */

868     public static boolean isReferenced(final MailResource res, final ConfigContext ctx) throws ConfigException {
869         return isReferencedByResourceRef(res.getJndiName(), ctx);
870     }
871     
872         /**
873            Return a set containing all the elements that reference the
874            given {@link MailResource} resource within the given {@link
875            ConfigContext}
876            @param res the {@link MailResource}
877            @param ctx the {@link ConfigContext}
878            @throws ConfigException if there's a problem in accessing
879            teh config context.
880            @return a {@link java.util.Set} (never null, possibly
881            empty)
882         */

883     public static Set getReferers(final MailResource res, final ConfigContext ctx) throws ConfigException{
884         return getResourceRefsReferencing(res.getJndiName(), ctx);
885     }
886         /**
887            Indicate if the given {@link PersistenceManagerFactoryResource} resource is
888            referenced by one or more other elements within the given
889            {@link ConfigContext}.
890            @param res the {@link PersistenceManagerFactoryResource} (possibly) referenced resource
891            @param ctx the {@link ConfigContext}
892            @throws ConfigException if there's a problem in accessing
893            the config context.
894            @return true iff the resource is referenced within
895            the context
896         */

897     public static boolean isReferenced(final PersistenceManagerFactoryResource res, final ConfigContext ctx) throws ConfigException {
898         return isReferencedByResourceRef(res.getJndiName(), ctx);
899     }
900     
901         /**
902            Return a set containing all the elements that reference the
903            given {@link PersistenceManagerFactoryResource} resource within the given {@link
904            ConfigContext}
905            @param res the {@link PersistenceManagerFactoryResource}
906            @param ctx the {@link ConfigContext}
907            @throws ConfigException if there's a problem in accessing
908            teh config context.
909            @return a {@link java.util.Set} (never null, possibly
910            empty)
911         */

912     public static Set getReferers(final PersistenceManagerFactoryResource res, final ConfigContext ctx) throws ConfigException{
913         return getResourceRefsReferencing(res.getJndiName(), ctx);
914     }
915
916         /**
917            Indicate if the given {@link ResourceAdapterConfig} resource is
918            referenced by one or more other elements within the given
919            {@link ConfigContext}.
920            @param res the {@link ResourceAdapterConfig} (possibly) referenced resource
921            @param ctx the {@link ConfigContext}
922            @throws ConfigException if there's a problem in accessing
923            the config context.
924            @return true iff the resource is referenced within
925            the context
926         */

927     public static boolean isReferenced(final ResourceAdapterConfig res, final ConfigContext ctx) throws ConfigException {
928         return isReferencedByResourceRef(res.getResourceAdapterName(), ctx);
929     }
930     
931         /**
932            Return a set containing all the elements that reference the
933            given {@link ResourceAdapterConfig} resource within the given {@link
934            ConfigContext}
935            @param res the {@link ResourceAdapterConfig}
936            @param ctx the {@link ConfigContext}
937            @throws ConfigException if there's a problem in accessing
938            teh config context.
939            @return a {@link java.util.Set} (never null, possibly
940            empty)
941         */

942     public static Set getReferers(final ResourceAdapterConfig res, final ConfigContext ctx) throws ConfigException{
943         return getResourceRefsReferencing(res.getResourceAdapterName(), ctx);
944     }
945     
946         /**
947            Indicate if the given {@link Server} resource is
948            referenced by one or more other elements within the given
949            {@link ConfigContext}.
950            @param res the {@link Server} (possibly) referenced resource
951            @param ctx the {@link ConfigContext}
952            @throws ConfigException if there's a problem in accessing
953            the config context.
954            @return true iff the resource is referenced within
955            the context
956         */

957     public static boolean isReferenced(final Server res, final ConfigContext ctx) throws ConfigException {
958         return isReferencedByServerRef(res.getName(), ctx);
959     }
960     
961         /**
962            Return a set containing all the elements that reference the
963            given {@link Server} resource within the given {@link
964            ConfigContext}
965            @param res the {@link Server}
966            @param ctx the {@link ConfigContext}
967            @throws ConfigException if there's a problem in accessing
968            teh config context.
969            @return a {@link java.util.Set} (never null, possibly
970            empty)
971         */

972     public static Set getReferers(final Server res, final ConfigContext ctx) throws ConfigException{
973         return getServerRefsReferencing(res.getName(), ctx);
974     }
975         /**
976            Indicate if the given {@link WebModule} element is
977            referenced by one or more other elements within the given
978            {@link ConfigContext}.
979            @param cm the {@link WebModule}
980            @param ctx the {@link ConfigContext}
981            @throws ConfigException if there's a problem in accessing
982            the config context.
983            @return true iff the web module is referenced within
984            the context
985         */

986     public static boolean isReferenced(final WebModule cm, final ConfigContext ctx) throws ConfigException {
987         for (final Iterator JavaDoc it = getVirtualServers(ctx).iterator(); it.hasNext(); ){
988             if (cm.getName().equals(((VirtualServer) it.next()).getDefaultWebModule())){
989                 return true;
990             }
991         }
992         
993         return isReferencedByApplicationRef(cm.getName(), ctx);
994     }
995     
996         /**
997            Return a set containing all the elements that reference the
998            given {@link WebModule} within the given {@link
999            ConfigContext}
1000           @param cm the {@link WebModule}
1001           @param ctx the {@link ConfigContext}
1002           @throws ConfigException if there's a problem in accessing
1003           teh config context.
1004           @return a {@link java.util.Set} (never null, possibly
1005           empty)
1006        */

1007    public static Set getReferers(final WebModule cm, final ConfigContext ctx) throws ConfigException{
1008        return getApplicationRefsReferencing(cm.getName(), ctx);
1009    }
1010    
1011    private static boolean isReferencedByApplicationRef(final String JavaDoc name, final ConfigContext ctx) throws ConfigException {
1012        for (final Iterator JavaDoc it = getApplicationRefs(ctx).iterator(); it.hasNext(); ){
1013            if (name.equals(((ApplicationRef) it.next()).getRef())){
1014                return true;
1015            }
1016        }
1017        return false;
1018    }
1019    
1020    private static Set getApplicationRefsReferencing(final String JavaDoc name, final ConfigContext ctx) throws ConfigException {
1021        final Set refers = new HashSet();
1022        for (final Iterator JavaDoc it = getApplicationRefs(ctx).iterator(); it.hasNext(); ){
1023            final ApplicationRef ar = (ApplicationRef) it.next();
1024            if (name.equals(ar.getRef())){
1025                refers.add(ar);
1026            }
1027        }
1028        return refers;
1029    }
1030            
1031        
1032        /**
1033           Get all the application ref elements from the given
1034           configuration context wherever they are located.
1035           @param ctx the context from which the application refs are required.
1036           @return a {@link java.util.Set} of {@link ApplicationRef}
1037           objects from the given context. Never returns a null.
1038        */

1039    public static Set getApplicationRefs(final ConfigContext ctx) throws ConfigException {
1040        final Set refs = new HashSet();
1041        for (final Iterator JavaDoc it = getServers(ctx).iterator(); it.hasNext();){
1042            refs.addAll(Arrays.asList(((Server) it.next()).getApplicationRef()));
1043        }
1044
1045        for (final Iterator JavaDoc it = getClusters(ctx).iterator(); it.hasNext();){
1046            refs.addAll(Arrays.asList(((Cluster) it.next()).getApplicationRef()));
1047        }
1048
1049        return refs;
1050    }
1051
1052    private static boolean isReferencedByResourceRef(final String JavaDoc name, final ConfigContext ctx) throws ConfigException {
1053        for (final Iterator JavaDoc it = getResourceRefs(ctx).iterator(); it.hasNext(); ){
1054            if (name.equals(((ResourceRef) it.next()).getRef())){
1055                return true;
1056            }
1057        }
1058        return false;
1059    }
1060    
1061    private static Set getResourceRefsReferencing(final String JavaDoc name, final ConfigContext ctx) throws ConfigException {
1062        final Set refers = new HashSet();
1063        for (final Iterator JavaDoc it = getResourceRefs(ctx).iterator(); it.hasNext(); ){
1064            final ResourceRef ar = (ResourceRef) it.next();
1065            if (name.equals(ar.getRef())){
1066                refers.add(ar);
1067            }
1068        }
1069        return refers;
1070    }
1071    private static boolean isReferencedByServerRef(final String JavaDoc name, final ConfigContext ctx) throws ConfigException {
1072        for (final Iterator JavaDoc it = getServerRefs(ctx).iterator(); it.hasNext(); ){
1073            if (name.equals(((ServerRef) it.next()).getRef())){
1074                return true;
1075            }
1076        }
1077        return false;
1078    }
1079    
1080    private static Set getServerRefsReferencing(final String JavaDoc name, final ConfigContext ctx) throws ConfigException {
1081        final Set refers = new HashSet();
1082        for (final Iterator JavaDoc it = getServerRefs(ctx).iterator(); it.hasNext(); ){
1083            final ServerRef ar = (ServerRef) it.next();
1084            if (name.equals(ar.getRef())){
1085                refers.add(ar);
1086            }
1087        }
1088        return refers;
1089    }
1090            
1091        
1092        /**
1093           Get all the resource ref elements from the given
1094           configuration context wherever they are located.
1095           @param ctx the context from which the resource refs are required.
1096           @return a {@link java.util.Set} of {@link ResourceRef}
1097           objects from the given context. Never returns a null.
1098        */

1099    public static Set getResourceRefs(final ConfigContext ctx) throws ConfigException {
1100        final Set refs = new HashSet();
1101        for (final Iterator JavaDoc it = getServers(ctx).iterator(); it.hasNext();){
1102            refs.addAll(Arrays.asList(((Server) it.next()).getResourceRef()));
1103        }
1104
1105        for (final Iterator JavaDoc it = getClusters(ctx).iterator(); it.hasNext();){
1106            refs.addAll(Arrays.asList(((Cluster) it.next()).getResourceRef()));
1107        }
1108
1109        return refs;
1110    }
1111
1112        /**
1113           Get all the server ref elements from the given
1114           configuration context wherever they are located.
1115           @param ctx the context from which the server refs are required.
1116           @return a {@link java.util.Set} of {@link ServerRef}
1117           objects from the given context. Never returns a null.
1118        */

1119    public static Set getServerRefs(final ConfigContext ctx) throws ConfigException {
1120        final Set refs = new HashSet();
1121        for (final Iterator JavaDoc it = getLbConfigs(ctx).iterator(); it.hasNext();){
1122            refs.addAll(Arrays.asList(((LbConfig) it.next()).getServerRef()));
1123        }
1124        
1125        for (final Iterator JavaDoc it = getClusters(ctx).iterator(); it.hasNext();){
1126            refs.addAll(Arrays.asList(((Cluster) it.next()).getServerRef()));
1127        }
1128        return refs;
1129    }
1130
1131        /**
1132           Get a set containing all the configs within the given
1133           context.
1134           @param ctx the context to be searched.
1135           @return a non-null, possibly empty {@link java.util.Set}
1136           containing any and all {@link LbConfig} instances found
1137           within the given context. All objects in this list
1138           will be LbConfig objects.
1139           @throws ConfigContext if there's a problem in accessing the
1140           context.
1141        */

1142    public static Set getConfigs(final ConfigContext ctx) throws ConfigException {
1143        if (null == ctx) {
1144            return Collections.EMPTY_SET;
1145        }
1146        final Domain dom = (Domain) ctx.getRootConfigBean();
1147        if (null == dom){
1148            return Collections.EMPTY_SET;
1149        }
1150        final Configs configs = dom.getConfigs();
1151        if (null == configs || null == configs.getConfig()){
1152            return Collections.EMPTY_SET;
1153        }
1154        final Set result = new HashSet();
1155        result.addAll(Arrays.asList(configs.getConfig()));
1156        return result;
1157    }
1158        
1159        /**
1160           Get a set containing all the clusters within the given
1161           context.
1162           @param ctx the context to be searched.
1163           @return a non-null, possibly empty {@link java.util.Set}
1164           containing any and all {@link Cluster} instances found
1165           within the given context. All objects in this list
1166           will be Cluster objects.
1167           @throws ConfigContext if there's a problem in accessing the
1168           context.
1169        */

1170    public static Set getClusters(final ConfigContext ctx) throws ConfigException {
1171        if (null == ctx) {
1172            return Collections.EMPTY_SET;
1173        }
1174        final Domain dom = (Domain) ctx.getRootConfigBean();
1175        if (null == dom){
1176            return Collections.EMPTY_SET;
1177        }
1178        final Clusters clusters = dom.getClusters();
1179        if (null == clusters || null == clusters.getCluster()){
1180            return Collections.EMPTY_SET;
1181        }
1182        final Set result = new HashSet();
1183        result.addAll(Arrays.asList(clusters.getCluster()));
1184        return result;
1185    }
1186        /**
1187           Get a set containing all the servers within the given
1188           context.
1189           @param ctx the context to be searched.
1190           @return a non-null, possibly empty {@link java.util.Set}
1191           containing any and all {@link Server} instances found
1192           within the given context. All objects in this list
1193           will be LbServer objects.
1194           @throws ServerContext if there's a problem in accessing the
1195           context.
1196        */

1197    public static Set getServers(final ConfigContext ctx) throws ConfigException {
1198        if (null == ctx) {
1199            return Collections.EMPTY_SET;
1200        }
1201        final Domain dom = (Domain) ctx.getRootConfigBean();
1202        if (null == dom){
1203            return Collections.EMPTY_SET;
1204        }
1205        final Servers servers = dom.getServers();
1206        if (null == servers || null == servers.getServer()){
1207            return Collections.EMPTY_SET;
1208        }
1209        final Set result = new HashSet();
1210        result.addAll(Arrays.asList(servers.getServer()));
1211        return result;
1212    }
1213        
1214        /**
1215           Get a set containing all the virtual servers within the given
1216           context.
1217           @param ctx the context to be searched.
1218           @return a non-null, possibly empty {@link java.util.Set}
1219           containing any and all {@link VirtualServer} instances found
1220           within the given context. All objects in this list
1221           will be VirtualServer objects.
1222           @throws ConfigContext if there's a problem in accessing the
1223           context.
1224        */

1225    public static Set getVirtualServers(final ConfigContext ctx) throws ConfigException {
1226        final Set result = new HashSet();
1227        for (final Iterator JavaDoc it = getHttpServices(ctx).iterator(); it.hasNext(); ){
1228            final ConfigBean [] vs = ((HttpService) it.next()).getVirtualServer();
1229            if (null != vs){
1230                result.addAll(Arrays.asList(vs));
1231            }
1232        }
1233        return result;
1234    }
1235        /**
1236           Get a set containing all the http services within the given
1237           context.
1238           @param ctx the context to be searched.
1239           @return a non-null, possibly empty {@link java.util.Set}
1240           containing any and all {@link HttpService} instances found
1241           within the given context. All objects in this list
1242           will be VirtualServer objects.
1243           @throws ConfigContext if there's a problem in accessing the
1244           context.
1245        */

1246    public static Set getHttpServices(final ConfigContext ctx) throws ConfigException {
1247        final Set result = new HashSet();
1248        for (final Iterator JavaDoc it = getConfigs(ctx).iterator(); it.hasNext(); ){
1249            result.add(((Config) it.next()).getHttpService());
1250        }
1251        return result;
1252    }
1253        /**
1254           Get all the cluster refs in the given context, wherever
1255           they are located.
1256           @param ctx the {@link ConfigContext} in which the search is
1257           to be conducted.
1258           @return a {@link java.util.Set} (never null, possibly
1259           empty) containing all {@link ClusterRef} beans within the
1260           given context.
1261           @throws ConfigException if there's a problem accesing the context.
1262         */

1263    public static Set getClusterRefs(final ConfigContext ctx) throws ConfigException {
1264        final Set lbconfigs = getLbConfigs(ctx);
1265        if (lbconfigs.isEmpty()){
1266            return Collections.EMPTY_SET;
1267        }
1268        final Set result = new HashSet();
1269        for (final Iterator JavaDoc it = lbconfigs.iterator(); it.hasNext();){
1270            final ConfigBean [] cr = ((LbConfig) it.next()).getClusterRef();
1271            if (null != cr){
1272                result.addAll(Arrays.asList(cr));
1273            }
1274        }
1275        return result;
1276    }
1277
1278        /**
1279           Get a set containing all the connector resources within the
1280           given context.
1281           @param ctx the context to be searched.
1282           @return a non-null, possibly empty {@link java.util.Set}
1283           containing any and all {@link ConnectorResource} instances found
1284           within the given context. All objects in this list
1285           will be ConnectorResource objects.
1286           @throws ConfigContext if there's a problem in accessing the
1287           context.
1288        */

1289
1290    public static Set getConnectorResources(final ConfigContext ctx) throws ConfigException {
1291        final Set result = new HashSet();
1292        final ConfigBean [] cr = getResources(ctx, ServerTags.CONNECTOR_RESOURCE);
1293        if (null != cr){
1294            result.addAll(Arrays.asList(cr));
1295        }
1296        return result;
1297    }
1298    
1299
1300        /**
1301           Get a set containing all the lbconfigs within the given
1302           context.
1303           @param ctx the context to be searched.
1304           @return a non-null, possibly empty {@link java.util.Set}
1305           containing any and all {@link LbConfig} instances found
1306           within the given context. All objects in this list
1307           will be LbConfig objects.
1308           @throws ConfigContext if there's a problem in accessing the
1309           context.
1310        */

1311    public static Set getLbConfigs(final ConfigContext ctx) throws ConfigException {
1312        if (null == ctx) {
1313            return Collections.EMPTY_SET;
1314        }
1315        final Domain dom = (Domain) ctx.getRootConfigBean();
1316        if (null == dom){
1317            return Collections.EMPTY_SET;
1318        }
1319        final LbConfigs lbconfigs = dom.getLbConfigs();
1320        if (null == lbconfigs || null == lbconfigs.getLbConfig()){
1321            return Collections.EMPTY_SET;
1322        }
1323        final Set result = new HashSet();
1324        result.addAll(Arrays.asList(lbconfigs.getLbConfig()));
1325        return result;
1326    }
1327        
1328                    
1329   /**
1330    * Get virtual server given an Application
1331    * Virtual servers are maintained in the reference contained
1332    * in Server element. First, we need to find the server
1333    * and then get the virtual server from the correct reference
1334    *
1335    * @param ctx ConfigContext
1336    * @param appName Name of the app to get vs
1337    *
1338    * @return virtual servers as a string (separated by space or comma)
1339    *
1340    */

1341    public static String JavaDoc getVirtualServersByAppName(ConfigContext ctx,
1342                String JavaDoc appName)
1343                    throws ConfigException {
1344
1345    ApplicationRef ar = getServerBean(ctx).getApplicationRefByRef(appName);
1346    if (ar == null) return null; //fixme should I throw an exception??
1347

1348    return ar.getVirtualServers();
1349
1350
1351                                            /*
1352         VirtualServer[] vsList = getConfigBean(ctx).getHttpService().getVirtualServer();
1353
1354         if (vsList == null) return null;
1355
1356         String ret ="";
1357         for(int i=0;i< vsList.length; i++) {
1358             if(!ret.equals("")) {
1359                 ret+="'";
1360             }
1361            ret += vsList[i].getId();
1362         }
1363
1364         return ret;
1365                                             */

1366    }
1367    /** Method to get all the MBean <i> definitions </i> from the domain.xml. These are just the MBeans that are available in
1368     * the given domain. No consideration of references. This is to make sure that <i> names of applications, modules, mbeans
1369     * are unique in the domain, even if they're referenced from different servers. For example, following structure is to be deemed
1370     * invalid
1371     * &lt;applications&gt;
1372     * &lt;mbean name="foo"&gt;
1373     * &lt;mbean name="foo"&gt;
1374     * &lt;/applications&gt;
1375     */

1376    public static List<Mbean> getAllMBeanDefinitions(final ConfigContext cc) throws ConfigException {
1377        final Domain d = ServerBeansFactory.getDomainBean(cc);
1378        final Applications as = d.getApplications();
1379        final Mbean[] ma = as.getMbean();
1380        final List<Mbean> ml = Arrays.asList(ma);
1381        return ( Collections.unmodifiableList(ml) );
1382    }
1383    
1384    /** Method to get mbean definitions from domain.xml that are referenced from a given
1385     * server name.
1386     * @param cc a config context
1387     * @param sn String representing the <i> name </i> of the server
1388     * @throws ConfigException
1389     */

1390    public static List<Mbean> getReferencedMBeans(final ConfigContext cc, final String JavaDoc sn) throws ConfigException {
1391        final List<Mbean> em = new ArrayList<Mbean>();
1392        final Domain d = ServerBeansFactory.getDomainBean(cc);
1393        final Server[] ss = d.getServers().getServer();
1394        for(Server a : ss) {
1395            if (sn.equals(a.getName())) {
1396                ApplicationRef[] ars = a.getApplicationRef();
1397                for (ApplicationRef ar : ars) {
1398                    final Mbean tmp = getMBeanDefinition(cc, ar.getRef());
1399                    if (tmp != null)
1400                        em.add(tmp);
1401                }
1402            }
1403        }
1404        return ( Collections.unmodifiableList(em) );
1405    }
1406    
1407    public static boolean isReferencedMBean(final ConfigContext cc, final String JavaDoc sn, final String JavaDoc mn) throws ConfigException {
1408        final List<Mbean> rm = getReferencedMBeans(cc, sn);
1409        boolean refd = false;
1410        for (Mbean m : rm) {
1411            if (m.getName().equals(mn)) {
1412                refd = true;
1413                break;
1414            }
1415        }
1416        return ( refd );
1417    }
1418
1419    /**
1420     * <ul>
1421     * <li>Returns true if all of the instances in the given cluster reference the given custom mbean
1422     * <li>Return false if all of the instances in the given cluster <i>do not</i> reference the given custom mbean
1423     * or if there are no instances in the cluster
1424     * <li>throw a ConfigException if some instances have the ref and some do not.
1425     * @param cc the config context
1426     * @param cn the cluster's name
1427     * @param mn the mbean's name
1428     * @throws com.sun.enterprise.config.ConfigException see above
1429     * @return see above
1430     */

1431    
1432    public static boolean isReferencedMBeanInCluster(final ConfigContext cc, final String JavaDoc cn, final String JavaDoc mn) throws ConfigException {
1433        // by definition a cluster has no referenced mbeans -- only instances in the cluster do.
1434
assert cc != null && cn != null && mn != null;
1435        // in case asserts are off, return false. Normally I'd log it but this
1436
// file has no logger and I'm just visiting...
1437
if(cc == null || cn == null || mn == null)
1438            return false;
1439        
1440        final Server[] ss = ServerHelper.getServersInCluster(cc, cn);
1441
1442        if(ss == null || ss.length < 1)
1443            return false;
1444
1445        final boolean[] isrefs = new boolean[ss.length];
1446        
1447        for(int i = 0; i < ss.length; i++)
1448        {
1449            isrefs[i] = isReferencedMBean(cc, ss[i].getName(), mn);
1450        }
1451
1452        // see if they are not all exactly the same -- simply compare the first
1453
// with all the others.
1454
for(int i = 1; i < ss.length; i++)
1455        {
1456            if(isrefs[i] != isrefs[0])
1457            {
1458                String JavaDoc messy = prepareMessyStringMessage(ss, isrefs);
1459                String JavaDoc msg = StringManager.getManager(ServerBeansFactory.class).getString("CorruptClusterConfig",
1460                        new Object JavaDoc[] { cn, mn, messy} );
1461                throw new ConfigException(msg);
1462            }
1463        }
1464        return isrefs[0]; // they are all the same!
1465
}
1466    
1467    /** Method to get the completely enabled mbean definitions from the domain.xml, for a given server instance.
1468     * Note that an MBean is truly enabled if and only if it is enabled in its definition and a reference to its
1469     * definition from a server instance is enabled too.
1470     * @param cc server's ConfigContext
1471     * @param sn String representing name of a server instance
1472     * @return A List of Mbean elements. Never returns a null. Returns empty List in case no mbean is enabled
1473     * @throws ConfigException
1474     */

1475    public static List<Mbean> getFullyEnabledMBeans(final ConfigContext cc, final String JavaDoc sn) throws ConfigException {
1476        //while reaching here, if cc or sn are null, we are already in trouble
1477
final List<Mbean> em = new ArrayList<Mbean>();
1478        final Domain d = ServerBeansFactory.getDomainBean(cc);
1479        final Server[] ss = d.getServers().getServer();
1480        for(Server a : ss) {
1481            if (sn.equals(a.getName())) {
1482                ApplicationRef[] ars = a.getApplicationRef();
1483                for (ApplicationRef ar : ars) {
1484                    if (ar.isEnabled()) { // reference itself is enabled, this is generally not required, but to take care of some weird cases
1485
final Mbean tmp = getMBeanDefinition(cc, ar.getRef());
1486                        if (tmp != null && tmp.isEnabled()) { // the definition is also enabled
1487
//System.out.println(tmp.getName() + " will be loaded");
1488
em.add(tmp);
1489                        }
1490                    }
1491                }
1492            }
1493        }
1494        return ( Collections.unmodifiableList(em) );
1495    }
1496    
1497    public static Mbean getMBeanDefinition(final ConfigContext cc, final String JavaDoc n) throws ConfigException {
1498        final Domain d = ServerBeansFactory.getDomainBean(cc);
1499        final Applications a = d.getApplications();
1500        final Mbean[] ms = a.getMbean();
1501        for (Mbean m : ms) {
1502            if (n.equals(m.getName())) {
1503                return ( m );
1504            }
1505        }
1506        return ( null );
1507    }
1508    
1509    /** Method to get the completely enabled "user-defined" mbean definitions from the domain.xml, for a given server instance.
1510     * Note that an MBean is truly enabled if and only if it is enabled in its definition and a reference to its
1511     * definition from a server instance is enabled too.
1512     * @param cc server's ConfigContext
1513     * @param sn String representing name of a server instance
1514     * @return A List of Mbean elements. Never returns a null. Returns empty List in case no mbean is enabled
1515     * @throws ConfigException
1516     * @see #getFullyEnabledMBeans
1517     */

1518    public static List<Mbean> getFullyEnabledUserDefinedMBeans(final ConfigContext cc, final String JavaDoc sn) throws ConfigException {
1519        final List<Mbean> em = ServerBeansFactory.getFullyEnabledMBeans(cc, sn);
1520        final List<Mbean> uem = new ArrayList<Mbean>();
1521        for (Mbean m : em) {
1522           assert (m.isEnabled());
1523           if (IAdminConstants.USER.equals(m.getObjectType())) {
1524               uem.add(m);
1525           }
1526        }
1527        return ( Collections.unmodifiableList(uem) );
1528    }
1529    
1530    public static void addMbeanDefinition(final ConfigContext cc, final Mbean m) throws ConfigException {
1531        final Domain d = ServerBeansFactory.getDomainBean(cc);
1532        final Applications as = d.getApplications();
1533        as.addMbean(m);
1534    }
1535    
1536    public static void addMbeanReference(final ConfigContext cc, final String JavaDoc toThisMbean, final String JavaDoc server) throws ConfigException {
1537        final Domain d = ServerBeansFactory.getDomainBean(cc);
1538        final Servers ss = d.getServers();
1539        final Server s = ss.getServerByName(server);
1540        final ApplicationRef ar = new ApplicationRef();
1541        ar.setRef(toThisMbean);
1542        ar.setEnabled(true);
1543        s.addApplicationRef(ar);
1544    }
1545    
1546    public static void addClusterMbeanReference(final ConfigContext cc, final String JavaDoc toThisMbean, final String JavaDoc clusterName) throws ConfigException {
1547        final Cluster cluster = ClusterHelper.getClusterByName(cc, clusterName);
1548        final ApplicationRef ar = new ApplicationRef();
1549        ar.setRef(toThisMbean);
1550        ar.setEnabled(true);
1551        cluster.addApplicationRef(ar);
1552    }
1553    
1554    public static void removeMbeanDefinition(final ConfigContext cc, final String JavaDoc n) throws ConfigException {
1555        final Domain d = ServerBeansFactory.getDomainBean(cc);
1556        final Applications as = d.getApplications();
1557        as.removeMbean(as.getMbeanByName(n));
1558    }
1559    
1560    public static void removeMbeanReference(final ConfigContext cc, final String JavaDoc ref, final String JavaDoc server) throws ConfigException {
1561        final Domain d = ServerBeansFactory.getDomainBean(cc);
1562        final Servers ss = d.getServers();
1563        final Server s = ss.getServerByName(server);
1564        final ApplicationRef ar = s.getApplicationRefByRef(ref);
1565        s.removeApplicationRef(ar);
1566    }
1567
1568    public static void removeClusterMbeanReference(final ConfigContext cc, final String JavaDoc ref, final String JavaDoc clusterName) throws ConfigException {
1569        final Cluster cluster = ClusterHelper.getClusterByName(cc, clusterName);
1570        final ApplicationRef ar = cluster.getApplicationRefByRef(ref);
1571        cluster.removeApplicationRef(ar);
1572    }
1573
1574    public static Server[] getServersReferencingMBeanDefinition(final ConfigContext cc, final String JavaDoc name) throws ConfigException {
1575        //check to see if two or more servers have references to this mbean
1576
Server[] ss = null;
1577        final List<Server> list = new ArrayList<Server> ();
1578        ss = ServerBeansFactory.getDomainBean(cc).getServers().getServer();
1579        int numberOfReferencingServers = 0;
1580        for (Server s : ss) {
1581            boolean referenced = false;
1582            final String JavaDoc sName = s.getName();
1583            referenced = ServerBeansFactory.isReferencedMBean(cc, sName, name);
1584            if (referenced) {
1585                list.add(s);
1586            }
1587        }
1588        final Server[] rss = new Server[list.size()];
1589        return ( list.toArray(rss) );
1590    }
1591    private static String JavaDoc prepareMessyStringMessage(Server[] ss, boolean[] isrefs)
1592    {
1593        // the refs in this array of Servers are not the same. I.e. we have a scary inconsistent
1594
// corrupted cluster configuration. Let's painfully create a message for the user about it...
1595
StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
1596        
1597        for(int i = 0; i < ss.length; i++)
1598        {
1599            if(i != 0)
1600                sb.append(", ");
1601            String JavaDoc s = isrefs[i] ? "referenced" : "not-referenced";
1602            sb.append(ss[i].getName()).append(":").append(s);
1603        }
1604        
1605        return sb.toString();
1606    }
1607}
Popular Tags