View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.goetz.domino.log4j.config;
18  
19  import java.io.File;
20  import java.util.Date;
21  import java.util.Timer;
22  import java.util.TimerTask;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  
28  import org.apache.log4j.helpers.LogLog;
29  
30  /***
31   * Controller for the sample application.
32   *
33   * @author Bernd G?tz
34   * @deprecated in favour of file change notification. 
35   */
36  public class Log4JReloadServlet extends HttpServlet {
37  	
38  	ServerThread t;
39  	int port = 0;
40  	int delay = DEFAULTDELAY;
41  	
42  	static int DEFAULTDELAY = 60000*5; // 5 minutes 
43  
44  	public void init(ServletConfig config) throws ServletException {
45  		// TODO Auto-generated method stub
46  		super.init(config);
47  		
48  		String logj4ConfigDir = config.getInitParameter("configdir");
49  		String portStr = config.getInitParameter("port");
50  		if ((portStr != null) && (portStr.length() > 0)) {
51  			port = new Integer(portStr).intValue();
52  		}
53  		String delayStr = config.getInitParameter("delay");
54  		if ((delayStr != null) && (delayStr.length() > 0)) {
55  			delay = new Integer(delayStr).intValue();
56  		}
57  		
58  		// the only purpose of this servlet is to instantiate 
59  		// a listener on port x
60  		
61  		if (port != 0) {
62  			LogLog.debug("Starting the listener thread...");
63  			t = new ServerThread("Log4jReload", port, logj4ConfigDir);
64  			t.setDaemon(true);
65  			t.start();
66  			LogLog.debug("Listener thread successfully started");
67  		} else {
68  			LogLog.debug("Starting file watcher...");
69  			File f = new File(logj4ConfigDir + 
70  					File.separator + "log4j.properties");
71  			if (!f.exists()) {
72  				System.out.println("Could not initialize file watcher - file: " +
73  						f.getName());
74  			}
75  			TimerTask task = new ConfigFileWatcher(f);
76  			Timer t = new Timer();
77  			t.schedule(task, new Date(), delay);
78  			LogLog.debug("File watcher successfully started");
79  		}
80  		
81  	}
82  
83  	public void destroy() {
84  		LogLog.debug("super.destroy");
85  		super.destroy();
86  		LogLog.debug("Destroy log4j reload servlet");
87  		if (port != 0) {
88  			if (t != null) {
89  				LogLog.debug("Calling done on thread");
90  				t.done();
91  				LogLog.debug("Done called");
92  				t = null;
93  			}
94  		}
95  	}
96  	
97  }