This project has retired. For details please refer to its Attic page.
AutoRemoveConsumer 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.configuration.ArchivaConfiguration;
24  import org.apache.archiva.configuration.ConfigurationNames;
25  import org.apache.archiva.configuration.FileTypes;
26  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27  import org.apache.archiva.consumers.ConsumerException;
28  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29  import org.apache.archiva.redback.components.registry.Registry;
30  import org.apache.archiva.redback.components.registry.RegistryListener;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.springframework.context.annotation.Scope;
34  import org.springframework.stereotype.Service;
35  
36  import javax.annotation.PostConstruct;
37  import javax.inject.Inject;
38  import java.io.File;
39  import java.util.ArrayList;
40  import java.util.Date;
41  import java.util.List;
42  
43  /**
44   * AutoRemoveConsumer
45   */
46  @Service( "knownRepositoryContentConsumer#auto-remove" )
47  @Scope( "prototype" )
48  public class AutoRemoveConsumer
49      extends AbstractMonitoredConsumer
50      implements KnownRepositoryContentConsumer, RegistryListener
51  {
52  
53      private Logger log = LoggerFactory.getLogger( AutoRemoveConsumer.class );
54  
55      /**
56       * default-value="auto-remove"
57       */
58      private String id = "auto-remove";
59  
60      /**
61       * default-value="Automatically Remove File from Filesystem."
62       */
63      private String description = "Automatically Remove File from Filesystem.";
64  
65      /**
66       *
67       */
68      @Inject
69      private ArchivaConfiguration configuration;
70  
71      /**
72       *
73       */
74      @Inject
75      private FileTypes filetypes;
76  
77      private File repositoryDir;
78  
79      private List<String> includes = new ArrayList<>( 0 );
80  
81      @Override
82      public String getId( )
83      {
84          return this.id;
85      }
86  
87      @Override
88      public String getDescription( )
89      {
90          return this.description;
91      }
92  
93      @Override
94      public void beginScan( ManagedRepository repository, Date whenGathered )
95          throws ConsumerException
96      {
97          this.repositoryDir = new File( repository.getLocation( ) );
98      }
99  
100     @Override
101     public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
102         throws ConsumerException
103     {
104         beginScan( repository, whenGathered );
105     }
106 
107     @Override
108     public void completeScan( )
109     {
110         /* do nothing */
111     }
112 
113     @Override
114     public void completeScan( boolean executeOnEntireRepo )
115     {
116         completeScan( );
117     }
118 
119     @Override
120     public List<String> getExcludes( )
121     {
122         return null;
123     }
124 
125     @Override
126     public List<String> getIncludes( )
127     {
128         return includes;
129     }
130 
131     @Override
132     public void processFile( String path )
133         throws ConsumerException
134     {
135         File file = new File( this.repositoryDir, path );
136         if ( file.exists( ) )
137         {
138             log.info( "(Auto) Removing File: {}", file.getAbsolutePath( ) );
139             triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath( ) );
140             file.delete( );
141         }
142     }
143 
144     @Override
145     public void processFile( String path, boolean executeOnEntireRepo )
146         throws ConsumerException
147     {
148         processFile( path );
149     }
150 
151     @Override
152     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
153     {
154         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
155         {
156             initIncludes( );
157         }
158     }
159 
160     @Override
161     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
162     {
163         /* do nothing */
164     }
165 
166     private void initIncludes( )
167     {
168         includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.AUTO_REMOVE ) );
169     }
170 
171     @PostConstruct
172     public void initialize( )
173     {
174         configuration.addChangeListener( this );
175 
176         initIncludes( );
177     }
178 }