KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > stringifier > ObjectNameStringifier


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.jmx.stringifier;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Arrays JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34
35 import com.sun.appserv.management.util.stringifier.Stringifier;
36 import com.sun.appserv.management.util.misc.ListUtil;
37 import com.sun.appserv.management.util.misc.StringUtil;
38 import com.sun.appserv.management.util.misc.TypeCast;
39
40
41 /**
42     Stringifier for an ObjectName which sorts the properties in the ObjectName
43     for more consistent and readable output.
44  */

45 public final class ObjectNameStringifier implements Stringifier
46 {
47     public final static ObjectNameStringifier DEFAULT = new ObjectNameStringifier();
48     
49     private static List JavaDoc<String JavaDoc> PROPS = null;
50     
51         private synchronized static List JavaDoc<String JavaDoc>
52     getPROPS()
53     {
54         if ( PROPS == null )
55         {
56         PROPS = Collections.unmodifiableList( ListUtil.newListFromArray( new String JavaDoc[]
57             {
58                 "j2eeType","type",
59                 "name",
60                 
61                 "J2EEDomain",
62                 "J2EEServer",
63                 "JVM",
64                 "Node",
65                 "J2EEApplication",
66                 
67                 "AppClientModule",
68                 
69                 "EJBModule",
70                 "EntityBean",
71                 "StatefulSessionBean",
72                 "StatelessSessionBean",
73                 "MessageDrivenBean",
74                 
75                 "WebModule", "Servlet",
76                 
77                 "ResourceAdapterModule",
78                 "JavaMailResource",
79                 "JCAResource",
80                 "JCAConnectionFactory",
81                 "JCAManagedConnectionFactory",
82                 "JDBCResource",
83                 "JDBCDataSource",
84                 "JDBCDriver",
85                 "JMSResource",
86                 "JNDIResource",
87                 "JTAResource",
88                 "RMI_IIOPResource",
89                 "URL_Resource",
90                 
91             } ));
92         }
93         return( PROPS );
94     }
95     
96     
97     private List JavaDoc<String JavaDoc> mOrderedProps;
98     private boolean mOmitDomain;
99     
100         public
101     ObjectNameStringifier()
102     {
103         this( getPROPS() );
104     }
105     
106         public
107     ObjectNameStringifier( final List JavaDoc<String JavaDoc> props )
108     {
109         mOrderedProps = props;
110         mOmitDomain = false;
111     }
112     
113         public
114     ObjectNameStringifier( final String JavaDoc[] props )
115     {
116         this( ListUtil.newListFromArray( props ) );
117     }
118     
119     
120         private String JavaDoc
121     makeProp( final String JavaDoc name, final String JavaDoc value )
122     {
123         return( name + "=" + value );
124     }
125
126         public String JavaDoc
127     stringify( Object JavaDoc o )
128     {
129         if ( o == null )
130         {
131             return( "null" );
132         }
133
134
135         final ObjectName JavaDoc on = (ObjectName JavaDoc)o;
136         
137         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
138         if ( ! mOmitDomain )
139         {
140             buf.append( on.getDomain() + ":" );
141         }
142         
143         final Map JavaDoc<String JavaDoc,String JavaDoc> props = TypeCast.asMap( on.getKeyPropertyList() );
144         
145         final List JavaDoc<String JavaDoc> ordered = new ArrayList JavaDoc<String JavaDoc>( mOrderedProps );
146         ordered.retainAll( props.keySet() );
147         
148         // go through each ordered property, and if it exists, emit it
149
final Iterator JavaDoc iter = ordered.iterator();
150         while ( iter.hasNext() && props.keySet().size() >= 2 )
151         {
152             final String JavaDoc key = (String JavaDoc)iter.next();
153             final String JavaDoc value = (String JavaDoc)props.get( key );
154             if ( value != null )
155             {
156                 buf.append( makeProp( key, value ) + "," );
157                 props.remove( key );
158             }
159         }
160         
161         // emit all remaining properties in order
162
final Set JavaDoc<String JavaDoc> remainingSet = props.keySet();
163         final String JavaDoc[] remaining = new String JavaDoc[ remainingSet.size() ];
164         remainingSet.toArray( remaining );
165         Arrays.sort( remaining );
166         
167         for( int i = 0; i < remaining.length; ++i )
168         {
169             final String JavaDoc key = remaining[ i ];
170             final String JavaDoc value = (String JavaDoc)props.get( key );
171             buf.append( makeProp( key, value ) + "," );
172         }
173         
174         final String JavaDoc result = StringUtil.stripSuffix( buf.toString(), "," );
175
176         return( result );
177     }
178     
179         public List JavaDoc
180     getProps()
181     {
182         return( mOrderedProps );
183     }
184     
185         public void
186     setProps( final List JavaDoc<String JavaDoc> props )
187     {
188         mOrderedProps = props;
189     }
190     
191     
192         public boolean
193     getOmitDomain()
194     {
195         return( mOmitDomain );
196     }
197     
198         public void
199     setOmitDomain( final boolean omit )
200     {
201         mOmitDomain = omit;
202     }
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
Popular Tags