diff options
Diffstat (limited to 'plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifSearchLogger.java')
-rw-r--r-- | plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifSearchLogger.java | 264 |
1 files changed, 130 insertions, 134 deletions
diff --git a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifSearchLogger.java b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifSearchLogger.java index d2772af4a..0e4493857 100644 --- a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifSearchLogger.java +++ b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/io/api/LdifSearchLogger.java @@ -23,14 +23,16 @@ 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.ArrayList; +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; @@ -41,13 +43,13 @@ import java.util.logging.Logger; import javax.naming.directory.SearchControls; import org.apache.commons.lang3.StringUtils; +import org.apache.directory.api.ldap.model.entry.Attribute; import org.apache.directory.api.ldap.model.entry.Entry; -import org.apache.directory.api.ldap.model.entry.Modification; -import org.apache.directory.api.ldap.model.exception.LdapException; +import org.apache.directory.api.ldap.model.entry.Value; import org.apache.directory.api.ldap.model.message.Control; import org.apache.directory.api.ldap.model.message.Referral; -import org.apache.directory.api.ldap.model.name.Dn; import org.apache.directory.api.ldap.model.url.LdapUrl; +import org.apache.directory.api.util.Strings; import org.apache.directory.studio.connection.core.Connection; import org.apache.directory.studio.connection.core.Connection.AliasDereferencingMethod; import org.apache.directory.studio.connection.core.ConnectionCoreConstants; @@ -56,12 +58,16 @@ import org.apache.directory.studio.connection.core.ConnectionManager; import org.apache.directory.studio.connection.core.ILdapLogger; import org.apache.directory.studio.connection.core.ReferralsInfo; import org.apache.directory.studio.connection.core.Utils; +import org.apache.directory.studio.connection.core.io.StudioLdapException; import org.apache.directory.studio.ldifparser.LdifFormatParameters; +import org.apache.directory.studio.ldifparser.model.container.LdifContentRecord; +import org.apache.directory.studio.ldifparser.model.lines.LdifAttrValLine; import org.apache.directory.studio.ldifparser.model.lines.LdifCommentLine; +import org.apache.directory.studio.ldifparser.model.lines.LdifDnLine; import org.apache.directory.studio.ldifparser.model.lines.LdifLineBase; 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.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.InstanceScope; /** @@ -87,52 +93,48 @@ public class LdifSearchLogger implements ILdapLogger /** The loggers. */ private Map<String, Logger> loggers = new HashMap<String, Logger>(); - /** * Creates a new instance of LdifSearchLogger. */ public LdifSearchLogger() { - ConnectionCorePlugin.getDefault().getPluginPreferences().addPropertyChangeListener( - new IPropertyChangeListener() + IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode( ConnectionCoreConstants.PLUGIN_ID ); + prefs.addPreferenceChangeListener( event -> { + if ( ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_COUNT.equals( event.getKey() ) + || ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_SIZE.equals( event.getKey() ) ) { - public void propertyChange( PropertyChangeEvent event ) + // dispose all loggers/handlers + for ( Logger logger : loggers.values() ) { - if ( ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_COUNT.equals( event.getProperty() ) - || ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_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(); + } + } ); } @@ -186,12 +188,18 @@ public class LdifSearchLogger implements ILdapLogger handler.close(); } + File[] files = getLogFiles( connection ); + for ( File file : files ) + { + deleteFileWithRetry( file ); + } + loggers.remove( id ); } } - private void log( String text, String type, LdapException ex, Connection connection ) + private void log( String text, String type, StudioLdapException ex, Connection connection ) { String id = connection.getId(); if ( !loggers.containsKey( id ) ) @@ -204,29 +212,27 @@ public class LdifSearchLogger 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 - .create( "#!" + type + " ERROR" ).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ //$NON-NLS-2$ + lines.add( LdifCommentLine.create( "#!" + type + " ERROR" ) //$NON-NLS-1$//$NON-NLS-2$ + .toFormattedString( LdifFormatParameters.DEFAULT ) ); } else { - logger.log( Level.ALL, LdifCommentLine - .create( "#!" + type + " OK" ).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ //$NON-NLS-2$ + lines.add( LdifCommentLine.create( "#!" + type + " OK" ) //$NON-NLS-1$ //$NON-NLS-2$ + .toFormattedString( LdifFormatParameters.DEFAULT ) ); } - 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 - .create( "#!DATE " + df.format( new Date() ) ).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ + 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() ) ) //$NON-NLS-1$ + .toFormattedString( LdifFormatParameters.DEFAULT ) ); if ( ex != null ) { @@ -234,10 +240,12 @@ public class LdifSearchLogger 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() ); } } @@ -245,48 +253,9 @@ public class LdifSearchLogger implements ILdapLogger /** * {@inheritDoc} */ - public void logChangetypeAdd( Connection connection, final Entry entry, final Control[] controls, LdapException ex ) - { - // don't log changetypes - } - - - /** - * {@inheritDoc} - */ - public void logChangetypeDelete( Connection connection, final Dn dn, final Control[] controls, - LdapException ex ) - { - // don't log changetypes - } - - - /** - * {@inheritDoc} - */ - public void logChangetypeModify( Connection connection, final Dn dn, - final Collection<Modification> modifications, final Control[] controls, LdapException ex ) - { - // don't log changetypes - } - - - /** - * {@inheritDoc} - */ - public void logChangetypeModDn( Connection connection, final Dn oldDn, final Dn newDn, - final boolean deleteOldRdn, final Control[] controls, LdapException ex ) - { - // don't log changetypes - } - - - /** - * {@inheritDoc} - */ public void logSearchRequest( Connection connection, String searchBase, String filter, SearchControls searchControls, AliasDereferencingMethod aliasesDereferencingMethod, - Control[] controls, long requestNum, LdapException ex ) + Control[] controls, long requestNum, StudioLdapException ex ) { if ( !isSearchRequestLogEnabled() ) { @@ -348,8 +317,60 @@ public class LdifSearchLogger implements ILdapLogger /** * {@inheritDoc} */ + public void logSearchResultEntry( Connection connection, StudioSearchResult studioSearchResult, long requestNum, + StudioLdapException ex ) + { + if ( !isSearchResultEntryLogEnabled() ) + { + return; + } + + String formattedString; + if ( studioSearchResult != null ) + { + Set<String> maskedAttributes = getMaskedAttributes(); + Entry entry = studioSearchResult.getEntry(); + + LdifContentRecord record = new LdifContentRecord( LdifDnLine.create( entry.getDn().getName() ) ); + for ( Attribute attribute : entry ) + { + String attributeName = attribute.getUpId(); + for ( Value value : attribute ) + { + if ( maskedAttributes.contains( Strings.toLowerCaseAscii( attributeName ) ) ) + { + record.addAttrVal( LdifAttrValLine.create( attributeName, "**********" ) ); //$NON-NLS-1$ + } + else + { + if ( value.isHumanReadable() ) + { + record.addAttrVal( LdifAttrValLine.create( attributeName, value.getString() ) ); + } + else + { + record.addAttrVal( LdifAttrValLine.create( attributeName, value.getBytes() ) ); + } + } + } + } + record.finish( LdifSepLine.create() ); + formattedString = record.toFormattedString( LdifFormatParameters.DEFAULT ); + } + else + { + formattedString = LdifFormatParameters.DEFAULT.getLineSeparator(); + } + + log( formattedString, "SEARCH RESULT ENTRY (" + requestNum + ")", ex, connection ); //$NON-NLS-1$ //$NON-NLS-2$ + } + + + /** + * {@inheritDoc} + */ public void logSearchResultReference( Connection connection, Referral referral, - ReferralsInfo referralsInfo, long requestNum, LdapException ex ) + ReferralsInfo referralsInfo, long requestNum, StudioLdapException ex ) { if ( !isSearchResultEntryLogEnabled() ) { @@ -373,7 +394,7 @@ public class LdifSearchLogger implements ILdapLogger /** * {@inheritDoc} */ - public void logSearchResultDone( Connection connection, long count, long requestNum, LdapException ex ) + public void logSearchResultDone( Connection connection, long count, long requestNum, StudioLdapException ex ) { if ( !isSearchRequestLogEnabled() ) { @@ -413,7 +434,7 @@ public class LdifSearchLogger implements ILdapLogger try { - return getLogFiles( fileHandlers.get( id ) ); + return getLogFiles( connection ); } catch ( Exception e ) { @@ -428,51 +449,29 @@ public class LdifSearchLogger 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.getSearchLogFileName( 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 search request log is enabled. * * @return true, if search request log is enabled */ private boolean isSearchRequestLogEnabled() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getBoolean( - ConnectionCoreConstants.PREFERENCE_SEARCHREQUESTLOGS_ENABLE ); + return ConnectionCorePlugin.getDefault().isSearchRequestLogsEnabled(); } @@ -483,8 +482,7 @@ public class LdifSearchLogger implements ILdapLogger */ private boolean isSearchResultEntryLogEnabled() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getBoolean( - ConnectionCoreConstants.PREFERENCE_SEARCHRESULTENTRYLOGS_ENABLE ); + return ConnectionCorePlugin.getDefault().isSearchResultEntryLogsEnabled(); } @@ -495,8 +493,7 @@ public class LdifSearchLogger implements ILdapLogger */ private int getFileCount() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getInt( - ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_COUNT ); + return ConnectionCorePlugin.getDefault().getSearchLogsFileCount(); } @@ -507,8 +504,7 @@ public class LdifSearchLogger implements ILdapLogger */ private int getFileSizeInKb() { - return ConnectionCorePlugin.getDefault().getPluginPreferences().getInt( - ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_SIZE ); + return ConnectionCorePlugin.getDefault().getSearchLogsFileSize(); } |