1 23 24 package com.sun.enterprise.management.ext.logging; 25 26 import java.io.IOException ; 27 28 import java.util.Set ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.Properties ; 33 import java.util.Date ; 34 import java.util.logging.Level ; 35 import java.lang.reflect.Method ; 36 37 import java.io.Serializable ; 38 39 import javax.management.MBeanServerConnection ; 40 import javax.management.MBeanServerInvocationHandler ; 41 import javax.management.NotificationEmitter ; 42 import javax.management.NotificationListener ; 43 import javax.management.Notification ; 44 import javax.management.NotificationFilter ; 45 import javax.management.MBeanNotificationInfo ; 46 import javax.management.ListenerNotFoundException ; 47 import javax.management.JMException ; 48 import javax.management.ObjectName ; 49 import javax.management.MBeanInfo ; 50 import javax.management.Attribute ; 51 import javax.management.AttributeList ; 52 import javax.management.MBeanAttributeInfo ; 53 54 55 import com.sun.appserv.management.base.Util; 56 57 import com.sun.appserv.management.ext.logging.LogRecordFields; 58 import com.sun.appserv.management.ext.logging.LogQueryResult; 59 import com.sun.appserv.management.ext.logging.LogQueryResultImpl; 60 import com.sun.appserv.management.ext.logging.LogQueryEntry; 61 import com.sun.appserv.management.ext.logging.LogQueryEntryImpl; 62 63 import com.sun.appserv.management.util.stringifier.ArrayStringifier; 64 import com.sun.appserv.management.util.jmx.stringifier.AttributeStringifier; 65 import com.sun.appserv.management.util.jmx.stringifier.AttributeListStringifier; 66 import com.sun.appserv.management.util.misc.TypeCast; 67 68 69 import com.sun.enterprise.management.AMXTestBase; 70 import com.sun.enterprise.management.Capabilities; 71 72 import com.sun.enterprise.management.ext.logging.LogMBeanIntf; 73 74 78 public final class LogMBeanTest extends AMXTestBase 79 { 80 private LogMBeanIntf mLogMBean; 81 final ObjectName mTarget; 82 83 public 84 LogMBeanTest( ) 85 { 86 mTarget = 87 Util.newObjectName( "com.sun.appserv:name=logmanager,category=runtime,server=server" ); 88 89 mLogMBean = initLogMBean(); 90 } 91 92 public static Capabilities 93 getCapabilities() 94 { 95 return getOfflineCapableCapabilities( false ); 96 } 97 98 private void 99 addListener( final LogMBeanIntf logMBean ) 100 { 101 final MyLogMBeanListener listener = new MyLogMBeanListener(); 102 final NotificationFilter filter = null; 103 logMBean.addNotificationListener( listener, filter, null ); 104 } 105 106 public LogMBeanIntf 107 initLogMBean() 108 { 109 final LogMBeanIntf logMBean = (LogMBeanIntf) 110 MBeanServerInvocationHandler.newProxyInstance( 111 getConnection(), mTarget, LogMBeanIntf.class, true ); 112 113 return logMBean; 114 } 115 116 public LogMBeanIntf 117 getLogMBean() 118 { 119 return mLogMBean; 120 } 121 122 123 public void 124 testGetArchivedLogFiles() 125 { 126 final String [] names = getLogMBean().getArchivedLogfiles(); 127 } 129 130 131 public void 132 testGetLogFilesDirectory() 133 { 134 final String name = getLogMBean().getLogFilesDirectory(); 135 } 137 138 public void 139 testGetLoggerNames() 140 { 141 final List <String > names = TypeCast.asList( getLogMBean().getLoggerNames() ); 142 TypeCast.checkList( names, String .class ); 143 145 for( final String name : names ) 146 { 147 final String level = getLogMBean().getLogLevel( name ); 148 Level.parse( level ); 149 150 final List <String > unders = TypeCast.asList( getLogMBean().getLoggerNamesUnder( name ) ); 151 TypeCast.checkList( unders, String .class ); 152 } 153 } 154 155 private void 156 displayAttribute( final Attribute attr ) 157 { 158 trace( "Attribute: " + attr.getName() ); 159 final Object value = attr.getValue(); 160 trace( " Value: " + (value == null ? "null" : value.getClass().getName()) ); 161 } 162 163 private LogQueryResult 164 convertQueryResult( final AttributeList queryResult ) 165 { 166 final AttributeList fieldAttrs = (AttributeList )((Attribute )queryResult.get( 0 )).getValue(); 168 final String [] fieldHeaders = new String [ fieldAttrs.size() ]; 169 assert( fieldHeaders.length == LogRecordFields.NUM_FIELDS ); 170 for( int i = 0; i < fieldHeaders.length; ++i ) 171 { 172 final Attribute attr = (Attribute )fieldAttrs.get( i ); 173 fieldHeaders[ i ] = (String )attr.getValue(); 174 } 176 177 final List <List <Serializable >> records = 179 TypeCast.asList( ((Attribute )queryResult.get( 1 )).getValue() ); 180 final LogQueryEntry[] entries = new LogQueryEntry[ records.size() ]; 181 for( int recordIdx = 0; recordIdx < records.size(); ++recordIdx ) 182 { 183 final List <Serializable > record = records.get( recordIdx ); 184 TypeCast.checkList( record, Serializable .class ); 185 186 assert( record.size() == fieldHeaders.length ); 187 final Serializable [] fieldValues = new Serializable [ fieldHeaders.length ]; 188 for( int fieldIdx = 0; fieldIdx < fieldValues.length; ++fieldIdx ) 189 { 190 fieldValues[ fieldIdx ] = record.get( fieldIdx ); 191 } 192 193 entries[ recordIdx ] = new LogQueryEntryImpl( fieldValues ); 194 } 195 196 return new LogQueryResultImpl( fieldHeaders, entries ); 197 } 198 199 public void 200 testQuery() 201 { 202 final String filename = "server.log"; 203 final Boolean searchForward = Boolean.TRUE; 204 final Boolean sortAscending = Boolean.TRUE; 205 final int startRecord = 0; 206 final int requestedCount = 1000 * 1000; 207 final Date fromDate = null; 208 final Date toDate = null; 209 final List <Object > listOfModules = null; 210 final Boolean levelOnly = Boolean.FALSE; 211 final Properties props = null; 212 213 final AttributeList attrs = getLogMBean().getLogRecordsUsingQuery( 214 filename, 215 new Long ( startRecord ), 216 searchForward, 217 sortAscending, 218 new Integer ( requestedCount ), 219 fromDate, 220 toDate, 221 Level.WARNING.toString(), 222 levelOnly, 223 listOfModules, 224 props ); 225 226 final LogQueryResult result = convertQueryResult( attrs ); 227 228 final LogQueryEntry[] entries = result.getEntries(); 229 assert( entries.length != 0 ); 230 231 for( final LogQueryEntry entry : entries ) 232 { 233 final String messageID = entry.getMessageID(); 234 235 final ArrayList causes = getLogMBean().getDiagnosticCausesForMessageId( messageID ); 236 final ArrayList checks = getLogMBean().getDiagnosticChecksForMessageId( messageID ); 238 final String uri = getLogMBean().getDiagnosticURIForMessageId( messageID ); 240 } 242 243 } 245 246 247 private final class MyLogMBeanListener implements NotificationListener 248 { 249 public MyLogMBeanListener() {} 250 251 public void 252 handleNotification( 253 final Notification notif, 254 final Object handback ) 255 { 256 trace( "LoggingImpl.java: received Notification: " + notif ); 257 } 258 } 259 260 } 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | Popular Tags |