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.transport;
22  
23  import javax.net.ssl.HostnameVerifier;
24  import javax.net.ssl.HttpsURLConnection;
25  import javax.net.ssl.SSLSession;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.jumpmind.symmetric.common.Constants;
29  import org.jumpmind.symmetric.common.ParameterConstants;
30  import org.jumpmind.symmetric.service.INodeService;
31  import org.jumpmind.symmetric.service.IParameterService;
32  import org.jumpmind.symmetric.transport.http.HttpTransportManager;
33  import org.jumpmind.symmetric.transport.internal.InternalTransportManager;
34  import org.springframework.beans.factory.FactoryBean;
35  
36  public class TransportManagerFactoryBean implements FactoryBean {
37  
38      private INodeService nodeService;
39  
40      private IParameterService parameterService;
41  
42      public Object getObject() throws Exception {
43          String transport = parameterService.getString(ParameterConstants.TRANSPORT_TYPE);
44          if (Constants.PROTOCOL_HTTP.equalsIgnoreCase(transport)) {
45              final String httpSslVerifiedServerNames = parameterService
46                      .getString(ParameterConstants.TRANSPORT_HTTPS_VERIFIED_SERVERS);
47              HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
48                  public boolean verify(String s, SSLSession sslsession) {
49                      if (!StringUtils.isBlank(httpSslVerifiedServerNames)) {
50                          String[] names = httpSslVerifiedServerNames.split(",");
51                          for (String string : names) {
52                              if (s != null && s.equals(string.trim())) {
53                                  return true;
54                              }
55                          }
56                      }
57                      return false;
58                  }
59              });
60              return new HttpTransportManager(nodeService, parameterService);
61          } else if (Constants.PROTOCOL_INTERNAL.equalsIgnoreCase(transport)) {
62              return new InternalTransportManager(nodeService, parameterService);
63          } else {
64              throw new IllegalStateException("An invalid transport type of " + transport + " was specified.");
65          }
66      }
67  
68      public Class<ITransportManager> getObjectType() {
69          return ITransportManager.class;
70      }
71  
72      public boolean isSingleton() {
73          return true;
74      }
75  
76      public void setNodeService(INodeService nodeService) {
77          this.nodeService = nodeService;
78      }
79  
80      public void setParameterService(IParameterService parameterService) {
81          this.parameterService = parameterService;
82      }
83  
84  }