001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.imap.schedule; 017 018 import java.util.Date; 019 import java.util.HashMap; 020 import java.util.List; 021 import java.util.Map; 022 023 import org.apache.commons.collections.CollectionUtils; 024 import org.apache.commons.collections.MapUtils; 025 import org.apache.commons.lang.StringUtils; 026 import org.apache.commons.lang.time.DateFormatUtils; 027 import org.apache.log4j.Logger; 028 029 import com.hs.mail.imap.mailbox.MailboxManager; 030 import com.hs.mail.imap.message.search.ComparisonKey; 031 import com.hs.mail.imap.message.search.CompositeKey; 032 import com.hs.mail.imap.message.search.InternalDateKey; 033 034 /** 035 * 036 * @author Won Chul Doh 037 * @since Oct 2, 2010 038 * 039 */ 040 public class MessageExpunger { 041 042 static Logger logger = Logger.getLogger(MessageExpunger.class); 043 044 private MailboxManager manager; 045 046 public MessageExpunger(MailboxManager manager) { 047 this.manager = manager; 048 } 049 050 public void expunge(String prop, long timeLimit) { 051 Map<String, Date> criteria = getExpungeCriteria(prop); 052 if (MapUtils.isNotEmpty(criteria)) { 053 expungeMailboxes(criteria, timeLimit); 054 } 055 } 056 057 private Map<String, Date> getExpungeCriteria(String prop) { 058 String[] tokens = StringUtils.split(prop); 059 int sz = tokens.length / 2; 060 if (sz > 0) { 061 Map<String, Date> criteria = new HashMap<String, Date>(sz); 062 for (int i = 0; i < tokens.length / 2; i++) { 063 Date base = ScheduleUtils.getDateBefore(tokens[i + 1]); 064 if (base != null) { 065 criteria.put(tokens[i], base); 066 } 067 } 068 return criteria; 069 } 070 return null; 071 } 072 073 private void expungeMailboxes(Map<String, Date> criteria, long timeLimit) { 074 for (String name : criteria.keySet()) { 075 if (logger.isDebugEnabled()) { 076 logger.debug("Expunging messages from " 077 + name 078 + " which are older than " 079 + DateFormatUtils.ISO_DATE_FORMAT.format(criteria 080 .get(name)) + "."); 081 } 082 List<Long> mailboxIdes = manager.getMailboxIDList(name); 083 if (CollectionUtils.isNotEmpty(mailboxIdes)) { 084 for (Long mailboxID : mailboxIdes) { 085 if (System.currentTimeMillis() >= timeLimit) { 086 return; 087 } 088 expungeMessages(mailboxID, criteria.get(name)); 089 } 090 } 091 } 092 } 093 094 private void expungeMessages(long mailboxID, Date date) { 095 List<Long> uids = manager.search(null, mailboxID, new CompositeKey( 096 new InternalDateKey(ComparisonKey.LT, date))); 097 if (CollectionUtils.isNotEmpty(uids)) { 098 for (Long uid : uids) { 099 manager.deleteMessage(uid); 100 } 101 } 102 } 103 104 }