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
35
36 public class DBReceiver extends Receiver implements Pauseable, UnrecognizedElementHandler {
37
38
39
40
41 static int DEFAULT_REFRESH_MILLIS = 1000;
42 ConnectionSource connectionSource;
43 int refreshMillis = DEFAULT_REFRESH_MILLIS;
44 DBReceiverJob receiverJob;
45 boolean paused = false;
46
47 public void activateOptions() {
48
49 if(connectionSource == null) {
50 throw new IllegalStateException(
51 "DBAppender cannot function without a connection source");
52 }
53
54 receiverJob = new DBReceiverJob(this);
55 receiverJob.setLoggerRepository(repository);
56
57 if(this.repository == null) {
58 throw new IllegalStateException(
59 "DBAppender cannot function without a reference to its owning repository");
60 }
61
62 if (repository instanceof LoggerRepositoryEx) {
63 Scheduler scheduler = ((LoggerRepositoryEx) repository).getScheduler();
64
65 scheduler.schedule(
66 receiverJob, System.currentTimeMillis() + 500, refreshMillis);
67 }
68
69 }
70
71 public void setRefreshMillis(int refreshMillis) {
72 this.refreshMillis = refreshMillis;
73 }
74
75 public int getRefreshMillis() {
76 return refreshMillis;
77 }
78
79
80
81
82
83 public ConnectionSource getConnectionSource() {
84 return connectionSource;
85 }
86
87
88
89
90
91 public void setConnectionSource(ConnectionSource connectionSource) {
92 this.connectionSource = connectionSource;
93 }
94
95
96
97
98
99 public void shutdown() {
100 getLogger().info("removing receiverJob from the Scheduler.");
101
102 if(this.repository instanceof LoggerRepositoryEx) {
103 Scheduler scheduler = ((LoggerRepositoryEx) repository).getScheduler();
104 scheduler.delete(receiverJob);
105 }
106 }
107
108
109
110
111
112 public void setPaused(boolean paused) {
113 this.paused = paused;
114 }
115
116
117
118
119 public boolean isPaused() {
120 return paused;
121 }
122
123
124
125
126 public boolean parseUnrecognizedElement(Element element, Properties props) throws Exception {
127 if ("connectionSource".equals(element.getNodeName())) {
128 Object instance =
129 DOMConfigurator.parseElement(element, props, ConnectionSource.class);
130 if (instance instanceof ConnectionSource) {
131 ConnectionSource source = (ConnectionSource) instance;
132 source.activateOptions();
133 setConnectionSource(source);
134 }
135 return true;
136 }
137 return false;
138 }
139
140 }