001package org.apache.archiva.repository.scanner.functors; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.common.utils.BaseFile; 023import org.apache.archiva.consumers.RepositoryContentConsumer; 024import org.apache.commons.collections4.Closure; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import java.util.Map; 029 030/** 031 * ConsumerProcessFileClosure 032 */ 033public class ConsumerProcessFileClosure 034 implements Closure<RepositoryContentConsumer> 035{ 036 private Logger log = LoggerFactory.getLogger( ConsumerProcessFileClosure.class ); 037 038 private BaseFile basefile; 039 040 private boolean executeOnEntireRepo; 041 042 private Map<String, Long> consumerTimings; 043 044 private Map<String, Long> consumerCounts; 045 046 @Override 047 public void execute( RepositoryContentConsumer input ) 048 { 049 RepositoryContentConsumer consumer = (RepositoryContentConsumer) input; 050 051 String id = consumer.getId( ); 052 try 053 { 054 log.debug( "Sending to consumer: {}", id ); 055 056 long startTime = System.currentTimeMillis( ); 057 consumer.processFile( basefile.getRelativePath( ), executeOnEntireRepo ); 058 long endTime = System.currentTimeMillis( ); 059 060 if ( consumerTimings != null ) 061 { 062 Long value = consumerTimings.get( id ); 063 consumerTimings.put( id, ( value != null ? value : 0 ) + endTime - startTime ); 064 } 065 066 if ( consumerCounts != null ) 067 { 068 Long value = consumerCounts.get( id ); 069 consumerCounts.put( id, ( value != null ? value : 0 ) + 1 ); 070 } 071 } 072 catch ( Exception e ) 073 { 074 /* Intentionally Catch all exceptions. 075 * So that the discoverer processing can continue. 076 */ 077 log.error( "Consumer [{}] had an error when processing file [" 078 + "{}]: {}", id, basefile.getAbsolutePath( ), e.getMessage( ), e ); 079 } 080 081 } 082 083 public BaseFile getBasefile( ) 084 { 085 return basefile; 086 } 087 088 public void setBasefile( BaseFile basefile ) 089 { 090 this.basefile = basefile; 091 } 092 093 public boolean isExecuteOnEntireRepo( ) 094 { 095 return executeOnEntireRepo; 096 } 097 098 public void setExecuteOnEntireRepo( boolean executeOnEntireRepo ) 099 { 100 this.executeOnEntireRepo = executeOnEntireRepo; 101 } 102 103 public void setConsumerTimings( Map<String, Long> consumerTimings ) 104 { 105 this.consumerTimings = consumerTimings; 106 } 107 108 public void setConsumerCounts( Map<String, Long> consumerCounts ) 109 { 110 this.consumerCounts = consumerCounts; 111 } 112 113 public Logger getLogger( ) 114 { 115 return log; 116 } 117 118 public void setLogger( Logger logger ) 119 { 120 this.log = logger; 121 } 122}