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