1 |
655
|
berkley
|
/**
|
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$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
669
|
jones
|
*
|
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 |
655
|
berkley
|
*/
|
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 |
675
|
berkley
|
System.out.println("unknown host in DataFileUploadInterface.getSocket");
|
88 |
655
|
berkley
|
}
|
89 |
|
|
catch(IOException i)
|
90 |
|
|
{
|
91 |
|
|
//an ioexception is thrown if the port is not in use
|
92 |
|
|
//System.out.println("socket not connected");
|
93 |
|
|
return s;
|
94 |
|
|
}
|
95 |
|
|
return s;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
/**
|
99 |
|
|
* returns true if the port specified is not in use. false otherwise
|
100 |
|
|
*/
|
101 |
|
|
public static boolean portIsAvailable(int port)
|
102 |
|
|
{
|
103 |
|
|
Socket s;
|
104 |
|
|
String host = "localhost";
|
105 |
|
|
try
|
106 |
|
|
{
|
107 |
|
|
s = new Socket(host, port);
|
108 |
|
|
//s.close();
|
109 |
|
|
//we could create a socket on this port so the port is not available
|
110 |
|
|
//System.out.println("socket not available");
|
111 |
|
|
return false;
|
112 |
|
|
}
|
113 |
|
|
catch(UnknownHostException u)
|
114 |
|
|
{
|
115 |
675
|
berkley
|
System.out.println("unknow host error in " +
|
116 |
|
|
"DataFileUploadInterface.portIsAvailable");
|
117 |
655
|
berkley
|
}
|
118 |
|
|
catch(IOException i)
|
119 |
|
|
{
|
120 |
|
|
//an ioexception is thrown if the port is not in use
|
121 |
|
|
//System.out.println("socket available");
|
122 |
|
|
return true;
|
123 |
|
|
}
|
124 |
|
|
return true;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Updates xml_documents with the new data document information
|
129 |
|
|
* @param accnum the accession number of the new data file
|
130 |
|
|
* @param filename the filename of the new data file
|
131 |
|
|
* @param userOwner the document's owner's username
|
132 |
|
|
*/
|
133 |
|
|
public void updateDB(String accnum, String filename, String userOwner)
|
134 |
|
|
{
|
135 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
|
136 |
|
|
java.util.Date localtime = new java.util.Date();
|
137 |
|
|
String dateString = formatter.format(localtime);
|
138 |
|
|
|
139 |
|
|
String sqlDateString = "to_date('" + dateString + "', 'YY-MM-DD HH24:MI:SS')";
|
140 |
|
|
|
141 |
|
|
StringBuffer sql = new StringBuffer();
|
142 |
|
|
sql.append("insert into xml_documents (docid, docname, doctype, ");
|
143 |
|
|
sql.append("user_owner, user_updated, server_location, rev, date_created");
|
144 |
|
|
sql.append(", date_updated, public_access) values ('");
|
145 |
|
|
sql.append(accnum).append("','").append(filename).append("','BIN','");
|
146 |
|
|
sql.append(userOwner).append("','").append(userOwner).append("','1','");
|
147 |
|
|
sql.append("1',").append(sqlDateString).append(",");
|
148 |
|
|
sql.append(sqlDateString).append(",'0')");
|
149 |
|
|
//System.out.println("sql: " + sql.toString());
|
150 |
|
|
try
|
151 |
|
|
{
|
152 |
|
|
Connection conn = util.openDBConnection();
|
153 |
|
|
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
|
154 |
|
|
pstmt.execute();
|
155 |
667
|
berkley
|
pstmt.close();
|
156 |
655
|
berkley
|
conn.close();
|
157 |
|
|
}
|
158 |
|
|
catch(Exception e)
|
159 |
|
|
{
|
160 |
|
|
System.out.println("error with db connection in DataFileServer.updateDB" +
|
161 |
|
|
": " + e.getMessage());
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
}
|
165 |
|
|
}
|