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.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  		 * AudioSystem.getAudioInputStream requires jdk 1.3,
64  		 * so we use applet.newaudioclip instead
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 		//nothing to do
106 	}
107 
108     /***
109      * Gets whether appender requires a layout.
110      * @return false
111      */
112   public boolean requiresLayout() {
113       return false;
114   }
115 
116 }