This project has retired. For details please refer to its Attic page.
AutoRenameConsumer 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.admin.model.beans.ManagedRepository;
23  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
24  import org.apache.archiva.consumers.ConsumerException;
25  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
26  import org.apache.commons.io.FileUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.context.annotation.Scope;
30  import org.springframework.stereotype.Service;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.Date;
36  import java.util.HashMap;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * AutoRenameConsumer
43   */
44  @Service( "knownRepositoryContentConsumer#auto-rename" )
45  @Scope( "prototype" )
46  public class AutoRenameConsumer
47      extends AbstractMonitoredConsumer
48      implements KnownRepositoryContentConsumer
49  {
50      private Logger log = LoggerFactory.getLogger( AutoRenameConsumer.class );
51  
52      private String id = "auto-rename";
53  
54      private String description = "Automatically rename common artifact mistakes.";
55  
56      private static final String RENAME_FAILURE = "rename_failure";
57  
58      private File repositoryDir;
59  
60      private List<String> includes = new ArrayList<>( 3 );
61  
62      private Map<String, String> extensionRenameMap = new HashMap<>( );
63  
64      public AutoRenameConsumer( )
65      {
66          includes.add( "**/*.distribution-tgz" );
67          includes.add( "**/*.distribution-zip" );
68          includes.add( "**/*.plugin" );
69  
70          extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
71          extensionRenameMap.put( ".distribution-zip", ".zip" );
72          extensionRenameMap.put( ".plugin", ".jar" );
73      }
74  
75      @Override
76      public String getId( )
77      {
78          return this.id;
79      }
80  
81      @Override
82      public String getDescription( )
83      {
84          return this.description;
85      }
86  
87      @Override
88      public void beginScan( ManagedRepository repository, Date whenGathered )
89          throws ConsumerException
90      {
91          this.repositoryDir = new File( repository.getLocation( ) );
92      }
93  
94      @Override
95      public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
96          throws ConsumerException
97      {
98          beginScan( repository, whenGathered );
99      }
100 
101     @Override
102     public void completeScan( )
103     {
104         /* do nothing */
105     }
106 
107     @Override
108     public void completeScan( boolean executeOnEntireRepo )
109     {
110         completeScan( );
111     }
112 
113     @Override
114     public List<String> getExcludes( )
115     {
116         return null;
117     }
118 
119     @Override
120     public List<String> getIncludes( )
121     {
122         return includes;
123     }
124 
125     @Override
126     public void processFile( String path )
127         throws ConsumerException
128     {
129         File file = new File( this.repositoryDir, path );
130         if ( file.exists( ) )
131         {
132             Iterator<String> itExtensions = this.extensionRenameMap.keySet( ).iterator( );
133             while ( itExtensions.hasNext( ) )
134             {
135                 String extension = itExtensions.next( );
136                 if ( path.endsWith( extension ) )
137                 {
138                     String fixedExtension = this.extensionRenameMap.get( extension );
139                     String correctedPath = path.substring( 0, path.length( ) - extension.length( ) ) + fixedExtension;
140                     File to = new File( this.repositoryDir, correctedPath );
141                     try
142                     {
143                         // Rename the file.
144                         FileUtils.moveFile( file, to );
145                     }
146                     catch ( IOException e )
147                     {
148                         log.warn( "Unable to rename {} to {} :", path, correctedPath, e );
149                         triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
150                             ": " + e.getMessage( ) );
151                     }
152                 }
153             }
154 
155             log.info( "(Auto) Removing File: {} ", file.getAbsolutePath( ) );
156             triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath( ) );
157             file.delete( );
158         }
159     }
160 
161     @Override
162     public void processFile( String path, boolean executeOnEntireRepo )
163         throws ConsumerException
164     {
165         processFile( path );
166     }
167 }