Project

General

Profile

1 2384 costa
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 University of New Mexico and the
4
 *             Regents of the University of California
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24
25
package edu.ucsb.nceas.metacat.harvesterClient;
26
27 2995 costa
import java.io.File;
28
import java.io.IOException;
29
30 2384 costa
import javax.servlet.*;
31
import javax.servlet.http.*;
32
33 2995 costa
import edu.ucsb.nceas.utilities.Options;
34
35 2384 costa
/**
36
 * HarvesterServlet class allows Harvester to be run as a background
37
 * process. This eliminates the need to run Harvester in a terminal window.
38
 * To activate this servlet, uncomment the HarvesterServlet entry in the
39
 * appropriate 'lib/web.xml.tomcat*' file.
40
 *
41
 * @author costa
42
 */
43
public class HarvesterServlet extends HttpServlet implements Runnable {
44
45 2995 costa
  /*
46
   * Class fields
47
   */
48
  private static final String SCHEMA_DIR = "harvester";
49
  private static String SCHEMA_LOCATION;
50
  private static final String SCHEMA_NAME = "harvestList.xsd";
51
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
52
53
  /*
54
   * Object fields
55
   */
56 2384 costa
  Thread harvesterThread;                // background Harvester thread
57
58 2995 costa
59 2384 costa
  /**
60 2995 costa
   * When the thread is destroyed, sets the Harvester.keepRunning flag to false.
61 2384 costa
   */
62 2995 costa
  public void destroy() {
63
    Harvester.setKeepRunning(false);
64
  }
65
66
67
  /**
68
   * Initializes the servlet. Reads properties and initializes object fields.
69
   *
70
   * @throws ServletException
71
   */
72
  public void init(ServletConfig config) throws ServletException {
73
    ServletContext context = null;
74
    String fileSeparator = System.getProperty("file.separator");
75
    String schemaPath;
76
77
    super.init(config);
78
    context = config.getServletContext();
79
    schemaPath = context.getRealPath(SCHEMA_DIR) + fileSeparator + SCHEMA_NAME;
80
    SCHEMA_LOCATION = "eml://ecoinformatics.org/harvestList " + schemaPath;
81 2384 costa
    harvesterThread = new Thread(this);
82
    harvesterThread.setPriority(Thread.MIN_PRIORITY);  // be a good citizen
83
    harvesterThread.start();
84
  }
85
86 2995 costa
87 2384 costa
  /**
88
   * Runs the Harvester main program in a separate thread. First sleeps for
89
   * 30 seconds to give Metacat a chance to fully initialize.
90
   */
91
  public void run() {
92
      String[] args = new String[1];
93 2995 costa
      args[0] = SCHEMA_LOCATION;
94 2384 costa
95
      try {
96
        Thread.sleep(30000);    // Sleep 30 seconds before starting Harvester
97
      }
98
      catch (InterruptedException e) {
99
        System.err.println("InterruptedException: " + e.getMessage());
100
      }
101
102
      Harvester.main(args);
103
  }
104
105
}