1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.db;
19
20 import org.apache.log4j.plugins.Pauseable;
21 import org.apache.log4j.plugins.Receiver;
22 import org.apache.log4j.scheduler.Scheduler;
23 import org.apache.log4j.spi.LoggerRepositoryEx;
24 import org.apache.log4j.xml.DOMConfigurator;
25 import org.apache.log4j.xml.UnrecognizedElementHandler;
26 import org.w3c.dom.Element;
27
28 import java.util.Properties;
29
30
31
32
33
34 public class DBReceiver extends Receiver implements Pauseable, UnrecognizedElementHandler {
35
36
37
38
39
40 static int DEFAULT_REFRESH_MILLIS = 1000;
41 ConnectionSource connectionSource;
42 int refreshMillis = DEFAULT_REFRESH_MILLIS;
43 DBReceiverJob receiverJob;
44 boolean paused = false;
45
46 public void activateOptions() {
47
48 if (connectionSource == null) {
49 throw new IllegalStateException(
50 "DBAppender cannot function without a connection source");
51 }
52
53 receiverJob = new DBReceiverJob(this);
54 receiverJob.setLoggerRepository(repository);
55
56 if (this.repository == null) {
57 throw new IllegalStateException(
58 "DBAppender cannot function without a reference to its owning repository");
59 }
60
61 if (repository instanceof LoggerRepositoryEx) {
62 Scheduler scheduler = ((LoggerRepositoryEx) repository).getScheduler();
63
64 scheduler.schedule(
65 receiverJob, System.currentTimeMillis() + 500, refreshMillis);
66 }
67
68 }
69
70 public void setRefreshMillis(int refreshMillis) {
71 this.refreshMillis = refreshMillis;
72 }
73
74 public int getRefreshMillis() {
75 return refreshMillis;
76 }
77
78
79
80
81
82 public ConnectionSource getConnectionSource() {
83 return connectionSource;
84 }
85
86
87
88
89
90 public void setConnectionSource(ConnectionSource connectionSource) {
91 this.connectionSource = connectionSource;
92 }
93
94
95
96
97
98 public void shutdown() {
99 getLogger().info("removing receiverJob from the Scheduler.");
100
101 if (this.repository instanceof LoggerRepositoryEx) {
102 Scheduler scheduler = ((LoggerRepositoryEx) repository).getScheduler();
103 scheduler.delete(receiverJob);
104 }
105 }
106
107
108
109
110
111 public void setPaused(boolean paused) {
112 this.paused = paused;
113 }
114
115
116
117
118 public boolean isPaused() {
119 return paused;
120 }
121
122
123
124
125 public boolean parseUnrecognizedElement(Element element, Properties props) throws Exception {
126 if ("connectionSource".equals(element.getNodeName())) {
127 Object instance =
128 DOMConfigurator.parseElement(element, props, ConnectionSource.class);
129 if (instance instanceof ConnectionSource) {
130 ConnectionSource source = (ConnectionSource) instance;
131 source.activateOptions();
132 setConnectionSource(source);
133 }
134 return true;
135 }
136 return false;
137 }
138
139 }