You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2022/05/10 06:53:22 UTC

[maven-build-cache-extension] branch master updated: [MBUILDCACHE-17] IllegalStateException: Cache is not initialized. Actual state: null (#9)

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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-build-cache-extension.git


The following commit(s) were added to refs/heads/master by this push:
     new 78e6b06  [MBUILDCACHE-17] IllegalStateException: Cache is not initialized. Actual state: null (#9)
78e6b06 is described below

commit 78e6b066251d000ce7aeff3a0a7c1a340327de54
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue May 10 08:53:18 2022 +0200

    [MBUILDCACHE-17] IllegalStateException: Cache is not initialized. Actual state: null (#9)
---
 .../maven/buildcache/xml/CacheConfigImpl.java      | 122 +++++++++++----------
 1 file changed, 65 insertions(+), 57 deletions(-)

diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
index fcc5dd8..313cf58 100644
--- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
+++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
@@ -92,7 +92,7 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
     private final XmlService xmlService;
     private final MavenSession session;
 
-    private CacheState state;
+    private volatile CacheState state;
     private CacheConfig cacheConfig;
     private HashFactory hashFactory;
     private List<Pattern> excludePatterns;
@@ -110,69 +110,77 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
     {
         if ( state == null )
         {
-            final String enabled = getProperty( CACHE_ENABLED_PROPERTY_NAME, "true" );
-            if ( !Boolean.parseBoolean( enabled ) )
+            synchronized ( this )
             {
-                LOGGER.info( "Cache disabled by command line flag, project will be built fully and not cached" );
-                state = CacheState.DISABLED;
-            }
-            else
-            {
-                Path configPath;
-
-                String configPathText = getProperty( CONFIG_PATH_PROPERTY_NAME, null );
-                if ( StringUtils.isNotBlank( configPathText ) )
+                if ( state == null )
                 {
-                    configPath = Paths.get( configPathText );
-                }
-                else
-                {
-                    configPath = getMultimoduleRoot( session ).resolve( ".mvn" )
-                            .resolve( "maven-build-cache-config.xml" );
-                }
-
-                if ( !Files.exists( configPath ) )
-                {
-                    LOGGER.info( "Cache configuration is not available at configured path {}, "
-                            + "cache is enabled with defaults", configPath );
-                    cacheConfig = new CacheConfig();
-                }
-                else
-                {
-                    try
+                    final String enabled = getProperty( CACHE_ENABLED_PROPERTY_NAME, "true" );
+                    if ( !Boolean.parseBoolean( enabled ) )
                     {
-                        LOGGER.info( "Loading cache configuration from {}", configPath );
-                        cacheConfig = xmlService.loadCacheConfig( configPath.toFile() );
-                        fillWithDefaults( cacheConfig );
+                        LOGGER.info(
+                                "Cache disabled by command line flag, project will be built fully and not cached" );
+                        state = CacheState.DISABLED;
                     }
-                    catch ( Exception e )
+                    else
                     {
-                        throw new IllegalArgumentException(
-                                "Cannot initialize cache because xml config is not valid or not available", e );
-                    }
-                }
-                fillWithDefaults( cacheConfig );
+                        Path configPath;
+
+                        String configPathText = getProperty( CONFIG_PATH_PROPERTY_NAME, null );
+                        if ( StringUtils.isNotBlank( configPathText ) )
+                        {
+                            configPath = Paths.get( configPathText );
+                        }
+                        else
+                        {
+                            configPath = getMultimoduleRoot( session ).resolve( ".mvn" )
+                                    .resolve( "maven-build-cache-config.xml" );
+                        }
+
+                        if ( !Files.exists( configPath ) )
+                        {
+                            LOGGER.info( "Cache configuration is not available at configured path {}, "
+                                    + "cache is enabled with defaults", configPath );
+                            cacheConfig = new CacheConfig();
+                        }
+                        else
+                        {
+                            try
+                            {
+                                LOGGER.info( "Loading cache configuration from {}", configPath );
+                                cacheConfig = xmlService.loadCacheConfig( configPath.toFile() );
+                                fillWithDefaults( cacheConfig );
+                            }
+                            catch ( Exception e )
+                            {
+                                throw new IllegalArgumentException(
+                                        "Cannot initialize cache because xml config is not valid or not available", e );
+                            }
+                        }
+                        fillWithDefaults( cacheConfig );
 
-                if ( !cacheConfig.getConfiguration().isEnabled() )
-                {
-                    state = CacheState.DISABLED;
-                }
-                else
-                {
-                    String hashAlgorithm = null;
-                    try
-                    {
-                        hashAlgorithm = getConfiguration().getHashAlgorithm();
-                        hashFactory = HashFactory.of( hashAlgorithm );
-                        LOGGER.info( "Using {} hash algorithm for cache", hashAlgorithm );
+                        if ( !cacheConfig.getConfiguration().isEnabled() )
+                        {
+                            state = CacheState.DISABLED;
+                        }
+                        else
+                        {
+                            String hashAlgorithm = null;
+                            try
+                            {
+                                hashAlgorithm = getConfiguration().getHashAlgorithm();
+                                hashFactory = HashFactory.of( hashAlgorithm );
+                                LOGGER.info( "Using {} hash algorithm for cache", hashAlgorithm );
+                            }
+                            catch ( Exception e )
+                            {
+                                throw new IllegalArgumentException( "Unsupported hashing algorithm: " + hashAlgorithm,
+                                        e );
+                            }
+
+                            excludePatterns = compileExcludePatterns();
+                            state = CacheState.INITIALIZED;
+                        }
                     }
-                    catch ( Exception e )
-                    {
-                        throw new IllegalArgumentException( "Unsupported hashing algorithm: " + hashAlgorithm, e );
-                    }
-
-                    excludePatterns = compileExcludePatterns();
-                    state = CacheState.INITIALIZED;
                 }
             }
         }