View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.mitosis.service;
21  
22  
23  import org.apache.directory.mitosis.common.Constants;
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.core.interceptor.Interceptor;
26  import org.quartz.Job;
27  import org.quartz.JobExecutionContext;
28  import org.quartz.JobExecutionException;
29  
30  import java.util.Map;
31  
32  
33  /**
34   * A <a href="http://www.opensymphony.com/quartz/">OpenSymphony Quartz</a>
35   * {@link Job} that purges old replication logs and the old entries marked as
36   * 'deleted' (i.e. {@link Constants#ENTRY_DELETED} is <tt>TRUE</tt>).  This
37   * {@link Job} just calls {@link ReplicationInterceptor#purgeAgedData()} to
38   * purge old data. 
39   * 
40   * @author The Apache Directory Project Team
41   */
42  public class ReplicationLogCleanJob implements Job
43  {
44      public static final String INSTANCE_ID = "instanceId";
45      private Map<String,DirectoryService> services;
46  
47  
48      public ReplicationLogCleanJob( Map<String,DirectoryService> services )
49      {
50          this.services = services;
51      }
52  
53  
54      public void execute( JobExecutionContext ctx ) throws JobExecutionException
55      {
56          String instanceId = ctx.getJobDetail().getJobDataMap().getString( INSTANCE_ID );
57          if ( instanceId == null )
58          {
59              // Execute for all instances in the VM if instanceId is not specified.
60              for ( DirectoryService service : services.values() )
61              {
62                  execute0( service );
63              }
64          }
65          else
66          {
67              // Execute for the instance with the specified instanceId if
68              // it is specified.
69  
70              execute0( services.get( instanceId ) );
71          }
72      }
73  
74  
75      private void execute0( DirectoryService service ) throws JobExecutionException
76      {
77          for ( Interceptor interceptor : service.getInterceptorChain().getAll() )
78          {
79              if ( interceptor instanceof ReplicationInterceptor )
80              {
81                  try
82                  {
83                      ( ( ReplicationInterceptor ) interceptor ).purgeAgedData();
84                  }
85                  catch ( Exception e )
86                  {
87                      throw new JobExecutionException( e );
88                  }
89              }
90          }
91      }
92  }