📄 server.java
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Core License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: Server.java,v 1.6 2002/09/21 16:06:35 per_nyfelt Exp $
package org.ozoneDB.core;
import java.lang.reflect.*;
import java.io.*;
import org.ozoneDB.DxLib.*;
import org.ozoneDB.*;
import org.ozoneDB.util.*;
import org.ozoneDB.tools.*;
/**
* Main class to start the stand-alone server.
*
* @author <a href="http://www.softwarebuero.de/">SMB</a>
* @version $Revision: 1.6 $Date: 2002/09/21 16:06:35 $
*/
public class Server {
public static Env env;
public static boolean stop;
public static boolean stopped;
public static void main( String[] args ) throws Exception {
String dir = File.separator + "tmp" + File.separator + "db";
String debugLevel = null;
boolean help = false;
boolean verbose = false;
boolean createDB = false;
DxArrayBag users = new DxArrayBag();
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith( "-debug" )) {
debugLevel = args[i].substring("-debug".length());
} else if (args[i].startsWith( "-d" )) {
dir = args[i].substring( 2 );
} else if (args[i].startsWith( "-c" )) {
createDB = true;
} else if (args[i].startsWith( "-u" )) {
String name = args[i].substring( 2 );
users.add( name );
} else if (args[i].startsWith( "-h" )) {
help = true;
} else {
System.out.println( "illegal option: " + args[i] );
help = true;
}
}
if (args.length == 0 || help) {
System.out.println( "usage: ozone -d<dir> [-h] [-debug] [-v]" );
System.out.println( " -d<directory> database directory" );
System.out.println( " -debug<level> debug level, levels are one of the string constants in the OzoneDebugLevel class" );
System.out.println( " -v starts the ozonometer (broken)" );
System.out.println( " -c create a database if neccessary" );
System.out.println( " -u<name> add user <name>" );
System.out.println( " -h shows this help" );
System.exit( 0 );
}
try {
if (createDB) {
// Check if a DB exists and make a new database if not.
if (!Install.dbExists(dir)) {
System.out.println( "Installing new database in " + dir + "..." );
Setup defaults = new Setup( null );
defaults.addProperties( System.getProperties(), "ozoneDB." );
Install.createDB( dir, defaults, new PrintWriter( System.out, true ) );
System.out.println( "Edit " + dir + Env.CONFIG_FILE + " to change settings." );
System.out.println( "install complete\n" );
}
}
System.out.println( "initializing environment..." );
env = new Env( dir, debugLevel);
// This is a good place to set up our shutdown hook. Doing it
// earlier would case a NullPointerException in race conditions.
// Doing it now is fine in terms of preventing database corruption
// because request processing has no yet begun.
setupShutdownHook();
int freeSlot = 101;
for (int i = 0; i < users.count(); i++) {
String name = (String)users.elementAtIndex( i );
// don't insert a name twice
if (env.userManager.userForName( name ) != null) {
env.logWriter.newEntry( env, "User " + name + " already exists.", LogWriter.INFO );
continue;
}
// find a free id
while (env.userManager.userForID( freeSlot ) != null) {
freeSlot++;
}
// add the user
env.userManager.newUser( name, freeSlot );
env.logWriter.newEntry( env, "user added: " + name + " ID: " + freeSlot, LogWriter.INFO );
}
env.storeSetup();
env.startExternalEventProcessing();
env.startDeadlockRecognition();
System.out.println( "(Ctrl-C or 'q' to shutdown without admin tool)" );
InputStreamReader is = new InputStreamReader( System.in );
Thread.currentThread().setPriority( Env.SERVER_THREAD_PRIORITY );
stop = false;
stopped = false;
// the main loop
while (!stop) {
int gcCount = 0;
int stateCount = 0;
while (!is.ready() && !env.shuttingdown && !stop) {
final int sleepMillis = 250;
// this forces also task switches
Thread.currentThread().sleep( sleepMillis );
// check component states every 5sec; this is not really a
// good solution but it works for now
// if (stateCount++ >= (5000/sleepMillis)) {
// stateCount = 0;
// if (env.isComponentStateChanged()) {
// env.logWriter.newEntry( env, "storing server state...", LogWriter.DEBUG );
// env.storeSetup();
// }
// }
// force GC
if (env.transactionManager.taTableCount() == 0) {
gcCount++;
}
else {
gcCount = 0;
}
if (gcCount >= (10000/sleepMillis)) {
// env.logWriter.newEntry( env, "forcing GC...", LogWriter.DEBUG3 );
System.gc();
gcCount = 0;
}
}
if (is.ready()) {
int c = is.read();
if (c == 'q') {
stop = true;
}
}
}
// wait for the admin thread to deconnect
Thread.sleep( 1000 );
// shutdown the system
env.shutdown();
stopped = true;
}
catch (Exception e) {
if (env != null) {
env.shutdown();
}
// env.logWriter.newEntry (null, "", e, LogWriter.ERROR);
System.out.println( "Unable to initialize server." );
e.printStackTrace();
}
System.exit( 1 );
}
/**
* Attempt to add the shutdown hook using reflection.
*/
private static void setupShutdownHook() {
try {
// We will now attempt to add the shutdown hook
// using reflection. If we're running in a VM that does
// not support shutdown hooks (i.e., pre-1.3), it will yield
// a NoSuchMethodException, which we quietly ignore.
Class runtimeClass = Runtime.class;
Method hookMethod = runtimeClass.getMethod("addShutdownHook", new Class[]{Thread.class});
Runtime rt = Runtime.getRuntime();
hookMethod.invoke(rt, new Object[]{createShutdownHook()});
env.logWriter.newEntry(Server.class, "Shutdown hook successfully added.",
LogWriter.INFO);
}
catch (NoSuchMethodException nsme) {
/* Pre-1.3 VM. */
env.logWriter.newEntry(Server.class, "Running on pre-1.3 VM; no shutdown hook added.", LogWriter.INFO);
}
catch (SecurityException se) {
/* Either a screwy setup, or permission deliberately denied. */
env.logWriter.newEntry(Server.class, "Shutdown hook not added; permission denied.", LogWriter.WARN);
}
catch (InvocationTargetException ite) {
/* Runtime.addShutdownHook() failed */
Throwable te = ite.getTargetException();
env.logWriter.newEntry(Server.class, "WARNING: Shutdown hook addition failed: "
+ te.getClass().getName() + ": " + te.getMessage(),
LogWriter.ERROR);
}
catch (IllegalAccessException iae) {
/* The VM is FUBAR. */
env.logWriter.newEntry(Server.class,"Runtime.addShutdownHook() not public? VM bug?", LogWriter.ERROR);
}
}
/**
* Constructs and returns a Thread to be used as a shutdown hook.
*/
private static Runnable createShutdownHook() {
return new Thread() {
public void run() {
if (env.shuttingdown == false) {
env.logWriter.newEntry(Server.class, "Shutdown hook...", LogWriter.INFO);
try {
// signal the main loop to stop
stop = true;
// wait for the mail thread to finish shutdown
while (!stopped) {
sleep(500);
}
}
catch (Exception e) {
}
env.logWriter.newEntry(Server.class, "Shutdown hook... shutdown finished.", LogWriter.INFO);
}
}
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -