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 javax.management.AttributeList;
021    import javax.management.MBeanAttributeInfo;
022    import javax.management.MBeanInfo;
023    import javax.management.MBeanServer;
024    import javax.management.ObjectName;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class GetAttributesAction extends BaseJMXManageAction<AttributeList> {
030    
031            public GetAttributesAction(MBean mBean) {
032                    _mBean = mBean;
033            }
034    
035            @Override
036            public AttributeList action() throws ManageActionException {
037                    try {
038                            ObjectName objectName = _mBean.getObjectName();
039    
040                            MBeanServer mBeanServer = getMBeanServer();
041    
042                            MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName);
043    
044                            MBeanAttributeInfo[] mBeanAttributeInfos =
045                                    mBeanInfo.getAttributes();
046    
047                            String[] attributeNames = new String[mBeanAttributeInfos.length];
048    
049                            for (int i = 0; i < attributeNames.length; i++) {
050                                    attributeNames[i] = mBeanAttributeInfos[i].getName();
051                            }
052    
053                            return mBeanServer.getAttributes(objectName, attributeNames);
054                    }
055                    catch (Exception e) {
056                            throw new ManageActionException(e);
057                    }
058            }
059    
060            private MBean _mBean;
061    
062    }