1 23 package com.sun.appserv.management.util.jmx.stringifier; 24 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 import java.util.Arrays ; 32 33 import javax.management.ObjectName ; 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 45 public final class ObjectNameStringifier implements Stringifier 46 { 47 public final static ObjectNameStringifier DEFAULT = new ObjectNameStringifier(); 48 49 private static List <String > PROPS = null; 50 51 private synchronized static List <String > 52 getPROPS() 53 { 54 if ( PROPS == null ) 55 { 56 PROPS = Collections.unmodifiableList( ListUtil.newListFromArray( new String [] 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 <String > mOrderedProps; 98 private boolean mOmitDomain; 99 100 public 101 ObjectNameStringifier() 102 { 103 this( getPROPS() ); 104 } 105 106 public 107 ObjectNameStringifier( final List <String > props ) 108 { 109 mOrderedProps = props; 110 mOmitDomain = false; 111 } 112 113 public 114 ObjectNameStringifier( final String [] props ) 115 { 116 this( ListUtil.newListFromArray( props ) ); 117 } 118 119 120 private String 121 makeProp( final String name, final String value ) 122 { 123 return( name + "=" + value ); 124 } 125 126 public String 127 stringify( Object o ) 128 { 129 if ( o == null ) 130 { 131 return( "null" ); 132 } 133 134 135 final ObjectName on = (ObjectName )o; 136 137 final StringBuffer buf = new StringBuffer (); 138 if ( ! mOmitDomain ) 139 { 140 buf.append( on.getDomain() + ":" ); 141 } 142 143 final Map <String ,String > props = TypeCast.asMap( on.getKeyPropertyList() ); 144 145 final List <String > ordered = new ArrayList <String >( mOrderedProps ); 146 ordered.retainAll( props.keySet() ); 147 148 final Iterator iter = ordered.iterator(); 150 while ( iter.hasNext() && props.keySet().size() >= 2 ) 151 { 152 final String key = (String )iter.next(); 153 final String value = (String )props.get( key ); 154 if ( value != null ) 155 { 156 buf.append( makeProp( key, value ) + "," ); 157 props.remove( key ); 158 } 159 } 160 161 final Set <String > remainingSet = props.keySet(); 163 final String [] remaining = new String [ remainingSet.size() ]; 164 remainingSet.toArray( remaining ); 165 Arrays.sort( remaining ); 166 167 for( int i = 0; i < remaining.length; ++i ) 168 { 169 final String key = remaining[ i ]; 170 final String value = (String )props.get( key ); 171 buf.append( makeProp( key, value ) + "," ); 172 } 173 174 final String result = StringUtil.stripSuffix( buf.toString(), "," ); 175 176 return( result ); 177 } 178 179 public List 180 getProps() 181 { 182 return( mOrderedProps ); 183 } 184 185 public void 186 setProps( final List <String > 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 |