package pkg.crawler.data.conn; import java.io.IOException; import java.io.ObjectInputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Properties; import javax.mail.Folder; import javax.mail.MessagingException; import javax.mail.Session; import com.sun.mail.imap.ACL; import com.sun.mail.imap.IMAPFolder; import com.sun.mail.imap.IMAPStore; import com.sun.mail.imap.Rights; import com.sun.mail.imap.Rights.Right; public class Crawler{ private String host; private String user; private String password; private String mbox; private String hostSolr; private ACL acl; public Crawler(String host, String password, String hostSolr) { this.host = host; this.user = "expresso-admin"; this.password = password; this.mbox = "user"; this.hostSolr = hostSolr; //Cria a ACL a ser configurada acl = new ACL("expresso-admin"); //Define direito de leitura Rights rights = new Rights(); rights.add(Right.READ); //Configura direito de leitura para a ACL acl.setRights(rights); } public void run() { int iAux0 = 0; while (iAux0 == 0) { iAux0 = 1; //Inicializando as propriedades default da máquina para conexão //com o servidor IMAP Properties props = System.getProperties(); Session session = Session.getInstance(props, null); IMAPStore store = new IMAPStore(session, null); try { //Conectando com o servidor IMAP store.connect(host, user, password); //Definindo a pasta base a ser usada IMAPFolder imapFolder = (IMAPFolder)store.getFolder(mbox); //Faz a iteração entre os usuários/pastas do usuário ArrayList userMailBoxes = new ArrayList(); for (Folder fINBOXAux : imapFolder.list()) { System.out.println(fINBOXAux.getFullName()); userMailBoxes.add(fINBOXAux.getFullName()); } ServerSocket server = null; ObjectInputStream ois = null; Socket connection = null; try { server = new ServerSocket(8090); while (userMailBoxes.size() > 0 ) { ProcessBuilder proc = new ProcessBuilder("java", "-jar", "-Xms2048m", "client.jar", this.host, userMailBoxes.get(0), this.password, this.hostSolr); try { proc.start(); System.out.println("Iniciou processo: "+ "java -jar client.jar "+ "\""+this.host+"\" \""+userMailBoxes.get(0)+"\" "+ "\""+this.password+"\" \""+this.hostSolr+"\""); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } connection = server.accept(); ois = new ObjectInputStream(connection.getInputStream()); try { String socketMessage = (String)ois.readObject(); if(socketMessage.equals(userMailBoxes.get(0))) { System.out.println(userMailBoxes.get(0)); userMailBoxes.remove(0); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } imapFolder.close(true); imapFolder = null; store.close(); store = null; } catch (MessagingException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } } }