📄 server.java
字号:
/* * Copyright (c) 2000, Niklas Mehner * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.j3de;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.RemoteServer;
import java.rmi.registry.LocateRegistry;
import java.lang.reflect.InvocationTargetException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.j3de.exception.LogExceptionHandler;
import org.j3de.exception.ExceptionHandler;
import org.j3de.interfaces.ApplicationServer;
import org.j3de.interfaces.EnvironmentServer;
import org.j3de.server.ClassPathManager;
import org.j3de.server.EnvironmentList;
import org.j3de.server.ApplicationList;
import org.j3de.server.DefaultApplicationServer;
import org.j3de.server.DefaultEnvironmentServer;
import org.j3de.util.Configuration;
import org.j3de.util.ConfigurationException;
/** * Server is the main class of the j3de server. It is responsible for parsing the configuration file * and creating all necessairy components, as well as setting up exception-handling and logging. * * The server consists basically of a list of environments and a list of applications, that are provided * to the clients. Additional a webserver to server the needed (jar-)files can be started (not yet implemented). * * @author Niklas Mehner * @version $Revision: 1.7 $, $Date: 2000/09/08 20:50:56 $ * @since j3de 1.0 */public class Server {
private static final String CONFIG_FILE = "config-server.xml";
private static final String LOG_FILE = "log" + File.separator + "server.log";
private static final String TRUE = "true";
private static final String APPLICATIONS = "applications";
private static final String ENVIRONMENTS = "environments";
private static final String CREATE_REGISTRY = "createRegistry";
private static final String CREATE_REGISTRY_DEFAULT = "false";
private static final String REGISTRY_PORT = "registryPort";
private static final String REGISTRY_PORT_DEFAULT = "1099";
private static final String APP_BINDING_NAME = "appBindingName";
private static final String APP_BINDING_NAME_DEFAULT = "j3de-appserver";
private static final String ENV_BINDING_NAME = "envBindingName";
private static final String ENV_BINDING_NAME_DEFAULT = "j3de-envserver";
private static final String RMI_CLASS_PATH = "rmi_classpath";
private static final String RMI_CLASS_PATH_DEFAULT = "";
private Configuration configuration;
private String configFileName;
private ApplicationList applicationList;
private EnvironmentList environmentList;
private ApplicationServer applicationServer;
private EnvironmentServer environmentServer;
private ClassPathManager classPathManager;
private boolean createRegistry;
private int registryPort;
private String appBindingName;
private String envBindingName;
private String rmiClassPath;
/** Creates a new Server. All informations are read from the configuration file. * @param configFileName path to the configuration file. * @throws IOException if the configuration file cannot be opened. * @throws ParserConfigurationException if the xml parser is not configured correctly. * @throws SAXException if the configuration file cannot be parsed. * @throws ConfigurationException if there are semantic errors in the configuration file. */ public Server(String configFileName) throws IOException,
ParserConfigurationException,
SAXException,
ConfigurationException {
System.setProperty("java.rmi.server.useCodebaseOnly", "true");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
this.configFileName = configFileName;
configuration = new Configuration(configFileName);
createRegistry = configuration.getProperty(CREATE_REGISTRY,
CREATE_REGISTRY_DEFAULT,
ExceptionHandler.SHOW_WARNING)
.toLowerCase().equals(TRUE);
appBindingName = configuration.getProperty(APP_BINDING_NAME,
APP_BINDING_NAME_DEFAULT,
ExceptionHandler.SHOW_WARNING);
envBindingName = configuration.getProperty(ENV_BINDING_NAME,
ENV_BINDING_NAME_DEFAULT,
ExceptionHandler.SHOW_WARNING);
registryPort = new Integer(configuration.getProperty(REGISTRY_PORT,
REGISTRY_PORT_DEFAULT,
ExceptionHandler.SHOW_WARNING)).intValue();
rmiClassPath = configuration.getProperty(RMI_CLASS_PATH,
RMI_CLASS_PATH_DEFAULT,
ExceptionHandler.SHOW_WARNING);
applicationList = (ApplicationList)configuration.getComponent(APPLICATIONS,
org.j3de.server.DefaultApplicationList.class);
environmentList = (EnvironmentList)configuration.getComponent(ENVIRONMENTS,
org.j3de.server.DefaultEnvironmentList.class);
classPathManager = new ClassPathManager(environmentList, applicationList, rmiClassPath);
classPathManager.setServerClassPath();
applicationServer = new DefaultApplicationServer(applicationList);
environmentServer = new DefaultEnvironmentServer(environmentList);
if (createRegistry)
LocateRegistry.createRegistry(registryPort);
RemoteServer.setLog(new FileOutputStream("log" + File.separator + "rmi.log"));
UnicastRemoteObject.exportObject(applicationServer);
UnicastRemoteObject.exportObject(environmentServer);
Naming.rebind(appBindingName, applicationServer);
Naming.rebind(envBindingName, environmentServer);
System.out.println("Server ready"); }
/** Saves the configuration of the given server. * @param server the server the configuration should be saved for. * @param fileName the fileName the configuration should be saved to. */
public static void saveConfiguration(Server server, String fileName) {
try {
if ((server != null) && server.configuration.isConfigurationChanged())
server.configuration.saveConfiguration(fileName);
} catch (IOException e) {
ExceptionHandler.handleFatalException(e);
}
}
/** * Starts the server. * @param args Contains either a configuration file or is empty. In that case the server * tries to read the default configuration file <code>config-server.xml</code> */ public static void main(String[] args) {
try {
Server server = null;
String configFileName = CONFIG_FILE;
try {
if (args.length == 1)
configFileName = args[0];
new LogExceptionHandler(LOG_FILE);
server = new Server(configFileName);
} catch (Exception e) {
e.printStackTrace();
ExceptionHandler.handleFatalException(e);
} finally {
saveConfiguration(server, configFileName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -