KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > ObjectName


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.management;
17
18 import org.jmanage.core.util.JManageProperties;
19
20 import javax.management.MalformedObjectNameException JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  *
25  * date: Aug 12, 2004
26  * @author Rakesh Kalra
27  */

28 public class ObjectName implements java.io.Serializable JavaDoc {
29
30     private final String JavaDoc objectName;
31     private final String JavaDoc canonicalName;
32
33     public ObjectName(String JavaDoc objectName){
34         this.objectName = objectName;
35         try {
36             this.canonicalName =
37                     new javax.management.ObjectName JavaDoc(objectName).getCanonicalName();
38         } catch (MalformedObjectNameException e) {
39             throw new RuntimeException JavaDoc(e);
40         } catch (NullPointerException JavaDoc e) {
41             throw new RuntimeException JavaDoc(e);
42         }
43     }
44
45     public ObjectName(String JavaDoc objectName, String JavaDoc canonicalName){
46         this.objectName = objectName;
47         this.canonicalName = canonicalName;
48     }
49
50     public String JavaDoc getCanonicalName(){
51         return canonicalName;
52     }
53
54     public String JavaDoc toString(){
55         return objectName;
56     }
57
58     public String JavaDoc getDisplayName(){
59         return JManageProperties.isDisplayCanonicalName()?
60                 canonicalName : objectName;
61     }
62
63     public static String JavaDoc getShortName(String JavaDoc objectName){
64         String JavaDoc shortName = objectName;
65         if(shortName.length() > 90){
66             shortName = shortName.substring(0, 90) + "...";
67         }
68         return shortName;
69     }
70
71     /**
72      * If the object name is longer than X characters, this method
73      * takes the first X characters, adds "..." and returns the result.
74      * @return
75      */

76     public String JavaDoc getShortName(){
77         return getShortName(getDisplayName());
78     }
79
80     /**
81      * Wraps a long object name by added \n and \t characters.
82      *
83      * @return
84      */

85     public String JavaDoc getWrappedName(){
86         String JavaDoc wrappedName = getDisplayName();
87         if(wrappedName.length() > 80){
88             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(wrappedName, ",");
89             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(wrappedName.length());
90             int multiplier = 1;
91             while(tokenizer.hasMoreTokens()){
92                 String JavaDoc nextToken = tokenizer.nextToken();
93                 if((buff.length() + nextToken.length()) > 80 * multiplier){
94                     buff.append("\n\t");
95                     multiplier ++;
96                 }
97                 buff.append(nextToken);
98                 if(tokenizer.hasMoreTokens()){
99                     buff.append(",");
100                 }
101             }
102             wrappedName = buff.toString();
103         }
104         return wrappedName;
105     }
106
107     public boolean equals(Object JavaDoc obj){
108         if(obj instanceof ObjectName){
109             ObjectName on = (ObjectName)obj;
110             return on.objectName.equals(this.objectName);
111         }
112         return false;
113     }
114 }
115
Popular Tags