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.BufferedReader;
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.PrintWriter;
24  import java.net.ServerSocket;
25  import java.net.Socket;
26  
27  import lotus.domino.NotesThread;
28  
29  import org.apache.log4j.LogManager;
30  import org.apache.log4j.PropertyConfigurator;
31  import org.apache.log4j.helpers.LogLog;
32  
33  // http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
34  
35  /***
36   * Class to reload log4j configurations based on calls to the JVM on a certain 
37   * port.
38   * 
39   * @author Bernd G?tz
40   * @deprecated in favour of file change notification. 
41   */
42  public class ServerThread extends NotesThread {
43  	
44  	private int port;
45  	private String configDir;
46  	boolean done = false;
47  	
48  	private ServerSocket serverSocket;
49  	
50  	public ServerThread(String arg0, int port, String configDir) {
51  		super(arg0);
52  		this.port = port;
53  		this.configDir = configDir;
54  	}
55  
56      public void done() {
57      	LogLog.debug("Signalling thread done");
58      	done = true;
59      	try {
60      		LogLog.debug("Now closing socket");
61          	serverSocket.close();
62      		LogLog.debug("Socket closed");
63      	}
64      	catch (IOException e) {
65      		// ok.
66      	}
67      }
68  
69  	public void runNotes() {
70  		
71  		// it must be started as a daemon thread:
72          serverSocket = null;
73          try {
74          	LogLog.debug("creating the server socket on port " + port + "...");
75          	serverSocket = new ServerSocket(port);
76          	LogLog.debug("Server socket created");
77          } catch (IOException e) {
78          	LogLog.error("Could not listen on port " + port);
79          	return;
80          }
81  		
82  		while (!done) {
83  	        Socket clientSocket = null;
84  	        try {
85  	        	LogLog.debug("Accept connections...");
86  	            clientSocket = serverSocket.accept();
87  	        	LogLog.debug("Connection accepted...");
88  
89  	            PrintWriter out = new PrintWriter(
90  	            		clientSocket.getOutputStream(), true);
91  	            BufferedReader in = new BufferedReader(
92  	    				new InputStreamReader(
93  	    				clientSocket.getInputStream()));
94  	            
95  	            out.println("hello");
96  
97  	            String inputLine = in.readLine();
98  	            
99  	            LogLog.debug("Incoming command: " + inputLine);
100 	            
101             	if (inputLine.equals("reload")) {
102             		// do it:
103      	            LogLog.debug("Resetting log4j configuration now");
104                     LogManager.resetConfiguration();
105                     String name = configDir + File.separator + 
106                     	"log4j.properties";
107                     PropertyConfigurator.configure(name);
108                     out.write("ok");
109             	} else {
110             		out.write("notok");
111             	}
112 	            out.close();
113 	            in.close();
114 	            clientSocket.close();
115 	            
116 	        } catch (IOException e) {
117 	            LogLog.debug("Accept has been interrupted");
118 	            break;
119 	        }
120 		}
121 		try {
122 	        serverSocket.close();
123 		}
124 		catch (IOException e) {
125 			LogLog.debug("Got exception: " + e.getMessage());
126 		}
127 		LogLog.debug("Resetting configuration now...");
128         LogManager.resetConfiguration();
129 		LogLog.debug("Thread now exits run method");
130 
131 	}
132 
133 }