src/org/sonews/mlgw/MailPoller.java
changeset 37 74139325d305
parent 35 ed84c8bdd87b
     1.1 --- a/src/org/sonews/mlgw/MailPoller.java	Sun Aug 29 17:43:58 2010 +0200
     1.2 +++ b/src/org/sonews/mlgw/MailPoller.java	Sun Aug 29 18:17:37 2010 +0200
     1.3 @@ -42,110 +42,94 @@
     1.4  public class MailPoller extends AbstractDaemon
     1.5  {
     1.6  
     1.7 -  static class PasswordAuthenticator extends Authenticator
     1.8 -  {
     1.9 -    
    1.10 -    @Override
    1.11 -    public PasswordAuthentication getPasswordAuthentication()
    1.12 -    {
    1.13 -      final String username = 
    1.14 -        Config.inst().get(Config.MLPOLL_USER, "user");
    1.15 -      final String password = 
    1.16 -        Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    1.17 +	static class PasswordAuthenticator extends Authenticator
    1.18 +	{
    1.19  
    1.20 -      return new PasswordAuthentication(username, password);
    1.21 -    }
    1.22 -    
    1.23 -  }
    1.24 -  
    1.25 -  @Override
    1.26 -  public void run()
    1.27 -  {
    1.28 -    Log.get().info("Starting Mailinglist Poller...");
    1.29 -    int errors = 0;
    1.30 -    while(isRunning())
    1.31 -    {
    1.32 -      try
    1.33 -      {
    1.34 -        // Wait some time between runs. At the beginning has advantages,
    1.35 -        // because the wait is not skipped if an exception occurs.
    1.36 -        Thread.sleep(60000 * (errors + 1)); // one minute * errors
    1.37 -        
    1.38 -        final String host     = 
    1.39 -          Config.inst().get(Config.MLPOLL_HOST, "samplehost");
    1.40 -        final String username = 
    1.41 -          Config.inst().get(Config.MLPOLL_USER, "user");
    1.42 -        final String password = 
    1.43 -          Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    1.44 -        
    1.45 -        Stats.getInstance().mlgwRunStart();
    1.46 -        
    1.47 -        // Create empty properties
    1.48 -        Properties props = System.getProperties();
    1.49 -        props.put("mail.pop3.host", host);
    1.50 -        props.put("mail.mime.address.strict", "false");
    1.51 +		@Override
    1.52 +		public PasswordAuthentication getPasswordAuthentication()
    1.53 +		{
    1.54 +			final String username =
    1.55 +				Config.inst().get(Config.MLPOLL_USER, "user");
    1.56 +			final String password =
    1.57 +				Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    1.58  
    1.59 -        // Get session
    1.60 -        Session session = Session.getInstance(props);
    1.61 +			return new PasswordAuthentication(username, password);
    1.62 +		}
    1.63 +	}
    1.64  
    1.65 -        // Get the store
    1.66 -        Store store = session.getStore("pop3");
    1.67 -        store.connect(host, 110, username, password);
    1.68 +	@Override
    1.69 +	public void run()
    1.70 +	{
    1.71 +		Log.get().info("Starting Mailinglist Poller...");
    1.72 +		int errors = 0;
    1.73 +		while (isRunning()) {
    1.74 +			try {
    1.75 +				// Wait some time between runs. At the beginning has advantages,
    1.76 +				// because the wait is not skipped if an exception occurs.
    1.77 +				Thread.sleep(60000 * (errors + 1)); // one minute * errors
    1.78  
    1.79 -        // Get folder
    1.80 -        Folder folder = store.getFolder("INBOX");
    1.81 -        folder.open(Folder.READ_WRITE);
    1.82 +				final String host =
    1.83 +					Config.inst().get(Config.MLPOLL_HOST, "samplehost");
    1.84 +				final String username =
    1.85 +					Config.inst().get(Config.MLPOLL_USER, "user");
    1.86 +				final String password =
    1.87 +					Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    1.88  
    1.89 -        // Get directory
    1.90 -        Message[] messages = folder.getMessages();
    1.91 +				Stats.getInstance().mlgwRunStart();
    1.92  
    1.93 -        // Dispatch messages and delete it afterwards on the inbox
    1.94 -        for(Message message : messages)
    1.95 -        {
    1.96 -          if(Dispatcher.toGroup(message)
    1.97 -            || Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false))
    1.98 -          {
    1.99 -            // Delete the message
   1.100 -            message.setFlag(Flag.DELETED, true);
   1.101 -          }
   1.102 -        }
   1.103 +				// Create empty properties
   1.104 +				Properties props = System.getProperties();
   1.105 +				props.put("mail.pop3.host", host);
   1.106 +				props.put("mail.mime.address.strict", "false");
   1.107  
   1.108 -        // Close connection 
   1.109 -        folder.close(true); // true to expunge deleted messages
   1.110 -        store.close();
   1.111 -        errors = 0;
   1.112 -        
   1.113 -        Stats.getInstance().mlgwRunEnd();
   1.114 -      }
   1.115 -      catch(NoSuchProviderException ex)
   1.116 -      {
   1.117 -        Log.get().severe(ex.toString());
   1.118 -        shutdown();
   1.119 -      }
   1.120 -      catch(AuthenticationFailedException ex)
   1.121 -      {
   1.122 -        // AuthentificationFailedException may be thrown if credentials are
   1.123 -        // bad or if the Mailbox is in use (locked).
   1.124 -        ex.printStackTrace();
   1.125 -        errors = errors < 5 ? errors + 1 : errors;
   1.126 -      }
   1.127 -      catch(InterruptedException ex)
   1.128 -      {
   1.129 -        System.out.println("sonews: " + this + " returns: " + ex);
   1.130 -        return;
   1.131 -      }
   1.132 -      catch(MessagingException ex)
   1.133 -      {
   1.134 -        ex.printStackTrace();
   1.135 -        errors = errors < 5 ? errors + 1 : errors;
   1.136 -      }
   1.137 -      catch(Exception ex)
   1.138 -      {
   1.139 -        ex.printStackTrace();
   1.140 -        errors = errors < 5 ? errors + 1 : errors;
   1.141 -      }
   1.142 -    }
   1.143 -    Log.get().severe("MailPoller exited.");
   1.144 -  }
   1.145 -  
   1.146 +				// Get session
   1.147 +				Session session = Session.getInstance(props);
   1.148 +
   1.149 +				// Get the store
   1.150 +				Store store = session.getStore("pop3");
   1.151 +				store.connect(host, 110, username, password);
   1.152 +
   1.153 +				// Get folder
   1.154 +				Folder folder = store.getFolder("INBOX");
   1.155 +				folder.open(Folder.READ_WRITE);
   1.156 +
   1.157 +				// Get directory
   1.158 +				Message[] messages = folder.getMessages();
   1.159 +
   1.160 +				// Dispatch messages and delete it afterwards on the inbox
   1.161 +				for (Message message : messages) {
   1.162 +					if (Dispatcher.toGroup(message)
   1.163 +						|| Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false)) {
   1.164 +						// Delete the message
   1.165 +						message.setFlag(Flag.DELETED, true);
   1.166 +					}
   1.167 +				}
   1.168 +
   1.169 +				// Close connection
   1.170 +				folder.close(true); // true to expunge deleted messages
   1.171 +				store.close();
   1.172 +				errors = 0;
   1.173 +
   1.174 +				Stats.getInstance().mlgwRunEnd();
   1.175 +			} catch (NoSuchProviderException ex) {
   1.176 +				Log.get().severe(ex.toString());
   1.177 +				shutdown();
   1.178 +			} catch (AuthenticationFailedException ex) {
   1.179 +				// AuthentificationFailedException may be thrown if credentials are
   1.180 +				// bad or if the Mailbox is in use (locked).
   1.181 +				ex.printStackTrace();
   1.182 +				errors = errors < 5 ? errors + 1 : errors;
   1.183 +			} catch (InterruptedException ex) {
   1.184 +				System.out.println("sonews: " + this + " returns: " + ex);
   1.185 +				return;
   1.186 +			} catch (MessagingException ex) {
   1.187 +				ex.printStackTrace();
   1.188 +				errors = errors < 5 ? errors + 1 : errors;
   1.189 +			} catch (Exception ex) {
   1.190 +				ex.printStackTrace();
   1.191 +				errors = errors < 5 ? errors + 1 : errors;
   1.192 +			}
   1.193 +		}
   1.194 +		Log.get().severe("MailPoller exited.");
   1.195 +	}
   1.196  }