]> source.dussan.org Git - archiva.git/blob
ccb27874cc6846eb15678947824a9a80bf98353b
[archiva.git] /
1 package org.apache.maven.archiva.repository.scanner;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.commons.collections.Closure;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections.functors.IfClosure;
25 import org.apache.maven.archiva.common.utils.BaseFile;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
29 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
32 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
33 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
34 import org.codehaus.plexus.logging.AbstractLogEnabled;
35
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 /**
43  * RepositoryContentConsumerUtil 
44  *
45  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46  * @version $Id$
47  * 
48  * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers"
49  */
50 public class RepositoryContentConsumers
51     extends AbstractLogEnabled
52 {
53     /**
54      * @plexus.requirement
55      */
56     private ArchivaConfiguration archivaConfiguration;
57
58     /**
59      * @plexus.requirement role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
60      */
61     private List<KnownRepositoryContentConsumer> availableKnownConsumers;
62
63     /**
64      * @plexus.requirement role="org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer"
65      */
66     private List<InvalidRepositoryContentConsumer> availableInvalidConsumers;
67
68     public List<String> getSelectedKnownConsumerIds()
69     {
70         RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
71         return scanning.getKnownContentConsumers();
72     }
73
74     public List<String> getSelectedInvalidConsumerIds()
75     {
76         RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
77         return scanning.getInvalidContentConsumers();
78     }
79
80     public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
81     {
82         Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
83
84         List<String> knownSelected = getSelectedKnownConsumerIds();
85
86         for ( KnownRepositoryContentConsumer consumer : availableKnownConsumers )
87         {
88             if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
89             {
90                 consumerMap.put( consumer.getId(), consumer );
91             }
92         }
93
94         return consumerMap;
95     }
96
97     public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
98     {
99         Map<String, InvalidRepositoryContentConsumer> consumerMap = new HashMap<String, InvalidRepositoryContentConsumer>();
100
101         List<String> invalidSelected = getSelectedInvalidConsumerIds();
102
103         for ( InvalidRepositoryContentConsumer consumer : availableInvalidConsumers )
104         {
105             if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
106             {
107                 consumerMap.put( consumer.getId(), consumer );
108             }
109         }
110
111         return consumerMap;
112     }
113
114     public List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
115     {
116         List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
117
118         List<String> knownSelected = getSelectedInvalidConsumerIds();
119
120         for ( KnownRepositoryContentConsumer consumer : availableKnownConsumers )
121         {
122             if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
123             {
124                 ret.add( consumer );
125             }
126         }
127
128         return ret;
129     }
130
131     public List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
132     {
133         List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
134
135         List<String> invalidSelected = getSelectedInvalidConsumerIds();
136
137         for ( InvalidRepositoryContentConsumer consumer : availableInvalidConsumers )
138         {
139             if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
140             {
141                 ret.add( consumer );
142             }
143         }
144
145         return ret;
146     }
147
148     public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
149     {
150         return availableKnownConsumers;
151     }
152
153     public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
154     {
155         return availableInvalidConsumers;
156     }
157
158     public void setAvailableKnownConsumers( List<KnownRepositoryContentConsumer> availableKnownConsumers )
159     {
160         this.availableKnownConsumers = availableKnownConsumers;
161     }
162
163     public void setAvailableInvalidConsumers( List<InvalidRepositoryContentConsumer> availableInvalidConsumers )
164     {
165         this.availableInvalidConsumers = availableInvalidConsumers;
166     }
167
168     public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile )
169     {
170         // Run the repository consumers
171         try
172         {
173             Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getLogger() );
174
175             CollectionUtils.forAllDo( availableKnownConsumers, triggerBeginScan );
176             CollectionUtils.forAllDo( availableInvalidConsumers, triggerBeginScan );
177
178             // yuck. In case you can't read this, it says
179             // "process the file if the consumer has it in the includes list, and not in the excludes list"
180             BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
181             ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
182             predicate.setBasefile( baseFile );
183             ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure( getLogger() );
184             closure.setBasefile( baseFile );
185             predicate.setCaseSensitive( false );
186             Closure processIfWanted = IfClosure.getInstance( predicate, closure );
187
188             CollectionUtils.forAllDo( availableKnownConsumers, processIfWanted );
189
190             if ( predicate.getWantedFileCount() <= 0 )
191             {
192                 // Nothing known processed this file.  It is invalid!
193                 CollectionUtils.forAllDo( availableInvalidConsumers, closure );
194             }
195         }
196         finally
197         {
198             /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
199                         CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
200                         CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
201             */
202         }
203     }
204
205 }