You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2019/01/02 10:37:39 UTC

[directory-ldap-api] branch master updated: Fixing Sonar errors

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 0169c46  Fixing Sonar errors
0169c46 is described below

commit 0169c469ce68f2d4ca23c13e7cadd4a0f828fc33
Author: Emmanuel Lecharny <el...@apache.org>
AuthorDate: Wed Jan 2 11:37:37 2019 +0100

    Fixing Sonar errors
---
 .../directory/api/ldap/sp/JavaStoredProcUtils.java | 40 ++++-------
 .../directory/api/ldap/model/schema/package.html   |  4 ++
 .../api/ldap/schema/converter/SchemaToLdif.java    | 48 ++++++--------
 .../ldap/schema/extractor/impl/ResourceMap.java    | 31 +++++----
 .../ldap/schema/loader/JarLdifSchemaLoader.java    | 24 +++----
 .../schema/manager/impl/DefaultSchemaManager.java  |  2 +-
 .../directory/api/util/SynchronizedLRUMap.java     | 77 ++++++++--------------
 7 files changed, 92 insertions(+), 134 deletions(-)

diff --git a/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java b/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java
index f27689b..bd343ab 100644
--- a/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java
+++ b/ldap/extras/sp/src/main/java/org/apache/directory/api/ldap/sp/JavaStoredProcUtils.java
@@ -75,24 +75,17 @@ public final class JavaStoredProcUtils
         int lastDot = fullClassName.lastIndexOf( '.' );
         String classFileName = fullClassName.substring( lastDot + 1 ) + ".class";
         URL url = clazz.getResource( classFileName );
-        InputStream in;
-
+        InputStream in = null;
+        
         try
         {
             in = url.openStream();
-        }
-        catch ( IOException ioe )
-        {
-            NamingException ne = new NamingException();
-            ne.setRootCause( ioe );
-            throw ne;
-        }
-
-        File file;
+            File file = new File( url.toURI() );
+            int size = ( int ) file.length();
+            byte[] buf = new byte[size];
+            in.read( buf );
 
-        try
-        {
-            file = new File( url.toURI() );
+            return buf;
         }
         catch ( URISyntaxException urie )
         {
@@ -100,26 +93,19 @@ public final class JavaStoredProcUtils
             ne.setRootCause( urie );
             throw ne;
         }
-
-        int size = ( int ) file.length();
-        byte[] buf = new byte[size];
-
-        try
-        {
-            in.read( buf );
-        }
-        catch ( IOException e )
+        catch ( IOException ioe )
         {
             NamingException ne = new NamingException();
-            ne.setRootCause( e );
+            ne.setRootCause( ioe );
             throw ne;
         }
         finally
         {
-            IOUtils.closeQuietly( in );
+            if ( in != null )
+            {
+                IOUtils.closeQuietly( in );
+            }
         }
-
-        return buf;
     }
 
 
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/package.html b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/package.html
index 15abed8..787f6d4 100644
--- a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/package.html
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/package.html
@@ -19,6 +19,10 @@
 -->
 
 <HTML>
+<HEAD>
+  <TITLE>LDAP Model schema package</TITLE>
+</HEAD>
+
 <BODY LANG="en-US" DIR="LTR">
 <P>
 Contains interfaces and base classes for representing the LDAP schema domain
diff --git a/ldap/schema/converter/src/main/java/org/apache/directory/api/ldap/schema/converter/SchemaToLdif.java b/ldap/schema/converter/src/main/java/org/apache/directory/api/ldap/schema/converter/SchemaToLdif.java
index 8393177..23a1a24 100644
--- a/ldap/schema/converter/src/main/java/org/apache/directory/api/ldap/schema/converter/SchemaToLdif.java
+++ b/ldap/schema/converter/src/main/java/org/apache/directory/api/ldap/schema/converter/SchemaToLdif.java
@@ -129,34 +129,28 @@ public final class SchemaToLdif
      */
     private static void generate( Schema schema ) throws Exception
     {
-        if ( schema == null )
+        try ( InputStream in = schema.getInput() )
         {
-            LOG.error( I18n.err( I18n.ERR_15001_NULL_SCHEMA ) );
-            throw new IllegalArgumentException( I18n.err( I18n.ERR_15005_NO_PROPERTY ) );
-        }
-
-        InputStream in = schema.getInput();
-        Writer out = schema.getOutput();
-
-        // First parse the schema
-        SchemaParser parser = new SchemaParser();
-        List<SchemaElement> elements = parser.parse( in );
-
-        // Start with the header (apache licence)
-        out.write( HEADER );
-
-        // Iterate through each schema elemnts
-        for ( SchemaElement element : elements )
-        {
-            out.write( element.toLdif( schema.getName() ) );
-
-            out.write( '\n' );
+            try ( Writer out = schema.getOutput() )
+            {
+                // First parse the schema
+                SchemaParser parser = new SchemaParser();
+                List<SchemaElement> elements = parser.parse( in );
+        
+                // Start with the header (apache licence)
+                out.write( HEADER );
+        
+                // Iterate through each schema elemnts
+                for ( SchemaElement element : elements )
+                {
+                    out.write( element.toLdif( schema.getName() ) );
+        
+                    out.write( '\n' );
+                }
+        
+                // Done. Flush the result and close the reader and writer
+                out.flush();
+            }
         }
-
-        // Done. Flush the result and close the reader and writer
-        out.flush();
-
-        out.close();
-        in.close();
     }
 }
