View Javadoc

1   package org.apache.continuum.purge.task;
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.DirectoryPurgeConfiguration;
24  import org.apache.continuum.model.repository.LocalRepository;
25  import org.apache.continuum.model.repository.RepositoryPurgeConfiguration;
26  import org.apache.continuum.purge.PurgeConfigurationService;
27  import org.apache.continuum.purge.controller.PurgeController;
28  import org.apache.continuum.purge.executor.ContinuumPurgeExecutorException;
29  import org.apache.continuum.purge.repository.scanner.RepositoryScanner;
30  import org.codehaus.plexus.PlexusConstants;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.context.Context;
34  import org.codehaus.plexus.context.ContextException;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36  import org.codehaus.plexus.taskqueue.Task;
37  import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
38  import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
39  
40  /**
41   * @author Maria Catherine Tan
42   */
43  public class PurgeTaskExecutor
44      implements TaskExecutor, Contextualizable
45  {
46      /**
47       * @plexus.requirement
48       */
49      private PurgeConfigurationService purgeConfigurationService;
50  
51      /**
52       * @plexus.requirement
53       */
54      private RepositoryScanner scanner;
55  
56      private PlexusContainer container;
57  
58      public void executeTask( Task task )
59          throws TaskExecutionException
60      {
61          PurgeTask purgeTask = (PurgeTask) task;
62  
63          AbstractPurgeConfiguration purgeConfig =
64              purgeConfigurationService.getPurgeConfiguration( purgeTask.getPurgeConfigurationId() );
65  
66          try
67          {
68              if ( purgeConfig != null && purgeConfig instanceof RepositoryPurgeConfiguration )
69              {
70                  RepositoryPurgeConfiguration repoPurge = (RepositoryPurgeConfiguration) purgeConfig;
71  
72                  LocalRepository repository = repoPurge.getRepository();
73  
74                  if ( repository == null )
75                  {
76                      throw new TaskExecutionException(
77                          "Error while executing purge repository task: no repository set" );
78                  }
79  
80                  PurgeController purgeController =
81                      (PurgeController) container.lookup( PurgeController.ROLE, "purge-repository" );
82  
83                  purgeController.initializeExecutors( repoPurge );
84  
85                  if ( repoPurge.isDeleteAll() )
86                  {
87                      purgeController.doPurge( repository.getLocation() );
88                  }
89                  else
90                  {
91                      scanner.scan( repository, purgeController );
92                  }
93              }
94              else if ( purgeConfig != null && purgeConfig instanceof DirectoryPurgeConfiguration )
95              {
96                  DirectoryPurgeConfiguration dirPurge = (DirectoryPurgeConfiguration) purgeConfig;
97  
98                  PurgeController purgeController =
99                      (PurgeController) container.lookup( PurgeController.ROLE, "purge-directory" );
100 
101                 purgeController.initializeExecutors( dirPurge );
102 
103                 purgeController.doPurge( dirPurge.getLocation() );
104             }
105 
106         }
107         catch ( ComponentLookupException e )
108         {
109             throw new TaskExecutionException( "Error while executing purge task", e );
110         }
111         catch ( ContinuumPurgeExecutorException e )
112         {
113             throw new TaskExecutionException( "Error while executing purge task", e );
114         }
115     }
116 
117     public void contextualize( Context context )
118         throws ContextException
119     {
120         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
121     }
122 }