KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ServerConfigHelper.java
25  *
26  * Created on October 23, 2003, 11:15 AM
27  */

28
29 package com.sun.enterprise.config.serverbeans;
30
31 import com.sun.enterprise.config.ConfigContext;
32 import com.sun.enterprise.config.ConfigException;
33
34 import com.sun.enterprise.security.store.IdentityManager;
35
36 import com.sun.enterprise.admin.util.JMXConnectorConfig;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.io.IOException JavaDoc;
42
43 import javax.management.MBeanServerConnection JavaDoc;
44 import javax.management.remote.JMXConnector JavaDoc;
45 import javax.management.remote.JMXConnectorFactory JavaDoc;
46 import javax.management.remote.JMXServiceURL JavaDoc;
47 import com.sun.enterprise.admin.jmx.remote.server.rmi.JmxServiceUrlFactory;
48 import com.sun.enterprise.util.SystemPropertyConstants;
49 import java.net.InetAddress JavaDoc;
50
51
52 /**
53  *
54  * @author kebbs
55  * TODO: We may want to hide the DAS (instance "server") completely from any server list, etc.
56  * Currently the DAS is treated like any other server instance; however, the user should
57  * be able to specify "server" as a target, thus allowing the PE like deployment. Obviously
58  * this needs some thought.
59  */

