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.ArrayList;
021    import java.util.Collection;
022    import java.util.Hashtable;
023    import javax.management.ObjectName;
024    
025    import org.apache.geronimo.gbean.GBeanInfo;
026    import org.apache.geronimo.gbean.GBeanInfoBuilder;
027    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
028    import org.apache.geronimo.kernel.ObjectNameUtil;
029    import org.apache.geronimo.kernel.config.ConfigurationManager;
030    import org.apache.geronimo.kernel.repository.ListableRepository;
031    import org.apache.geronimo.kernel.repository.WritableListableRepository;
032    import org.apache.geronimo.management.AppClientModule;
033    import org.apache.geronimo.management.EJBModule;
034    import org.apache.geronimo.management.J2EEDeployedObject;
035    import org.apache.geronimo.management.J2EEResource;
036    import org.apache.geronimo.management.geronimo.EJBManager;
037    import org.apache.geronimo.management.geronimo.J2EEApplication;
038    import org.apache.geronimo.management.geronimo.J2EEServer;
039    import org.apache.geronimo.management.geronimo.JMSManager;
040    import org.apache.geronimo.management.geronimo.JVM;
041    import org.apache.geronimo.management.geronimo.KeystoreManager;
042    import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
043    import org.apache.geronimo.management.geronimo.SecurityRealm;
044    import org.apache.geronimo.management.geronimo.WebManager;
045    import org.apache.geronimo.management.geronimo.WebModule;
046    import org.apache.geronimo.system.serverinfo.ServerInfo;
047    import org.apache.geronimo.system.threads.ThreadPool;
048    
049    /**
050     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
051     */
052    public class J2EEServerImpl implements J2EEServer {
053        private static final String SERVER_VENDOR = "The Apache Software Foundation";
054        private final String objectName;
055        private final ServerInfo serverInfo;
056        private final Collection jvms;
057        private final Collection resources;
058        private final Collection j2eeApplications;
059        private final Collection appClientModules;
060        private final Collection webModules;
061        private final Collection ejbModules;
062        private final Collection resourceAdapterModules;
063        private final Collection webManagers;
064        private final Collection ejbManagers;
065        private final Collection jmsManagers;
066        private final Collection threadPools;
067        private final Collection repositories;
068        private final Collection writableRepos;
069        private final Collection securityRealms;
070        private final Collection keystoreManagers;
071        private final ConfigurationManager configurationManager;
072    
073        public J2EEServerImpl(String objectName,
074                              ServerInfo serverInfo,
075                              Collection jvms,
076                              Collection resources,
077                              Collection applications,
078                              Collection appClientModules,
079                              Collection webModules,
080                              Collection ejbModules,
081                              Collection resourceAdapterModules,
082                              Collection webManagers,
083                              Collection ejbManagers,
084                              Collection jmsManagers,
085                              Collection threadPools,
086                              Collection repositories,
087                              Collection writableRepos,
088                              Collection securityRealms,
089                              Collection keystoreManagers,
090                              ConfigurationManager configurationManager) {
091    
092            this.objectName = objectName;
093            ObjectName myObjectName = ObjectNameUtil.getObjectName(this.objectName);
094            verifyObjectName(myObjectName);
095    
096            this.serverInfo = serverInfo;
097            this.jvms = jvms;
098            this.resources = resources;
099    
100            this.j2eeApplications = applications;
101            this.appClientModules = appClientModules;
102            this.webModules = webModules;
103            this.ejbModules = ejbModules;
104            this.resourceAdapterModules = resourceAdapterModules;
105    
106            this.webManagers = webManagers;
107            this.ejbManagers = ejbManagers;
108            this.jmsManagers = jmsManagers;
109    
110            this.threadPools = threadPools;
111            this.repositories = repositories;
112            this.writableRepos = writableRepos;
113            this.securityRealms = securityRealms;
114            this.keystoreManagers = keystoreManagers;
115            this.configurationManager = configurationManager;
116        }
117    
118        public String getObjectName() {
119            return objectName;
120        }
121    
122        public boolean isStateManageable() {
123            return true;
124        }
125    
126        public boolean isStatisticsProvider() {
127            return false;
128        }
129    
130        public boolean isEventProvider() {
131            return true;
132        }
133    
134        /**
135         * ObjectName must match this pattern:
136         * <p/>
137         * domain:j2eeType=J2EEServer,name=MyName
138         * @param objectName object name to verify pattern
139         */
140        private void verifyObjectName(ObjectName objectName) {
141            if (objectName.isPattern()) {
142                throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
143            }
144            Hashtable keyPropertyList = objectName.getKeyPropertyList();
145            if (!"J2EEServer".equals(keyPropertyList.get("j2eeType"))) {
146                throw new InvalidObjectNameException("J2EEServer object name j2eeType property must be 'J2EEServer'", objectName);
147            }
148            if (!keyPropertyList.containsKey("name")) {
149                throw new InvalidObjectNameException("J2EEServer object must contain a name property", objectName);
150            }
151            if (keyPropertyList.size() != 2) {
152                throw new InvalidObjectNameException("J2EEServer object name can only have j2eeType, and name", objectName);
153            }
154        }
155    
156    
157        public String[] getDeployedObjects() {
158            return Util.getObjectNames(getDeployedObjectInstances());
159        }
160    
161        public J2EEDeployedObject[] getDeployedObjectInstances() {
162            ArrayList objects = new ArrayList();
163            if (j2eeApplications != null) {
164                objects.addAll(j2eeApplications);
165            }
166            if (appClientModules  != null) {
167                objects.addAll(appClientModules);
168            }
169            if (ejbModules  != null) {
170                objects.addAll(ejbModules);
171            }
172            if (webModules  != null) {
173                objects.addAll(webModules);
174            }
175            if (resourceAdapterModules != null) {
176                objects.addAll(resourceAdapterModules);
177            }
178    
179            return (J2EEDeployedObject[]) objects.toArray(new J2EEDeployedObject[objects.size()]);
180        }
181    
182        public String[] getResources() {
183            return Util.getObjectNames(getResourceInstances());
184        }
185    
186        public J2EEResource[] getResourceInstances() {
187            if (resources == null) return new J2EEResource[0];
188            return (J2EEResource[]) resources.toArray(new J2EEResource[resources.size()]);
189        }
190    
191        public String[] getJavaVMs() {
192            return Util.getObjectNames(getJavaVMInstances());
193        }
194    
195        public JVM[] getJavaVMInstances() {
196            if (jvms == null) return new JVM[0];
197            return (JVM[]) jvms.toArray(new JVM[jvms.size()]);
198        }
199    
200        public J2EEApplication[] getApplications() {
201            if (j2eeApplications == null) return new J2EEApplication[0];
202            return (J2EEApplication[]) j2eeApplications.toArray(new J2EEApplication[j2eeApplications.size()]);
203        }
204    
205        public AppClientModule[] getAppClients() {
206            if (appClientModules == null) return new AppClientModule[0];
207            return (AppClientModule[]) appClientModules.toArray(new AppClientModule[appClientModules.size()]);
208        }
209    
210        public WebModule[] getWebModules() {
211            if (webModules == null) return new WebModule[0];
212            return (WebModule[]) webModules.toArray(new WebModule[webModules.size()]);
213        }
214    
215        public EJBModule[] getEJBModules() {
216            if (ejbModules == null) return new EJBModule[0];
217            return (EJBModule[]) ejbModules.toArray(new EJBModule[ejbModules.size()]);
218        }
219    
220        public ResourceAdapterModule[] getResourceAdapterModules() {
221            if (resourceAdapterModules == null) return new ResourceAdapterModule[0];
222            return (ResourceAdapterModule[]) resourceAdapterModules.toArray(new ResourceAdapterModule[resourceAdapterModules.size()]);
223        }
224    
225    
226        public WebManager[] getWebManagers() {
227            if (webManagers == null) return new WebManager[0];
228            return (WebManager[]) webManagers.toArray(new WebManager[webManagers.size()]);
229        }
230    
231        public EJBManager[] getEJBManagers() {
232            if (ejbManagers == null) return new EJBManager[0];
233            return (EJBManager[]) ejbManagers.toArray(new EJBManager[ejbManagers.size()]);
234        }
235    
236        public JMSManager[] getJMSManagers() {
237            if (jmsManagers == null) return new JMSManager[0];
238            return (JMSManager[]) jmsManagers.toArray(new JMSManager[jmsManagers.size()]);
239        }
240    
241        public ThreadPool[] getThreadPools() {
242            if (threadPools == null) return new ThreadPool[0];
243            return (ThreadPool[]) threadPools.toArray(new ThreadPool[threadPools.size()]);
244        }
245    
246        public ListableRepository[] getRepositories() {
247            if (repositories == null) return new ListableRepository[0];
248            return (ListableRepository[]) repositories.toArray(new ListableRepository[repositories.size()]);
249        }
250    
251        public WritableListableRepository[] getWritableRepositories() {
252            if (writableRepos == null) return new WritableListableRepository[0];
253            return (WritableListableRepository[]) writableRepos.toArray(new WritableListableRepository[writableRepos.size()]);
254        }
255    
256        public SecurityRealm[] getSecurityRealms() {
257            if (securityRealms == null) return new SecurityRealm[0];
258            return (SecurityRealm[]) securityRealms.toArray(new SecurityRealm[securityRealms.size()]);
259        }
260    
261        public ServerInfo getServerInfo() {
262            return serverInfo;
263        }
264    
265        public KeystoreManager getKeystoreManager() {
266            if (keystoreManagers == null) return null;
267            return (KeystoreManager) keystoreManagers.iterator().next();
268        }
269    
270        public ConfigurationManager getConfigurationManager() {
271            return configurationManager;
272        }
273    
274        public String getServerVendor() {
275            return SERVER_VENDOR;
276        }
277    
278        public String getServerVersion() {
279            return serverInfo.getVersion();
280        }
281    
282        public static final GBeanInfo GBEAN_INFO;
283    
284        static {
285            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(J2EEServerImpl.class, NameFactory.J2EE_SERVER);
286    
287            infoFactory.addReference("ServerInfo", ServerInfo.class, NameFactory.GERONIMO_SERVICE);
288            infoFactory.addReference("JVMs", JVM.class, NameFactory.JVM);
289            infoFactory.addReference("Resources", J2EEResource.class); // several types match this
290            infoFactory.addReference("Applications", J2EEApplication.class, NameFactory.J2EE_APPLICATION);
291            infoFactory.addReference("AppClientModules", AppClientModule.class, NameFactory.APP_CLIENT_MODULE);
292            infoFactory.addReference("WebModules", WebModule.class, NameFactory.WEB_MODULE);
293            infoFactory.addReference("EJBModules", EJBModule.class, NameFactory.EJB_MODULE);
294            infoFactory.addReference("ResourceAdapterModules", ResourceAdapterModule.class, NameFactory.RESOURCE_ADAPTER_MODULE);
295            infoFactory.addReference("WebManagers", WebManager.class);
296            infoFactory.addReference("EJBManagers", EJBManager.class);
297            infoFactory.addReference("JMSManagers", JMSManager.class);
298            infoFactory.addReference("ThreadPools", ThreadPool.class);
299            infoFactory.addReference("Repositories", ListableRepository.class);
300            infoFactory.addReference("WritableRepos", WritableListableRepository.class);
301            infoFactory.addReference("SecurityRealms", SecurityRealm.class);
302            infoFactory.addReference("KeystoreManagers", KeystoreManager.class);
303            infoFactory.addReference("ConfigurationManager", ConfigurationManager.class);
304    
305            infoFactory.setConstructor(new String[]{
306                    "objectName",
307                    "ServerInfo",
308                    "JVMs",
309                    "Resources",
310                    "Applications",
311                    "AppClientModules",
312                    "WebModules",
313                    "EJBModules",
314                    "ResourceAdapterModules",
315                    "WebManagers",
316                    "EJBManagers",
317                    "JMSManagers",
318                    "ThreadPools",
319                    "Repositories",
320                    "WritableRepos",
321                    "SecurityRealms",
322                    "KeystoreManagers",
323                    "ConfigurationManager",
324            });
325    
326            GBEAN_INFO = infoFactory.getBeanInfo();
327        }
328    
329        public static GBeanInfo getGBeanInfo() {
330            return GBEAN_INFO;
331        }
332    }