1 package org.opensync.tools;
2
3 import java.io.IOException;
4 import java.io.InterruptedIOException;
5 import java.io.File;
6 import java.net.MalformedURLException;
7
8 import org.opensync.tools.Constants;
9 import org.opensync.tools.Utils;
10
11 /***
12 * Connect to url, send data, and recieve data insided a timed thread
13 * so we don't block indefinitely if url has a socket issue.
14 * @author Keith Stumpf
15 */
16 public class DeleteFile
17 {
18
19 /***
20 *
21 */
22 public DeleteFile()
23 {
24 }
25
26 /***
27 * Attempts to delete a file
28 *
29 * @param sFileName Filename to delete
30 * @param lSleepBeforeDelete Amount of time to sleep before deleting file.
31 * @param lTimeoutInterval Amount of time in milliseconds to timeout request.
32 * @param lSleepTime Amount of time to sleep in between checking timeout interval.
33 * @exception MalformedURLException If there is a URL error
34 * @exception InterruptedIOException If there is an interupt sent to the thread.
35 * @exception IOException If there is an I/O error
36 */
37 public void go(String sFileName,
38 long lSleepBeforeDelete,
39 long lTimeoutInterval,
40 long lSleepTime)
41 throws Exception, InterruptedIOException, IOException, MalformedURLException
42 {
43
44
45
46
47
48
49 DeleteFileThread deleteFileThread = new DeleteFileThread(sFileName,lSleepBeforeDelete);
50 deleteFileThread.start();
51
52
53 if (lTimeoutInterval == 0){
54 return;
55 }
56 long lTimer = 0;
57 for (;;)
58 {
59
60 if (deleteFileThread.isFinished())
61 {
62
63
64 break;
65 }
66 else
67 {
68
69 if (deleteFileThread.isError())
70 {
71
72 throw (deleteFileThread.getException());
73 }
74 try
75 {
76
77 Thread.sleep (lSleepTime);
78 }
79 catch (InterruptedException ie) {}
80
81
82 lTimer += lSleepTime;
83
84
85 if (lTimer > lTimeoutInterval)
86 {
87 String sErrorText = "Timed out trying to delete file="+sFileName+ " within " + lTimeoutInterval + " milliseconds";
88 throw new Exception(sErrorText);
89 }
90 }
91 }
92
93 }
94
95 /***
96 * Inner thread class which actually does the file delete
97 * @author Keith Stumpf
98 */
99 class DeleteFileThread extends Thread
100 {
101
102
103
104 private Exception m_exception = null;
105
106 boolean m_bFinished = false;
107
108 String m_sFilename;
109 long m_lSleepBeforeDelete;
110
111
112 public DeleteFileThread (String sFilename, long lSleepBeforeDelete )
113 {
114
115 m_sFilename = sFilename;
116 m_lSleepBeforeDelete = lSleepBeforeDelete;
117 }
118
119 public void run()
120 {
121 if (Utils.debug) System.out.println("DeleteFileThread::run - begin="+m_sFilename+" Thread name="+getName());
122 try
123 {
124 m_bFinished = false;
125 if (m_sFilename == null || m_sFilename.equals("")){
126 m_bFinished = true;
127 if (Utils.debug) System.out.println("DeleteFileThread::run - end - no file to delete.");
128 return;
129 }
130
131
132 if (Constants.DELETE_FILES_IND){
133 try{
134 Thread.currentThread().sleep(m_lSleepBeforeDelete);
135 }catch(InterruptedException e){ }
136 if (m_sFilename != null && !m_sFilename.equals("")){
137 File fileImage = new File (m_sFilename);
138 if (Utils.debug) System.out.println("DeleteFileThread::run - deleted file="+m_sFilename+" Thread name="+getName());
139 fileImage.delete();
140 }
141 }
142 m_bFinished = true;
143 }
144 catch (Exception e)
145 {
146
147 m_exception = e;
148 return;
149 }
150 if (Utils.debug) System.out.println("DeleteFileThread::run - end");
151 }
152
153
154 public boolean isFinished()
155 {
156 return m_bFinished;
157 }
158
159
160 public boolean isError()
161 {
162 if (m_exception == null)
163 return false;
164 else
165 return true;
166 }
167
168
169 public Exception getException()
170 {
171 return m_exception;
172 }
173 }
174 }
175