001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.management.j2ee;
023:
024: import javax.management.MBeanServer;
025: import javax.management.MalformedObjectNameException;
026: import javax.management.ObjectName;
027: import java.util.ArrayList;
028: import java.util.List;
029: import java.util.Set;
030:
031: /**
032: * Root class of the JBoss JSR-77 implementation of
033: *
034: * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
035: * @author <a href="mailto:scott.stark@jboss.org">Scott Stark</a>
036: * @author <a href="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
037: * @version $Revision: 57197 $
038: */
039: public class J2EEDomain extends J2EEManagedObject implements
040: J2EEDomainMBean {
041:
042: // Attributes ----------------------------------------------------
043: /**
044: * The local server J2EEDomain implementation name
045: */
046: private static String domainName = null;
047:
048: /**
049: * list of servers associated with the domain as strings
050: */
051: private List serverNames = new ArrayList();
052:
053: // Static --------------------------------------------------------
054: /**
055: * Get the local J2EEDomain instance name
056: *
057: * @return the J2EEDomain object name for the local server.
058: */
059: public static String getDomainName() {
060: return domainName;
061: }
062:
063: /**
064: * Query for the J2EEServer MBean in the given domain.
065: *
066: * @param mbeanServer the local MBeanServer
067: * @return the J2EEServer name if found, null otherwise
068: */
069: public static ObjectName getDomainServerName(MBeanServer mbeanServer) {
070: ObjectName domainServer = null;
071: try {
072: // Query for all MBeans matching the J2EEServer naming convention
073: ObjectName serverQuery = new ObjectName(domainName + ":"
074: + J2EEManagedObject.TYPE + "="
075: + J2EETypeConstants.J2EEServer + "," + "*");
076:
077: Set servers = mbeanServer.queryNames(serverQuery, null);
078: if (servers.isEmpty() == false) {
079: domainServer = (ObjectName) servers.iterator().next();
080: }
081: } catch (Exception ignore) {
082: }
083: return domainServer;
084: }
085:
086: // Constructors --------------------------------------------------
087:
088: public J2EEDomain(String domainName)
089: throws MalformedObjectNameException, InvalidParentException {
090: super (domainName, J2EETypeConstants.J2EEDomain, domainName);
091: J2EEDomain.domainName = domainName;
092: }
093:
094: // Public --------------------------------------------------------
095:
096: /**
097: * Return the J2EEServer names associated with this domain.
098: *
099: * @jmx:managed-attribute
100: */
101: public String[] getservers() {
102: String[] servers = new String[serverNames.size()];
103: serverNames.toArray(servers);
104: return servers;
105: }
106:
107: /**
108: * @jmx:managed-operation
109: */
110: public String getserver(int pIndex) {
111: if (pIndex >= 0 && pIndex < serverNames.size()) {
112: return (String) serverNames.get(pIndex);
113: }
114: return null;
115: }
116:
117: // J2EEManagedObject implementation ----------------------------------------------
118:
119: public void addChild(ObjectName pChild) {
120: String lType = J2EEManagedObject.getType(pChild);
121: if (J2EETypeConstants.J2EEServer.equals(lType)) {
122: serverNames.add(pChild.getCanonicalName());
123: }
124: }
125:
126: public void removeChild(ObjectName pChild) {
127: String lType = J2EEManagedObject.getType(pChild);
128: if (J2EETypeConstants.J2EEServer.equals(lType)) {
129: serverNames.remove(pChild.getCanonicalName());
130: }
131: }
132:
133: // Object overrides ---------------------------------------------------
134:
135: public String toString() {
136: return "J2EEDomain { " + super .toString() + " } [ "
137: + ", servers: " + serverNames + " ]";
138: }
139:
140: // Package protected ---------------------------------------------
141:
142: // Protected -----------------------------------------------------
143:
144: // Private -------------------------------------------------------
145:
146: // Inner classes -------------------------------------------------
147: }
|