View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net;
15  
16  import javax.net.SocketFactory;
17  import javax.net.ssl.SSLContext;
18  
19  import ch.qos.logback.core.net.ssl.ConfigurableSSLSocketFactory;
20  import ch.qos.logback.core.net.ssl.SSLComponent;
21  import ch.qos.logback.core.net.ssl.SSLConfiguration;
22  import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;
23  
24  /**
25   * An abstract base for module specific {@code SSLSocketAppender}
26   * implementations located in other logback modules.
27   *
28   * @author Carl Harris
29   */
30  public abstract class AbstractSSLSocketAppender<E> extends AbstractSocketAppender<E> implements SSLComponent {
31  
32      private SSLConfiguration ssl;
33      private SocketFactory socketFactory;
34  
35      /**
36       * Constructs a new appender.
37       */
38      protected AbstractSSLSocketAppender() {
39      }
40  
41      /**
42       * Gets an {@link SocketFactory} that produces SSL sockets using an
43       * {@link SSLContext} that is derived from the appender's configuration.
44       * @return socket factory
45       */
46      @Override
47      protected SocketFactory getSocketFactory() {
48          return socketFactory;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public void start() {
56          try {
57              SSLContext sslContext = getSsl().createContext(this);
58              SSLParametersConfiguration parameters = getSsl().getParameters();
59              parameters.setContext(getContext());
60              socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory());
61              super.start();
62          } catch (Exception ex) {
63              addError(ex.getMessage(), ex);
64          }
65      }
66  
67      /**
68       * Gets the SSL configuration.
69       * @return SSL configuration; if no configuration has been set, a
70       *    default configuration is returned
71       */
72      public SSLConfiguration getSsl() {
73          if (ssl == null) {
74              ssl = new SSLConfiguration();
75          }
76          return ssl;
77      }
78  
79      /**
80       * Sets the SSL configuration.
81       * @param ssl the SSL configuration to set
82       */
83      public void setSsl(SSLConfiguration ssl) {
84          this.ssl = ssl;
85      }
86  
87  }