Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 University of New Mexico and the 
4
 *             Regents of the University of California
5
 *
6
 *   '$Author: daigle $'
7
 *     '$Date: 2008-07-15 10:11:14 -0700 (Tue, 15 Jul 2008) $'
8
 * '$Revision: 4125 $'
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
import javax.servlet.*;
28
import javax.servlet.http.*;
29
/**
30
 * HarvesterServlet class allows Harvester to be run as a background
31
 * process. This eliminates the need to run Harvester in a terminal window.
32
 * To activate this servlet, uncomment the HarvesterServlet entry in the
33
 * appropriate 'lib/web.xml.tomcat*' file.
34
 * 
35
 * @author costa
36
 */
37
public class HarvesterServlet extends HttpServlet implements Runnable {
38

    
39
  /*
40
   * Class fields
41
   */
42
  private static final String SCHEMA_DIR = "harvester";
43
  private static String SCHEMA_LOCATION;
44
  private static final String SCHEMA_NAME = "harvestList.xsd";
45
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
46

    
47
  /*
48
   * Object fields
49
   */  
50
  Thread harvesterThread;                // background Harvester thread
51

    
52

    
53
  /**
54
   * When the thread is destroyed, sets the Harvester.keepRunning flag to false.
55
   */
56
  public void destroy() {
57
    Harvester.setKeepRunning(false);
58
  }
59
  
60

    
61
  /**
62
   * Initializes the servlet. Reads properties and initializes object fields.
63
   * 
64
   * @throws ServletException
65
   */
66
  public void init(ServletConfig config) throws ServletException {
67
    ServletContext context = null;
68
    String fileSeparator = System.getProperty("file.separator");
69
    String schemaPath;
70

    
71
    super.init(config);
72
    context = config.getServletContext();
73
    schemaPath = context.getRealPath(SCHEMA_DIR) + fileSeparator + SCHEMA_NAME;
74
    SCHEMA_LOCATION = "eml://ecoinformatics.org/harvestList " + schemaPath;
75
    harvesterThread = new Thread(this);
76
    harvesterThread.setPriority(Thread.MIN_PRIORITY);  // be a good citizen
77
    harvesterThread.start();
78
  }
79

    
80

    
81
  /**
82
   * Runs the Harvester main program in a separate thread. First sleeps for
83
   * 30 seconds to give Metacat a chance to fully initialize.
84
   */
85
  public void run() {
86
      String[] args = new String[1];
87
      args[0] = SCHEMA_LOCATION;
88

    
89
      try {
90
        Thread.sleep(30000);    // Sleep 30 seconds before starting Harvester
91
      }
92
      catch (InterruptedException e) {
93
        System.err.println("InterruptedException: " + e.getMessage());
94
      }
95

    
96
      Harvester.main(args);
97
  }
98

    
99
}
(9-9/11)