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 *
45 * @return socket factory
46 */
47 @Override
48 protected SocketFactory getSocketFactory() {
49 return socketFactory;
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public void start() {
57 try {
58 SSLContext sslContext = getSsl().createContext(this);
59 SSLParametersConfiguration parameters = getSsl().getParameters();
60 parameters.setContext(getContext());
61 socketFactory = new ConfigurableSSLSocketFactory(parameters, sslContext.getSocketFactory());
62 super.start();
63 } catch (Exception ex) {
64 addError(ex.getMessage(), ex);
65 }
66 }
67
68 /**
69 * Gets the SSL configuration.
70 *
71 * @return SSL configuration; if no configuration has been set, a default
72 * configuration is returned
73 */
74 public SSLConfiguration getSsl() {
75 if (ssl == null) {
76 ssl = new SSLConfiguration();
77 }
78 return ssl;
79 }
80
81 /**
82 * Sets the SSL configuration.
83 *
84 * @param ssl the SSL configuration to set
85 */
86 public void setSsl(SSLConfiguration ssl) {
87 this.ssl = ssl;
88 }
89
90 }