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