KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > internal > model > ModificationLogger


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.internal.model;
22
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.util.logging.FileHandler JavaDoc;
28 import java.util.logging.Formatter JavaDoc;
29 import java.util.logging.Handler JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.LogRecord JavaDoc;
32 import java.util.logging.Logger JavaDoc;
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 JavaDoc fileHandler;
43
44     private Logger JavaDoc 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 JavaDoc logfileName = ConnectionManager.getModificationLogFileName( connection.getName() );
59         try
60         {
61             fileHandler = new FileHandler JavaDoc( logfileName, 100000, 10, true );
62             fileHandler.setFormatter( new Formatter JavaDoc()
63             {
64                 public String JavaDoc format( LogRecord JavaDoc record )
65                 {
66                     return record.getMessage();
67                 }
68             } );
69             this.logger.addHandler( fileHandler );
70         }
71         catch ( SecurityException JavaDoc e )
72         {
73             e.printStackTrace();
74         }
75         catch ( IOException JavaDoc e )
76         {
77             e.printStackTrace();
78         }
79     }
80
81
82     public void dispose()
83     {
84         if ( this.logger != null )
85         {
86             Handler JavaDoc[] 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 JavaDoc 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 JavaDoc[] 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 JavaDoc e )
129         {
130             return new File JavaDoc[0];
131         }
132     }
133
134
135     private static File JavaDoc[] getLogFiles( FileHandler JavaDoc fileHandler ) throws Exception JavaDoc
136     {
137         Field JavaDoc field = getFieldFromClass( "java.util.logging.FileHandler", "files" ); //$NON-NLS-1$ //$NON-NLS-2$
138
field.setAccessible( true );
139         File JavaDoc[] files = ( File JavaDoc[] ) field.get( fileHandler );
140         return files;
141     }
142
143
144     private static Field JavaDoc getFieldFromClass( String JavaDoc className, String JavaDoc fieldName ) throws Exception JavaDoc
145     {
146         Class JavaDoc clazz = Class.forName( className );
147         Field JavaDoc[] 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