1 20 21 package org.apache.directory.ldapstudio.browser.core.internal.model; 22 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.lang.reflect.Field ; 27 import java.util.logging.FileHandler ; 28 import java.util.logging.Formatter ; 29 import java.util.logging.Handler ; 30 import java.util.logging.Level ; 31 import java.util.logging.LogRecord ; 32 import java.util.logging.Logger ; 33 34 import org.apache.directory.ldapstudio.browser.core.ConnectionManager; 35 36 37 public class ModificationLogger 38 { 39 40 private Connection connection; 41 42 private FileHandler fileHandler; 43 44 private Logger logger; 45 46 47 public ModificationLogger( Connection connection ) 48 { 49 this.connection = connection; 50 } 51 52 53 private void initModificationLogger() 54 { 55 this.logger = Logger.getAnonymousLogger(); 56 this.logger.setLevel( Level.ALL ); 57 58 String logfileName = ConnectionManager.getModificationLogFileName( connection.getName() ); 59 try 60 { 61 fileHandler = new FileHandler ( logfileName, 100000, 10, true ); 62 fileHandler.setFormatter( new Formatter () 63 { 64 public String format( LogRecord record ) 65 { 66 return record.getMessage(); 67 } 68 } ); 69 this.logger.addHandler( fileHandler ); 70 } 71 catch ( SecurityException e ) 72 { 73 e.printStackTrace(); 74 } 75 catch ( IOException e ) 76 { 77 e.printStackTrace(); 78 } 79 } 80 81 82 public void dispose() 83 { 84 if ( this.logger != null ) 85 { 86 Handler [] handlers = this.logger.getHandlers(); 87 for ( int i = 0; i < handlers.length; i++ ) 88 { 89 handlers[i].close(); 90 } 91 92 this.logger = null; 93 } 94 } 95 96 97 public void log( String s ) 98 { 99 if ( this.logger == null ) 100 { 101 if ( connection.getName() != null ) 102 { 103 this.initModificationLogger(); 104 } 105 } 106 107 if ( this.logger != null ) 108 { 109 this.logger.log( Level.ALL, s ); 110 } 111 } 112 113 114 public File [] getFiles() 115 { 116 if ( this.logger == null ) 117 { 118 if ( connection.getName() != null ) 119 { 120 this.initModificationLogger(); 121 } 122 } 123 124 try 125 { 126 return getLogFiles( this.fileHandler ); 127 } 128 catch ( Exception e ) 129 { 130 return new File [0]; 131 } 132 } 133 134 135 private static File [] getLogFiles( FileHandler fileHandler ) throws Exception 136 { 137 Field field = getFieldFromClass( "java.util.logging.FileHandler", "files" ); field.setAccessible( true ); 139 File [] files = ( File [] ) field.get( fileHandler ); 140 return files; 141 } 142 143 144 private static Field getFieldFromClass( String className, String fieldName ) throws Exception 145 { 146 Class clazz = Class.forName( className ); 147 Field [] fields = clazz.getDeclaredFields(); 148 149 for ( int i = 0; i < fields.length; i++ ) 150 { 151 if ( fields[i].getName().equals( fieldName ) ) 152 return fields[i]; 153 } 154 return null; 155 } 156 157 } 158 | Popular Tags |