📄 startarena.java
字号:
/* * Copyright 2004 (C) Applied Software Engineering--TU Muenchen * http://wwwbruegge.in.tum.de * * This file is part of ARENA. * * ARENA is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * ARENA 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ARENA; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.globalse.arena.server;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.net.MalformedURLException;import java.rmi.Naming;import java.rmi.RMISecurityManager;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.server.RMIClassLoader;import java.util.logging.LogManager;import java.util.logging.Logger;import org.globalse.arena.remote.LeagueInfo;import org.globalse.arena.remote.MatchPanelFactory;import org.globalse.arena.remote.RemoteLeague;import org.globalse.arena.remote.RemoteTournament;import org.globalse.arena.remote.TournamentInfo;import org.globalse.arena.remote.exceptions.ArenaException;import org.globalse.arena.remote.exceptions.GameAlreadyExistsException;import org.globalse.arena.remote.exceptions.GameNotFoundException;import org.globalse.arena.user.DefaultAccessPolicy;import org.globalse.arena.user.User;import org.globalse.arena.util.PropertyLoader;/** * This class provides the main method for starting an arena server. It takes an * optional argument on the command line which specified a filename or a URL for * a properties file. The properties understood by StartArena include: * <UL> * <LI><code>ArenaPort</code> (default 1099) The TCP/IP port on which the arena server should accept RMI connections</LI> * <LI><code>CodeBase</code> (no default) A list of space-separated URLs specifying where to load game-specified and tournament style specific classes.</LI> * <LI><code>TournamentStyles</code> (no default) A space-separated list of fully qualified class names of tournament styles to be loaded into this arena.</LI> * <LI><code>Games</code> (no default) A space-separated list of fully qualified class names of games to be loaded into this arena.</LI> * <LI><code>SetupDemo</code> (default false) A flag specifying whether test users, leagues, tournaments, and matches should be created.</LI> * </UL> * <P>In addition, this class will use the properties file to initialize the loggers. See the * documentation on java.util.logging for information about logging properties.</P> * * <P>Once it creates and intializes the arena, this class dynamically loads the specified tournament styles * and games and registers them. If at any point the initialization or loading fails, the main method * exists with status 1.</P> * * @author Allen Dutoit */public class StartArena { private static Logger logger = Logger.getLogger("org.globalse.arena.server"); private static Arena arena = Arena.getInstance(); private static String codeBase = null; // Constants used by the setupDemo method for loading the tic tac toe example private static final String TICTACTOE = "TicTacToe"; private static final String TICTACTOE_CLASSNAME = "org.globalse.arena.ttt.TicTacToe"; private static final String TICTACTOEFACTORY_CLASSNAME = TICTACTOE_CLASSNAME + "MatchPanelFactory"; private static final String KNOCK_OUT = "KnockOutStyle"; private static final String KNOCKOUT_CLASSNAME = "org.globalse.arena.styles.KnockOutStyle"; private static void tellUser(String message) { System.out.println(message); } private static PropertyLoader loadProperties(String fileName) { PropertyLoader propertyLoader = null; try { propertyLoader = new PropertyLoader(fileName); } catch (FileNotFoundException e) { tellUser("Properties file \"" + fileName + "\" not found."); System.exit(1); } catch (IOException e) { tellUser("An exception occured while reading from properties file \"" + fileName + "\": " + e.getMessage()); e.printStackTrace(); System.exit(1); } return propertyLoader; } private static void initLogger(String fileName) { if (fileName != null) { LogManager logManager = LogManager.getLogManager(); try { logManager.readConfiguration(new FileInputStream(new File(fileName))); } catch (Exception e) { tellUser("An exception occured while configuring the logger from properties file \"" + fileName + "\": " + e.getMessage()); } } } private static void initArena(PropertyLoader propertyLoader) { try { Arena.init(); arena = Arena.getInstance(); // TODO: Use properties to determine the class name of the access policy. arena.setAccessPolicy(new DefaultAccessPolicy()); // set the admnistrator String operatorName = propertyLoader.getStringProperty("Operator", "admin"); String operatorPassword = propertyLoader.getStringProperty("OperatorPassword", "adminpass"); User operator = arena.createUser(operatorName, operatorPassword); arena.setOperator(operator); int serverPort = propertyLoader.getIntProperty("ArenaPort", 1099); LocateRegistry.createRegistry(serverPort); tellUser("Registering arena on port " + serverPort + " ..."); Naming.rebind("//localhost:" + serverPort + "/ArenaServer", arena); tellUser("... arena successfully registered as a remote object."); } catch (Exception e) { tellUser("Failed to initialize arena: " + e.getMessage()); e.printStackTrace(); } } private static Object getInstanceOfClass(String className) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, SecurityException, IllegalAccessException, IllegalArgumentException, MalformedURLException { Class result = null; try { tellUser("Loading class " + className + " from classpath."); result = Class.forName(className); } catch (ClassNotFoundException e) { tellUser(className + " not found in classpath, trying remote loading from codebase (" + codeBase + ")."); result = RMIClassLoader.loadClass(codeBase, className); } return result.getMethod("getInstance", null).invoke(null, null); } private static void registerTournamentStyles(PropertyLoader propertyLoader) { String [] styleClassNames = propertyLoader.getStringArrayProperty("TournamentStyles"); String styleClassName = null; boolean loadingSucceeded = false; try { for (int i = 0; i < styleClassNames.length; i++) { styleClassName = styleClassNames[i]; String defaultStyleName = styleClassName.substring(styleClassName.lastIndexOf(".")+1); String styleName = propertyLoader.getStringProperty(styleClassName + ".name", defaultStyleName); TournamentStyle style = (TournamentStyle)getInstanceOfClass(styleClassName); arena.registerTournamentStyle(styleName, style); } loadingSucceeded = true; } catch (ClassNotFoundException e) { tellUser("Tournament style class \"" + styleClassName + "\" not found (neither in class path nor in codebase)."); } catch (MalformedURLException e) { tellUser("Failed to remotely load tournament style class \"" + styleClassName + "\" because codebase URL is not well-formed (\"" + codeBase + "\")."); } catch (NoSuchMethodException e) { tellUser("Tournament style class \"" + styleClassName + "\" does not define a public static method getInstance()."); } catch (Exception e) { tellUser("Tournament style class \"" + styleClassName + "\" static method getInstance() threw exception: " + e.getMessage()); e.printStackTrace(); } if (!loadingSucceeded) { System.exit(1); } } private static String registerOneGame(PropertyLoader propertyLoader, String gameClassName) throws MalformedURLException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, ClassNotFoundException, InvocationTargetException, GameAlreadyExistsException { String defaultStyleName = gameClassName.substring(gameClassName.lastIndexOf(".")+1); String gameName = propertyLoader.getStringProperty(gameClassName + ".name", defaultStyleName); String gameDescription = propertyLoader.getStringProperty(gameClassName + ".description", ""); Game game = (Game)getInstanceOfClass(gameClassName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -