001 /*
002  * Copyright (C) 1998 by ETHZ/INF/CS
003  * All rights reserved
004  
005  * @version $Id: MetaSearchImpl.java,v 1.1 2001/03/16 17:55:07 praun Exp $
006  * @author Christoph von Praun
007  */
008 
009 import java.util.*;
010 import java.io.*;
011 import EDU.oswego.cs.dl.util.concurrent.*;
012 import ethz.util.Generator;
013 
014 /**
015  * An object of type RequestDispatchServlet is the entry point for http requests to the HEDC system. 
016  * The communication with the WWW server is done through the Servlet API.
017  */
018 public class MetaSearchImpl implements MetaSearch {
019 
020     private static final String TFA01_ = "TFA01 - failed to parse date from string '%1'";
021     private static final String TFA02_ = "TFA02 - failed to create uniqueInstance (%1)";
022     private static final int MSR_DEFAULT_DURATION_ = 5000;
023     private static final int MSR_MAX_THREADS_ = 50;
024     private static String MSR_TEMPLATE_LOCATION_ = null;
025     private static String MSR_FRAME_TEMPLATE_ = "hedc_synoptic_frame";
026     private static char[] MSR_ROW_HEADER_TEMPLATE_ = null;
027     private static char[] MSR_ROW_LINE_TEMPLATE_ = null;
028     private static char[] MSR_ROW_EMPTY_LINE_TEMPLATE_ = null;
029     private static MetaSearchImpl uniqueInstance_ = null;
030     private TaskFactory taskFac_ = null;
031     private PooledExecutorWithInvalidate executor_ = null;
032 
033     public static MetaSearchImpl getUniqueInstance() {
034   //if (uniqueInstance_ == null)
035   //    try {
036     uniqueInstance_ = new MetaSearchImpl();
037   //    } catch (Exception e) {
038   //  Messages.error(TFA02_, e);
039   //    }
040   return uniqueInstance_;
041     }
042 
043     private MetaSearchImpl() {
044   taskFac_ = new TaskFactory();
045   executor_ = new PooledExecutorWithInvalidate(new LinkedQueue(),
046                  MSR_MAX_THREADS_);
047   executor_.setKeepAliveTime(-1)// threads live forever
048   MSR_ROW_HEADER_TEMPLATE_ = FormFiller.internalize("hedc_synoptic_row_header");
049   MSR_ROW_LINE_TEMPLATE_ = FormFiller.internalize("hedc_synoptic_row_body");
050   MSR_ROW_EMPTY_LINE_TEMPLATE_ = FormFiller.internalize("hedc_synoptic_row_empty_body");
051   Messages.debug(0"MetaSearchImpl:: constructor done");
052     }
053 
054     public long search(Hashtable h, Writer wrt, MetaSearchRequest rthrows IOException {
055   List results = search(h, r);
056   return writeResults_(results, wrt);
057     }
058 
059     public List search(Hashtable h, MetaSearchRequest r) {
060   // create tasks
061   List taskList = null;
062   String dateString = (Stringh.get("DATETIME");
063   Date date = null;
064   long waitTime = MSR_DEFAULT_DURATION_;
065   try {
066       waitTime = Long.valueOf((String)h.get("WAIT_TIME")).longValue() 1000;
067   catch (Exception e) {}
068   if (dateString != null
069       try {
070     Messages.debug(-1"MetaSearch::search before parse");
071     date = RandomDate.parse(dateString);
072     Messages.debug(-1"MetaSearch::search after parse date=%1", date);
073       catch (Exception e) {
074     Messages.error(TFA01_, dateString);
075       }
076   else
077       Messages.error(TFA01_, dateString);
078   
079   if (date != null) {
080       Thread t = Thread.currentThread();
081       taskList = taskFac_.makeTasks(h, date, r);
082       // take precaution that the issueing Thread is interrupted when all tasks are done
083       r.registerInterrupt(t, taskList.size());
084       try {
085     for (Iterator e = taskList.iterator(); e.hasNext())
086         executor_.execute((Taske.next());
087     // sleep
088     t.sleep(waitTime);
089       catch (InterruptedException e) {
090     // if all tasks have been done, before waitTime was over
091       }
092       
093       // invalidate all tasks and interrupt the corresponding threads
094       for (Iterator e = taskList.iterator(); e.hasNext())
095     ((Taske.next()).cancel();
096   }
097   return taskList;
098     }
099 
100 
101     /**
102      * Input is a list of tasks
103      */
104     private long writeResults_(List l, Writer wthrows IOException {
105   long ret = -1;
106   StringWriter sw = new StringWriter();
107   Hashtable h = new Hashtable();
108   for (Iterator e = l.iterator(); e.hasNext()) {
109       MetaSearchResult r = (MetaSearchResulte.next();
110       Iterator i = r.getInfo();
111       if (i.hasNext()) {
112      h = (Hashtablei.next();
113      if (h.get("URL"!= null) {
114          do {
115        Generator.generate(sw, h, MSR_ROW_LINE_TEMPLATE_);
116          while (i.hasNext() && (h = (Hashtablei.next()) != null);
117      else
118          Generator.generate(sw, h, MSR_ROW_EMPTY_LINE_TEMPLATE_);
119       }
120   }
121 
122   h.put("ROWS", sw.getBuffer().toString())
123   FormFiller f = new FormFiller(w, h, MSR_FRAME_TEMPLATE_);
124   f.fillForm();
125   return ret;
126     }
127 
128     private final void printResults_(List l) {
129   for (Iterator e = l.iterator(); e.hasNext()) {
130       MetaSearchResult t = (MetaSearchResulte.next();
131       System.out.println(t.getInfo());
132       System.out.println(t.results);
133   }
134     }
135 
136     private static final String RDI01_ = "RDI01 - an error occurred while opening your session (%1)";
137     private static final String RDI02_ = "RDI02 - no session with ID %1 found";
138     private static final String RDI03_ = "RDI03 - the RequestDispatch service is implicitly started through the server RequestDispatchServlet";
139     private static final String RDI04_ = "RDI04 - session id %1 was not opened through this API";
140 }