001 package com.croftsoft.apps.mars.net; 002 003 import javax.servlet.*; 004 import javax.servlet.http.*; 005 006 import com.croftsoft.core.CroftSoftConstants; 007 import com.croftsoft.core.beans.XmlBeanCoder; 008 import com.croftsoft.core.io.SerializableCoder; 009 import com.croftsoft.core.lang.NullArgumentException; 010 import com.croftsoft.core.servlet.HttpGatewayServlet; 011 012 /********************************************************************* 013 * Mars servlet. 014 * 015 * @version 016 * 2003-06-13 017 * @since 018 * 2003-04-06 019 * @author 020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 021 *********************************************************************/ 022 023 public final class MarsServlet 024 extends HttpGatewayServlet 025 ////////////////////////////////////////////////////////////////////// 026 ////////////////////////////////////////////////////////////////////// 027 { 028 029 private static final String VERSION = "2003-06-13"; 030 031 private static final String TITLE = "CroftSoft Mars Server"; 032 033 private static final String SERVLET_INFO 034 = "\n" + TITLE 035 + "\n" + CroftSoftConstants.COPYRIGHT 036 + "\n" + CroftSoftConstants.HOME_PAGE 037 + "\n" + "Version " + VERSION 038 + "\n" + CroftSoftConstants.DEFAULT_LICENSE 039 + "\n"; 040 041 private static final String GAME_INIT_PATH = "/WEB-INF/mars.xml"; 042 043 private static final String PRIMARY_FILENAME = "mars.dat"; 044 045 private static final String FALLBACK_FILENAME = "mars.bak"; 046 047 // 048 049 private final MarsServer marsServer; 050 051 ////////////////////////////////////////////////////////////////////// 052 ////////////////////////////////////////////////////////////////////// 053 054 public MarsServlet ( ) 055 ////////////////////////////////////////////////////////////////////// 056 { 057 this ( new MarsServer ( PRIMARY_FILENAME, FALLBACK_FILENAME ) ); 058 } 059 060 ////////////////////////////////////////////////////////////////////// 061 ////////////////////////////////////////////////////////////////////// 062 063 public String getServletInfo ( ) 064 ////////////////////////////////////////////////////////////////////// 065 { 066 return SERVLET_INFO; 067 } 068 069 ////////////////////////////////////////////////////////////////////// 070 ////////////////////////////////////////////////////////////////////// 071 072 public void init ( ) 073 throws ServletException 074 ////////////////////////////////////////////////////////////////////// 075 { 076 System.out.println ( SERVLET_INFO ); 077 078 try 079 { 080 GameInit gameInit = ( GameInit ) XmlBeanCoder.decodeFromXml ( 081 getServletContext ( ).getResourceAsStream ( GAME_INIT_PATH ) ); 082 083 marsServer.setGameInitAccessor ( gameInit ); 084 085 marsServer.init ( ); 086 } 087 catch ( Exception ex ) 088 { 089 throw ( ServletException ) 090 new UnavailableException ( ex.getMessage ( ) ).initCause ( ex ); 091 } 092 } 093 094 public void destroy ( ) 095 ////////////////////////////////////////////////////////////////////// 096 { 097 try 098 { 099 marsServer.destroy ( ); 100 } 101 catch ( Exception ex ) 102 { 103 log ( ex.getMessage ( ), ex ); 104 } 105 } 106 107 ////////////////////////////////////////////////////////////////////// 108 // private methods 109 ////////////////////////////////////////////////////////////////////// 110 111 private MarsServlet ( MarsServer marsServer ) 112 ////////////////////////////////////////////////////////////////////// 113 { 114 super ( 115 marsServer, 116 SerializableCoder.INSTANCE, 117 SerializableCoder.INSTANCE ); 118 119 NullArgumentException.check ( this.marsServer = marsServer ); 120 } 121 122 ////////////////////////////////////////////////////////////////////// 123 ////////////////////////////////////////////////////////////////////// 124 }