\ No newline at end of file
diff --git a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java
index 6a7a6f2..a759c45 100644
--- a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java
+++ b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/extractor/impl/ResourceMap.java
@@ -227,23 +227,26 @@ public final class ResourceMap
                 try
                 {
                     index = indexes.nextElement();
-                    InputStream in = index.openStream();
-                    BufferedReader reader = new BufferedReader( new InputStreamReader( in, StandardCharsets.UTF_8 ) );
-                    String line = reader.readLine();
-
-                    while ( line != null )
+                    
+                    try ( InputStream in = index.openStream() )
                     {
-                        boolean accept = pattern.matcher( line ).matches();
-
-                        if ( accept )
+                        try ( BufferedReader reader = new BufferedReader( new InputStreamReader( in, StandardCharsets.UTF_8 ) ) )
                         {
-                            map.put( line, Boolean.TRUE );
-                        }
-
-                        line = reader.readLine();
+                            String line = reader.readLine();
+            
+                            while ( line != null )
+                            {
+                                boolean accept = pattern.matcher( line ).matches();
+            
+                                if ( accept )
+                                {
+                                    map.put( line, Boolean.TRUE );
+                                }
+            
+                                line = reader.readLine();
+                            }
+                        }        
                     }
-
-                    reader.close();
                 }
                 catch ( IOException ioe )
                 {
diff --git a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java
index 9a626b6..e3acb2f 100644
--- a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java
+++ b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/loader/JarLdifSchemaLoader.java
@@ -121,19 +121,19 @@ public class JarLdifSchemaLoader extends AbstractSchemaLoader
             if ( pat.matcher( file ).matches() )
             {
                 URL resource = getResource( file, "schema LDIF file" );
-                InputStream in = resource.openStream();
 
-                try
+                try ( InputStream in = resource.openStream() )
                 {
-                    LdifReader reader = new LdifReader( in );
-                    LdifEntry entry = reader.next();
-                    reader.close();
-                    Schema schema = getSchema( entry.getEntry() );
-                    schemaMap.put( schema.getSchemaName(), schema );
-
-                    if ( LOG.isDebugEnabled() )
+                    try ( LdifReader reader = new LdifReader( in ) )
                     {
-                        LOG.debug( I18n.msg( I18n.MSG_16007_SCHEMA_INITIALIZED, schema ) );
+                        LdifEntry entry = reader.next();
+                        Schema schema = getSchema( entry.getEntry() );
+                        schemaMap.put( schema.getSchemaName(), schema );
+    
+                        if ( LOG.isDebugEnabled() )
+                        {
+                            LOG.debug( I18n.msg( I18n.MSG_16007_SCHEMA_INITIALIZED, schema ) );
+                        }
                     }
                 }
                 catch ( LdapException le )
@@ -141,10 +141,6 @@ public class JarLdifSchemaLoader extends AbstractSchemaLoader
                     LOG.error( I18n.err( I18n.ERR_16009_LDIF_LOAD_FAIL, file ), le );
                     throw le;
                 }
-                finally
-                {
-                    in.close();
-                }
             }
         }
     }
diff --git a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/manager/impl/DefaultSchemaManager.java b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/manager/impl/DefaultSchemaManager.java
index 9107ac5..bf9ef43 100644
--- a/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/manager/impl/DefaultSchemaManager.java
+++ b/ldap/schema/data/src/main/java/org/apache/directory/api/ldap/schema/manager/impl/DefaultSchemaManager.java
@@ -2122,7 +2122,7 @@ public class DefaultSchemaManager implements SchemaManager
                 // Remove the cloned registries
                 clonedRegistries.clear();
 
-                // If we didn't get any error, apply the addition to the real retistries
+                // If we didn't get any error, apply the addition to the real registries
                 if ( !errorHandler.wasError() )
                 {
                     // Copy again as the clonedRegistries clear has removed the previous copy
diff --git a/util/src/main/java/org/apache/directory/api/util/SynchronizedLRUMap.java b/util/src/main/java/org/apache/directory/api/util/SynchronizedLRUMap.java
index 2e8b56a..a5556a4 100644
--- a/util/src/main/java/org/apache/directory/api/util/SynchronizedLRUMap.java
+++ b/util/src/main/java/org/apache/directory/api/util/SynchronizedLRUMap.java
@@ -51,8 +51,11 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
     // without changing the format, we can still deserialize properly.
     private static final long serialVersionUID = 2197433140769957051L;
 
+    /** Maximum size */
     private int maximumSize = 0;
 
+    /** Default maximum size */
+    protected static final int DEFAULT_MAX_SIZE = 100;
 
     /**
      * Default constructor, primarily for the purpose of de-externalization.
@@ -61,7 +64,7 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
      */
     public SynchronizedLRUMap()
     {
-        this( 100 );
+        this( DEFAULT_MAX_SIZE );
     }
 
 
@@ -70,13 +73,12 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
      * capacity is achieved, subsequent gets and puts will push keys out of the
      * map. See .
      * 
-     * @param i
-     *            Maximum capacity of the LRUMap
+     * @param maxSize Maximum capacity of the LRUMap
      */
-    public SynchronizedLRUMap( int i )
+    public SynchronizedLRUMap( int maxSize )
     {
-        super( i );
-        maximumSize = i;
+        super( maxSize );
+        maximumSize = maxSize;
     }
 
 
@@ -88,8 +90,7 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
      * over keys, values, etc. is currently unsupported.
      * </p>
      * 
-     * @param key
-     *            Key to retrieve
+     * @param key Key to retrieve
      * @return Returns the value. Returns null if the key has a null value <i>or</i>
      *         if the key has no value.
      */
@@ -103,6 +104,7 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
 
         Object value = remove( key );
         super.put( key, value );
+        
         return value;
     }
 
@@ -117,43 +119,22 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
      * for removeLRU() for more details.)
      * </p>
      * 
-     * @param key
-     *            Key of the Object to add.
-     * @param value
-     *            Object to add
+     * @param key Key of the Object to add.
+     * @param value Object to add
      * @return Former value of the key
      */
     @Override
-    public Object put( Object key, Object value )
+    public synchronized Object put( Object key, Object value )
     {
-
-        int mapSize = size();
-        Object retval;
-
-        if ( mapSize >= maximumSize )
-        {
-            synchronized ( this )
-            {
-                // don't retire LRU if you are just
-                // updating an existing key
-                if ( !containsKey( key ) )
-                {
-                    // lets retire the least recently used item in the cache
-                    removeLRU();
-                }
-
-                retval = super.put( key, value );
-            }
-        }
-        else
+        // don't retire LRU if you are just
+        // updating an existing key
+        if ( ( maximumSize <= size() ) && ( !containsKey( key ) ) )
         {
-            synchronized ( this )
-            {
-                retval = super.put( key, value );
-            }
+            // lets retire the least recently used item in the cache
+            removeLRU();
         }
 
-        return retval;
+        return super.put( key, value );
     }
 
 
@@ -172,14 +153,12 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
     }
 
 
-    // Properties
-    // -------------------------------------------------------------------------
     /**
      * Getter for property maximumSize.
      * 
      * @return Value of property maximumSize.
      */
-    public int getMaximumSize()
+    public synchronized int getMaximumSize()
     {
         return maximumSize;
     }
@@ -188,19 +167,15 @@ public final class SynchronizedLRUMap extends SequencedHashMap implements Extern
     /**
      * Setter for property maximumSize.
      * 
-     * @param maximumSize
-     *            New value of property maximumSize.
+     * @param maximumSize New value of property maximumSize.
      */
-    public void setMaximumSize( int maximumSize )
+    public synchronized void setMaximumSize( int maximumSize )
     {
-        synchronized ( this )
-        {
-            this.maximumSize = maximumSize;
+        this.maximumSize = maximumSize;
 
-            while ( size() > maximumSize )
-            {
-                removeLRU();
-            }
+        while ( size() > maximumSize )
+        {
+            removeLRU();
         }
     }
 }