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    package org.apache.geronimo.j2ee.management.impl;
018    
019    import java.util.Hashtable;
020    import java.util.Collection;
021    import java.util.ArrayList;
022    import javax.management.ObjectName;
023    import org.apache.geronimo.gbean.GBeanInfo;
024    import org.apache.geronimo.gbean.GBeanInfoBuilder;
025    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
026    import org.apache.geronimo.kernel.ObjectNameUtil;
027    import org.apache.geronimo.management.AppClientModule;
028    import org.apache.geronimo.management.EJBModule;
029    import org.apache.geronimo.management.J2EEModule;
030    import org.apache.geronimo.management.J2EEResource;
031    import org.apache.geronimo.management.geronimo.J2EEApplication;
032    import org.apache.geronimo.management.geronimo.J2EEServer;
033    import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
034    import org.apache.geronimo.management.geronimo.WebModule;
035    
036    /**
037     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
038     */
039    public class J2EEApplicationImpl implements J2EEApplication {
040        private final String objectName;
041        private final String deploymentDescriptor;
042        private final J2EEServer server;
043        private final Collection resources;
044        private final Collection appClientModules;
045        private final Collection ejbModules;
046        private final Collection resourceAdapterModules;
047        private final Collection webModules;
048    
049        public J2EEApplicationImpl(String objectName,
050                String deploymentDescriptor,
051                J2EEServer server,
052                Collection resources,
053                Collection appClientModules,
054                Collection ejbModules,
055                Collection resourceAdapterModules,
056                Collection webModules) {
057    
058            this.objectName = objectName;
059            ObjectName myObjectName = ObjectNameUtil.getObjectName(this.objectName);
060            verifyObjectName(myObjectName);
061    
062            this.deploymentDescriptor = deploymentDescriptor;
063            this.server = server;
064            this.resources = resources;
065            this.appClientModules = appClientModules;
066            this.ejbModules = ejbModules;
067            this.resourceAdapterModules = resourceAdapterModules;
068            this.webModules = webModules;
069        }
070    
071        public String getObjectName() {
072            return objectName;
073        }
074    
075        public boolean isStateManageable() {
076            return true;
077        }
078    
079        public boolean isStatisticsProvider() {
080            return false;
081        }
082    
083        public boolean isEventProvider() {
084            return true;
085        }
086    
087        /**
088         * ObjectName must match this pattern:
089         * <p/>
090         * domain:j2eeType=J2EEApplication,name=MyName,J2EEServer=MyServer
091         */
092        private void verifyObjectName(ObjectName objectName) {
093            if (objectName.isPattern()) {
094                throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
095            }
096            Hashtable keyPropertyList = objectName.getKeyPropertyList();
097            if (!"J2EEApplication".equals(keyPropertyList.get("j2eeType"))) {
098                throw new InvalidObjectNameException("J2EEApplication object name j2eeType property must be 'J2EEApplication'", objectName);
099            }
100            if (!keyPropertyList.containsKey("name")) {
101                throw new InvalidObjectNameException("J2EEApplication object must contain a J2EEServer property", objectName);
102            }
103            if (!keyPropertyList.containsKey("J2EEServer")) {
104                throw new InvalidObjectNameException("J2EEApplication object name must contain a J2EEServer property", objectName);
105            }
106            if (keyPropertyList.size() != 3) {
107                throw new InvalidObjectNameException("J2EEApplication object name can only have j2eeType, name, and J2EEServer properties", objectName);
108            }
109        }
110    
111        public String[] getModules() {
112            return Util.getObjectNames(getModulesInstances());
113        }
114    
115        public J2EEModule[] getModulesInstances() {
116            ArrayList objects = new ArrayList();
117            if (appClientModules != null) {
118                objects.addAll(appClientModules);
119            }
120            if (ejbModules != null) {
121                objects.addAll(ejbModules);
122            }
123            if (webModules != null) {
124                objects.addAll(webModules);
125            }
126            if (resourceAdapterModules != null) {
127                objects.addAll(resourceAdapterModules);
128            }
129    
130            return (J2EEModule[]) objects.toArray(new J2EEModule[objects.size()]);
131        }
132    
133        public J2EEResource[] getResources() {
134            if (resources == null) return new J2EEResource[0];
135            return (J2EEResource[]) resources.toArray(new J2EEResource[resources.size()]);
136        }
137    
138        public AppClientModule[] getClientModules() {
139            if (appClientModules == null) return new AppClientModule[0];
140            return (AppClientModule[]) appClientModules.toArray(new AppClientModule[appClientModules.size()]);
141        }
142    
143        public EJBModule[] getEJBModules() {
144            if (ejbModules == null) return new EJBModule[0];
145            return (EJBModule[]) ejbModules.toArray(new EJBModule[ejbModules.size()]);
146        }
147    
148        public ResourceAdapterModule[] getRAModules() {
149            if (resourceAdapterModules == null) return new ResourceAdapterModule[0];
150            return (ResourceAdapterModule[]) resourceAdapterModules.toArray(new ResourceAdapterModule[resourceAdapterModules.size()]);
151        }
152    
153        public WebModule[] getWebModules() {
154            if (webModules == null) return new WebModule[0];
155            return (WebModule[]) webModules.toArray(new WebModule[webModules.size()]);
156        }
157    
158        public String getDeploymentDescriptor() {
159            return deploymentDescriptor;
160        }
161    
162        public String getServer() {
163            return server.getObjectName();
164        }
165    
166        public static final GBeanInfo GBEAN_INFO;
167    
168        static {
169            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(J2EEApplicationImpl.class, NameFactory.J2EE_APPLICATION);
170            infoFactory.addAttribute("deploymentDescriptor", String.class, true);
171            infoFactory.addReference("Server", J2EEServer.class);
172            infoFactory.addReference("Resources", J2EEResource.class);
173            infoFactory.addReference("AppClientModules", AppClientModule.class);
174            infoFactory.addReference("EJBModules", EJBModule.class);
175            infoFactory.addReference("ResourceAdapterModules", ResourceAdapterModule.class);
176            infoFactory.addReference("WebModules", WebModule.class);
177    
178            infoFactory.setConstructor(new String[]{
179                    "objectName",
180                    "deploymentDescriptor",
181                    "Server",
182                    "Resources",
183                    "AppClientModules",
184                    "EJBModules",
185                    "ResourceAdapterModules",
186                    "WebModules",
187            });
188    
189            GBEAN_INFO = infoFactory.getBeanInfo();
190        }
191    
192        public static GBeanInfo getGBeanInfo() {
193            return GBEAN_INFO;
194        }
195    }