001    /**
002     * Copyright (c) 2000-2010 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.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            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() throws MalformedObjectNameException {
090                    if (_objectName == null) {
091                            _objectName = new ObjectName(
092                                    _domainName.concat(StringPool.COLON).concat(_mBeanName));
093                    }
094    
095                    return _objectName;
096            }
097    
098            public List<String> getPath() {
099                    if (_path == null) {
100                            String[] parts = StringUtil.split(_mBeanName);
101    
102                            _path = new ArrayList<String>(parts.length);
103    
104                            for (String part : parts) {
105                                    String[] kvp = StringUtil.split(part, StringPool.EQUAL);
106    
107                                    if (kvp.length != 2) {
108                                            _log.error("Invalid MBean name syntax " + _mBeanName);
109                                    }
110                                    else {
111                                            _path.add(kvp[1]);
112                                    }
113                            }
114                    }
115    
116                    return _path;
117            }
118    
119            public int hashCode() {
120                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
121    
122                    hashCode.append(_domainName);
123                    hashCode.append(_mBeanName);
124    
125                    return hashCode.toHashCode();
126            }
127    
128            public boolean isLoaded() {
129                    return _loaded;
130            }
131    
132            private static Log _log = LogFactoryUtil.getLog(MBean.class);
133    
134            private String _domainName;
135            private boolean _loaded;
136            private MBeanInfo _mBeanInfo;
137            private String _mBeanName;
138            private ObjectName _objectName;
139            private List<String> _path;
140    
141    }