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 initialContext context variables passed to each execution context 047 */ 048 public void visit(Folder folder, ExecutionContext initialContext) throws MessagingException { 049 for (Message message : folder.getMessages()) { 050 ExecutionContext context = new ExecutionContext(message, initialContext); 051 for (MessageAction action : pipe) { 052 action.reset(context); 053 boolean result = action.execute(context); 054 if (!result) { 055 break; 056 } 057 } 058 } 059 Folder[] folders = {}; 060 try { 061 folders = folder.list(); 062 } catch (MessagingException e) { 063 // do, nothing, list() not implemented. 064 } 065 for (Folder f : folders) { 066 visit(f, initialContext); 067 } 068 } 069 070 /** 071 * Visit given messages 072 * 073 * @param initialContext context variables passed to each execution context 074 */ 075 public void visit(Message[] messages, ExecutionContext initialContext) throws MessagingException { 076 for (Message message : messages) { 077 ExecutionContext context = new ExecutionContext(message, initialContext); 078 for (MessageAction action : pipe) { 079 action.reset(context); 080 boolean result = action.execute(context); 081 if (!result) { 082 break; 083 } 084 } 085 } 086 } 087 088}