/**
 * 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.pkg.maven.content.cache;

import org.commonjava.indy.IndyWorkflowException;
import org.commonjava.indy.content.DirectContentAccess;
import org.commonjava.indy.model.core.StoreKey;
import org.commonjava.indy.pkg.maven.content.MetadataInfo;
import org.commonjava.indy.pkg.maven.content.MetadataKey;
import org.commonjava.maven.galley.event.EventMetadata;
import org.commonjava.maven.galley.model.Transfer;
import org.infinispan.client.hotrod.annotation.ClientCacheEntryExpired;
import org.infinispan.client.hotrod.annotation.ClientListener;
import org.infinispan.client.hotrod.event.ClientCacheEntryExpiredEvent;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryExpired;
import org.infinispan.notifications.cachelistener.event.CacheEntryExpiredEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.io.IOException;

import static org.commonjava.indy.data.StoreDataManager.IGNORE_READONLY;

@ApplicationScoped
@Listener
@ClientListener
public class MavenMetadataCacheListener
{

    @Inject
    private DirectContentAccess fileManager;

    @CacheEntryExpired
    public void metadataExpired( CacheEntryExpiredEvent<MetadataKey, MetadataInfo> event )
    {
        handleMetadataExpired( event.getKey() );
    }

    @ClientCacheEntryExpired
    public void metadataExpired( ClientCacheEntryExpiredEvent<MetadataKey> event )
    {
        handleMetadataExpired( event.getKey() );
    }

    private void handleMetadataExpired( MetadataKey key )
    {
        Logger logger = LoggerFactory.getLogger( getClass() );

        StoreKey storeKey = key.getStoreKey();
        String path = key.getPath();

        try
        {
            Transfer target = fileManager.getTransfer( storeKey, path );

            if ( target != null && target.exists() )
            {
                EventMetadata eventMetadata = new EventMetadata().set( IGNORE_READONLY, true );
                target.delete( true, eventMetadata );
            }
        }
        catch ( IndyWorkflowException | IOException e )
        {
            logger.warn( "On cache expiration, metadata file deletion failed for: " + path + " in store: " + storeKey, e );
        }
    }

}

@@ -781,34 +781,131 @@
 ontent.D
-irectContentAccess
+ownloadManager;%0Aimport org.commonjava.indy.data.IndyDataException;%0Aimport org.commonjava.indy.data.StoreDataManager
 ;%0Aimport
@@ -1123,59 +1123,8 @@
 ta;%0A
-import org.commonjava.maven.galley.model.Transfer;%0A
 impo
@@ -1672,36 +1672,8 @@
 ect;
-%0Aimport java.io.IOException;
 %0A%0Aim
@@ -1861,31 +1861,91 @@
 te D
-irectContentAccess file
+ownloadManager downloadManager;%0A%0A    @Inject%0A    private StoreDataManager storeData
 Mana
@@ -2551,49 +2551,65 @@
     
-Transfer target = fileManager.getTransfer
+downloadManager.delete( storeDataManager.getArtifactStore
 ( st
@@ -2618,18 +2618,17 @@
 eKey
+ )
 , path
- );%0A
+,
 %0A   
@@ -2640,217 +2640,84 @@
     
-if ( target != null && target.exists() )%0A            %7B%0A                EventMetadata eventMetadata = new EventMetadata().set( IGNORE_READONLY, true );%0A                target.delete( true, eventMetadata );%0A    
+                        new EventMetadata().set( IGNORE_READONLY, true ) );%0A
     
@@ -2722,26 +2722,16 @@
       %7D%0A
-        %7D%0A
         
@@ -2737,17 +2737,16 @@
  catch (
- 
 IndyWork
@@ -2762,17 +2762,23 @@
 tion %7C I
-O
+ndyData
 Exceptio

Fix the deletion of transfer on cache expired