1 | |
package org.paneris.jammyjoes.upload; |
2 | |
|
3 | |
import java.text.SimpleDateFormat; |
4 | |
import java.util.Calendar; |
5 | |
import java.util.Date; |
6 | |
|
7 | |
import org.melati.LogicalDatabase; |
8 | |
|
9 | |
import org.melati.poem.AccessToken; |
10 | |
import org.melati.poem.PoemTask; |
11 | |
import org.melati.util.DatabaseInitException; |
12 | |
import org.paneris.jammyjoes.model.JammyjoesDatabase; |
13 | |
|
14 | |
import com.cqs.ftp.FTP; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class Warden implements Runnable { |
32 | |
|
33 | |
private JammyjoesDatabase database; |
34 | 0 | private WardenLauncher launcher = null; |
35 | |
|
36 | |
|
37 | 0 | SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy"); |
38 | |
private String uploadDir; |
39 | |
|
40 | 0 | private static String databaseName = "jammyjoes"; |
41 | 0 | private static long pausetime = 1000 * 60 * 60; |
42 | 0 | private static int runHour = 19; |
43 | 0 | private static String ftphost = "hedwig.google.com"; |
44 | 0 | private static String ftpuser = "timj"; |
45 | 0 | private static String ftppass = "camel55"; |
46 | 0 | private String filename = "jammyjoes.txt"; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public Warden(WardenLauncher launcher) { |
56 | 0 | this.launcher = launcher; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public void run() { |
71 | 0 | if (database == null) { |
72 | |
try { |
73 | 0 | database = (JammyjoesDatabase) LogicalDatabase.getDatabase(databaseName); |
74 | 0 | } catch (DatabaseInitException e) { |
75 | 0 | e.printStackTrace(); |
76 | 0 | } |
77 | |
} |
78 | 0 | final Thread myThread = Thread.currentThread(); |
79 | |
|
80 | |
|
81 | 0 | while (launcher.warden == myThread) { |
82 | 0 | database.inSession(AccessToken.root, new PoemTask() { |
83 | 0 | public void run() { |
84 | 0 | uploadDir = database.getSettingTable().get("UploadDir") + "/../"; |
85 | 0 | doUploading(); |
86 | 0 | } |
87 | |
}); |
88 | |
try { |
89 | |
|
90 | 0 | Thread.sleep(pausetime); |
91 | 0 | } catch (InterruptedException e) { |
92 | 0 | } |
93 | |
} |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
void doUploading() { |
104 | |
try { |
105 | 0 | Calendar rightNow = Calendar.getInstance(); |
106 | 0 | if (rightNow.get(Calendar.HOUR_OF_DAY) == runHour) { |
107 | |
|
108 | 0 | System.err.println(new Date() + " Generating files for JammyJoes"); |
109 | 0 | generateFiles(); |
110 | 0 | System.err.println(new Date() + " Generated files for JammyJoes"); |
111 | 0 | ftpUpload(); |
112 | 0 | System.err.println(new Date() + " Uploaded Product file to Froogle"); |
113 | |
} |
114 | 0 | } catch (Exception e) { |
115 | 0 | e.printStackTrace(System.err); |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | |
public void generateFiles() throws Exception { |
120 | 0 | Froogle froogle = new Froogle(database); |
121 | 0 | froogle.writeFile(uploadDir, filename); |
122 | 0 | } |
123 | |
|
124 | |
public void ftpUpload() { |
125 | 0 | FTP ftp = new FTP(ftphost, 21); |
126 | 0 | ftp.setLogFile(uploadDir + "/ftp.log"); |
127 | |
|
128 | 0 | int i = 0; |
129 | 0 | boolean connected = false; |
130 | 0 | while (!connected && i < 20) { |
131 | |
try { |
132 | 0 | Thread.sleep(1000*30); |
133 | 0 | } catch (InterruptedException e) { |
134 | 0 | e.printStackTrace(); |
135 | 0 | } |
136 | 0 | i++; |
137 | 0 | connected = connect(ftp); |
138 | |
} |
139 | 0 | if (connected) { |
140 | |
|
141 | 0 | ftp.setTransfer(FTP.TRANSFER_PASV); |
142 | 0 | ftp.setMode(FTP.MODE_AUTO); |
143 | |
|
144 | 0 | ftp.upload(uploadDir + "/" + filename, filename); |
145 | 0 | System.err.print(ftp.lastReply()); |
146 | 0 | if (ftp.lastCode() != FTP.CODE_TRANSFER_OK) { |
147 | 0 | System.err.println("error while uploading."); |
148 | 0 | ftp.disconnect(); |
149 | 0 | return; |
150 | |
} |
151 | |
|
152 | 0 | ftp.disconnect(); |
153 | 0 | System.err.print(ftp.lastReply()); |
154 | 0 | if (ftp.lastCode() != FTP.CODE_DISCONNECT_OK) { |
155 | 0 | System.err.println("disconnection failed."); |
156 | 0 | return; |
157 | |
} |
158 | |
|
159 | 0 | ftp.closeLogFile(); |
160 | 0 | System.err.println("OK."); |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
private boolean connect(FTP ftp) { |
165 | 0 | ftp.connect(); |
166 | 0 | System.err.print(ftp.lastReply()); |
167 | 0 | if (ftp.lastCode() != FTP.CODE_CONNECT_OK) { |
168 | 0 | System.err.println("Connection failed."); |
169 | 0 | return false; |
170 | |
} |
171 | |
|
172 | 0 | ftp.login(ftpuser, ftppass); |
173 | 0 | System.err.print(ftp.lastReply()); |
174 | 0 | if (ftp.lastCode() != FTP.CODE_LOGGEDIN) { |
175 | 0 | System.err.println("incorrect login."); |
176 | 0 | ftp.disconnect(); |
177 | 0 | return false; |
178 | |
} |
179 | 0 | return true; |
180 | |
|
181 | |
} |
182 | |
} |