package org.prevayler.socketserver.server;

/*
 * prevayler.socketServer, a socket-based server (and client library)
 * to help create client-server Prevayler applications
 * 
 * Copyright (C) 2003 Advanced Systems Concepts, Inc.
 * 
 * Written by David Orme <daveo@swtworkbench.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.IOException;
import java.net.ServerSocket;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.socketserver.util.Log;

/**
 * This is where the server starts everything
 * @author DaveO
 */
public class Main {

    private static SnapshotThread snapshotThread;
    private static Prevayler prevayler;

    private static int port;

    // Init the Prevayler persistence engine and fork snapshot thread
    private static void initPrevayler() throws Exception {
        // Set up the repository location
        //String prevalenceBase = System.getProperty("user.dir") + "/prevalenceBase";
        String prevalenceBase = (String) ServerConfig.properties.get("Repository");
        Log.message("Snapshot/log file dir: " + prevalenceBase);

        // Set up the default port
        port = Integer.parseInt((String) ServerConfig.properties.get("BasePort"));
        
        // Set up the root object class
        String rootObjectClassName = (String) ServerConfig.properties.get("RootObjectClass");
        Class rootObjectClass = Class.forName(rootObjectClassName);

        // Create an instance of the root object class and start the server
        prevayler = PrevaylerFactory.createPrevayler(rootObjectClass.newInstance(), prevalenceBase);
        snapshotThread = new SnapshotThread(prevayler);
        snapshotThread.start();
    }

    private static void runNotificationServer() {
        new Notification(port+1).start();
    }

    // Command-processing socket server is here    
    private static void runCommandServer() throws Exception {
        ServerSocket ss = null;
        boolean listening = true;

        // Listen dynamically
        try {
            ss = new ServerSocket(port);
        } catch (IOException e) {
        	Log.error(e, "Couldn't open command server port: " + port);
            System.exit(-1);
        }

        while (listening)
            new CommandThread(prevayler, ss.accept()).start();

        ss.close();
    }

    // Everything starts here
	public static void main(String[] args) {
        try {
            new ServerConfig();
            initPrevayler();
            runNotificationServer();
            runCommandServer();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
    
}


@@ -2230,16 +2230,18 @@
         
+//
 prevayle
@@ -2325,16 +2325,243 @@
 eBase);%0A
+        PrevaylerFactory factory = new PrevaylerFactory();%0A        factory.configurePrevalentSystem(rootObjectClass.newInstance());%0A        factory.configurePrevalenceBase(prevalenceBase);%0A        prevayler = factory.create();%0A
         

fix compile issue with latest PrevaylerFactory.