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