View Javadoc

1   /*
2    $Id: ClientExtractorThread.java,v 1.1.1.1 2003/08/25 18:36:05 mch Exp $
3    */
4   
5   package org.astrogrid.ace.client;
6   
7   import java.io.*;
8   
9   import javax.swing.ProgressMonitor;
10  import javax.swing.JOptionPane;
11  
12  import org.w3c.dom.Element;
13  
14  import org.astrogrid.ace.client.AceServerList;
15  import org.astrogrid.ace.web.Ace;
16  
17  import org.astrogrid.common.client.ServiceClient;
18  import org.astrogrid.common.service.TempDirManager;
19  import org.astrogrid.tools.xml.DomDumper;
20  
21  import org.astrogrid.ui.UserCancelledException;
22  
23  import org.astrogrid.log.Log;
24  
25  import org.astrogrid.common.myspace.*;
26  
27  /***
28   * Takes extraction criteria, and carries out the extraction on a remote or
29   * local extractor as required, returning results to the given consumer.
30   * Displays a progress box
31   */
32  
33  public class ClientExtractorThread extends Thread
34  {
35     protected Element extractionCriteria;
36     protected String aceServer;
37     protected ProgressMonitor progressMonitor;
38     protected VotConsumer votConsumer;
39  //   protected Element results;
40  
41     public ClientExtractorThread(Element givenExtractionCriteria, String givenAceServer, VotConsumer givenConsumer)
42     {
43        this.extractionCriteria = givenExtractionCriteria;
44        this.aceServer = givenAceServer;
45        this.votConsumer = givenConsumer;
46  
47        progressMonitor = new ProgressMonitor(
48              null,
49              "Extracting catalogue...",
50              "",
51              0,3
52        );
53  
54        progressMonitor.setMillisToDecideToPopup(0);
55        progressMonitor.setMillisToPopup(0);
56  
57     }
58  
59     /***
60      * Carries out extraction, whether local or remote. keeping the results
61      * as an XML tree (see getResults)
62      */
63     public void run()
64     {
65        progressMonitor.setNote("Preparing to extract");
66        progressMonitor.setProgress(0);
67  
68        try
69        {
70           if (progressMonitor.isCanceled())
71              throw new UserCancelledException();
72  
73           Element results = null;
74  
75           if (AceServerList.isLocal(aceServer))
76           {
77              Ace ace = new Ace();
78  
79              progressMonitor.setNote("Running local Extractor");
80              progressMonitor.setProgress(1);
81  
82              results = ace.runApplication(extractionCriteria);
83           }
84           else
85           {
86              //publish image(s) etc to myspace (this should automatically do nothing if nothing needs done)
87              //             publishFiles(configDom);
88  
89              progressMonitor.setNote("Calling ACE server");
90              progressMonitor.setProgress(1);
91  
92              //call service
93              Log.logInfo("Calling ACE server at "+aceServer+", please wait...");
94  
95              ServiceClient client = new ServiceClient(AceServerList.getUrl(aceServer));
96  
97              if (progressMonitor.isCanceled())
98                 throw new UserCancelledException();
99  
100             results = client.httpPost(extractionCriteria);
101          }
102 
103             if (progressMonitor.isCanceled())
104                throw new UserCancelledException();
105 
106             progressMonitor.setNote("Examining Results");
107             progressMonitor.setProgress(2);
108 
109             votConsumer.consumeAceResults(results);
110       }
111       catch (IOException ioe)
112       {
113          Log.logError("Service Failed",ioe);
114 
115          JOptionPane.showMessageDialog(null, "ACE Service Failed: "+ioe,
116                                        "ACE Failure", JOptionPane.ERROR_MESSAGE);
117       }
118       catch (UserCancelledException uce)
119       {
120          Log.logInfo("User Cancelled");
121       }
122       catch (Throwable e) //report any other messages
123       {
124          Log.logError("Application Error",e);
125 
126          JOptionPane.showMessageDialog(null, "ACE Application Error: "+e,
127                                        "ACE Application Failure", JOptionPane.ERROR_MESSAGE);
128       }
129 
130       progressMonitor.setProgress(progressMonitor.getMaximum()+1);   //close
131 
132    }
133 
134    /***
135     * Looks through DOM for files that might need publishing to a public area
136     * visible to the web service.  Updates the references within the DOM to
137     * point to these new public areas
138     *
139    protected void publishFiles(Element configDom) throws IOException
140    {
141       //Look through all the files given and make sure they will be visible
142       //to the service.
143       MySpaceClient myspace = myspaceServers.getMySpaceClient((String) spaceSelector.getSelectedItem());
144       myspace.connect();
145 
146       publishAFile(configDom, "ImageToMeasure", myspace);
147       publishAFile(configDom, "ImageToCatalog", myspace);
148 
149       //disconnect from myspace
150       myspace.disconnect();
151    }
152 
153    protected void publishAFile(Element configDom, String tag, MySpaceClient mySpace) throws IOException
154    {
155       Log.logInfo("Resolving public access for '"+tag+"', please wait...");
156 
157       Element node = (Element) configDom.getElementsByTagName(tag).item(0);
158 
159       if (node == null)
160       {
161          Log.logInfo("...no tag found");
162          return;
163       }
164 
165       node = (Element) node.getElementsByTagName("arg").item(0);
166 
167       String givenPath = node.getFirstChild().getNodeValue();
168 
169       //publish image(s) etc to myspace (this should automatically do nothing if nothing needs done)
170       String publicPath = mySpace.publicise("test",givenPath);
171 
172       Log.logInfo("...resolved to "+publicPath);
173       node.setNodeValue(publicPath);
174    }
175     /**/
176 
177 }
178 
179 /*
180 $Log: ClientExtractorThread.java,v $
181 Revision 1.1.1.1  2003/08/25 18:36:05  mch
182 Reimported to fit It02 source structure
183 
184 Revision 1.4  2003/07/11 10:42:44  mch
185 Thread now stops if user presses cancel
186 
187 Revision 1.3  2003/07/02 19:19:23  mch
188 Catches & reports any throwable error
189 
190 Revision 1.2  2003/06/26 19:14:37  mch
191 Minor change to progress monitor
192 
193 Revision 1.1  2003/06/18 16:01:41  mch
194 Removing circular dependency on Aladin, tidying up threading
195 
196 */
197