Package org.prevayler.socketserver.server

Source Code of org.prevayler.socketserver.server.Main

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 org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;
import org.prevayler.socketserver.util.Log;

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

/**
* 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);
    PrevaylerFactory factory = new PrevaylerFactory();
    factory.configurePrevalentSystem(rootObjectClass.newInstance());
    factory.configurePrevalenceDirectory(prevalenceBase);
    prevayler = factory.create();
    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();
    }
  }

}
TOP

Related Classes of org.prevayler.socketserver.server.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.