1 package astrogrid.registry.harvest.daemon;
2
3 import java.util.*;
4 import java.io.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7
8
9 import org.astrogrid.config.Config;
10 import org.astrogrid.registry.server.harvest.RegistryHarvestService;
11 import org.astrogrid.registry.RegistryException;
12
13
14
15 public class HarvestDaemon extends HttpServlet
16 {
17
18 int myCounter;
19 int harvestInterval;
20 Date lastHarvestTime;
21 Date servletInitTime;
22 RegistryHarvestService rhs;
23 boolean harvestOnLoad = false;
24 boolean valuesSet = false;
25 public static boolean harvestStarted;
26
27 private ServletContext context = null;
28
29 public static final String INTERVAL_HOURS_PROPERTY =
30 "org.astrogrid.registry.harvest.daemon.interval-hours";
31 public static Config conf = null;
32 private Timer timer = null;
33 private Timer scheduleTimer = null;
34
35 static {
36 if(conf == null) {
37 conf = org.astrogrid.config.SimpleConfig.getSingleton();
38 harvestStarted = false;
39 }
40 }
41
42
43 public void destroy() {
44 super.destroy();
45 System.out.println("exiting HarvestDaemon; trying to destroy timers");
46 if(timer != null) {
47 timer.cancel();
48 }
49 if(scheduleTimer != null) {
50 scheduleTimer.cancel();
51 }
52 }
53
54 public void init(ServletConfig config)
55 throws ServletException
56 {
57 super.init(config);
58
59 System.out.println("initialized HarvestDaemon");
60
61 servletInitTime = new Date();
62 rhs = new RegistryHarvestService();
63 if(scheduleTimer != null) {
64
65
66 scheduleTimer.cancel();
67 scheduleTimer = null;
68 }
69 scheduleTimer = new Timer();
70 boolean harvestEnabled = conf.getBoolean("registry.harvest.enabled",false);
71 if(harvestEnabled) {
72 if(!valuesSet) {
73 System.out.println("harvest is enabled");
74 valuesSet = true;
75 harvestInterval = conf.getInt(INTERVAL_HOURS_PROPERTY);
76
77 harvestOnLoad = false;
78 if(harvestInterval <= 0) {
79 System.out.println("ERROR CANNOT HAVE A HARVESTINTERVAL LESS THAN 1; DEFAULTING TO 2");
80 harvestInterval = 2;
81 }
82 }
83 System.out.println("in init of harvestDaemon and starting thread.");
84
85 scheduleTimer.scheduleAtFixedRate(new HarvestTimer("HarvestNow"),
86 harvestInterval*3600*1000,
87 harvestInterval*3600*1000);
88
89
90 }else {
91 System.out.println("harvest not enabled.");
92
93 return;
94 }
95
96 }
97
98
99 public void doGet(HttpServletRequest req, HttpServletResponse res)
100 throws IOException, ServletException
101 {
102 String nowParam = req.getParameter("HarvestNow");
103 if(timer != null) {
104
105
106 timer.cancel();
107 timer = null;
108 }
109 timer = new Timer();
110
111 if( nowParam != null && !harvestStarted ) {
112
113 timer.schedule(new HarvestTimer("HarvestNow"),5000);
114 harvestStarted = true;
115 }
116 String replicate = req.getParameter("ReplicateNow");
117 if( replicate != null && !harvestStarted ) {
118
119 timer.schedule(new HarvestTimer("ReplicateNow"),5000);
120 harvestStarted = true;
121 }
122
123
124 ServletOutputStream out = res.getOutputStream();
125 res.setContentType("text/html");
126 out.println("<html><head><title>Astrogrid Registry Harvest</title></head>");
127 out.println("<body><a href=\"http://www.astrogrid.org\">");
128 out.println("<img src=\"http://www.astrogrid.org/image/AGlogo\"" +
129 "align=right alt=\"AG logo\"> </a>");
130 out.println("<h1>Astrogrid Registry Harvest Control Servlet</h1>" +
131 "<br><h2>Servlet initialized " + servletInitTime +
132 "<br>Harvest interval = " + harvestInterval + " hours" +
133 "<br>Number of Harvests initiated = " + myCounter +
134 "<br>Last harvest time = " + lastHarvestTime +
135 "<form method=\"get\"><input type=\"submit\" " +
136 " name=\"HarvestNow\" value=\"Harvest now!\">" +
137 "<br /><strong>Replication can be dangerous replicates all resources not by date</strong><br />" +
138 "<input type=\"submit\" name=\"ReplicateNow\" value=\"Replicate Now\" /></form>");
139 if(harvestStarted) {
140 out.println("<br /><i>A Harvest/Replicate has began or still running.</i>");
141 }
142 out.println("</h2></body></html>");
143 }
144
145 class HarvestTimer extends TimerTask {
146 private String task = null;
147
148 public HarvestTimer(String harvestTask) {
149 task = harvestTask;
150 }
151
152 public void run() {
153
154 try {
155 if("HarvestNow".equals(task)) {
156 System.out.println("Immediate harvesting will be commenced!");
157 try {
158 rhs.harvestAll(true,true);
159 }catch(RegistryException e) {
160 e.printStackTrace();
161 }
162 }
163 if("ReplicateNow".equals(task)) {
164 System.out.println("Immediate replicate is beginning!");
165 try {
166 rhs.harvestAll(true,false);
167 }catch(RegistryException e) {
168 e.printStackTrace();
169 }
170 }
171 } finally {
172
173 HarvestDaemon.harvestStarted = false;
174 }
175 }
176 }
177
178
179
180 }