/**
 * Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * Licensed 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.commonjava.indy.inject;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.enterprise.inject.Default;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;

import org.commonjava.indy.stats.IndyDeprecatedApis;
import org.commonjava.indy.stats.IndyVersioning;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Producer class that reads a properties file off the classpath containing version info for Indy, and assembles an instance of {@link IndyVersioning},
 * which this component then provides for injecting into other components.
 */
@Singleton
public class IndyVersioningProvider
{

    private static final String INDY_VERSIONING_PROPERTIES = "indy-version.properties";

    private static final String INDY_DEPRECATED_APIS_PROPERTIES = "deprecated-apis.properties";

    private final IndyVersioning versioning;

    private final IndyDeprecatedApis deprecatedApis;

    private final Logger logger = LoggerFactory.getLogger( getClass() );

    public IndyVersioningProvider()
    {
        // Load indy-version
        final Properties props = new Properties();
        try (InputStream is = Thread.currentThread()
                                    .getContextClassLoader()
                                    .getResourceAsStream( INDY_VERSIONING_PROPERTIES ))
        {
            if ( is != null )
            {
                props.load( is );
            }
        }
        catch ( final IOException e )
        {
            logger.error( "Failed to read Indy versioning information from classpath resource: "
                                          + INDY_VERSIONING_PROPERTIES, e );
        }

        versioning = new IndyVersioning( props.getProperty( "version", "unknown" ),
                                         props.getProperty( "builder", "unknown" ),
                                         props.getProperty( "commit.id", "unknown" ),
                                         props.getProperty( "timestamp", "unknown" ),
                                         props.getProperty( "api-version", "unknown" ) );

        // Load deprecated-apis
        String deprecatedApiFile = System.getProperty( "ENV_DEPRECATED_API_FILE", INDY_DEPRECATED_APIS_PROPERTIES );
        logger.info( "Get deprecatedApiFile: {}", deprecatedApiFile );

        final Properties deprApis = new Properties();
        try (InputStream is = Thread.currentThread()
                                    .getContextClassLoader()
                                    .getResourceAsStream( deprecatedApiFile ))
        {
            if ( is != null )
            {
                deprApis.load( is );
            }
        }
        catch ( final IOException e )
        {
            logger.error( "Failed to read Indy deprecated api information from classpath resource: "
                                          + deprecatedApiFile, e );
        }

        deprecatedApis = new IndyDeprecatedApis( deprApis );

    }

    @Produces
    @Default
    public IndyVersioning getVersioningInstance()
    {
        return versioning;
    }

    @Produces
    @Default
    public IndyDeprecatedApis getDeprecatedApis()
    {
        return deprecatedApis;
    }

}

@@ -1716,32 +1716,105 @@
 rovider()%0A    %7B%0A
+        ClassLoader cl = IndyVersioningProvider.class.getClassLoader();%0A%0A
         // Load 
@@ -1911,51 +1911,70 @@
 s = 
-Thread.currentThread()%0A            
+cl.getResourceAsStream( INDY_VERSIONING_PROPERTIES ))%0A
         
     
@@ -1961,32 +1961,34 @@
 TIES ))%0A        
+%7B%0A
                 
@@ -1987,35 +1987,24 @@
     
-    .getContextClassLoader(
+if ( is != null 
 )%0A  
@@ -2009,32 +2009,26 @@
             
-        
+%7B%0A
             
@@ -2035,59 +2035,25 @@
     
-.getResourceAsStream( INDY_VERSIONING_PROPERTIES ))
+props.load( is );
 %0A   
@@ -2049,33 +2049,37 @@
 ( is );%0A        
-%7B
+    %7D
 %0A            if 
@@ -2075,33 +2075,20 @@
         
-if ( is != null )
+else
 %0A       
@@ -2106,38 +2106,111 @@
             
-props.load( is
+logger.warn( %22Resource not found, file: %7B%7D, loader: %7B%7D%22, INDY_VERSIONING_PROPERTIES, cl
  );%0A        
@@ -3198,51 +3198,61 @@
 s = 
-Thread.currentThread()%0A            
+cl.getResourceAsStream( deprecatedApiFile ))%0A
         
     
@@ -3239,32 +3239,34 @@
 File ))%0A        
+%7B%0A
                 
@@ -3265,42 +3265,26 @@
     
-    .getContextClassLoader()%0A     
+if ( is != null )%0A
     
@@ -3283,35 +3283,34 @@
 l )%0A            
-   
+%7B%0A
                 
@@ -3313,50 +3313,28 @@
     
-.getResourceAsStream( deprecatedApiFile ))
+deprApis.load( is );
 %0A   
@@ -3330,33 +3330,37 @@
 ( is );%0A        
-%7B
+    %7D
 %0A            if 
@@ -3356,33 +3356,20 @@
         
-if ( is != null )
+else
 %0A       
@@ -3391,33 +3391,108 @@
         
-deprApis.load( is
+logger.warn( %22Resource not found, file: %7B%7D, loader: %7B%7D%22, INDY_DEPRECATED_APIS_PROPERTIES, cl
  );%0A    

Use getClass().getClassLoader() to load version properties file (#1604)