001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.management.jmx;
016    
017    import com.liferay.portal.kernel.jmx.model.MBean;
018    import com.liferay.portal.kernel.management.ManageActionException;
019    
020    import java.util.HashSet;
021    import java.util.Set;
022    
023    import javax.management.MBeanServer;
024    import javax.management.MalformedObjectNameException;
025    import javax.management.ObjectName;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class ListMBeansAction extends BaseJMXManageAction<Set<MBean>> {
031    
032            public ListMBeansAction(String domainName) {
033                    _domainName = domainName;
034            }
035    
036            public Set<MBean> action() throws ManageActionException {
037                    try {
038                            MBeanServer mBeanServer = getMBeanServer();
039    
040                            Set<ObjectName> objectNames = mBeanServer.queryNames(
041                                    null, new ObjectName(_domainName.concat(":*")));
042    
043                            Set<MBean> mBeans = new HashSet<MBean>(objectNames.size());
044    
045                            for (ObjectName objectName : objectNames) {
046                                    mBeans.add(new MBean(objectName));
047                            }
048    
049                            return mBeans;
050                    }
051                    catch (MalformedObjectNameException mone) {
052                            throw new ManageActionException(mone);
053                    }
054            }
055    
056            private String _domainName;
057    
058    }