View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  		 * AudioSystem.getAudioInputStream requires jdk 1.3,
61  		 * so we use applet.newaudioclip instead
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 		//nothing to do
103 	}
104 
105     /***
106      * Gets whether appender requires a layout.
107      * @return false
108      */
109   public boolean requiresLayout() {
110       return false;
111   }
112 
113 }