diff options
Diffstat (limited to 'plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifModificationLogger.java')
-rw-r--r-- | plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifModificationLogger.java | 152 |
1 files changed, 65 insertions, 87 deletions
diff --git a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifModificationLogger.java b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifModificationLogger.java index 3d8895de4..c0c6c706e 100644 --- a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifModificationLogger.java +++ b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifModificationLogger.java @@ -23,14 +23,15 @@ package org.apache.directory.studio.connection.core.io.api; import java.io.File; import java.io.IOException; -import java.lang.reflect.Field; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.StringJoiner; import java.util.logging.FileHandler; import java.util.logging.Formatter; import java.util.logging.Handler; @@ -70,8 +71,9 @@ import org.apache.directory.studio.ldifparser.model.lines.LdifModSpecSepLine; import org.apache.directory.studio.ldifparser.model.lines.LdifNewrdnLine; import org.apache.directory.studio.ldifparser.model.lines.LdifNewsuperiorLine; import org.apache.directory.studio.ldifparser.model.lines.LdifSepLine; -import org.eclipse.core.runtime.Preferences.IPropertyChangeListener; -import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; /** @@ -97,52 +99,48 @@ public class LdifModificationLogger implements ILdapLogger /** The loggers. */ private Map<String, Logger> loggers = new HashMap<String, Logger>(); - /** * Creates a new instance of LdifModificationLogger. */ public LdifModificationLogger() { - ConnectionCorePlugin.getDefault().getPluginPreferences().addPropertyChangeListener( - new IPropertyChangeListener() + IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode( ConnectionCoreConstants.PLUGIN_ID ); + prefs.addPreferenceChangeListener( event -> { + if ( ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_COUNT.equals( event.getKey() ) + || ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_SIZE.equals( event.getKey() ) ) { - public void propertyChange( PropertyChangeEvent event ) + // dispose all loggers/handlers + for ( Logger logger : loggers.values() ) { - if ( ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_COUNT.equals( event.getProperty() ) - || ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_SIZE.equals( event.getProperty() ) ) + for ( Handler handler : logger.getHandlers() ) { - // dispose all loggers/handlers - for ( Logger logger : loggers.values() ) - { - for ( Handler handler : logger.getHandlers() ) - { - handler.close(); - } - } + handler.close(); + } + } - // delete files with index greater than new file count - for ( FileHandler fh : fileHandlers.values() ) + // delete files with index greater than new file count + Connection[] connections = ConnectionCorePlugin.getDefault().getConnectionManager().getConnections(); + for ( Connection connection : connections ) + { + try + { + File[] logFiles = getLogFiles( connection ); + for ( int i = getFileCount(); i < logFiles.length; i++ ) { - try - { - File[] logFiles = getLogFiles( fh ); - for ( int i = getFileCount(); i < logFiles.length; i++ ) - { - if ( logFiles[i] != null && logFiles[i].exists() ) - { - logFiles[i].delete(); - } - } - } - catch ( Exception e ) + if ( logFiles[i] != null && logFiles[i].exists() ) { + logFiles[i].delete(); } } - - loggers.clear(); + } + catch ( Exception e ) + { } } - } ); + + loggers.clear(); + } + } ); } @@ -214,28 +212,26 @@ public class LdifModificationLogger implements ILdapLogger if ( loggers.containsKey( id ) ) { - Logger logger = loggers.get( id ); + StringJoiner lines = new StringJoiner( "" ); DateFormat df = new SimpleDateFormat( ConnectionCoreConstants.DATEFORMAT ); df.setTimeZone( ConnectionCoreConstants.UTC_TIME_ZONE ); if ( ex != null ) { - logger.log( Level.ALL, LdifCommentLine + lines.add( LdifCommentLine .create( "#!RESULT ERROR" ).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ } else { - logger.log( Level.ALL, LdifCommentLine + lines.add( LdifCommentLine .create( "#!RESULT OK" ).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ } - logger - .log( - Level.ALL, - LdifCommentLine - .create( "#!CONNECTION ldap://" + connection.getHost() + ":" + connection.getPort() ) //$NON-NLS-1$//$NON-NLS-2$ - .toFormattedString( LdifFormatParameters.DEFAULT ) ); - logger.log( Level.ALL, LdifCommentLine + lines.add( + LdifCommentLine + .create( "#!CONNECTION ldap://" + connection.getHost() + ":" + connection.getPort() ) //$NON-NLS-1$//$NON-NLS-2$ + .toFormattedString( LdifFormatParameters.DEFAULT ) ); + lines.add( LdifCommentLine .create( "#!DATE " + df.format( new Date() ) ).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ if ( ex != null ) @@ -244,10 +240,12 @@ public class LdifModificationLogger implements ILdapLogger errorComment = errorComment.replaceAll( "\r", " " ); //$NON-NLS-1$ //$NON-NLS-2$ errorComment = errorComment.replaceAll( "\n", " " ); //$NON-NLS-1$ //$NON-NLS-2$ LdifCommentLine errorCommentLine = LdifCommentLine.create( errorComment ); - logger.log( Level.ALL, errorCommentLine.toFormattedString( LdifFormatParameters.DEFAULT ) ); + lines.add( errorCommentLine.toFormattedString( LdifFormatParameters.DEFAULT ) ); } - logger.log( Level.ALL, text ); + lines.add( text ); + Logger logger = loggers.get( id ); + logger.log( Level.ALL, lines.toString() ); } } @@ -255,7 +253,8 @@ public class LdifModificationLogger implements ILdapLogger /** * {@inheritDoc} */ - public void logChangetypeAdd( Connection connection, final Entry entry, final Control[] controls, StudioLdapException ex ) + public void logChangetypeAdd( Connection connection, final Entry entry, final Control[] controls, + StudioLdapException ex ) { if ( !isModificationLogEnabled() ) { @@ -271,7 +270,7 @@ public class LdifModificationLogger implements ILdapLogger String attributeName = attribute.getUpId(); for ( Value value : attribute ) { - if ( maskedAttributes.contains( Strings.toLowerCase( attributeName ) ) ) + if ( maskedAttributes.contains( Strings.toLowerCaseAscii( attributeName ) ) ) { record.addAttrVal( LdifAttrValLine.create( attributeName, "**********" ) ); //$NON-NLS-1$ } @@ -351,7 +350,7 @@ public class LdifModificationLogger implements ILdapLogger } for ( Value value : item.getAttribute() ) { - if ( maskedAttributes.contains( Strings.toLowerCase( attributeName ) ) ) + if ( maskedAttributes.contains( Strings.toLowerCaseAscii( attributeName ) ) ) { modSpec.addAttrVal( LdifAttrValLine.create( attributeName, "**********" ) ); //$NON-NLS-1$ } @@ -411,7 +410,7 @@ public class LdifModificationLogger implements ILdapLogger * @param record the recored * @param controls the controls */ - private static void addControlLines( LdifChangeRecord record,Control[] controls ) + private static void addControlLines( LdifChangeRecord record, Control[] controls ) { if ( controls != null ) { @@ -447,7 +446,7 @@ public class LdifModificationLogger implements ILdapLogger try { - return getLogFiles( fileHandlers.get( id ) ); + return getLogFiles( connection ); } catch ( Exception e ) { @@ -462,51 +461,30 @@ public class LdifModificationLogger implements ILdapLogger * @param fileHandler the file handler * * @return the log files - * - * @throws Exception the exception */ - private static File[] getLogFiles( FileHandler fileHandler ) throws Exception + private static File[] getLogFiles( Connection connection ) { - Field field = getFieldFromClass( "java.util.logging.FileHandler", "files" ); //$NON-NLS-1$ //$NON-NLS-2$ - field.setAccessible( true ); - File[] files = ( File[] ) field.get( fileHandler ); + String logfileNamePattern = ConnectionManager.getModificationLogFileName( connection ); + File file = new File( logfileNamePattern ); + String pattern = file.getName().replace( "%u", "\\d+" ).replace( "%g", "\\d+" ); + File dir = file.getParentFile(); + File[] files = dir.listFiles( ( d, f ) -> { + return f.matches( pattern ); + } ); + Arrays.sort( files ); return files; } /** - * Gets the field from class. - * - * @param className the class name - * @param fieldName the field name - * - * @return the field from class - * - * @throws Exception the exception - */ - private static Field getFieldFromClass( String className, String fieldName ) throws Exception - { - Class<?> clazz = Class.forName( className ); - Field[] fields = clazz.getDeclaredFields(); - - for ( int i = 0; i < fields.length; i++ ) - { - if ( fields[i].getName().equals( fieldName ) ) - return fields[i]; - } - return null; - } - - - /** * Checks if modification log is enabled. * * @return true, if modification log is enabled */ private boolean isModificationLogEnabled() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getBoolean( - ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_ENABLE ); + return Platform.getPreferencesService().getBoolean( ConnectionCoreConstants.PLUGIN_ID, + ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_ENABLE, true, null ); } @@ -517,8 +495,8 @@ public class LdifModificationLogger implements ILdapLogger */ private int getFileCount() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getInt( - ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_COUNT ); + return Platform.getPreferencesService().getInt( ConnectionCoreConstants.PLUGIN_ID, + ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_COUNT, 10, null ); } @@ -529,8 +507,8 @@ public class LdifModificationLogger implements ILdapLogger */ private int getFileSizeInKb() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getInt( - ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_SIZE ); + return Platform.getPreferencesService().getInt( ConnectionCoreConstants.PLUGIN_ID, + ConnectionCoreConstants.PREFERENCE_MODIFICATIONLOGS_FILE_SIZE, 100, null ); } |