View Javadoc

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      //long lTimeoutInterval = new Long(sTimeoutInterval).longValue();
44      //long lSleepTime       = new Long(sSleepTime).longValue();
45      //long lSleepBeforeDelete       = new Long(nSleepBeforeDelete).longValue();
46      //String sResult="";
47  
48      // Create a new socket thread, and start it running
49      DeleteFileThread deleteFileThread = new DeleteFileThread(sFileName,lSleepBeforeDelete);
50      deleteFileThread.start();
51  
52      // Don't wait if timeout interval is zero.
53      if (lTimeoutInterval == 0){
54        return;
55      }
56      long lTimer = 0;
57      for (;;)
58      {
59        // Check to see if a thread is finished
60        if (deleteFileThread.isFinished())
61        {
62          // Yes ...  assign result data to thread and break out of loop
63          //sResult = DeleteFileThread.getResult();
64          break;
65        }
66        else
67        {
68          // Check to see if an error occurred
69          if (deleteFileThread.isError())
70          {
71            // No connection could be established
72            throw (deleteFileThread.getException());
73          }
74          try
75          {
76            // Sleep for a short period of time
77            Thread.sleep (lSleepTime);
78          }
79          catch (InterruptedException ie) {}
80  
81          // Increment timer
82          lTimer += lSleepTime;
83  
84          // Check to see if time limit exceeded
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      //return sResult;
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     // Exception in the event an exception was thrown
104     private Exception m_exception = null;
105     //
106     boolean m_bFinished = false;
107 
108     String m_sFilename;
109     long m_lSleepBeforeDelete;
110 
111     // Connect to the specified host and port number
112     public DeleteFileThread (String sFilename, long lSleepBeforeDelete )
113     {
114       // Assign to member variables
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         // Allow some time to render the html before deleting image files.
131         // Otherwise image files are gone before painting html in browser.
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         // Assign to our exception member variable
147         m_exception = e;
148         return;
149       }
150       if (Utils.debug) System.out.println("DeleteFileThread::run - end");
151     }
152 
153     // Are we finished?
154     public boolean isFinished()
155     {
156       return m_bFinished;
157     }
158 
159     // Did an error occur?
160     public boolean isError()
161     {
162       if (m_exception == null)
163         return false;
164       else
165         return true;
166     }
167 
168     // Get exception
169     public Exception getException()
170     {
171       return m_exception;
172     }
173   }
174 }
175