(no commit message)
[utils] / crawler / kissweb / src / org / wamblee / crawler / kiss / servlet / CrawlerServlet.java
diff --git a/crawler/kissweb/src/org/wamblee/crawler/kiss/servlet/CrawlerServlet.java b/crawler/kissweb/src/org/wamblee/crawler/kiss/servlet/CrawlerServlet.java
new file mode 100644 (file)
index 0000000..479c60b
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2006 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wamblee.crawler.kiss.servlet;
+
+import java.io.IOException;
+import java.util.Date;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.wamblee.crawler.kiss.scheduling.CrawlerSchedule;
+import org.wamblee.crawler.kiss.scheduling.CrawlerScheduler;
+import org.wamblee.general.BeanKernel;
+
+/**
+ * 
+ */
+public class CrawlerServlet extends HttpServlet {
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
+     *      javax.servlet.http.HttpServletResponse)
+     */
+    @Override
+    protected void doPost(HttpServletRequest aRequest,
+            HttpServletResponse aResponse) throws ServletException, IOException {
+
+        CrawlerScheduler scheduler = BeanKernel.getBeanFactory().find(
+                CrawlerScheduler.class);
+        CrawlerSchedule status = BeanKernel.getBeanFactory().find(
+                CrawlerSchedule.class);
+
+        try {
+            if ( aRequest.getParameter("runnow") != null ) {
+                status.setLastExecuted(new Date(System.currentTimeMillis() - 24*3600*1000));
+                scheduler.scheduleNow();
+                aResponse.sendRedirect("");
+                return;
+            }
+            aRequest.setAttribute("running", scheduler.isCrawlerRunning());
+            aRequest.setAttribute("lastExecuted", status.getLastExecuted());
+            aRequest.setAttribute("lastResult", status.getLastResult());
+            aRequest.setAttribute("lastException", status.getLastException());
+            String msg = ""; 
+            Throwable e = status.getLastException(); 
+            while ( e != null ) { 
+                msg = msg + e.getMessage() + "<br/>"; 
+                e = e.getCause();
+            }
+            aRequest.setAttribute("lastMessage", msg);
+        } catch (Exception e) {
+            throw new ServletException("Error getting status", e);
+        }
+        aRequest.getRequestDispatcher("WEB-INF/overview.jsp").forward(aRequest,
+                aResponse);
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
+     *      javax.servlet.http.HttpServletResponse)
+     */
+    @Override
+    protected void doGet(HttpServletRequest aRequest,
+            HttpServletResponse aResponse) throws ServletException, IOException {
+        doPost(aRequest, aResponse);
+    }
+}