1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.jmx.model;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.HashCode;
20  import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.io.Serializable;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import javax.management.MBeanInfo;
31  import javax.management.MalformedObjectNameException;
32  import javax.management.ObjectName;
33  
34  /**
35   * <a href="MBean.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Shuyang Zhou
38   */
39  public class MBean implements Serializable {
40  
41      public MBean(ObjectName objectName) {
42          this(objectName.getDomain(), objectName.getKeyPropertyListString());
43  
44          _objectName = objectName;
45      }
46  
47      public MBean(ObjectName objectName, MBeanInfo mBeanInfo) {
48          _domainName = objectName.getDomain();
49          _mBeanName = objectName.getKeyPropertyListString();
50          _mBeanInfo = mBeanInfo;
51          _loaded = true;
52      }
53  
54      public MBean(String domainName, String mBeanName) {
55          _domainName = domainName;
56          _mBeanName = mBeanName;
57      }
58  
59      public boolean equals(Object obj) {
60          if (this == obj) {
61              return true;
62          }
63  
64          if (!(obj instanceof MBean)) {
65              return false;
66          }
67  
68          MBean mBean = (MBean)obj;
69  
70          if (Validator.equals(_domainName, mBean._domainName) &&
71              Validator.equals(_mBeanName, mBean._mBeanName)) {
72  
73              return true;
74          }
75  
76          return false;
77      }
78  
79      public String getDomainName() {
80          return _domainName;
81      }
82  
83      public MBeanInfo getMBeanInfo() {
84          return _mBeanInfo;
85      }
86  
87      public String getMBeanName() {
88          return _mBeanName;
89      }
90  
91      public ObjectName getObjectName() throws MalformedObjectNameException {
92          if (_objectName == null) {
93              _objectName = new ObjectName(
94                  _domainName.concat(StringPool.COLON).concat(_mBeanName));
95          }
96  
97          return _objectName;
98      }
99  
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, StringPool.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     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 }