package edu.mssm.pharm.maayanlab.Enrichr;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.mssm.pharm.maayanlab.FileUtils;

@WebServlet(urlPatterns = {"/count"}, loadOnStartup=1)
public class Counter extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -682732829814620653L;

	private AtomicInteger count = new AtomicInteger(0);
	private AtomicInteger share = new AtomicInteger(0);

	@Override
	public void init() throws ServletException {
		// Try to load the initial count from our saved persistent state
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader("/datasets/count"));
			count.set(Integer.parseInt(bufferedReader.readLine()));
			bufferedReader.close();
			
			bufferedReader = new BufferedReader(new FileReader("/datasets/share"));
			share.set(Integer.parseInt(bufferedReader.readLine()));
		}
		catch (FileNotFoundException ignored) { }  // no saved state
		catch (IOException ignored) { }            // problem during read
		catch (NumberFormatException ignored) { }  // corrupt saved state
		finally {
			// Make sure to close the file
			try {
				if (bufferedReader != null)
					bufferedReader.close();
			}
			catch (IOException ignored) { }
		}
		
		getServletContext().setAttribute("EnrichrCount", count);
		getServletContext().setAttribute("ShareCount", share);
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");                                
		response.getWriter().print(count);		
	}   
	
	@Override
	public void destroy() {                                            
		super.destroy();  // entirely optional

		// Try to save the accumulated count
		FileUtils.writeString("/datasets/count", count.toString());
		FileUtils.writeString("/datasets/share", share.toString());
	}                                                                  

}

@@ -1946,40 +1946,8 @@
 n%22);
-                                
 %0D%0A%09%09
@@ -1984,17 +1984,12 @@
 nt);
-%09%09
 %0D%0A%09%7D
-   
 %0D%0A%09%0D
@@ -2029,52 +2029,8 @@
 () %7B
-                                            
 %0D%0A%09%09
@@ -2243,74 +2243,8 @@
 %0D%0A%09%7D
-                                                                  
 %0D%0A%0D%0A

Clean whitespace