View Javadoc

1   package org.apache.continuum.purge.controller;
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.continuum.model.repository.AbstractPurgeConfiguration;
23  import org.apache.continuum.model.repository.RepositoryPurgeConfiguration;
24  import org.apache.continuum.purge.ContinuumPurgeConstants;
25  import org.apache.continuum.purge.PurgeConfigurationService;
26  import org.apache.continuum.purge.PurgeConfigurationServiceException;
27  import org.apache.continuum.purge.executor.CleanAllPurgeExecutor;
28  import org.apache.continuum.purge.executor.ContinuumPurgeExecutor;
29  import org.apache.continuum.purge.executor.ContinuumPurgeExecutorException;
30  import org.apache.continuum.purge.executor.DaysOldRepositoryPurgeExecutor;
31  import org.apache.continuum.purge.executor.ReleasedSnapshotsRepositoryPurgeExecutor;
32  import org.apache.continuum.purge.executor.RetentionCountRepositoryPurgeExecutor;
33  import org.apache.continuum.purge.repository.content.RepositoryManagedContent;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * DefaultPurgeController
39   *
40   * @author Maria Catherine Tan
41   * @plexus.component role="org.apache.continuum.purge.controller.PurgeController" role-hint="purge-repository"
42   */
43  public class RepositoryPurgeController
44      implements PurgeController
45  {
46      private static final Logger log = LoggerFactory.getLogger( RepositoryPurgeController.class );
47  
48      private ContinuumPurgeExecutor purgeExecutor;
49  
50      private ContinuumPurgeExecutor purgeReleasedSnapshotsExecutor;
51  
52      /**
53       * @plexus.requirement
54       */
55      private PurgeConfigurationService purgeConfigurationService;
56  
57      private boolean deleteReleasedSnapshots = false;
58  
59      public void initializeExecutors( AbstractPurgeConfiguration purgeConfig )
60          throws ContinuumPurgeExecutorException
61      {
62          RepositoryManagedContent repositoryContent;
63  
64          RepositoryPurgeConfiguration repoPurge = (RepositoryPurgeConfiguration) purgeConfig;
65  
66          try
67          {
68              repositoryContent = purgeConfigurationService.getManagedRepositoryContent(
69                  repoPurge.getRepository().getId() );
70          }
71          catch ( PurgeConfigurationServiceException e )
72          {
73              throw new ContinuumPurgeExecutorException( "Error while initializing purge executors", e );
74          }
75  
76          if ( repoPurge.isDeleteAll() )
77          {
78              purgeExecutor = new CleanAllPurgeExecutor( ContinuumPurgeConstants.PURGE_REPOSITORY );
79          }
80          else
81          {
82              if ( repoPurge.getDaysOlder() > 0 )
83              {
84                  purgeExecutor = new DaysOldRepositoryPurgeExecutor( repositoryContent, repoPurge.getDaysOlder(),
85                                                                      repoPurge.getRetentionCount() );
86              }
87              else
88              {
89                  purgeExecutor = new RetentionCountRepositoryPurgeExecutor( repositoryContent,
90                                                                             repoPurge.getRetentionCount() );
91              }
92  
93              purgeReleasedSnapshotsExecutor = new ReleasedSnapshotsRepositoryPurgeExecutor( repositoryContent );
94              deleteReleasedSnapshots = repoPurge.isDeleteReleasedSnapshots();
95          }
96      }
97  
98      public void doPurge( AbstractPurgeConfiguration purgeConfig )
99      {
100         RepositoryPurgeConfiguration repoPurge = (RepositoryPurgeConfiguration) purgeConfig;
101         log.info( "--- Start: Purging repository [{}] {} ---", repoPurge.getRepository().getId(),
102                   repoPurge.getRepository().getLocation() );
103         doPurge( repoPurge.getRepository().getLocation() );
104         log.info( "--- End: Purging repository [{}] {} ---", repoPurge.getRepository().getId(),
105                   repoPurge.getRepository().getLocation() );
106     }
107 
108     public void doPurge( String path )
109     {
110         try
111         {
112             if ( deleteReleasedSnapshots )
113             {
114                 purgeReleasedSnapshotsExecutor.purge( path );
115             }
116 
117             purgeExecutor.purge( path );
118         }
119         catch ( ContinuumPurgeExecutorException e )
120         {
121             log.error( e.getMessage(), e );
122         }
123     }
124 }