001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.HashCode;
020    import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
021    import com.liferay.portal.kernel.util.StringPool;
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.MalformedObjectNameException;
032    import javax.management.ObjectName;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class MBean implements Serializable {
038    
039            public MBean(ObjectName objectName) {
040                    this(objectName.getDomain(), objectName.getKeyPropertyListString());
041    
042                    _objectName = objectName;
043            }
044    
045            public MBean(ObjectName objectName, MBeanInfo mBeanInfo) {
046                    _domainName = objectName.getDomain();
047                    _mBeanName = objectName.getKeyPropertyListString();
048                    _mBeanInfo = mBeanInfo;
049                    _loaded = true;
050            }
051    
052            public MBean(String domainName, String mBeanName) {
053                    _domainName = domainName;
054                    _mBeanName = mBeanName;
055            }
056    
057            @Override
058            public boolean equals(Object obj) {
059                    if (this == obj) {
060                            return true;
061                    }
062    
063                    if (!(obj instanceof MBean)) {
064                            return false;
065                    }
066    
067                    MBean mBean = (MBean)obj;
068    
069                    if (Validator.equals(_domainName, mBean._domainName) &&
070                            Validator.equals(_mBeanName, mBean._mBeanName)) {
071    
072                            return true;
073                    }
074    
075                    return false;
076            }
077    
078            public String getDomainName() {
079                    return _domainName;
080            }
081    
082            public MBeanInfo getMBeanInfo() {
083                    return _mBeanInfo;
084            }
085    
086            public String getMBeanName() {
087                    return _mBeanName;
088            }
089    
090            public ObjectName getObjectName() throws MalformedObjectNameException {
091                    if (_objectName == null) {
092                            _objectName = new ObjectName(
093                                    _domainName.concat(StringPool.COLON).concat(_mBeanName));
094                    }
095    
096                    return _objectName;
097            }
098    
099            public List<String> getPath() {
100                    if (_path == null) {
101                            String[] parts = StringUtil.split(_mBeanName);
102    
103                            _path = new ArrayList<String>(parts.length);
104    
105                            for (String part : parts) {
106                                    String[] kvp = StringUtil.split(part, StringPool.EQUAL);
107    
108                                    if (kvp.length != 2) {
109                                            _log.error("Invalid MBean name syntax " + _mBeanName);
110                                    }
111                                    else {
112                                            _path.add(kvp[1]);
113                                    }
114                            }
115                    }
116    
117                    return _path;
118            }
119    
120            @Override
121            public int hashCode() {
122                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
123    
124                    hashCode.append(_domainName);
125                    hashCode.append(_mBeanName);
126    
127                    return hashCode.toHashCode();
128            }
129    
130            public boolean isLoaded() {
131                    return _loaded;
132            }
133    
134            private static Log _log = LogFactoryUtil.getLog(MBean.class);
135    
136            private String _domainName;
137            private boolean _loaded;
138            private MBeanInfo _mBeanInfo;
139            private String _mBeanName;
140            private ObjectName _objectName;
141            private List<String> _path;
142    
143    }