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