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