/**
* @@org.jboss.aspects.security.SecurityDomain ("other")
*/
public class Calculator {
/**
* @@org.jboss.aspects.security.Unchecked
*/
public Calculator () { }
/**
* @@org.jboss.aspects.security.Permissions ({"Authorized"})
* @@org.jboss.jbossaop.aspect.TrialLimit
*/
public double getPayment (int principal, double rate,
int term) {
rate = rate / 100.;
rate = rate / 12.;
double tmp = Math.pow(1.+rate, term);
return (principal * tmp * rate) / (tmp - 1.);
}
}
public class CalculatorServlet extends HttpServlet {
private Calculator cal = null;
public void init () throws ServletException {
cal = new Calculator ();
}
public void service(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
String para = null;
int principal = 200000;
double rate = 8.0;
int term = 360;
double payment = 0.;
para = request.getParameter("principal");
if (para != null && para.length() > 0) {
principal = Integer.parseInt(para);
}
para = request.getParameter("rate");
if (para != null && para.length() > 0) {
rate = Double.parseDouble(para);
}
para = request.getParameter("term");
if (para != null && para.length() > 0) {
term = Integer.parseInt(para);
}
try {
payment = cal.getPayment(principal, rate, term);
out.println("<html>");
out.println("<head><title>Mortgage Calculator</title></head>");
out.println("<body>");
out.println("<h1>Mortgage Calculator</h1>");
out.println("<form action=\"Calculator\" method=\"get\">");
out.println("Principal (dollar):");
out.println("<input type=\"text\" name=\"principal\" value=\"");
out.print(principal);
out.println("\">");
out.println("<br/>");
out.println("Interest rate (annual percentage point):");
out.println("<input type=\"text\" name=\"rate\" value=\"");
out.print(rate);
out.println("\">");
out.println("<br/>");
out.println("Term (months):");
out.println("<input type=\"text\" name=\"term\" value=\"");
out.print(term);
out.println("\">");
out.println("<br/>");
out.println("<input type=\"submit\">");
out.println("</form>");
out.println("<p>The monthly payment is");
out.println("<b>");
out.print(payment);
out.println("</b>");
out.println("dollars</p>");
out.println("</body>");
out.println("</html>");
} catch (Throwable e) {
out.println("<html>");
out.println("<head><title>Error</title></head>");
out.println("<body>");
out.println("<h1>Error</h1>");
out.println("<p>Maybe you hit the access limit ");
out.println("or do not have sufficient permission");
out.println("<br>");
out.println("<p>The error message is:");
out.println("<pre>");
out.println(e.getMessage());
out.println("</pre>");
out.println("</body>");
out.println("</html>");
}
}
}
public interface TrialLimit {
}
public class TrialLimitAspect {
private static int count = 0;
public Object checkLimit (Invocation invocation)
throws Throwable {
System.out.println("Check whether the trial limit is reached");
count++;
if (count < 5) {
return invocation.invokeNext();
} else {
throw new Exception("Hit the maximum access count");
}
}
}
--jboss-aop.xml
<aop>
<aspect class="org.jboss.jbossaop.aspect.TrialLimitAspect"
scope="PER_VM"/>
<bind pointcut="execution(* *->@org.jboss.jbossaop.aspect.TrialLimit(..))">
<advice name="checkLimit"
aspect="org.jboss.jbossaop.aspect.TrialLimitAspect"/>
</bind>
</aop>
No comments:
Post a Comment