Hi!

log4j is great, but I think that
PropertyConfigurator.configureAndWatch(String configFilename) is not good
enough for
dynamic modification, as you have to wait until the delay so that your
modification are taken into account.

So I imagine a solution: to put a *very* little FTP server in the
application on a particular port (example:2100).
When you put a file on this server, the file is not saved on disk, but sent
to PropertyConfigurator, so that modifications of the configu are taken into
account immediatly. (You can log with any username and password)

(Some text editor have embedded FTP client, so it can be very easy to change
the log4j configuration with them)

I have coded this little FTP server, so if you want to use it, all you have
to do is:

                PropertyConfigurator.configure("myconf.properties"); // As usually
                new FTPServer(2100, "myconf.properties"); // Launch the server on the 
2100

Note that when you put some files on the FTP server, data are *not* saved to
disk. It's only sent to log4j PropertyConfigurator. (You can also get the
last config from the FTP server).

Please free to improve it, or to integrate it in next version of log4j!

Arnaud Roques

FTPServer.java file:
------------------------

import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.log4j.*;

public class FTPServer extends Thread
{
        ServerSocket server;
        ArrayList data;

        FTPServer(int port, String fileName) throws Exception
        {
                data = new ArrayList();
                File f = new File(fileName);
                BufferedReader br = new BufferedReader(new FileReader(f));
                String s;
                while ((s=br.readLine())!=null)
                        data.add(s);
                server = new ServerSocket(port);
                System.err.println("FTP Server on " +
InetAddress.getLocalHost().getHostAddress()+" "+port);
                start();
        }

        public void run()
        {
                try
                {
                        while (true)
                        {
                                Socket client = server.accept(); // when the signal 
arrives, the server
build a new socket in order to communicate witht the client
                                Thread session = new FTPSession(client, data);
                                session.start();
                        }
                }
                catch (IOException e)
                {
                        e.printStackTrace();
                }
        }
}

class FTPSession extends Thread
{
    private PrintWriter out;
    private BufferedReader in;
    private String dataHost;
    private int dataPort = -1;
        private ArrayList data;

    public FTPSession(Socket s, ArrayList data)
    {
                this.data = data;
        try
        {
            in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
            out = new PrintWriter(s.getOutputStream(), true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    int reply(int code, String text)
        {
        out.println(code + " " + text);
        return code;
        }

    //
    // The run method...
    //
    public void run()
    {
                reply(220, "log4j FTP server");
                try
                {
                        while (true)
                        {
                                String response = in.readLine().toUpperCase();
                                if (response.startsWith("USER"))
                                {
                                        reply(331, "Password required");
                                }
                                else if (response.startsWith("PASS"))
                                {
                                        reply(230, "Logged in.");
                                }
                                else if (response.startsWith("SYST"))
                                {
                                        reply(215, "javaFTP");
                                }
                                else if (response.startsWith("TYPE"))
                                {
                                        reply(200, "Type OK");
                                }
                                else if (response.startsWith("PORT"))
                                {
                                        String portStr = 
response.substring(response.indexOf(' ')+1);
                                        StringTokenizer st = new 
StringTokenizer(portStr, ",");
                                        String h1 = st.nextToken();
                                        String h2 = st.nextToken();
                                        String h3 = st.nextToken();
                                        String h4 = st.nextToken();
                                        int p1 = Integer.parseInt(st.nextToken());
                                        int p2 = Integer.parseInt(st.nextToken());

                                        String dataHost = h1 + "." + h2 + "." + h3 + 
"." + h4;
                                        int dataPort = (p1 << 8) | p2;
                                        setDataPort(dataHost, dataPort);

                                        reply(200, "Port OK");
                                }
                                else if (response.startsWith("MODE"))
                                {
                                        reply(200, "Mode OK");
                                }
                                else if (response.startsWith("STRU"))
                                {
                                        reply(200, "Stru OK");
                                }
                                else if (response.startsWith("PWD"))
                                {
                                        reply(257, "/");
                                }
                                else if (response.startsWith("CWD"))
                                {
                                        reply(250, "CWD command successful.");
                                }
                                else if (response.startsWith("QUIT"))
                                {
                                        reply(221, "Goodbye");
                                        return;
                                }
                                else if (response.startsWith("RETR"))
                                {
                                        Socket dataSocket = new Socket(dataHost, 
dataPort);
                                        PrintWriter writer = new 
PrintWriter(dataSocket.getOutputStream());
                                        reply(150, "Opening connection");
                                        for (int i=0; i<data.size(); i++)
                                        {
                                                writer.print((String)data.get(i));
                                                writer.print("\r\n");
                                        }
                                        writer.flush();
                                        writer.close();
                                        reply(226, "Transfer complete.");
                                }
                                else if (response.startsWith("STOR"))
                                {
                                        Socket dataSocket = new Socket(dataHost, 
dataPort);
                                        BufferedReader br = new BufferedReader(
                                                new 
InputStreamReader(dataSocket.getInputStream()));
                                        reply(150, "Opening connection");
                                        String s;
                                        data.clear();
                                        StringBuffer sb = new StringBuffer();
                                        while ((s=br.readLine())!=null)
                                        {
                                                data.add(s);
                                                sb.append(s);
                                                sb.append('\n');
                                        }
                                        br.close();
                                        reply(226, "Transfer complete.");

                                        Properties prop = new Properties();
                                        prop.load(new 
ByteArrayInputStream(sb.toString().getBytes()));
                                        // Send data to log4J
                                        PropertyConfigurator.configure(prop);

                                }
                                else if (response.startsWith("LIST"))
                                {
                                        Socket dataSocket = new Socket(dataHost, 
dataPort);
                                        PrintWriter writer = new 
PrintWriter(dataSocket.getOutputStream());
                                        reply(150, "Opening connection");
                                        writer.print("total 0\r\n");
                                        writer.print("LOG4J.properties\r\n");
                                        writer.flush();
                                        writer.close();
                                        reply(226, "Transfer complete.");
                                }
                                else
                                {
                                        reply(500, "Not supported");
                                }
                        }
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }

    }

    public void setDataPort(String host, int port)
    {
        dataHost = host;
        dataPort = port;
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to