1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net.server;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URI;
26 import java.net.URL;
27 import java.util.Objects;
28
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import org.apache.logging.log4j.core.LogEventListener;
32 import org.apache.logging.log4j.core.config.Configuration;
33 import org.apache.logging.log4j.core.config.ConfigurationSource;
34 import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
35 import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
36 import org.apache.logging.log4j.util.Strings;
37
38
39
40
41
42
43
44
45 public abstract class AbstractSocketServer<T extends InputStream> extends LogEventListener implements Runnable {
46
47
48
49
50 protected static class ServerConfigurationFactory extends XmlConfigurationFactory {
51
52 private final String path;
53
54 public ServerConfigurationFactory(final String path) {
55 this.path = path;
56 }
57
58 @Override
59 public Configuration getConfiguration(final String name, final URI configLocation) {
60 if (Strings.isNotEmpty(path)) {
61 File file = null;
62 ConfigurationSource source = null;
63 try {
64 file = new File(path);
65 final FileInputStream is = new FileInputStream(file);
66 source = new ConfigurationSource(is, file);
67 } catch (final FileNotFoundException ex) {
68
69 }
70 if (source == null) {
71 try {
72 final URL url = new URL(path);
73 source = new ConfigurationSource(url.openStream(), url);
74 } catch (final MalformedURLException mue) {
75
76 } catch (final IOException ioe) {
77
78 }
79 }
80
81 try {
82 if (source != null) {
83 return new XmlConfiguration(source);
84 }
85 } catch (final Exception ex) {
86
87 }
88 System.err.println("Unable to process configuration at " + path + ", using default.");
89 }
90 return super.getConfiguration(name, configLocation);
91 }
92 }
93
94 protected static final int MAX_PORT = 65534;
95
96 private volatile boolean active = true;
97
98 protected final LogEventBridge<T> logEventInput;
99
100 protected final Logger logger;
101
102
103
104
105
106
107
108 public AbstractSocketServer(final int port, final LogEventBridge<T> logEventInput) {
109 this.logger = LogManager.getLogger(this.getClass().getName() + '.' + port);
110 this.logEventInput = Objects.requireNonNull(logEventInput, "LogEventInput");
111 }
112
113 protected boolean isActive() {
114 return this.active;
115 }
116
117 protected void setActive(final boolean isActive) {
118 this.active = isActive;
119 }
120
121
122
123
124
125
126 public Thread startNewThread() {
127 final Thread thread = new Thread(this);
128 thread.start();
129 return thread;
130 }
131
132 }