001    package 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    
022    import org.apache.commons.collections.Closure;
023    import org.apache.archiva.common.utils.BaseFile;
024    import org.apache.archiva.consumers.RepositoryContentConsumer;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    import java.util.Map;
029    
030    /**
031     * ConsumerProcessFileClosure 
032     *
033     *
034     */
035    public class ConsumerProcessFileClosure
036        implements Closure
037    {
038        private Logger log = LoggerFactory.getLogger( ConsumerProcessFileClosure.class );
039        
040        private BaseFile basefile;
041    
042        private boolean executeOnEntireRepo;
043    
044        private Map<String,Long> consumerTimings;
045        
046        private Map<String,Long> consumerCounts;
047    
048        public void execute( Object input )
049        {
050            if ( input instanceof RepositoryContentConsumer )
051            {
052                RepositoryContentConsumer consumer = (RepositoryContentConsumer) input;
053    
054                String id = consumer.getId();
055                try
056                {
057                    log.debug( "Sending to consumer: {}", id );
058    
059                    long startTime = System.currentTimeMillis();
060                    consumer.processFile( basefile.getRelativePath(), executeOnEntireRepo );
061                    long endTime = System.currentTimeMillis();
062    
063                    if ( consumerTimings != null )
064                    {
065                        Long value = consumerTimings.get( id );
066                        consumerTimings.put( id, ( value != null ? value : 0 ) + endTime - startTime );
067                    }
068    
069                    if ( consumerCounts != null )
070                    {
071                        Long value = consumerCounts.get( id );
072                        consumerCounts.put( id, ( value != null ? value : 0 ) + 1 );
073                    }
074                }
075                catch ( Exception e )
076                {
077                    /* Intentionally Catch all exceptions.
078                     * So that the discoverer processing can continue.
079                     */
080                    log.error( "Consumer [" + id + "] had an error when processing file ["
081                        + basefile.getAbsolutePath() + "]: " + e.getMessage(), e );
082                }
083            }
084        }
085    
086        public BaseFile getBasefile()
087        {
088            return basefile;
089        }
090    
091        public void setBasefile( BaseFile basefile )
092        {
093            this.basefile = basefile;
094        }
095    
096        public boolean isExecuteOnEntireRepo()
097        {
098            return executeOnEntireRepo;
099        }
100    
101        public void setExecuteOnEntireRepo( boolean executeOnEntireRepo )
102        {
103            this.executeOnEntireRepo = executeOnEntireRepo;
104        }
105    
106        public void setConsumerTimings( Map<String, Long> consumerTimings )
107        {
108            this.consumerTimings = consumerTimings;
109        }
110    
111        public void setConsumerCounts( Map<String, Long> consumerCounts )
112        {
113            this.consumerCounts = consumerCounts;
114        }
115    
116        public Logger getLogger()
117        {
118            return log;
119        }
120    
121        public void setLogger( Logger logger )
122        {
123            this.log = logger;
124        }
125    }