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  
21  package org.jumpmind.symmetric.service.impl;
22  
23  import java.io.IOException;
24  import java.net.ConnectException;
25  import java.net.SocketException;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.jumpmind.symmetric.common.ErrorConstants;
31  import org.jumpmind.symmetric.model.Node;
32  import org.jumpmind.symmetric.service.IDataLoaderService;
33  import org.jumpmind.symmetric.service.INodeService;
34  import org.jumpmind.symmetric.service.IPullService;
35  import org.jumpmind.symmetric.service.IRegistrationService;
36  import org.jumpmind.symmetric.transport.AuthenticationException;
37  import org.jumpmind.symmetric.transport.ConnectionRejectedException;
38  import org.jumpmind.symmetric.transport.TransportException;
39  
40  public class PullService extends AbstractService implements IPullService {
41  
42      private static final Log logger = LogFactory.getLog(PullService.class);
43  
44      private INodeService nodeService;
45  
46      private IDataLoaderService dataLoaderService;
47  
48      private IRegistrationService registrationService;
49      
50      public void pullData() {
51  
52          // register if we haven't already been registered
53          if (!registrationService.isRegisteredWithServer()) {
54              registrationService.registerWithServer();
55          }
56  
57          List<Node> nodes = nodeService.findNodesToPull();
58          if (nodes != null && nodes.size() > 0) {
59              for (Node node : nodes) {
60                  String nodeName = " for " + node;
61                  try {
62                      logger.info("Pull requested" + nodeName);
63                      if (dataLoaderService.loadData(node, nodeService.findIdentity())) {
64                          logger.info("Pull data received" + nodeName);
65                      } else {
66                          logger.info("Pull no data received" + nodeName);
67                      }
68                  } catch (ConnectException ex) {
69                      logger.warn(ErrorConstants.COULD_NOT_CONNECT_TO_TRANSPORT + " url=" + node.getSyncURL());
70                  } catch (ConnectionRejectedException ex) {
71                      logger.warn(ErrorConstants.TRANSPORT_REJECTED_CONNECTION);
72                  } catch (AuthenticationException ex) {
73                      logger.warn(ErrorConstants.NOT_AUTHENTICATED);
74                  } catch (SocketException ex) {
75                      logger.warn(ex.getMessage());
76                  } catch (TransportException ex) {
77                      logger.warn(ex.getMessage());
78                  } catch (IOException e) {
79                      logger.error(e, e);
80                  }
81              }
82          }
83      }
84  
85      public void setNodeService(INodeService nodeService) {
86          this.nodeService = nodeService;
87      }
88  
89      public void setDataLoaderService(IDataLoaderService dataLoaderService) {
90          this.dataLoaderService = dataLoaderService;
91      }
92  
93      public void setRegistrationService(IRegistrationService registrationService) {
94          this.registrationService = registrationService;
95      }
96  
97  }