(no commit message)
[utils] / crawler / kissweb / src / org / wamblee / crawler / kiss / servlet / CrawlerServlet.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.crawler.kiss.servlet;
18
19 import java.io.IOException;
20 import java.util.Date;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.wamblee.crawler.kiss.scheduling.CrawlerSchedule;
28 import org.wamblee.crawler.kiss.scheduling.CrawlerScheduler;
29 import org.wamblee.general.BeanKernel;
30
31 /**
32  * 
33  */
34 public class CrawlerServlet extends HttpServlet {
35
36     /*
37      * (non-Javadoc)
38      * 
39      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
40      *      javax.servlet.http.HttpServletResponse)
41      */
42     @Override
43     protected void doPost(HttpServletRequest aRequest,
44             HttpServletResponse aResponse) throws ServletException, IOException {
45
46         CrawlerScheduler scheduler = BeanKernel.getBeanFactory().find(
47                 CrawlerScheduler.class);
48         CrawlerSchedule status = BeanKernel.getBeanFactory().find(
49                 CrawlerSchedule.class);
50
51         try {
52             if ( aRequest.getParameter("runnow") != null ) {
53                 status.setLastExecuted(new Date(System.currentTimeMillis() - 24*3600*1000));
54                 scheduler.scheduleNow();
55                 aResponse.sendRedirect("");
56                 return;
57             }
58             aRequest.setAttribute("running", scheduler.isCrawlerRunning());
59             aRequest.setAttribute("lastExecuted", status.getLastExecuted());
60             aRequest.setAttribute("lastResult", status.getLastResult());
61             aRequest.setAttribute("lastException", status.getLastException());
62             String msg = ""; 
63             Throwable e = status.getLastException(); 
64             while ( e != null ) { 
65                 msg = msg + e.getMessage() + "<br/>"; 
66                 e = e.getCause();
67             }
68             aRequest.setAttribute("lastMessage", msg);
69         } catch (Exception e) {
70             throw new ServletException("Error getting status", e);
71         }
72         aRequest.getRequestDispatcher("WEB-INF/overview.jsp").forward(aRequest,
73                 aResponse);
74     }
75
76     /*
77      * (non-Javadoc)
78      * 
79      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
80      *      javax.servlet.http.HttpServletResponse)
81      */
82     @Override
83     protected void doGet(HttpServletRequest aRequest,
84             HttpServletResponse aResponse) throws ServletException, IOException {
85         doPost(aRequest, aResponse);
86     }
87 }