This project has retired. For details please refer to its Attic page.
ArtifactMissingChecksumsConsumer xref
View Javadoc
1   package org.apache.archiva.consumers.core;
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.archiva.checksum.*;
23  import org.apache.archiva.configuration.ArchivaConfiguration;
24  import org.apache.archiva.configuration.FileTypes;
25  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
26  import org.apache.archiva.consumers.ConsumerException;
27  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28  import org.apache.archiva.repository.ManagedRepository;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.context.annotation.Scope;
32  import org.springframework.stereotype.Service;
33  
34  import javax.annotation.PostConstruct;
35  import javax.inject.Inject;
36  import java.io.IOException;
37  import java.nio.file.Files;
38  import java.nio.file.Path;
39  import java.nio.file.Paths;
40  import java.util.ArrayList;
41  import java.util.Date;
42  import java.util.List;
43  
44  /**
45   * ArtifactMissingChecksumsConsumer - Create missing and/or fix invalid checksums for the artifact.
46   */
47  @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
48  @Scope( "prototype" )
49  public class ArtifactMissingChecksumsConsumer
50      extends AbstractMonitoredConsumer
51      implements KnownRepositoryContentConsumer
52      // it's prototype bean so we assume configuration won't change during a run
53      //, RegistryListener
54  {
55  
56      private Logger log = LoggerFactory.getLogger( ArtifactMissingChecksumsConsumer.class );
57  
58      private String id = "create-missing-checksums";
59  
60      private String description = "Create Missing and/or Fix Invalid Checksum files.";
61  
62      private ArchivaConfiguration configuration;
63  
64      private FileTypes filetypes;
65  
66      private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
67  
68      private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
69  
70      private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
71  
72      private Path repositoryDir;
73  
74      private List<String> includes = new ArrayList<>( 0 );
75      private List<ChecksumAlgorithm> algorithms;
76  
77      @Inject
78      public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration, FileTypes filetypes )
79      {
80          this.configuration = configuration;
81          this.filetypes = filetypes;
82  
83          //configuration.addChangeListener( this );
84  
85          initIncludes( );
86      }
87  
88      @Override
89      public String getId( )
90      {
91          return this.id;
92      }
93  
94      @Override
95      public String getDescription( )
96      {
97          return this.description;
98      }
99  
100     @Override
101     public void beginScan( ManagedRepository repo, Date whenGathered )
102         throws ConsumerException
103     {
104         this.repositoryDir = Paths.get( repo.getLocation( ) );
105     }
106 
107     @Override
108     public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
109         throws ConsumerException
110     {
111         beginScan( repo, whenGathered );
112     }
113 
114     @Override
115     public void completeScan( )
116     {
117         /* do nothing */
118     }
119 
120     @Override
121     public void completeScan( boolean executeOnEntireRepo )
122     {
123         completeScan( );
124     }
125 
126     @Override
127     public List<String> getExcludes( )
128     {
129         return getDefaultArtifactExclusions( );
130     }
131 
132     @Override
133     public List<String> getIncludes( )
134     {
135         return includes;
136     }
137 
138     @Override
139     public void processFile( String path )
140         throws ConsumerException
141     {
142         Path artifactPath = repositoryDir.resolve(path);
143         ChecksummedFileummedFile.html#ChecksummedFile">ChecksummedFile csFile = new ChecksummedFile(artifactPath);
144         UpdateStatusList result = csFile.fixChecksums(algorithms);
145         if (result.getTotalStatus()== UpdateStatus.ERROR) {
146             log.warn( "Error accessing file {}. ", path );
147             triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
148                     "Error accessing file " + path + "." );
149         } else {
150             result.getStatusList().stream().forEach(st ->
151                     triggerInfo(path, st));
152         }
153     }
154 
155     private void triggerInfo(String path, UpdateStatus status) {
156         switch (status.getValue()) {
157             case UpdateStatus.ERROR:
158                 log.error( "Cannot create checksum for file {} :", path, status.getError() );
159                 triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + path +
160                         ": " + status.getError().getMessage( ) );
161                 break;
162             case UpdateStatus.CREATED:
163                 log.info( "Created missing checksum file {}", path );
164                 triggerConsumerInfo( "Created missing checksum file " + path );
165                 break;
166             case UpdateStatus.UPDATED:
167                 log.info( "Fixed checksum file {}", path );
168                 triggerConsumerInfo( "Fixed checksum file " + path );
169                 break;
170 
171         }
172     }
173 
174     @Override
175     public void processFile( String path, boolean executeOnEntireRepo )
176         throws ConsumerException
177     {
178         processFile( path );
179     }
180 
181 
182     /*
183     @Override
184     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
185     {
186         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
187         {
188             initIncludes();
189         }
190     }
191 
192 
193     @Override
194     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
195     {
196         // do nothing
197     }
198 
199     */
200 
201     private void initIncludes( )
202     {
203         includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
204 
205     }
206 
207     @PostConstruct
208     public void initialize( )
209     {
210         //configuration.addChangeListener( this );
211 
212         initIncludes( );
213         algorithms = ChecksumUtil.getAlgorithms(configuration.getConfiguration().getArchivaRuntimeConfiguration().getChecksumTypes());
214     }
215 }