001    /**
002     * Copyright (c) 2000-present 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.jmx.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.HashCode;
021    import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.io.Serializable;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    import javax.management.MBeanInfo;
031    import javax.management.ObjectName;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class MBean implements Serializable {
037    
038            public MBean(ObjectName objectName) {
039                    _objectName = objectName;
040    
041                    _domainName = objectName.getDomain();
042                    _mBeanName = objectName.getKeyPropertyListString();
043                    _loaded = false;
044                    _mBeanInfo = null;
045            }
046    
047            public MBean(ObjectName objectName, MBeanInfo mBeanInfo) {
048                    _objectName = objectName;
049                    _mBeanInfo = mBeanInfo;
050    
051                    _domainName = objectName.getDomain();
052                    _mBeanName = objectName.getKeyPropertyListString();
053                    _loaded = true;
054            }
055    
056            @Override
057            public boolean equals(Object obj) {
058                    if (this == obj) {
059                            return true;
060                    }
061    
062                    if (!(obj instanceof MBean)) {
063                            return false;
064                    }
065    
066                    MBean mBean = (MBean)obj;
067    
068                    if (Validator.equals(_domainName, mBean._domainName) &&
069                            Validator.equals(_mBeanName, mBean._mBeanName)) {
070    
071                            return true;
072                    }
073    
074                    return false;
075            }
076    
077            public String getDomainName() {
078                    return _domainName;
079            }
080    
081            public MBeanInfo getMBeanInfo() {
082                    return _mBeanInfo;
083            }
084    
085            public String getMBeanName() {
086                    return _mBeanName;
087            }
088    
089            public ObjectName getObjectName() {
090                    return _objectName;
091            }
092    
093            public List<String> getPath() {
094                    if (_path == null) {
095                            String[] parts = StringUtil.split(_mBeanName);
096    
097                            _path = new ArrayList<>(parts.length);
098    
099                            for (String part : parts) {
100                                    String[] kvp = StringUtil.split(part, CharPool.EQUAL);
101    
102                                    if (kvp.length != 2) {
103                                            _log.error("Invalid MBean name syntax " + _mBeanName);
104                                    }
105                                    else {
106                                            _path.add(kvp[1]);
107                                    }
108                            }
109                    }
110    
111                    return _path;
112            }
113    
114            @Override
115            public int hashCode() {
116                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
117    
118                    hashCode.append(_domainName);
119                    hashCode.append(_mBeanName);
120    
121                    return hashCode.toHashCode();
122            }
123    
124            public boolean isLoaded() {
125                    return _loaded;
126            }
127    
128            private static final Log _log = LogFactoryUtil.getLog(MBean.class);
129    
130            private final String _domainName;
131            private final boolean _loaded;
132            private final MBeanInfo _mBeanInfo;
133            private final String _mBeanName;
134            private final ObjectName _objectName;
135            private List<String> _path;
136    
137    }