60 public class ServerHelper extends ConfigAPIHelper {
61
62     public static final String JavaDoc ADMIN_HTTP_LISTNER_ID = "admin-listener";
63     
64     /**
65      * Return the given server array as a comma separated string
66      */

67     public static String JavaDoc getServersAsString(Server[] servers)
68     {
69         String JavaDoc result = "";
70         for (int i = 0; i < servers.length; i++) {
71             result += servers[i].getName();
72             if (i < servers.length - 1) {
73                 result += ",";
74             }
75         }
76         return result;
77     }
78     
79     /**
80      * Return all the servers in the domain. We do not check for null since we assume
81      * at least one server instance ("server") in the domain.
82      */

83     public static Server[] getServersInDomain(ConfigContext configContext)
84         throws ConfigException
85     {
86         final Domain domain = getDomainConfigBean(configContext);
87         return domain.getServers().getServer();
88     }
89       
90     /**
91      * Return true if the given server instance is a server instance.
92      */

93     public static boolean isAServer(ConfigContext configContext, String JavaDoc instanceName)
94         throws ConfigException
95     {
96         final Domain domain = getDomainConfigBean(configContext);
97         final Server server = domain.getServers().getServerByName(instanceName);
98         return (server != null ? true : false);
99     }
100     
101     /**
102      * Return the server associated with the given instanceName. An exception is
103      * thrown if the instanceName does not correspond to a server instance, or does
104      * not exist.
105      */

106     public static Server getServerByName(ConfigContext configContext, String JavaDoc instanceName)
107         throws ConfigException
108     {
109         final Domain domain = getDomainConfigBean(configContext);
110         final Server server = domain.getServers().getServerByName(instanceName);
111         if (server == null) {
112             throw new ConfigException(_strMgr.getString("noSuchInstance",
113                 instanceName));
114         }
115         return server;
116     }
117      
118     public static Server getDAS(ConfigContext configContext)
119         throws ConfigException
120     {
121         //Now find all server instances that reference that config.
122
Server[] servers = getServersInDomain(configContext);
123         Server das = null;
124         for (int i = 0; i < servers.length; i++) {
125             if (isDAS(configContext, servers[i])) {
126                 if (das == null) {
127                     das = servers[i];
128                 } else {
129                     //Throw an exception if we found more than 1 DAS. This is
130
//more of a sanity check and can be removed if there are
131
//performance concerns.
132
throw new ConfigException(_strMgr.getString("tooManyDASFound", das.getName(),
133                         servers[i].getName()));
134                 }
135             }
136         }
137         if (das == null) {
138             //Throw an exception if we could not identify the DAS.
139
throw new ConfigException(_strMgr.getString("noDASFound"));
140         }
141         return das;
142     }
143     
144     /**
145      * Return the server instances referencing the given configName or a zero length list.
146      */

147     public static Server[] getServersReferencingConfig(ConfigContext configContext, String JavaDoc configName)
148         throws ConfigException
149     {
150         //First ensure that the config exists
151
Config config = getConfigByName(configContext, configName);
152         
153         //Now find all server instances that reference that config.
154
Server[] servers = getServersInDomain(configContext);
155         ArrayList JavaDoc result = new ArrayList JavaDoc();
156         for (int i = 0; i < servers.length; i++) {
157             if (servers[i].getConfigRef().equals(configName)) {
158                 result.add(servers[i]);
159             }
160         }
161         return (Server[])result.toArray(new Server[result.size()]);
162     }
163
164
165     /**
166      * Return the list of stand-alone instances
167      * Based on DTD each stand-alone instances points to a named config.
168      * If the server instance is part of a cluster then it will not have a config.
169      */

170     public static Server[] getStandAloneServers(
171         ConfigContext configContext, boolean excludeDASInstance)
172         throws ConfigException
173     {
174         Server[] servers = getServersInDomain(configContext);
175         ArrayList JavaDoc result = new ArrayList JavaDoc();
176         for (int i = 0; i < servers.length; i++) {
177             if (isDAS(configContext, servers[i]) && excludeDASInstance) {
178                 continue;
179             }
180             if (isServerStandAlone(configContext, servers[i].getName())) {
181                 result.add(servers[i]);
182             }
183         }
184         return (Server[])result.toArray(new Server[result.size()]);
185     }
186
187     /**
188         Returns an array of unclustered servers. Note that the difference
189         between an unclustered server and a standalone server is that an
190         unclustered server may share its config with other servers whereas
191         a standalone server must not share its config with other servers.
192      */

193     public static Server[] getUnclusteredServers(
194         ConfigContext configContext, boolean excludeDASInstance)
195         throws ConfigException
196     {
197         Server[] servers = getServersInDomain(configContext);
198         ArrayList JavaDoc result = new ArrayList JavaDoc();
199         for (int i = 0; i < servers.length; i++) {
200             if (isDAS(configContext, servers[i]) && excludeDASInstance) {
201                 continue;
202             }
203             if (!isServerClustered(configContext, servers[i])) {
204                 result.add(servers[i]);
205             }
206         }
207         return (Server[])result.toArray(new Server[result.size()]);
208     }
209
210     /**
211      * Return the clusters in the given clusterName or a zero length list. The
212      * cluster (and each of its instances) must exist or an exception is thrown.
213      */

214     public static Server[] getServersInCluster(ConfigContext configContext,
215         String JavaDoc clusterName) throws ConfigException
216     {
217         //first ensure that the cluster exists
218
Cluster cluster = ClusterHelper.getClusterByName(configContext, clusterName);
219         
220         //Now fetch the server instances in the cluster.
221
ServerRef[] serverRefs = cluster.getServerRef();
222         
223         Server[] result = new Server[serverRefs.length];
224         for (int i = 0; i < serverRefs.length; i++) {
225             try {
226                 result[i] = getServerByName(configContext, serverRefs[i].getRef());
227             } catch (ConfigException ex) {
228                 throw new ConfigException(_strMgr.getString("noSuchClusterInstance",
229                     clusterName, serverRefs[i].getRef()));
230             }
231         }
232         return result;
233     }
234     
235     /**
236      * Returns true if the given server is the Domain Administration Server.
237      * There is a large assumption being made here that the DAS is the only server
238      * instance with out a node controller ref.
239      *
240      * NOTE: This method has unused paramenters (configContext) and an unnecessary
241      * ConfigException. This is to support a more complete check in the future
242      * (e.g. checking the to see if the server is the 0th element, or checking the
243      * admin-service of the server's configuration) should we determine that
244      * checking for an empty node controller ref is not sufficient.s
245      *
246      **/

247     public static boolean isDAS(ConfigContext configContext, Server server)
248         throws ConfigException
249     {
250         if (server.getNodeAgentRef() == null) {
251             return true;
252         }
253         return false;
254     }
255     
256     /**
257      * Returns true if the given server is the Domain Administration Server.
258      */

259     public static boolean isDAS(ConfigContext configContext, String JavaDoc instanceName)
260         throws ConfigException
261     {
262         final Domain domain = getDomainConfigBean(configContext);
263         final Server server = domain.getServers().getServerByName(instanceName);
264         return isDAS(configContext, server);
265     }
266     
267     /**
268      * Return all the servers associated with the given node agent or a zero length
269      * list. The node agent must exist or an exception is thrown.
270      */

271     public static Server[] getServersOfANodeAgent(ConfigContext configContext, String JavaDoc agentName)
272         throws ConfigException
273     {
274         //First ensure that the config exists
275
NodeAgent controller = NodeAgentHelper.getNodeAgentByName(configContext, agentName);
276         
277         //Next fetch all the server instances that reference the node agent
278
Server[] servers = getServersInDomain(configContext);
279         ArrayList JavaDoc result = new ArrayList JavaDoc();
280         for (int i = 0; i < servers.length; i++) {
281             if (!isDAS(configContext, servers[i]) &&
282                 servers[i].getNodeAgentRef().equals(agentName))
283             {
284                 result.add(servers[i]);
285             }
286         }
287         return (Server[])result.toArray(new Server[result.size()]);
288     }
289     
290     /**
291      * Return the system jmx connector for the specified server instance. The specified server
292      * instance and its connector must exist or an exception is thrown.
293      */

294     public static JmxConnector getServerSystemConnector(ConfigContext configContext, String JavaDoc instanceName)
295         throws ConfigException
296     {
297         //Find the admin service element of the config
298
final AdminService adminService = getAdminServiceForServer(configContext, instanceName);
299         
300         final String JavaDoc systemConnectorName = adminService.getSystemJmxConnectorName();
301         final JmxConnector connector = adminService.getJmxConnectorByName(
302             systemConnectorName);
303         if (connector == null) {
304             throw new ConfigException(_strMgr.getString("noInstanceSystemConnector", instanceName,
305                 systemConnectorName));
306         }
307         return connector;
308     }
309     
310     /**
311      * Return mbean server connection info for a server instance (including the DAS). This returns
312      * the information necessary to connect to the exposed mbean server's connector.
313      */

314     public static JMXConnectorConfig getJMXConnectorInfo(ConfigContext configContext, String JavaDoc instanceName)
315         throws ConfigException
316     {
317         //Fetch the server instances system jmx connector defined in its configuration.
318
JmxConnector connector = ServerHelper.getServerSystemConnector(
319             configContext, instanceName);
320
321         //If the port number is not a number, but instead a property reference of the form
322
//${port-number}, we must resolve the property reference manually. The issue is that
323
//this code is running in the DAS, and the port-number system property will not
324
//be defined there. It will only be defined in the server instance.
325

326         //FIXTHIS: This is a HACK since connector.getPort() will return a
327
//port with all the system properties resolved. We need the raw attribute value.
328
String JavaDoc portAttribute = connector.getRawAttributeValue(ServerTags.PORT);
329         String JavaDoc port = new PropertyResolver(configContext, instanceName).resolve(
330             portAttribute);
331
332
333         //Now set up client connection info
334
Server server = ServerHelper.getServerByName(configContext, instanceName);
335         ElementProperty hostProp = null;
336         AdminService adminService = ServerHelper.getAdminServiceForServer(
337             configContext, instanceName);
338
339         //If we are connecting to the DAS, then use connector as is. If we are connecting
340
//to a remote server instance then we have to obtain its user, password, host
341
//information from its Node Agent.
342
if (ServerHelper.isDAS(configContext, server)) { //DAS
343
hostProp = connector.getElementPropertyByName(HOST_PROPERTY_NAME);
344         } else {
345             //Take the client connection properties from the node agent referenced by the
346
//given server instance. In other words, the host, user, password used to connect to the
347
//server instance should be identical to the host, user, password used to connect to the
348
//server's node agent.
349
final JmxConnector agentConnector = NodeAgentHelper.getNodeAgentSystemConnector(
350                 configContext, server.getNodeAgentRef());
351             hostProp = agentConnector.getElementPropertyByName(HOST_PROPERTY_NAME);
352         }
353
354         String JavaDoc adminUser = IdentityManager.getUser();
355         String JavaDoc adminPassword = IdentityManager.getPassword();
356         
357         if (adminUser == null || adminPassword == null || hostProp == null) {
358             throw new ConfigException(_strMgr.getString("missingInstanceConnectorAuth",
359                 instanceName));
360         }
361
362         return new JMXConnectorConfig(hostProp.getValue(), port,
363             adminUser, adminPassword, connector.getProtocol());
364     }
365      
366     /**
367      * Fetch the AdminService for the given server instance. The server instance must exist or
368      * an exception is thrown.
369      */

370     public static AdminService getAdminServiceForServer(ConfigContext configContext, String JavaDoc instanceName)
371         throws ConfigException
372     {
373         final Config config = getConfigForServer(configContext, instanceName);
374         
375         //Find the admin service element of the config
376
final AdminService adminService = config.getAdminService();
377         if (adminService == null) {
378             throw new ConfigException(_strMgr.getString("noAdminService",
379                 config.getName(), instanceName));
380         }
381         return adminService;
382     }
383     
384     /**
385      * Return the configuration of the given server instance. Both the server instance
386      * or its configuration must exist or an exception is thrown.
387      */

388     public static Config getConfigForServer(ConfigContext configContext, String JavaDoc instanceName)
389         throws ConfigException
390     {
391         final Server server = getServerByName(configContext, instanceName);
392         return getConfigByName(configContext, server.getConfigRef());
393     }
394     
395     /**
396      * Return true if the given server instance is part of a cluster.
397      */

398     public static boolean isServerClustered(ConfigContext configContext, Server server)
399         throws ConfigException
400     {
401         //Return the server only if it is part of a cluster (i.e. only if a cluster
402
//has a reference to it).
403
final String JavaDoc instanceName = server.getName();
404         final Cluster[] clusters = ClusterHelper.getClustersInDomain(configContext);
405         for (int i = 0; i < clusters.length; i++) {
406             final ServerRef[] servers = clusters[i].getServerRef();
407             for (int j = 0; j < servers.length; j++) {
408                 if (servers[j].getRef().equals(instanceName)) {
409                     // check to see if the server exists as a sanity check.
410
// NOTE: we are not checking for duplicate server instances here.
411
return true;
412                 }
413             }
414         }
415         return false;
416     }
417     
418     /**
419      * Return true if the given server instance is part of a cluster.
420      */

421     public static boolean isServerClustered(ConfigContext configContext, String JavaDoc instanceName)
422         throws ConfigException
423     {
424         return isServerClustered(configContext,
425             getServerByName(configContext, instanceName));
426     }
427     
428     /**
429      * Return true if the server is standalone. A standalone server has a configuration
430      * named <serverName>-config and it configuration is referenced by the server
431      * only. No other clusters or servers may refer to its configuration.
432      */

433     public static boolean isServerStandAlone(ConfigContext configContext, String JavaDoc instanceName)
434         throws ConfigException
435     {
436        final Server server = getServerByName(configContext, instanceName);
437        final String JavaDoc configName = server.getConfigRef();
438        if (isConfigurationNameStandAlone(configName, instanceName)) {
439             if (!isServerClustered(configContext, server)) {
440                 if (isConfigurationReferencedByServerOnly(configContext, configName, instanceName)) {
441                    return true;
442                }
443            }
444        }
445        return false;
446     }
447    
448     /**
449      * Return true if the given server instance references the stated application.
450      */

451     public static boolean serverReferencesApplication(ConfigContext configContext,
452         String JavaDoc instanceName, String JavaDoc appName) throws ConfigException
453     {
454         final Server server = getServerByName(configContext, instanceName);
455         return serverReferencesApplication(server, appName);
456     }
457     
458     public static boolean serverReferencesApplication(Server server, String JavaDoc appName)
459         throws ConfigException
460     {
461         final ApplicationRef ref = server.getApplicationRefByRef(appName);
462         return (ref == null) ? false : true;
463     }
464     
465     /**
466      * Return the server instances referencing the given configName or a zero length list.
467      */

468     public static Server[] getServersReferencingApplication(ConfigContext configContext, String JavaDoc appName)
469         throws ConfigException
470     {
471         //Now find all server instances that reference the application
472
Server[] servers = getServersInDomain(configContext);
473         ArrayList JavaDoc result = new ArrayList JavaDoc();
474         for (int i = 0; i < servers.length; i++) {
475             if (serverReferencesApplication(servers[i], appName)) {
476                 result.add(servers[i]);
477             }
478         }
479         return (Server[])result.toArray(new Server[result.size()]);
480     }
481
482     public static boolean serverReferencesJdbcConPool(ConfigContext ctx,
483         String JavaDoc instanceName, String JavaDoc poolName) throws ConfigException
484     {
485         final Server server = getServerByName(ctx, instanceName);
486         return serverReferencesJdbcConPool(server, poolName);
487     }
488     
489     public static boolean serverReferencesJdbcConPool(Server server,
490         String JavaDoc poolName) throws ConfigException
491     {
492         final ResourceRef ref = server.getResourceRefByRef(poolName);
493         return (ref == null) ? false : true;
494     }
495        
496     
497     /**
498      * Return true if the given server instance references the stated resource.
499      */

500     public static boolean serverReferencesResource(ConfigContext configContext,
501         String JavaDoc instanceName, String JavaDoc resourceName) throws ConfigException
502     {
503         final Server server = getServerByName(configContext, instanceName);
504         return serverReferencesResource(server, resourceName);
505     }
506     
507     public static boolean serverReferencesResource(Server server,
508         String JavaDoc resourceName) throws ConfigException
509     {
510         final ResourceRef ref = server.getResourceRefByRef(resourceName);
511         return (ref == null) ? false : true;
512     }
513        
514     /**
515      * Return the server instances referencing the given configName or a zero length list.
516      */

517     public static Server[] getServersReferencingResource(ConfigContext configContext, String JavaDoc resName)
518         throws ConfigException
519     {
520         //Now find all server instances that reference the application
521
Server[] servers = getServersInDomain(configContext);
522         ArrayList JavaDoc result = new ArrayList JavaDoc();
523         for (int i = 0; i < servers.length; i++) {
524             if (serverReferencesResource(servers[i], resName)) {
525                 result.add(servers[i]);
526             }
527         }
528         return (Server[])result.toArray(new Server[result.size()]);
529     }
530
531     /**
532      * Returns all servers referencing the given JDBC pool. There is no
533      * explicit reference from a server to a jdbc pool. This implementation
534      * examines the jdbc resource refs to return the associated servers.
535      *
536      * @param ctx config context
537      * @param poolName name of the jdbc pool
538      *
539      * @return servers associated to the pool
540      * @throws ConfigException if an error while parsing domain.xml
541      */

542     public static Server[] getServersReferencingJdbcPool(ConfigContext ctx,
543             String JavaDoc poolName) throws ConfigException {
544
545         Server[] servers = getServersInDomain(ctx);
546         ArrayList JavaDoc result = new ArrayList JavaDoc();
547         for (int i = 0; i < servers.length; i++) {
548             if (ResourceHelper.isJdbcPoolReferenced(ctx, poolName,
549                                             servers[i].getName())) {
550                 result.add(servers[i]);
551             }
552         }
553         return (Server[])result.toArray(new Server[result.size()]);
554     }
555
556     /**
557      * Returns all servers referencing the given connector connection pool.
558      * There is no explicit reference from a server to a connector connection
559      * pool. This implementation examines the connector resource references
560      * to return the associated servers.
561      *
562      * @param ctx config context
563      * @param poolName name of the connector connection pool
564      *
565      * @return servers associated to the connector connection pool
566      *
567      * @throws ConfigException if an error while parsing domain.xml
568      */

569     public static Server[] getServersReferencingConnectorPool(ConfigContext ctx,
570             String JavaDoc poolName) throws ConfigException {
571
572         Server[] servers = getServersInDomain(ctx);
573         ArrayList JavaDoc result = new ArrayList JavaDoc();
574         for (int i = 0; i < servers.length; i++) {
575             if (ResourceHelper.isConnectorPoolReferenced(ctx, poolName,
576                                             servers[i].getName())) {
577                 result.add(servers[i]);
578             }
579         }
580         return (Server[])result.toArray(new Server[result.size()]);
581     }
582     
583     /**
584      * Return all the application refs of the server
585      */

586     public static ApplicationRef[] getApplicationReferences(ConfigContext configContext,
587         String JavaDoc serverName) throws ConfigException
588     {
589         final Server server = getServerByName(configContext, serverName);
590         if (server.getApplicationRef() == null) {
591             return new ApplicationRef[0];
592         } else {
593             return server.getApplicationRef();
594         }
595     }
596     
597     /**
598      * Return all the resource refs of the server
599      */

600     public static ResourceRef[] getResourceReferences(ConfigContext configContext,
601         String JavaDoc serverName) throws ConfigException
602     {
603         final Server server = getServerByName(configContext, serverName);
604         if (server.getResourceRef() == null) {
605             return new ResourceRef[0];
606         } else {
607             return server.getResourceRef();
608         }
609     }
610
611     /**
612      * Returns all the associated J2EE Applications bean for this
613      * given sever.
614      */

615     public static J2eeApplication[] getAssociatedJ2eeApplications(
616             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
617
618         ArrayList JavaDoc list = new ArrayList JavaDoc();
619         Domain domain = (Domain) ctx.getRootConfigBean();
620         Applications applications = domain.getApplications();
621
622         Servers servers = domain.getServers();
623         Server s = servers.getServerByName(serverName);
624         if (s != null) {
625             J2eeApplication[] j2eeApps = applications.getJ2eeApplication();
626             for (int i=0; i<j2eeApps.length; i++) {
627                 if (serverReferencesApplication(ctx, serverName,
628                                                 j2eeApps[i].getName()) ) {
629                     list.add(j2eeApps[i]);
630                 }
631             }
632         }
633
634         J2eeApplication[] associatedApps = new J2eeApplication[list.size()];
635         return ((J2eeApplication[]) list.toArray(associatedApps));
636     }
637
638     public static J2eeApplication[] getUnAssociatedJ2eeApplications(
639             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
640
641         ArrayList JavaDoc list = new ArrayList JavaDoc();
642         Domain domain = (Domain) ctx.getRootConfigBean();
643         Applications applications = domain.getApplications();
644
645         Servers servers = domain.getServers();
646         Server s = servers.getServerByName(serverName);
647         if (s != null) {
648             J2eeApplication[] j2eeApps = applications.getJ2eeApplication();
649             for (int i=0; i<j2eeApps.length; i++) {
650                 if (!serverReferencesApplication(ctx, serverName,
651                                                 j2eeApps[i].getName()) ) {
652                     list.add(j2eeApps[i]);
653                 }
654             }
655         }
656
657         J2eeApplication[] unassociatedApps = new J2eeApplication[list.size()];
658         return ((J2eeApplication[]) list.toArray(unassociatedApps));
659     }
660
661     /**
662      * Returns all the associated EJB modules bean for this
663      * given sever.
664      */

665     public static EjbModule[] getAssociatedEjbModules(
666             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
667
668         ArrayList JavaDoc list = new ArrayList JavaDoc();
669         Domain domain = (Domain) ctx.getRootConfigBean();
670         Applications applications = domain.getApplications();
671
672         Servers servers = domain.getServers();
673         Server s = servers.getServerByName(serverName);
674         if (s != null) {
675             EjbModule[] ejbMods = applications.getEjbModule();
676             for (int i=0; i<ejbMods.length; i++) {
677                 if (serverReferencesApplication(ctx, serverName,
678                                                 ejbMods[i].getName()) ) {
679                     list.add(ejbMods[i]);
680                 }
681             }
682         }
683
684         EjbModule[] associatedApps = new EjbModule[list.size()];
685         return ((EjbModule[]) list.toArray(associatedApps));
686     }
687
688     public static EjbModule[] getUnAssociatedEjbModules(
689             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
690
691         ArrayList JavaDoc list = new ArrayList JavaDoc();
692         Domain domain = (Domain) ctx.getRootConfigBean();
693         Applications applications = domain.getApplications();
694
695         Servers servers = domain.getServers();
696         Server s = servers.getServerByName(serverName);
697         if (s != null) {
698             EjbModule[] ejbMods = applications.getEjbModule();
699             for (int i=0; i<ejbMods.length; i++) {
700                 if (!serverReferencesApplication(ctx, serverName,
701                                                 ejbMods[i].getName()) ) {
702                     list.add(ejbMods[i]);
703                 }
704             }
705         }
706
707         EjbModule[] unassociatedApps = new EjbModule[list.size()];
708         return ((EjbModule[]) list.toArray(unassociatedApps));
709     }
710
711     /**
712      * Returns all the associated web modules bean for this
713      * given sever.
714      */

715     public static WebModule[] getAssociatedWebModules(
716             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
717
718         ArrayList JavaDoc list = new ArrayList JavaDoc();
719         Domain domain = (Domain) ctx.getRootConfigBean();
720         Applications applications = domain.getApplications();
721
722         Servers servers = domain.getServers();
723         Server s = servers.getServerByName(serverName);
724         if (s != null) {
725             WebModule[] webMods = applications.getWebModule();
726             for (int i=0; i<webMods.length; i++) {
727                 if (serverReferencesApplication(ctx, serverName,
728                                                 webMods[i].getName()) ) {
729                     list.add(webMods[i]);
730                 }
731             }
732         }
733
734         WebModule[] associatedApps = new WebModule[list.size()];
735         return ((WebModule[]) list.toArray(associatedApps));
736     }
737
738     public static WebModule[] getUnAssociatedWebModules(
739             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
740
741         ArrayList JavaDoc list = new ArrayList JavaDoc();
742         Domain domain = (Domain) ctx.getRootConfigBean();
743         Applications applications = domain.getApplications();
744
745         Servers servers = domain.getServers();
746         Server s = servers.getServerByName(serverName);
747         if (s != null) {
748             WebModule[] webMods = applications.getWebModule();
749             for (int i=0; i<webMods.length; i++) {
750                 if (!serverReferencesApplication(ctx, serverName,
751                                                 webMods[i].getName()) ) {
752                     list.add(webMods[i]);
753                 }
754             }
755         }
756
757         WebModule[] unassociatedApps = new WebModule[list.size()];
758         return ((WebModule[]) list.toArray(unassociatedApps));
759     }
760
761     public static ConnectorModule[] getUnAssociatedConnectorModules(
762             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
763
764         ArrayList JavaDoc list = new ArrayList JavaDoc();
765         Domain domain = (Domain) ctx.getRootConfigBean();
766         Applications applications = domain.getApplications();
767
768         Servers servers = domain.getServers();
769         Server s = servers.getServerByName(serverName);
770         if (s != null) {
771             ConnectorModule[] connMods = applications.getConnectorModule();
772             for (int i=0; i<connMods.length; i++) {
773                 if (!serverReferencesApplication(ctx, serverName,
774                                                 connMods[i].getName()) ) {
775                     list.add(connMods[i]);
776                 }
777             }
778         }
779
780         ConnectorModule[] unassociatedApps = new ConnectorModule[list.size()];
781         return ((ConnectorModule[]) list.toArray(unassociatedApps));
782     }
783
784     /**
785      * Returns all the associated connector modules bean for this
786      * given sever.
787      */

788     public static ConnectorModule[] getAssociatedConnectorModules(
789             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
790
791         ArrayList JavaDoc list = new ArrayList JavaDoc();
792         Domain domain = (Domain) ctx.getRootConfigBean();
793         Applications applications = domain.getApplications();
794
795         Servers servers = domain.getServers();
796         Server s = servers.getServerByName(serverName);
797         if (s != null) {
798             ConnectorModule[] connMods = applications.getConnectorModule();
799             for (int i=0; i<connMods.length; i++) {
800                 if (serverReferencesApplication(ctx, serverName,
801                                                 connMods[i].getName()) ) {
802                     list.add(connMods[i]);
803                 }
804             }
805         }
806
807         ConnectorModule[] associatedApps = new ConnectorModule[list.size()];
808         return ((ConnectorModule[]) list.toArray(associatedApps));
809     }
810
811     public static LifecycleModule[] getUnAssociatedLifecycleModules(
812             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
813
814         ArrayList JavaDoc list = new ArrayList JavaDoc();
815         Domain domain = (Domain) ctx.getRootConfigBean();
816         Applications applications = domain.getApplications();
817
818         Servers servers = domain.getServers();
819         Server s = servers.getServerByName(serverName);
820         if (s != null) {
821             LifecycleModule[] lcMods = applications.getLifecycleModule();
822             for (int i=0; i<lcMods.length; i++) {
823                 if (!serverReferencesApplication(ctx, serverName,
824                                                 lcMods[i].getName()) ) {
825                     list.add(lcMods[i]);
826                 }
827             }
828         }
829
830         LifecycleModule[] unassociatedApps = new LifecycleModule[list.size()];
831         return ((LifecycleModule[]) list.toArray(unassociatedApps));
832     }
833
834     /**
835      * Returns all the associated Lifecycle modules bean for this
836      * given sever.
837      */

838     public static LifecycleModule[] getAssociatedLifecycleModules(
839             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
840
841         ArrayList JavaDoc list = new ArrayList JavaDoc();
842         Domain domain = (Domain) ctx.getRootConfigBean();
843         Applications applications = domain.getApplications();
844
845         Servers servers = domain.getServers();
846         Server s = servers.getServerByName(serverName);
847         if (s != null) {
848             LifecycleModule[] lcMods = applications.getLifecycleModule();
849             for (int i=0; i<lcMods.length; i++) {
850                 if (serverReferencesApplication(ctx, serverName,
851                                                 lcMods[i].getName()) ) {
852                     list.add(lcMods[i]);
853                 }
854             }
855         }
856
857         LifecycleModule[] associatedApps = new LifecycleModule[list.size()];
858         return ((LifecycleModule[]) list.toArray(associatedApps));
859     }
860
861     public static AppclientModule[] getUnAssociatedAppclientModules(
862             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
863
864         ArrayList JavaDoc list = new ArrayList JavaDoc();
865         Domain domain = (Domain) ctx.getRootConfigBean();
866         Applications applications = domain.getApplications();
867
868         Servers servers = domain.getServers();
869         Server s = servers.getServerByName(serverName);
870         if (s != null) {
871             AppclientModule[] appclntMods = applications.getAppclientModule();
872             for (int i=0; i<appclntMods.length; i++) {
873                 if (!serverReferencesApplication(ctx, serverName,
874                                                 appclntMods[i].getName()) ) {
875                     list.add(appclntMods[i]);
876                 }
877             }
878         }
879
880         AppclientModule[] unassociatedApps = new AppclientModule[list.size()];
881         return ((AppclientModule[]) list.toArray(unassociatedApps));
882     }
883
884     /**
885      * Returns all the associated app client modules bean for this
886      * given sever.
887      */

888     public static AppclientModule[] getAssociatedAppclientModules(
889             ConfigContext ctx, String JavaDoc serverName) throws ConfigException {
890
891         ArrayList JavaDoc list = new ArrayList JavaDoc();
892         Domain domain = (Domain) ctx.getRootConfigBean();
893         Applications applications = domain.getApplications();
894
895         Servers servers = domain.getServers();
896         Server s = servers.getServerByName(serverName);
897         if (s != null) {
898             AppclientModule[] appclntMods = applications.getAppclientModule();
899             for (int i=0; i<appclntMods.length; i++) {
900                 if (serverReferencesApplication(ctx, serverName,
901                                                 appclntMods[i].getName()) ) {
902                     list.add(appclntMods[i]);
903                 }
904             }
905         }
906
907         AppclientModule[] associatedApps = new AppclientModule[list.size()];
908         return ((AppclientModule[]) list.toArray(associatedApps));
909     }
910
911     /**
912      * Returns the administrative.domain.name property value defined
913      * under domain element. administrative.domain.name is
914      * used to determine the jmx mbean server name for the AMX
915      * mbeans.
916      *
917      * @param ctx config contex
918      * @param serverName name of the server instance
919      *
920      * @return administrative.domain.name property value or null if not defined
921      *
922      * @throws ConfigException if an error while parsing domain configuration
923      */

924     public static String JavaDoc getAdministrativeDomainName(ConfigContext ctx,
925             String JavaDoc serverName) throws ConfigException {
926
927         // name of administrative domain name property
928
final String JavaDoc ADMIN_DOMAIN_PROP = "administrative.domain.name";
929
930         // administrative domain name property value
931
String JavaDoc aDomainName = null;
932
933         final Domain domain = getDomainConfigBean(ctx);
934
935         if (domain != null) {
936             // administrative domain name
937
ElementProperty aDomainNameProp =
938                 domain.getElementPropertyByName(ADMIN_DOMAIN_PROP);
939         
940             if (aDomainNameProp != null) {
941                 aDomainName = aDomainNameProp.getValue();
942             }
943         }
944         
945         return aDomainName;
946     }
947
948     /**
949      * Get MBeanServerConnection for the given
950      * configContext and serverName
951      */

952     public static MBeanServerConnection JavaDoc connect(
953     ConfigContext configContext, String JavaDoc instanceName)
954     throws ConfigException, IOException JavaDoc {
955
956     // fix me
957
// move this code to a common place which is being worked on by Kedar
958

959     // get jmx connector
960
JMXConnector JavaDoc conn = getJMXConnector(configContext, instanceName);
961
962     return conn.getMBeanServerConnection();
963     }
964
965
966     /**
967      * Get JMX Connector for the given
968      * configContext and serverName
969      */

970     public static JMXConnector JavaDoc getJMXConnector(
971     ConfigContext configContext, String JavaDoc instanceName)
972     throws ConfigException, IOException JavaDoc {
973
974     // fix me
975
// move this code to a common place which is being worked on by Kedar
976

977     // connection info
978
JMXConnectorConfig jcc = getJMXConnectorInfo(configContext, instanceName);
979
980     // url
981
JMXServiceURL JavaDoc url = null;
982     /* Currently this is totally hacked code. - May 2004.
983        This should clearly use a pool of connectors.
984        The pool of connectors should be created early in the life cycle.
985     */

986     try {
987         url = JmxServiceUrlFactory.forRmiWithJndiInAppserver
988         (jcc.getHost(), Integer.parseInt(jcc.getPort()));
989     }
990     catch(final Exception JavaDoc e) {
991         throw new RuntimeException JavaDoc(e);
992     }
993
994     final Map JavaDoc env = new HashMap JavaDoc();
995     // add user and password based on Abhijit's patch
996
env.put(JMXConnector.CREDENTIALS, new String JavaDoc[] {jcc.getUser(), jcc.getPassword()});
997     // connection
998
JMXConnector JavaDoc conn = JMXConnectorFactory.connect(url, env);
999
1000    return conn;
1001    }
1002
1003    public static HttpListener getHttpListener(final ConfigContext cc, final String JavaDoc sn, final String JavaDoc ln) throws ConfigException {
1004        final Config cfg = getConfigForServer(cc, sn);
1005        final HttpService hs = cfg.getHttpService();
1006        final HttpListener hl = hs.getHttpListenerById(ln);
1007        return ( hl );
1008    }
1009    
1010    public static HttpListener[] getHttpListeners(final ConfigContext cc, final String JavaDoc sn) throws ConfigException {
1011        final Config cfg = getConfigForServer(cc, sn);
1012        final HttpService hs = cfg.getHttpService();
1013        return ( hs.getHttpListener() );
1014    }
1015    public static String JavaDoc getUrlString(final HttpListener ls) {
1016        String JavaDoc url = ls.isSecurityEnabled() ? "https://" : "http://";
1017        final String JavaDoc address = SystemPropertyConstants.DEFAULT_SERVER_SOCKET_ADDRESS.equals(ls.getAddress()) ? "localhost" : ls.getAddress();
1018        url = url + address + ":" + ls.getPort();
1019        return ( url );
1020    }
1021    
1022    public static JMXServiceURL JavaDoc getJmxServiceUrl(final JmxConnector conn) {
1023        final String JavaDoc sport = conn.getPort();
1024        int port;
1025        //the protocol is assumed here.
1026
String JavaDoc local = null;
1027        try {
1028              port = Integer.parseInt(sport);
1029              local = InetAddress.getLocalHost().getHostName(); //UHE should have been a RuntimeException
1030
} catch (final Exception JavaDoc e) {
1031            throw new RuntimeException JavaDoc(e);
1032        }
1033        final String JavaDoc address = SystemPropertyConstants.DEFAULT_SERVER_SOCKET_ADDRESS.equals(conn.getAddress()) ? local : conn.getAddress();
1034        final JMXServiceURL JavaDoc url = JmxServiceUrlFactory.forJconsoleOverRmiWithJndiInAppserver(address, port);
1035        return ( url );
1036    }
1037}
1038
Popular Tags