1 | package pkg.crawler.data.conn; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.ObjectInputStream; |
---|
5 | import java.net.ServerSocket; |
---|
6 | import java.net.Socket; |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.Properties; |
---|
9 | |
---|
10 | import javax.mail.Folder; |
---|
11 | import javax.mail.MessagingException; |
---|
12 | import javax.mail.Session; |
---|
13 | |
---|
14 | import com.sun.mail.imap.ACL; |
---|
15 | import com.sun.mail.imap.IMAPFolder; |
---|
16 | import com.sun.mail.imap.IMAPStore; |
---|
17 | import com.sun.mail.imap.Rights; |
---|
18 | import com.sun.mail.imap.Rights.Right; |
---|
19 | |
---|
20 | public class Crawler{ |
---|
21 | |
---|
22 | private String host; |
---|
23 | private String user; |
---|
24 | private String password; |
---|
25 | private String mbox; |
---|
26 | private String hostSolr; |
---|
27 | private ACL acl; |
---|
28 | |
---|
29 | public Crawler(String host, String password, String hostSolr) |
---|
30 | { |
---|
31 | this.host = host; |
---|
32 | this.user = "expresso-admin"; |
---|
33 | this.password = password; |
---|
34 | this.mbox = "user"; |
---|
35 | this.hostSolr = hostSolr; |
---|
36 | |
---|
37 | //Cria a ACL a ser configurada |
---|
38 | acl = new ACL("expresso-admin"); |
---|
39 | |
---|
40 | //Define direito de leitura |
---|
41 | Rights rights = new Rights(); |
---|
42 | rights.add(Right.READ); |
---|
43 | |
---|
44 | //Configura direito de leitura para a ACL |
---|
45 | acl.setRights(rights); |
---|
46 | } |
---|
47 | |
---|
48 | public void run() |
---|
49 | { |
---|
50 | |
---|
51 | int iAux0 = 0; |
---|
52 | while (iAux0 == 0) |
---|
53 | { |
---|
54 | iAux0 = 1; |
---|
55 | |
---|
56 | //Inicializando as propriedades default da máquina para conexão |
---|
57 | //com o servidor IMAP |
---|
58 | Properties props = System.getProperties(); |
---|
59 | Session session = Session.getInstance(props, null); |
---|
60 | |
---|
61 | IMAPStore store = new IMAPStore(session, null); |
---|
62 | |
---|
63 | try { |
---|
64 | |
---|
65 | //Conectando com o servidor IMAP |
---|
66 | store.connect(host, user, password); |
---|
67 | //Definindo a pasta base a ser usada |
---|
68 | IMAPFolder imapFolder = (IMAPFolder)store.getFolder(mbox); |
---|
69 | //Faz a iteração entre os usuários/pastas do usuário |
---|
70 | |
---|
71 | ArrayList<String> userMailBoxes = new ArrayList<String>(); |
---|
72 | |
---|
73 | for (Folder fINBOXAux : imapFolder.list()) |
---|
74 | { |
---|
75 | |
---|
76 | System.out.println(fINBOXAux.getFullName()); |
---|
77 | userMailBoxes.add(fINBOXAux.getFullName()); |
---|
78 | |
---|
79 | } |
---|
80 | ServerSocket server = null; |
---|
81 | ObjectInputStream ois = null; |
---|
82 | Socket connection = null; |
---|
83 | |
---|
84 | try { |
---|
85 | server = new ServerSocket(8090); |
---|
86 | |
---|
87 | while (userMailBoxes.size() > 0 ) |
---|
88 | { |
---|
89 | ProcessBuilder proc = new ProcessBuilder("java", "-jar", |
---|
90 | "-Xms2048m", "client.jar", this.host, |
---|
91 | userMailBoxes.get(0), this.password, this.hostSolr); |
---|
92 | |
---|
93 | try |
---|
94 | { |
---|
95 | proc.start(); |
---|
96 | System.out.println("Iniciou processo: "+ |
---|
97 | "java -jar client.jar "+ |
---|
98 | "\""+this.host+"\" \""+userMailBoxes.get(0)+"\" "+ |
---|
99 | "\""+this.password+"\" \""+this.hostSolr+"\""); |
---|
100 | } |
---|
101 | catch (IOException e) { |
---|
102 | // TODO Auto-generated catch block |
---|
103 | e.printStackTrace(); |
---|
104 | } |
---|
105 | catch (Exception e) { |
---|
106 | // TODO Auto-generated catch block |
---|
107 | e.printStackTrace(); |
---|
108 | } |
---|
109 | |
---|
110 | connection = server.accept(); |
---|
111 | |
---|
112 | ois = new ObjectInputStream(connection.getInputStream()); |
---|
113 | try { |
---|
114 | String socketMessage = (String)ois.readObject(); |
---|
115 | if(socketMessage.equals(userMailBoxes.get(0))) |
---|
116 | { |
---|
117 | System.out.println(userMailBoxes.get(0)); |
---|
118 | userMailBoxes.remove(0); |
---|
119 | } |
---|
120 | } catch (ClassNotFoundException e) { |
---|
121 | // TODO Auto-generated catch block |
---|
122 | e.printStackTrace(); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|
127 | catch (IOException e) |
---|
128 | { |
---|
129 | // TODO Auto-generated catch block |
---|
130 | e.printStackTrace(); |
---|
131 | } |
---|
132 | |
---|
133 | imapFolder.close(true); |
---|
134 | imapFolder = null; |
---|
135 | store.close(); |
---|
136 | store = null; |
---|
137 | |
---|
138 | } catch (MessagingException e) { |
---|
139 | System.out.println(e.getMessage()); |
---|
140 | e.printStackTrace(); |
---|
141 | } |
---|
142 | catch (Exception e) |
---|
143 | { |
---|
144 | System.out.println(e.getMessage()); |
---|
145 | e.printStackTrace(); |
---|
146 | } |
---|
147 | |
---|
148 | } |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|