directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r657538 - in /directory/studio/trunk: connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/ ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/events/ ldapbrowser-ui/src/main/java/org/ap...
Date Sun, 18 May 2008 11:14:47 GMT
Author: seelmann
Date: Sun May 18 04:14:46 2008
New Revision: 657538

URL: http://svn.apache.org/viewvc?rev=657538&view=rev
Log:
DIRSTUDIO-328: Forgot class and fixed typo

Added:
    directory/studio/trunk/connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/LdifSearchLogger.java
Modified:
    directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/events/EventRegistry.java
    directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java

Added: directory/studio/trunk/connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/LdifSearchLogger.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/LdifSearchLogger.java?rev=657538&view=auto
==============================================================================
--- directory/studio/trunk/connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/LdifSearchLogger.java
(added)
+++ directory/studio/trunk/connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/LdifSearchLogger.java
Sun May 18 04:14:46 2008
@@ -0,0 +1,514 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+
+package org.apache.directory.studio.connection.core.io.jndi;
+
+
+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.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.FileHandler;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SearchControls;
+import javax.naming.ldap.Control;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.directory.shared.ldap.codec.util.LdapURL;
+import org.apache.directory.studio.connection.core.Connection;
+import org.apache.directory.studio.connection.core.ConnectionCoreConstants;
+import org.apache.directory.studio.connection.core.ConnectionCorePlugin;
+import org.apache.directory.studio.connection.core.ConnectionManager;
+import org.apache.directory.studio.connection.core.IJndiLogger;
+import org.apache.directory.studio.connection.core.Utils;
+import org.apache.directory.studio.connection.core.Connection.AliasDereferencingMethod;
+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;
+
+
+/**
+ * The LdifSearchLogger is used to log searches into a file.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class LdifSearchLogger implements IJndiLogger
+{
+
+    /** The ID. */
+    private String id;
+
+    /** The name. */
+    private String name;
+
+    /** The description. */
+    private String description;
+
+    /** The file handlers. */
+    private Map<String, FileHandler> fileHandlers = new HashMap<String, FileHandler>();
+
+    /** The loggers. */
+    private Map<String, Logger> loggers = new HashMap<String, Logger>();
+
+
+    /**
+     * Creates a new instance of LdifSearchLogger.
+     */
+    public LdifSearchLogger()
+    {
+    }
+
+
+    /**
+     * Inits the search logger.
+     */
+    private void initSearchLogger( Connection connection )
+    {
+        Logger logger = Logger.getAnonymousLogger();
+        loggers.put( connection.getId(), logger );
+        logger.setLevel( Level.ALL );
+
+        String logfileName = ConnectionManager.getSearchLogFileName( connection );
+        try
+        {
+            FileHandler fileHandler = new FileHandler( logfileName, 100000, 10, true );
+            fileHandlers.put( connection.getId(), fileHandler );
+            fileHandler.setFormatter( new Formatter()
+            {
+                public String format( LogRecord record )
+                {
+                    return record.getMessage();
+                }
+            } );
+            logger.addHandler( fileHandler );
+        }
+        catch ( SecurityException e )
+        {
+            e.printStackTrace();
+        }
+        catch ( IOException e )
+        {
+            e.printStackTrace();
+        }
+    }
+
+
+    /**
+     * Disposes the search logger of the given connection.
+     * 
+     * @param connection the connection
+     */
+    public void dispose( Connection connection )
+    {
+        String id = connection.getId();
+        if ( loggers.containsKey( id ) )
+        {
+            Handler[] handlers = loggers.get( id ).getHandlers();
+            for ( Handler handler : handlers )
+            {
+                handler.close();
+            }
+
+            loggers.remove( id );
+        }
+    }
+
+
+    /**
+     * Logs the given text to the search logger of the given connection.
+     * 
+     * @param text the text to log
+     * @param type the type, either SEARCH REQUEST or SEARCH RESULT
+     * @param ex the naming exception if an error occurred, null otherwise
+     * @param connection the connection
+     */
+    private void log( String text, String type, NamingException ex, Connection connection
)
+    {
+        String id = connection.getId();
+        if ( !loggers.containsKey( id ) )
+        {
+            if ( connection.getName() != null )
+            {
+                initSearchLogger( connection );
+            }
+        }
+
+        if ( loggers.containsKey( id ) )
+        {
+            Logger logger = loggers.get( id );
+            DateFormat df = new SimpleDateFormat( ConnectionCoreConstants.DATEFORMAT );
+
+            if ( ex != null )
+            {
+                logger.log( Level.ALL, LdifCommentLine
+                    .create( "#!" + type + " ERROR" ).toFormattedString( LdifFormatParameters.DEFAULT
) ); //$NON-NLS-1$
+            }
+            else
+            {
+                logger.log( Level.ALL, LdifCommentLine
+                    .create( "#!" + type + " OK" ).toFormattedString( LdifFormatParameters.DEFAULT
) ); //$NON-NLS-1$
+            }
+
+            logger
+                .log(
+                    Level.ALL,
+                    LdifCommentLine
+                        .create( "#!CONNECTION ldap://" + connection.getHost() + ":" + connection.getPort()
).toFormattedString( LdifFormatParameters.DEFAULT ) ); //$NON-NLS-1$ //$NON-NLS-2$
+            logger.log( Level.ALL, LdifCommentLine
+                .create( "#!DATE " + df.format( new Date() ) ).toFormattedString( LdifFormatParameters.DEFAULT
) ); //$NON-NLS-1$
+
+            if ( ex != null )
+            {
+                String errorComment = "#!ERROR " + ex.getMessage(); //$NON-NLS-1$
+                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
) );
+            }
+
+            logger.log( Level.ALL, text );
+        }
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logChangetypeAdd(org.apache.directory.studio.connection.core.Connection,
java.lang.String, javax.naming.directory.Attributes, javax.naming.ldap.Control[], javax.naming.NamingException)
+     */
+    public void logChangetypeAdd( Connection connection, final String dn, final Attributes
attributes,
+        final Control[] controls, NamingException ex )
+    {
+        // don't log changetypes
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logChangetypeDelete(org.apache.directory.studio.connection.core.Connection,
java.lang.String, javax.naming.ldap.Control[], javax.naming.NamingException)
+     */
+    public void logChangetypeDelete( Connection connection, final String dn, final Control[]
controls,
+        NamingException ex )
+    {
+        // don't log changetypes
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logChangetypeModify(org.apache.directory.studio.connection.core.Connection,
java.lang.String, javax.naming.directory.ModificationItem[], javax.naming.ldap.Control[],
javax.naming.NamingException)
+     */
+    public void logChangetypeModify( Connection connection, final String dn,
+        final ModificationItem[] modificationItems, final Control[] controls, NamingException
ex )
+    {
+        // don't log changetypes
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logChangetypeModDn(org.apache.directory.studio.connection.core.Connection,
java.lang.String, java.lang.String, boolean, javax.naming.ldap.Control[], javax.naming.NamingException)
+     */
+    public void logChangetypeModDn( Connection connection, final String oldDn, final String
newDn,
+        final boolean deleteOldRdn, final Control[] controls, NamingException ex )
+    {
+        // don't log changetypes
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logSearchRequest(org.apache.directory.studio.connection.core.Connection,
java.lang.String, java.lang.String, javax.naming.directory.SearchControls, org.apache.directory.studio.connection.core.Connection.AliasDereferencingMethod,
javax.naming.ldap.Control[], javax.naming.NamingException)
+     */
+    public void logSearchRequest( Connection connection, String searchBase, String filter,
+        SearchControls searchControls, AliasDereferencingMethod aliasesDereferencingMethod,
Control[] controls,
+        long requestNum, NamingException ex )
+    {
+        if ( !isSearchRequestLogEnabled() )
+        {
+            return;
+        }
+
+        String scopeAsString = searchControls.getSearchScope() == SearchControls.SUBTREE_SCOPE
? "wholeSubtree (2)"
+            : searchControls.getSearchScope() == SearchControls.ONELEVEL_SCOPE ? "singleLevel
(1)" : "baseObject (0)";
+        String attributesAsString = searchControls.getReturningAttributes() == null ? "*"
: searchControls
+            .getReturningAttributes().length == 0 ? "1.1" : StringUtils.join( searchControls.getReturningAttributes(),
+            " " );
+        String aliasAsString = aliasesDereferencingMethod == AliasDereferencingMethod.ALWAYS
? "derefAlways (3)"
+            : aliasesDereferencingMethod == AliasDereferencingMethod.FINDING ? "derefFindingBaseObj
(2)"
+                : aliasesDereferencingMethod == AliasDereferencingMethod.SEARCH ? "derefInSearching
(1)"
+                    : "neverDerefAliases (0)";
+
+        // build LDAP URL
+        LdapURL url = Utils.getLdapURL( connection, searchBase, searchControls.getSearchScope(),
filter, searchControls
+            .getReturningAttributes() );
+
+        // build command line
+        String cmdLine = Utils.getLdapSearchCommandLine( connection, searchBase, searchControls.getSearchScope(),
+            aliasesDereferencingMethod, searchControls.getCountLimit(), searchControls.getTimeLimit(),
filter,
+            searchControls.getReturningAttributes() );
+
+        // build 
+        Collection<LdifLineBase> lines = new ArrayList<LdifLineBase>();
+        lines.add( LdifCommentLine.create( "# LDAP URL     : " + url.toString() ) );
+        lines.add( LdifCommentLine.create( "# command line : " + cmdLine.toString() ) );
+        lines.add( LdifCommentLine.create( "# baseObject   : " + searchBase ) );
+        lines.add( LdifCommentLine.create( "# scope        : " + scopeAsString ) );
+        lines.add( LdifCommentLine.create( "# derefAliases : " + aliasAsString ) );
+        lines.add( LdifCommentLine.create( "# sizeLimit    : " + searchControls.getTimeLimit()
) );
+        lines.add( LdifCommentLine.create( "# timeLimit    : " + searchControls.getCountLimit()
) );
+        lines.add( LdifCommentLine.create( "# typesOnly    : " + "False" ) );
+        lines.add( LdifCommentLine.create( "# filter       : " + filter ) );
+        lines.add( LdifCommentLine.create( "# attributes   : " + attributesAsString ) );
+        if ( controls != null )
+        {
+            for ( Control control : controls )
+            {
+                lines.add( LdifCommentLine.create( "# control      : " + control.getID()
) );
+            }
+        }
+        lines.add( LdifSepLine.create() );
+
+        String formattedString = "";
+        for ( LdifLineBase line : lines )
+        {
+            formattedString += line.toFormattedString( LdifFormatParameters.DEFAULT );
+        }
+
+        log( formattedString, "SEARCH REQUEST (" + requestNum + ")", ex, connection );
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logSearchResultEntry(org.apache.directory.studio.connection.core.Connection,
org.apache.directory.studio.connection.core.io.jndi.StudioSearchResult, javax.naming.NamingException)
+     */
+    public void logSearchResultEntry( Connection connection, StudioSearchResult studioSearchResult,
long requestNum,
+        NamingException ex )
+    {
+        if ( !isSearchResultEntryLogEnabled() )
+        {
+            return;
+        }
+
+        try
+        {
+            String dn = studioSearchResult.getNameInNamespace();
+            Attributes attributes = studioSearchResult.getAttributes();
+
+            LdifContentRecord record = new LdifContentRecord( LdifDnLine.create( dn ) );
+            NamingEnumeration<? extends Attribute> attributeEnumeration = attributes.getAll();
+            while ( attributeEnumeration.hasMore() )
+            {
+                Attribute attribute = attributeEnumeration.next();
+                String attributeName = attribute.getID();
+                NamingEnumeration<?> valueEnumeration = attribute.getAll();
+                while ( valueEnumeration.hasMore() )
+                {
+                    Object o = valueEnumeration.next();
+                    if ( o instanceof String )
+                    {
+                        record.addAttrVal( LdifAttrValLine.create( attributeName, ( String
) o ) );
+                    }
+                    if ( o instanceof byte[] )
+                    {
+                        record.addAttrVal( LdifAttrValLine.create( attributeName, ( byte[]
) o ) );
+                    }
+                }
+            }
+            record.finish( LdifSepLine.create() );
+
+            String formattedString = record.toFormattedString( LdifFormatParameters.DEFAULT
);
+            log( formattedString, "SEARCH RESULT ENTRY (" + requestNum + ")", ex, connection
);
+        }
+        catch ( NamingException e )
+        {
+        }
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.connection.core.IJndiLogger#logSearchResultDone(org.apache.directory.studio.connection.core.Connection,
long, javax.naming.NamingException)
+     */
+    public void logSearchResultDone( Connection connection, long count, long requestNum,
NamingException ex )
+    {
+        if ( !isSearchRequestLogEnabled() )
+        {
+            return;
+        }
+
+        Collection<LdifLineBase> lines = new ArrayList<LdifLineBase>();
+        lines.add( LdifCommentLine.create( "# numEntries : " + count ) );
+        lines.add( LdifSepLine.create() );
+
+        String formattedString = "";
+        for ( LdifLineBase line : lines )
+        {
+            formattedString += line.toFormattedString( LdifFormatParameters.DEFAULT );
+        }
+        log( formattedString, "SEARCH RESULT DONE (" + requestNum + ")", ex, connection );
+    }
+
+
+    /**
+     * Gets the files.
+     * 
+     * @param connection the connection
+     * 
+     * @return the files
+     */
+    public File[] getFiles( Connection connection )
+    {
+        String id = connection.getId();
+        if ( !loggers.containsKey( id ) )
+        {
+            if ( connection.getName() != null )
+            {
+                initSearchLogger( connection );
+            }
+        }
+
+        try
+        {
+            return getLogFiles( fileHandlers.get( id ) );
+        }
+        catch ( Exception e )
+        {
+            return new File[0];
+        }
+    }
+
+
+    /**
+     * Gets the log files.
+     * 
+     * @param fileHandler the file handler
+     * 
+     * @return the log files
+     * 
+     * @throws Exception the exception
+     */
+    private static File[] getLogFiles( FileHandler fileHandler ) throws Exception
+    {
+        Field field = getFieldFromClass( "java.util.logging.FileHandler", "files" ); //$NON-NLS-1$
//$NON-NLS-2$
+        field.setAccessible( true );
+        File[] files = ( File[] ) field.get( fileHandler );
+        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 );
+    }
+
+
+    /**
+     * Checks if search result entry log is enabled.
+     * 
+     * @return true, if search result log is enabled
+     */
+    private boolean isSearchResultEntryLogEnabled()
+    {
+        return ConnectionCorePlugin.getDefault().getPluginPreferences().getBoolean(
+            ConnectionCoreConstants.PREFERENCE_SEARCHRESULTENTRYLOGS_ENABLE );
+    }
+
+
+    public String getId()
+    {
+        return id;
+    }
+
+
+    public void setId( String id )
+    {
+        this.id = id;
+    }
+
+
+    public String getName()
+    {
+        return name;
+    }
+
+
+    public void setName( String name )
+    {
+        this.name = name;
+    }
+
+
+    public String getDescription()
+    {
+        return description;
+    }
+
+
+    public void setDescription( String description )
+    {
+        this.description = description;
+    }
+
+}

Modified: directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/events/EventRegistry.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/events/EventRegistry.java?rev=657538&r1=657537&r2=657538&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/events/EventRegistry.java
(original)
+++ directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/events/EventRegistry.java
Sun May 18 04:14:46 2008
@@ -244,7 +244,7 @@
      *
      * @param listener the listener
      */
-    public static void removeBrowserConnectionpdateListener( BrowserConnectionUpdateListener
listener )
+    public static void removeBrowserConnectionUpdateListener( BrowserConnectionUpdateListener
listener )
     {
         if ( browserConnectionUpdateListeners.containsKey( listener ) )
         {

Modified: directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java?rev=657538&r1=657537&r2=657538&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java
(original)
+++ directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java
Sun May 18 04:14:46 2008
@@ -126,7 +126,7 @@
 
             EventRegistry.removeEntryUpdateListener( this );
             EventRegistry.removeSearchUpdateListener( this );
-            EventRegistry.removeBrowserConnectionpdateListener( this );
+            EventRegistry.removeBrowserConnectionUpdateListener( this );
             view = null;
         }
     }



Mime
View raw message