001    /**
002     * Copyright (c) 2000-2013 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            @Override
037            public Set<MBean> action() throws ManageActionException {
038                    try {
039                            MBeanServer mBeanServer = getMBeanServer();
040    
041                            Set<ObjectName> objectNames = mBeanServer.queryNames(
042                                    null, new ObjectName(_domainName.concat(":*")));
043    
044                            Set<MBean> mBeans = new HashSet<MBean>(objectNames.size());
045    
046                            for (ObjectName objectName : objectNames) {
047                                    mBeans.add(new MBean(objectName));
048                            }
049    
050                            return mBeans;
051                    }
052                    catch (MalformedObjectNameException mone) {
053                            throw new ManageActionException(mone);
054                    }
055            }
056    
057            private String _domainName;
058    
059    }