001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.j2ee.management.impl;
019    
020    import java.util.Hashtable;
021    import java.util.Collection;
022    import javax.management.ObjectName;
023    
024    import org.apache.geronimo.gbean.GBeanInfo;
025    import org.apache.geronimo.gbean.GBeanInfoBuilder;
026    import org.apache.geronimo.kernel.ObjectNameUtil;
027    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
028    import org.apache.geronimo.management.geronimo.J2EEDomain;
029    import org.apache.geronimo.management.geronimo.J2EEServer;
030    
031    /**
032     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
033     */
034    public class J2EEDomainImpl implements J2EEDomain {
035        private final String objectName;
036        private final Collection servers;
037    
038        public J2EEDomainImpl(String objectName, Collection servers) {
039            this.objectName = objectName;
040            ObjectName myObjectName = ObjectNameUtil.getObjectName(this.objectName);
041            verifyObjectName(myObjectName);
042            this.servers = servers;
043        }
044    
045        public String getObjectName() {
046            return objectName;
047        }
048    
049        public boolean isStateManageable() {
050            return true;
051        }
052    
053        public boolean isStatisticsProvider() {
054            return false;
055        }
056    
057        public boolean isEventProvider() {
058            return true;
059        }
060    
061        /**
062         * ObjectName must match this pattern:
063         * <p/>
064         * domain:j2eeType=J2EEDomain,name=domain
065         */
066        private void verifyObjectName(ObjectName objectName) {
067            if (objectName.isPattern()) {
068                throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
069            }
070            Hashtable keyPropertyList = objectName.getKeyPropertyList();
071            if (!"J2EEDomain".equals(keyPropertyList.get("j2eeType"))) {
072                throw new InvalidObjectNameException("J2EEDomain object name j2eeType property must be 'J2EEDomain'", objectName);
073            }
074            String name = (String) keyPropertyList.get("name");
075            if (!objectName.getDomain().equals(name)) {
076                throw new InvalidObjectNameException("Domain part of J2EEDomain object name must match name propert", objectName);
077            }
078        }
079    
080    
081        public String[] getServers() {
082            return Util.getObjectNames(getServerInstances());
083        }
084    
085        public J2EEServer[] getServerInstances() {
086            if (servers == null) return new J2EEServer[0];
087            return (J2EEServer[]) servers.toArray(new J2EEServer[servers.size()]);
088        }
089    
090        public static final GBeanInfo GBEAN_INFO;
091    
092        static {
093            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(J2EEDomainImpl.class, NameFactory.J2EE_DOMAIN);
094    
095            infoFactory.addReference("Servers", J2EEServer.class, NameFactory.J2EE_SERVER);
096            infoFactory.setConstructor(new String[]{"objectName", "Servers"});
097    
098            GBEAN_INFO = infoFactory.getBeanInfo();
099        }
100    
101        public static GBeanInfo getGBeanInfo() {
102            return GBEAN_INFO;
103        }
104    }