001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.management;
016:
017: import org.jmanage.core.util.JManageProperties;
018:
019: import javax.management.MalformedObjectNameException;
020: import java.util.StringTokenizer;
021:
022: /**
023: *
024: * date: Aug 12, 2004
025: * @author Rakesh Kalra
026: */
027: public class ObjectName implements java.io.Serializable {
028:
029: private final String objectName;
030: private final String canonicalName;
031:
032: public ObjectName(String objectName) {
033: this .objectName = objectName;
034: try {
035: this .canonicalName = new javax.management.ObjectName(
036: objectName).getCanonicalName();
037: } catch (MalformedObjectNameException e) {
038: throw new RuntimeException(e);
039: } catch (NullPointerException e) {
040: throw new RuntimeException(e);
041: }
042: }
043:
044: public ObjectName(String objectName, String canonicalName) {
045: this .objectName = objectName;
046: this .canonicalName = canonicalName;
047: }
048:
049: public String getCanonicalName() {
050: return canonicalName;
051: }
052:
053: public String toString() {
054: return objectName;
055: }
056:
057: public String getDisplayName() {
058: return JManageProperties.isDisplayCanonicalName() ? canonicalName
059: : objectName;
060: }
061:
062: public static String getShortName(String objectName) {
063: String shortName = objectName;
064: if (shortName.length() > 90) {
065: shortName = shortName.substring(0, 90) + "...";
066: }
067: return shortName;
068: }
069:
070: /**
071: * If the object name is longer than X characters, this method
072: * takes the first X characters, adds "..." and returns the result.
073: * @return
074: */
075: public String getShortName() {
076: return getShortName(getDisplayName());
077: }
078:
079: /**
080: * Wraps a long object name by added \n and \t characters.
081: *
082: * @return
083: */
084: public String getWrappedName() {
085: String wrappedName = getDisplayName();
086: if (wrappedName.length() > 80) {
087: StringTokenizer tokenizer = new StringTokenizer(
088: wrappedName, ",");
089: StringBuffer buff = new StringBuffer(wrappedName.length());
090: int multiplier = 1;
091: while (tokenizer.hasMoreTokens()) {
092: String nextToken = tokenizer.nextToken();
093: if ((buff.length() + nextToken.length()) > 80 * multiplier) {
094: buff.append("\n\t");
095: multiplier++;
096: }
097: buff.append(nextToken);
098: if (tokenizer.hasMoreTokens()) {
099: buff.append(",");
100: }
101: }
102: wrappedName = buff.toString();
103: }
104: return wrappedName;
105: }
106:
107: public boolean equals(Object obj) {
108: if (obj instanceof ObjectName) {
109: ObjectName on = (ObjectName) obj;
110: return on.objectName.equals(this .objectName);
111: }
112: return false;
113: }
114: }
|