View Javadoc

1   /*
2    * $Id: TraceTriServer.java,v 1.1 2005/03/21 18:33:40 mch Exp $
3    */
4   
5   package org.astrogrid.dataservice.impl.ukssdc;
6   
7   import java.io.*;
8   
9   import java.sql.Connection;
10  import java.sql.ResultSet;
11  import java.sql.SQLException;
12  import java.sql.Statement;
13  import java.util.Date;
14  import javax.servlet.http.HttpServletRequest;
15  import javax.servlet.http.HttpServletResponse;
16  import org.apache.commons.logging.Log;
17  import org.astrogrid.cfg.ConfigFactory;
18  import org.astrogrid.dataservice.queriers.DatabaseAccessException;
19  import org.astrogrid.io.Piper;
20  import org.astrogrid.tableserver.jdbc.JdbcConnections;
21  import org.astrogrid.tableserver.jdbc.SqlResults;
22  import org.astrogrid.webapp.DefaultServlet;
23  
24  /***
25   * Serves the contents of 'TRI' combined files given a tape ID and file ID.
26   *
27   * The 'tri' files are collections of FITS files in some special-to-SolarSoft form,
28   * that are kept on tapes served by a tape juke box
29   *
30   * @author mch
31   */
32  public class TraceTriServer extends DefaultServlet {
33   
34     JdbcConnections connectionManager = null;
35     
36     public static final String HD_CACHE_KEY = "trace.filecache";
37  
38     protected static final Log log = org.apache.commons.logging.LogFactory.getLog(SqlResults.class);
39     
40     public void doGet(HttpServletRequest request, HttpServletResponse response)  throws IOException {
41        
42  //      String tapeId = request.getParameter("tape");
43        String fileId = request.getParameter("trifile");
44  
45        File file = getFileFromTape(fileId);
46        
47        InputStream in = new FileInputStream(file);
48        Piper.bufferedPipe(in, response.getOutputStream());
49  
50     }
51     
52     public File getFileFromTape(String fileId) throws IOException {
53        
54        //look up which tape to access
55        String[] resp = getTapeDetails(fileId);
56        String tapeId = resp[0];
57        String fileNum = resp[1];
58        String date = resp[2];
59        
60        File dir = new File( ConfigFactory.getCommonConfig().getString(HD_CACHE_KEY, "/data/ukssdc/TRACE/")+
61                               date.substring(0,4)+"/"+
62                               date.substring(5,6)+"/"+
63                               date.substring(7,8)+"/"
64                           );
65        
66        //check and make if they don't exist
67        if (!dir.exists()) {
68           dir.mkdirs();
69        }
70        File unloadedFile = new File(dir, fileId);
71  
72        log.trace("Unloading file to "+unloadedFile);
73  
74        String command = "tape -r -m "+fileNum+" -f "+unloadedFile+" SG "+tapeId;
75  
76        log.trace("Command: "+command);
77        
78        Process proc = Runtime.getRuntime().exec(command);
79  
80        if (!unloadedFile.exists()) {
81           
82           InputStream in = proc.getInputStream();
83           StringWriter sw = new StringWriter();
84           Piper.pipe(new InputStreamReader(in), sw);
85           throw new IOException("Failed to unload file "+fileId+" to "+unloadedFile+" from tape "+tapeId+" ("+fileNum+"): "+sw.toString());
86        }
87        
88        return unloadedFile;
89     }
90  
91  
92     /*** Returns the tape number and file number on that tape for the given
93      * tri file name by querying the database table trace_cat,
94      */
95     public String[] getTapeDetails(String triFile) throws IOException {
96  
97        String sql = "SELECT trace_cat.tape_id, trace_cat.file_number, trace_cat.datetime FROM trace_cat WHERE trace_cat.filename = '"+triFile+"'";
98        log.debug("TraceTriServer: "+sql);
99        
100       Connection jdbcConnection = null;
101       
102       try {
103       
104          //connect to database
105         if (connectionManager == null) {
106             connectionManager = JdbcConnections.makeFromConfig();
107          }
108          jdbcConnection = connectionManager.createConnection();
109          Statement statement = jdbcConnection.createStatement();
110 
111          //execute query
112          statement.execute(sql);
113 
114          ResultSet results = statement.getResultSet();
115 
116          if (!results.next()) {
117             throw new DatabaseAccessException("No match found in database for TRI file "+triFile);
118          }
119 
120          String tapeId = results.getString(1); //first column
121          String fileNum = results.getString(2); //second column
122          String datetime = results.getString(3);
123 
124          log.debug("TraceTriServer: returned "+tapeId+", "+fileNum+", "+datetime);
125 
126          if (results.next()) {
127             throw new DatabaseAccessException("More than one match found in database for TRI file "+triFile);
128          }
129          
130          return new String[] { tapeId, fileNum, datetime };
131       }
132       catch (SQLException e) {
133          //we don't really need to store stack info for the SQL exception, which saves logging...
134          throw new DatabaseAccessException(e+" using '" + sql + "': ",e);
135       }
136       finally {
137          //try to tidy up now
138          try {
139             if (jdbcConnection != null) { jdbcConnection.close(); }
140          } catch (SQLException e) { } //ignore
141       }
142 
143    }
144 
145 
146 }
147