001         package com.croftsoft.apps.ajgp.http;
002    
003         import java.io.*;
004    
005         import javax.servlet.*;
006         import javax.servlet.http.*;
007    
008         import com.croftsoft.core.CroftSoftConstants;
009         import com.croftsoft.core.io.Encoder;
010         import com.croftsoft.core.io.Parser;
011         import com.croftsoft.core.io.SerializableLib;
012         import com.croftsoft.core.io.StringCoder;
013         import com.croftsoft.core.role.Server;
014         import com.croftsoft.core.servlet.HttpGatewayServlet;
015    
016         /*********************************************************************
017         * High score servlet.
018         *
019         * @version
020         *   $Date: 2008/04/19 21:30:59 $
021         * @since
022         *   2003-06-01
023         * @author
024         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
025         *********************************************************************/
026    
027         public final class  ScoreServlet
028           extends HttpGatewayServlet
029         //////////////////////////////////////////////////////////////////////
030         //////////////////////////////////////////////////////////////////////
031         {
032    
033         private static final String  VERSION = "2003-06-03";
034    
035         private static final String  TITLE   = "CroftSoft ScoreServlet";
036    
037         private static final String  SERVLET_INFO
038           = "\n" + TITLE
039           + "\n" + CroftSoftConstants.COPYRIGHT
040           + "\n" + CroftSoftConstants.HOME_PAGE
041           + "\n" + "Version " + VERSION
042           + "\n" + CroftSoftConstants.DEFAULT_LICENSE
043           + "\n";
044    
045         private static final String  PRIMARY_FILENAME = "score.dat";
046    
047         private static final String  BACKUP_FILENAME  = "score.bak";
048    
049         private static final String  CHAR_SET_NAME    = StringCoder.UTF_8;
050    
051         private static final String  GET_REQUEST      = "get";
052    
053         private static final String  SET_REQUEST      = "set ";
054    
055         private static final StringCoder  STRING_CODER
056           = new StringCoder ( CHAR_SET_NAME );
057    
058         //
059    
060         private Long  highScoreLong;      
061    
062         //////////////////////////////////////////////////////////////////////
063         // constructor methods
064         //////////////////////////////////////////////////////////////////////
065    
066         public  ScoreServlet ( )
067         //////////////////////////////////////////////////////////////////////
068         {
069           super ( ( Server ) null, STRING_CODER, STRING_CODER );
070         }
071    
072         //////////////////////////////////////////////////////////////////////
073         // overridden Servlet methods
074         //////////////////////////////////////////////////////////////////////
075    
076         public String  getServletInfo ( ) { return SERVLET_INFO; }
077    
078         public void  init ( )
079           throws ServletException
080         //////////////////////////////////////////////////////////////////////
081         {
082           System.out.println ( SERVLET_INFO );
083    
084           try
085           {
086             highScoreLong = ( Long )
087               SerializableLib.load ( PRIMARY_FILENAME, BACKUP_FILENAME );
088           }
089           catch ( FileNotFoundException  ex )
090           {
091           }
092           catch ( Exception  ex )
093           {
094             log ( ex.getMessage ( ), ex );
095           }
096    
097           if ( highScoreLong == null )
098           {
099             highScoreLong = new Long ( 0 );
100           }
101         }
102    
103         public void  destroy ( )
104         //////////////////////////////////////////////////////////////////////
105         {
106           try
107           {
108             SerializableLib.save (
109               highScoreLong, PRIMARY_FILENAME, BACKUP_FILENAME );
110           }
111           catch ( Exception  ex )
112           {
113             log ( ex.getMessage ( ), ex );
114           }
115         }
116    
117         //////////////////////////////////////////////////////////////////////
118         // overridden HttpGatewayServlet methods
119         //////////////////////////////////////////////////////////////////////
120    
121         protected Object  serve ( Object  request )
122         //////////////////////////////////////////////////////////////////////
123         {
124           String  requestString
125             = ( ( String ) request ).trim ( ).toLowerCase ( );
126    
127           if ( requestString.equals ( GET_REQUEST ) )
128           {
129             return highScoreLong;
130           }
131    
132           if ( requestString.startsWith ( SET_REQUEST ) )
133           {
134             String  newHighScoreString
135               = requestString.substring ( SET_REQUEST.length ( ) );
136    
137             long  newHighScore = Long.parseLong ( newHighScoreString );
138    
139             synchronized ( this )
140             {
141               if ( newHighScore > highScoreLong.longValue ( ) )
142               {
143                 highScoreLong = new Long ( newHighScore );
144               }
145             }
146    
147             return null;
148           }
149    
150           throw new IllegalArgumentException ( );
151         }
152    
153         //////////////////////////////////////////////////////////////////////
154         //////////////////////////////////////////////////////////////////////
155         }