001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.mail.action; 023 024import javax.mail.Folder; 025import javax.mail.Message; 026import javax.mail.MessagingException; 027 028/** 029 * @author Alexandre Russel 030 */ 031public class Visitor { 032 033 private final MessageActionPipe pipe; 034 035 public Visitor(MessageActionPipe pipe) { 036 this.pipe = pipe; 037 } 038 039 public void visit(Folder folder) throws MessagingException { 040 visit(folder, null); 041 } 042 043 /** 044 * Visit every message of given folder and every message of its subfolders. 045 * 046 * @param folder 047 * @param initialContext context variables passed to each execution context 048 */ 049 public void visit(Folder folder, ExecutionContext initialContext) throws MessagingException { 050 for (Message message : folder.getMessages()) { 051 ExecutionContext context = new ExecutionContext(message, initialContext); 052 for (MessageAction action : pipe) { 053 action.reset(context); 054 boolean result = action.execute(context); 055 if (!result) { 056 break; 057 } 058 } 059 } 060 Folder[] folders = {}; 061 try { 062 folders = folder.list(); 063 } catch (MessagingException e) { 064 // do, nothing, list() not implemented. 065 } 066 for (Folder f : folders) { 067 visit(f, initialContext); 068 } 069 } 070 071 /** 072 * Visit given messages 073 * 074 * @param messages 075 * @param initialContext context variables passed to each execution context 076 */ 077 public void visit(Message[] messages, ExecutionContext initialContext) throws MessagingException { 078 for (Message message : messages) { 079 ExecutionContext context = new ExecutionContext(message, initialContext); 080 for (MessageAction action : pipe) { 081 action.reset(context); 082 boolean result = action.execute(context); 083 if (!result) { 084 break; 085 } 086 } 087 } 088 } 089 090}