| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package net.sf.webphotos.util.legacy; |
| 17 | |
|
| 18 | |
import java.io.*; |
| 19 | |
import java.util.*; |
| 20 | |
import net.sf.webphotos.util.Util; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 0 | public class CacheFTP extends ArrayList<ComandoFTP> { |
| 31 | |
|
| 32 | |
private static final long serialVersionUID = -3489616830358888490L; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public static final int UPLOAD = 1; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public static final int DOWNLOAD = 2; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public static final int DELETE = 3; |
| 45 | 3 | private static File arquivoControle = new File("CacheFTP.txt"); |
| 46 | 3 | private static final CacheFTP instancia = new CacheFTP(); |
| 47 | |
|
| 48 | |
private CacheFTP() { |
| 49 | 3 | super(); |
| 50 | 3 | Util.log("Inicializando Cache FTP"); |
| 51 | 3 | Util.out.println(); |
| 52 | 3 | loadFile(); |
| 53 | 3 | sort(); |
| 54 | 3 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static CacheFTP getCache() { |
| 61 | 6 | return instancia; |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public void addCommand(int acao, int album, int foto) { |
| 71 | 75 | if (album == 0) { |
| 72 | 3 | return; |
| 73 | |
} |
| 74 | 72 | if (acao != UPLOAD && acao != DOWNLOAD && acao != DELETE) { |
| 75 | 6 | return; |
| 76 | |
} |
| 77 | |
|
| 78 | 66 | if ((this.add(new ComandoFTP(acao, album, foto)) == true)) { |
| 79 | 60 | sort(); |
| 80 | 60 | Util.log("Comando FTP adicionado. (" + acao + " " + album + " " + foto + ")"); |
| 81 | |
} else { |
| 82 | 6 | Util.log("Comando FTP é redundante. Não foi adicionado"); |
| 83 | |
} |
| 84 | 66 | } |
| 85 | |
|
| 86 | |
private void sort() { |
| 87 | 63 | Collections.<ComandoFTP>sort((List<ComandoFTP>) this); |
| 88 | 63 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public boolean add(ComandoFTP a) { |
| 99 | 102 | ComandoFTP b = a; |
| 100 | |
|
| 101 | |
|
| 102 | 102 | if (this.contains(a)) { |
| 103 | 9 | return false; |
| 104 | |
} |
| 105 | |
|
| 106 | 93 | int acao = b.getOperacao(); |
| 107 | 93 | int albumID = b.getAlbumID(); |
| 108 | 93 | int fotoID = b.getFotoID(); |
| 109 | |
|
| 110 | |
|
| 111 | 93 | if (!(acao == UPLOAD) |
| 112 | |
&& !(acao == DOWNLOAD) |
| 113 | |
&& !(acao == DELETE)) { |
| 114 | 0 | return false; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | 93 | Iterator<ComandoFTP> i = this.iterator(); |
| 119 | |
ComandoFTP l; |
| 120 | |
|
| 121 | |
|
| 122 | 93 | if (fotoID == 0) { |
| 123 | 18 | i = this.iterator(); |
| 124 | |
|
| 125 | 162 | while (i.hasNext()) { |
| 126 | 144 | l = i.next(); |
| 127 | |
|
| 128 | 144 | if ((l.getOperacao() == acao && l.getAlbumID() == albumID) |
| 129 | |
|| (acao == DELETE && l.getOperacao() == UPLOAD && l.getAlbumID() == albumID)) { |
| 130 | 6 | i.remove(); |
| 131 | |
} |
| 132 | |
|
| 133 | |
} |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | 93 | i = this.iterator(); |
| 139 | 492 | while (i.hasNext()) { |
| 140 | 405 | l = (ComandoFTP) i.next(); |
| 141 | 405 | if (!l.recebe(b)) { |
| 142 | 6 | return false; |
| 143 | |
} |
| 144 | |
} |
| 145 | 87 | super.add(b); |
| 146 | 87 | return true; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
private void loadFile() { |
| 151 | |
String linha; |
| 152 | |
|
| 153 | |
|
| 154 | 3 | if (arquivoControle.isFile() && arquivoControle.canRead()) { |
| 155 | |
try { |
| 156 | 3 | Util.out.println("load file: " + arquivoControle.getCanonicalPath()); |
| 157 | |
StringTokenizer tok; |
| 158 | 3 | BufferedReader entrada = new BufferedReader(new FileReader(arquivoControle)); |
| 159 | 3 | while ((linha = entrada.readLine()) != null) { |
| 160 | 0 | if (!linha.startsWith("#")) { |
| 161 | 0 | Util.out.println("processando linha:" + linha); |
| 162 | 0 | tok = new StringTokenizer(linha); |
| 163 | 0 | if (tok.countTokens() == 3) { |
| 164 | 0 | add(new ComandoFTP( |
| 165 | |
Integer.parseInt(tok.nextToken()), |
| 166 | |
Integer.parseInt(tok.nextToken()), |
| 167 | |
Integer.parseInt(tok.nextToken()))); |
| 168 | |
} |
| 169 | 0 | tok = null; |
| 170 | |
} |
| 171 | |
} |
| 172 | 3 | entrada.close(); |
| 173 | 3 | entrada = null; |
| 174 | 0 | } catch (IOException e) { |
| 175 | 0 | Util.log("[CacheFTP.loadFile]/ERRO:" + e.getMessage()); |
| 176 | 3 | } |
| 177 | |
} |
| 178 | 3 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public void saveFile() { |
| 187 | |
|
| 188 | |
BufferedWriter saida; |
| 189 | 6 | Util.out.println("escrevendo arquivo de Cache FTP: " + arquivoControle.getAbsolutePath()); |
| 190 | |
|
| 191 | 6 | if (!arquivoControle.isFile()) { |
| 192 | |
|
| 193 | |
try { |
| 194 | 0 | saida = new BufferedWriter(new FileWriter(arquivoControle)); |
| 195 | 0 | saida.write("# arquivo de envio / exclusão ftp - não deve ser alterado manualmente\r\n"); |
| 196 | 0 | saida.write("# 1-Upload (envio) 2-Download (recepção) 3-Delete (apagar)\r\n"); |
| 197 | 0 | saida.flush(); |
| 198 | |
|
| 199 | 0 | } catch (IOException e) { |
| 200 | 0 | Util.log("[CacheFTP.getArquivoSaida]/ERRO: não foi possível criar ou escrever no novo arquivo de controle de FTP " + e.getMessage()); |
| 201 | 0 | return; |
| 202 | 0 | } |
| 203 | |
|
| 204 | |
} else { |
| 205 | |
|
| 206 | 6 | if (!arquivoControle.canWrite()) { |
| 207 | 0 | Util.log("[CacheFTP.getArquivoSaida]/ERRO: o arquivo está protegido contra gravação"); |
| 208 | 0 | return; |
| 209 | |
} |
| 210 | |
|
| 211 | |
try { |
| 212 | 6 | saida = new BufferedWriter(new FileWriter(arquivoControle, false)); |
| 213 | 0 | } catch (Exception e) { |
| 214 | 0 | Util.log("[CacheFTP.getArquivoSaida]/ERRO: não foi possível abrir o arquivo para adição."); |
| 215 | 0 | return; |
| 216 | 6 | } |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | 6 | Iterator<ComandoFTP> i = this.iterator(); |
| 221 | |
ComandoFTP l; |
| 222 | |
|
| 223 | 6 | while (i.hasNext()) { |
| 224 | 0 | l = i.next(); |
| 225 | |
try { |
| 226 | 0 | saida.write(l.getOperacao() + " " + l.getAlbumID() + " " + l.getFotoID() + "\r\n"); |
| 227 | 0 | } catch (Exception e) { |
| 228 | 0 | Util.log("[CacheFTP.saveFile.1]/ERRO: " + e.getMessage()); |
| 229 | 0 | } |
| 230 | |
} |
| 231 | |
try { |
| 232 | 6 | saida.flush(); |
| 233 | 6 | saida.close(); |
| 234 | 0 | } catch (Exception e) { |
| 235 | 0 | Util.log("[CacheFTP.saveFile.2]/ERRO: " + e.getMessage()); |
| 236 | 6 | } |
| 237 | 6 | saida = null; |
| 238 | 6 | l = null; |
| 239 | 6 | i = null; |
| 240 | 6 | } |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
@Override |
| 247 | |
public String toString() { |
| 248 | 0 | Iterator<ComandoFTP> i = this.iterator(); |
| 249 | |
ComandoFTP l; |
| 250 | 0 | String t = this.size() + " comando(s)\n-------------------------------------\n"; |
| 251 | |
|
| 252 | 0 | while (i.hasNext()) { |
| 253 | 0 | l = i.next(); |
| 254 | 0 | t += l.getOperacao() + " " + l.getAlbumID() + " " + l.getFotoID() + "\n"; |
| 255 | |
} |
| 256 | 0 | return t + "-------------------------------------\n"; |
| 257 | |
} |
| 258 | |
} |
| 259 | |
|