1   package ch.qos.logback.demo.prime;
2   
3   import ch.qos.logback.demo.Constants;
4   import org.apache.struts.action.Action;
5   import org.apache.struts.action.ActionForm;
6   import org.apache.struts.action.ActionForward;
7   import org.apache.struts.action.ActionMapping;
8   import org.slf4j.*;
9   
10  import javax.servlet.http.HttpServletRequest;
11  import javax.servlet.http.HttpServletResponse;
12  
13  public class PrimeAction extends Action {
14  
15      Logger logger = LoggerFactory.getLogger(PrimeAction.class);
16      Marker SMTP_TRIGGER = MarkerFactory.getMarker("SMTP_TRIGGER");
17  
18      public ActionForward execute(ActionMapping actionMapping,
19                                   ActionForm actionForm, HttpServletRequest request,
20                                   HttpServletResponse response) throws Exception {
21  
22          PrimeForm form = (PrimeForm) actionForm;
23  
24          Long number = form.getNumber();
25          if (number == 99) {
26              logger.info("99 is a magical value", new Exception("99 is supposedly invalid"));
27          }
28  
29          try {
30              NumberCruncher nc = new NumberCruncherImpl();
31              Long start = System.currentTimeMillis();
32              Long[] result = nc.factor(number);
33              Long duration = System.currentTimeMillis() - start;
34              logger.info("Results computed in {} ms", duration);
35  
36              request.setAttribute(Constants.PRIME_NUMBER, number);
37              request.setAttribute(Constants.PRIME_DURATION, duration);
38              request.setAttribute(Constants.PRIME_RESULTS, result);
39              return actionMapping.findForward("next");
40          } finally {
41              logger.info(SMTP_TRIGGER, "Prime computation ended");
42          }
43      }
44  }