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: jones $'
|
10
|
* '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
|
11
|
* '$Revision: 669 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.net.*;
|
31
|
import java.io.*;
|
32
|
import java.util.Properties;
|
33
|
import java.util.Date;
|
34
|
import java.text.SimpleDateFormat;
|
35
|
import java.sql.*;
|
36
|
|
37
|
abstract class DataFileUploadInterface extends Thread
|
38
|
{
|
39
|
private MetaCatUtil util = new MetaCatUtil();
|
40
|
private static String filedir = "";
|
41
|
private String user = null;
|
42
|
private String sess_id = null;
|
43
|
private int port;
|
44
|
protected Socket s;
|
45
|
|
46
|
/**
|
47
|
* Sets the port, user and sess_id
|
48
|
*/
|
49
|
DataFileUploadInterface(int port, String user, String sess_id)
|
50
|
{
|
51
|
this.sess_id = sess_id;
|
52
|
this.user = user;
|
53
|
this.port = port;
|
54
|
filedir = util.getOption("datafilepath");
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Override this method with the code to handle the socket connection to
|
59
|
* the port specified in the constructor.
|
60
|
*/
|
61
|
abstract void getFile(int port, String user, String sess_id);
|
62
|
|
63
|
/**
|
64
|
* This method is invoked when this class is broken off into a new thread
|
65
|
*/
|
66
|
public void run ()
|
67
|
{
|
68
|
getFile(port, user, sess_id);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* attempts to connect a socket, returns null if it is not successful
|
73
|
* returns the connected socket if it is successful.
|
74
|
*/
|
75
|
public static Socket getSocket(String host, int port)
|
76
|
{
|
77
|
Socket s = null;
|
78
|
try
|
79
|
{
|
80
|
s = new Socket(host, port);
|
81
|
//we could create a socket on this port so the port is not available
|
82
|
//System.out.println("socket connnected");
|
83
|
return s;
|
84
|
}
|
85
|
catch(UnknownHostException u)
|
86
|
{
|
87
|
}
|
88
|
catch(IOException i)
|
89
|
{
|
90
|
//an ioexception is thrown if the port is not in use
|
91
|
//System.out.println("socket not connected");
|
92
|
return s;
|
93
|
}
|
94
|
return s;
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* returns true if the port specified is not in use. false otherwise
|
99
|
*/
|
100
|
public static boolean portIsAvailable(int port)
|
101
|
{
|
102
|
Socket s;
|
103
|
String host = "localhost";
|
104
|
try
|
105
|
{
|
106
|
s = new Socket(host, port);
|
107
|
//s.close();
|
108
|
//we could create a socket on this port so the port is not available
|
109
|
//System.out.println("socket not available");
|
110
|
return false;
|
111
|
}
|
112
|
catch(UnknownHostException u)
|
113
|
{
|
114
|
//it better know localhost!
|
115
|
}
|
116
|
catch(IOException i)
|
117
|
{
|
118
|
//an ioexception is thrown if the port is not in use
|
119
|
//System.out.println("socket available");
|
120
|
return true;
|
121
|
}
|
122
|
return true;
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Updates xml_documents with the new data document information
|
127
|
* @param accnum the accession number of the new data file
|
128
|
* @param filename the filename of the new data file
|
129
|
* @param userOwner the document's owner's username
|
130
|
*/
|
131
|
public void updateDB(String accnum, String filename, String userOwner)
|
132
|
{
|
133
|
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
|
134
|
java.util.Date localtime = new java.util.Date();
|
135
|
String dateString = formatter.format(localtime);
|
136
|
|
137
|
String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH24:MI:SS')";
|
138
|
|
139
|
StringBuffer sql = new StringBuffer();
|
140
|
sql.append("insert into xml_documents (docid, docname, doctype, ");
|
141
|
sql.append("user_owner, user_updated, server_location, rev, date_created");
|
142
|
sql.append(", date_updated, public_access) values ('");
|
143
|
sql.append(accnum).append("','").append(filename).append("','BIN','");
|
144
|
sql.append(userOwner).append("','").append(userOwner).append("','1','");
|
145
|
sql.append("1',").append(sqlDateString).append(",");
|
146
|
sql.append(sqlDateString).append(",'0')");
|
147
|
//System.out.println("sql: " + sql.toString());
|
148
|
try
|
149
|
{
|
150
|
Connection conn = util.openDBConnection();
|
151
|
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
|
152
|
pstmt.execute();
|
153
|
pstmt.close();
|
154
|
conn.close();
|
155
|
}
|
156
|
catch(Exception e)
|
157
|
{
|
158
|
System.out.println("error with db connection in DataFileServer.updateDB" +
|
159
|
": " + e.getMessage());
|
160
|
}
|
161
|
|
162
|
}
|
163
|
}
|