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