View Javadoc

1   package pl.agh.iosr.ballamigos.webapp.actions;
2   
3   import java.io.IOException;
4   import java.util.InvalidPropertiesFormatException;
5   import java.util.Properties;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   import javax.servlet.http.HttpSession;
10  
11  import org.apache.struts.action.ActionErrors;
12  import org.apache.struts.action.ActionForm;
13  import org.apache.struts.action.ActionForward;
14  import org.apache.struts.action.ActionMapping;
15  import org.apache.struts.action.ActionMessage;
16  
17  import pl.agh.iosr.ballamigos.webapp.core.WebApplicationCommunicator;
18  
19  /***A struts action class used to redirect the player to the "Stats" page. It involves reading from configuration
20   * file and communicating with the server to get data for the page.
21   * 
22   * @author Piotr
23   *
24   */
25  public class StatsAction extends BallAmigosAction {
26  	
27  	private String configFileName = "/WEB-INF/config.xml";
28  	
29  	public ActionForward execute(ActionMapping mapping,
30  			ActionForm form,
31  			HttpServletRequest request,
32  			HttpServletResponse response)
33  	throws Exception {
34  		HttpSession session = request.getSession();		
35  		
36  		if(session.getAttribute("props") == null) {
37  			Properties properties = new Properties();
38  			try {
39  				properties.loadFromXML(servlet.getServletContext().getResourceAsStream(configFileName));
40  			} catch (InvalidPropertiesFormatException e) {
41  				// TODO Auto-generated catch block
42  				e.printStackTrace();
43  			} catch (IOException e) {
44  				// TODO Auto-generated catch block
45  				e.printStackTrace();
46  			}
47  			session.setAttribute("props",properties);			
48  		}
49  		
50  		WebApplicationCommunicator wac;
51  		ActionErrors errors = new ActionErrors();
52  		
53  		try {
54  			wac = (WebApplicationCommunicator) session.getAttribute("communicator"); 
55  			if(wac == null) { 
56  				wac = new WebApplicationCommunicator((Properties)session.getAttribute("props"));
57  				session.setAttribute("communicator",wac);
58  			}
59  			
60  			request.setAttribute("stats", wac.getServerStatistics());
61  		} catch (Exception e) {
62  			errors.add("rmiError", new ActionMessage("rmi.error"));
63  			e.printStackTrace();
64  		}		
65  		
66  		return(mapping.findForward("success"));	
67  	}
68  }