Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An abstract class that is a template for a data transfer class
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Chad Berkley
7
 *    Release: @release@
8
 *
9
 *   '$Author: berkley $'
10
 *     '$Date: 2001-01-11 13:36:37 -0800 (Thu, 11 Jan 2001) $'
11
 * '$Revision: 655 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
import java.net.*;
17
import java.io.*;
18
import java.util.Properties;
19
import java.util.Date;
20
import java.text.SimpleDateFormat;
21
import java.sql.*;
22

    
23
abstract class DataFileUploadInterface extends Thread 
24
{
25
  private MetaCatUtil util = new MetaCatUtil();
26
  private static String filedir = "";
27
  private String user = null;
28
  private String sess_id = null;
29
  private int port;
30
  protected Socket s;
31
  
32
  /**
33
   * Sets the port, user and sess_id
34
   */
35
  DataFileUploadInterface(int port, String user, String sess_id) 
36
  {
37
    this.sess_id = sess_id;
38
    this.user = user;
39
    this.port = port;
40
    filedir = util.getOption("datafilepath");
41
  }
42
  
43
  /**
44
   * Override this method with the code to handle the socket connection to 
45
   * the port specified in the constructor.
46
   */
47
  abstract void getFile(int port, String user, String sess_id);
48
  
49
  /**
50
   * This method is invoked when this class is broken off into a new thread
51
   */
52
  public void run () 
53
  {
54
    getFile(port, user, sess_id);
55
  }
56
  
57
  /**
58
   * attempts to connect a socket, returns null if it is not successful
59
   * returns the connected socket if it is successful.
60
   */
61
  public static Socket getSocket(String host, int port)
62
  {
63
    Socket s = null;
64
    try
65
    {
66
      s = new Socket(host, port);
67
      //we could create a socket on this port so the port is not available
68
      //System.out.println("socket connnected");
69
      return s;
70
    }
71
    catch(UnknownHostException u)
72
    {
73
    }
74
    catch(IOException i)
75
    {
76
      //an ioexception is thrown if the port is not in use
77
      //System.out.println("socket not connected");
78
      return s;
79
    }
80
    return s;
81
  }
82
  
83
  /**
84
   * returns true if the port specified is not in use.  false otherwise
85
   */
86
  public static boolean portIsAvailable(int port)
87
  {
88
    Socket s;
89
    String host = "localhost";
90
    try
91
    {
92
      s = new Socket(host, port);
93
      //s.close();
94
      //we could create a socket on this port so the port is not available
95
      //System.out.println("socket not available");
96
      return false;
97
    }
98
    catch(UnknownHostException u)
99
    {
100
      //it better know localhost!
101
    }
102
    catch(IOException i)
103
    {
104
      //an ioexception is thrown if the port is not in use
105
      //System.out.println("socket available");
106
      return true;
107
    }
108
    return true;
109
  }
110
  
111
  /**
112
   * Updates xml_documents with the new data document information
113
   * @param accnum the accession number of the new data file
114
   * @param filename the filename of the new data file
115
   * @param userOwner the document's owner's username
116
   */
117
  public void updateDB(String accnum, String filename, String userOwner)
118
  {
119
    SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
120
    java.util.Date localtime = new java.util.Date();
121
    String dateString = formatter.format(localtime);
122
    
123
    String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH24:MI:SS')";
124
    
125
    StringBuffer sql = new StringBuffer();
126
    sql.append("insert into xml_documents (docid, docname, doctype, ");
127
    sql.append("user_owner, user_updated, server_location, rev, date_created");
128
    sql.append(", date_updated, public_access) values ('");
129
    sql.append(accnum).append("','").append(filename).append("','BIN','");
130
    sql.append(userOwner).append("','").append(userOwner).append("','1','");
131
    sql.append("1',").append(sqlDateString).append(",");
132
    sql.append(sqlDateString).append(",'0')");
133
    //System.out.println("sql: " + sql.toString());
134
    try
135
    {
136
      Connection conn = util.openDBConnection();
137
      PreparedStatement pstmt = conn.prepareStatement(sql.toString());
138
      pstmt.execute();
139
      conn.close();
140
    }
141
    catch(Exception e)
142
    {
143
      System.out.println("error with db connection in DataFileServer.updateDB" +
144
                         ": " + e.getMessage());
145
    }
146
    
147
  }
148
}
(22-22/43)