1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.varia;
19
20 import java.applet.Applet;
21 import java.applet.AudioClip;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import org.apache.log4j.AppenderSkeleton;
26 import org.apache.log4j.spi.LoggingEvent;
27 import org.apache.log4j.helpers.LogLog;
28
29 /***
30 * Plays a sound clip created using Applet.newAudioClip when an event is received.
31 *
32 * If the audio format is not supported, a message stating the SoundAppender could
33 * not be initialized is logged.
34 *
35 * Use a filter in combination with this appender to control when the appender is
36 * triggered.
37 *
38 * For example, in the appender definition, include a LevelMatchFilter configured
39 * to accept WARN or greater, followed by a DenyAllFilter.
40 *
41 * @author Scott Deboy
42 *
43 */
44 public final class SoundAppender extends AppenderSkeleton {
45
46 private AudioClip clip;
47 private String audioURL;
48
49 public SoundAppender() {
50 }
51
52 /***
53 * Attempt to initialize the appender by creating a reference to an AudioClip.
54 *
55 * Will log a message if format is not supported, file not found, etc.
56 *
57 */
58 public void activateOptions() {
59
60
61
62
63
64 try {
65 clip = Applet.newAudioClip(new URL(audioURL));
66 } catch (MalformedURLException mue) {
67 LogLog.error("unable to initialize SoundAppender", mue);}
68 if (clip == null) {
69 LogLog.error("Unable to initialize SoundAppender");
70 }
71 }
72
73 /***
74 * Accessor
75 *
76 * @return audio file
77 */
78 public String getAudioURL() {
79 return audioURL;
80 }
81
82 /***
83 * Mutator - common format for a file-based url:
84 * file:///c:/path/someaudioclip.wav
85 *
86 * @param audioURL
87 */
88 public void setAudioURL(String audioURL) {
89 this.audioURL = audioURL;
90 }
91
92 /***
93 * Play the sound if an event is being processed
94 */
95 protected void append(LoggingEvent event) {
96 if (clip != null) {
97 clip.play();
98 }
99 }
100
101 public void close() {
102
103 }
104
105 /***
106 * Gets whether appender requires a layout.
107 * @return false
108 */
109 public boolean requiresLayout() {
110 return false;
111 }
112
113 }