View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  package org.jumpmind.symmetric.load;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.jumpmind.symmetric.common.ParameterConstants;
25  import org.jumpmind.symmetric.common.TableConstants;
26  import org.jumpmind.symmetric.model.IncomingBatchHistory;
27  import org.jumpmind.symmetric.service.IBootstrapService;
28  import org.jumpmind.symmetric.service.IParameterService;
29  
30  /***
31   * An out of the box filter that checks to see if the SymmetricDS trigger
32   * configuration has changed. If it has, it will synchronize triggers.
33   */
34  public class SyncTriggersRequiredFilter implements IDataLoaderFilter, IBatchListener {
35  
36      static final Log logger = LogFactory.getLog(SyncTriggersRequiredFilter.class);
37  
38      final String CTX_KEY_RESYNC_NEEDED = SyncTriggersRequiredFilter.class.getSimpleName() + hashCode();
39  
40      private IBootstrapService bootstrapService;
41  
42      private IParameterService parameterService;
43  
44      private String tablePrefix;
45  
46      public boolean filterDelete(IDataLoaderContext context, String[] keyValues) {
47          recordSyncNeeded(context);
48          return true;
49      }
50  
51      public boolean filterInsert(IDataLoaderContext context, String[] columnValues) {
52          recordSyncNeeded(context);
53          return true;
54      }
55  
56      public boolean filterUpdate(IDataLoaderContext context, String[] columnValues, String[] keyValues) {
57          recordSyncNeeded(context);
58          return true;
59      }
60  
61      private void recordSyncNeeded(IDataLoaderContext context) {
62          if (isSyncTriggersNeeded(context)) {
63              context.getContextCache().put(CTX_KEY_RESYNC_NEEDED, true);
64          }
65      }
66  
67      private boolean isSyncTriggersNeeded(IDataLoaderContext context) {
68          return matchesTable(context, TableConstants.SYM_TRIGGER);
69      }
70  
71      private boolean matchesTable(IDataLoaderContext context, String tableSuffix) {
72          return context.getTableName().equalsIgnoreCase(TableConstants.getTableName(tablePrefix, tableSuffix));
73      }
74  
75      public boolean isAutoRegister() {
76          return true;
77      }
78  
79      public void batchComplete(IDataLoader loader, IncomingBatchHistory history) {
80          if (loader.getContext().getContextCache().get(CTX_KEY_RESYNC_NEEDED) != null
81                  && parameterService.is(ParameterConstants.AUTO_SYNC_CONFIGURATION)) {
82              logger.info("About to syncTriggers because new configuration came through the dataloader.");
83              bootstrapService.syncTriggers();
84          }
85      }
86  
87      public void setBootstrapService(IBootstrapService bootstrapService) {
88          this.bootstrapService = bootstrapService;
89      }
90  
91      public void setParameterService(IParameterService parameterService) {
92          this.parameterService = parameterService;
93      }
94  
95      public void setTablePrefix(String tablePrefix) {
96          this.tablePrefix = tablePrefix;
97      }
98  
99  }