1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 21 22 package org.apache.james.transport.mailets; 23 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.james.Constants; 27 import org.apache.james.api.user.UsersRepository; 28 import org.apache.james.api.user.UsersStore; 29 import org.apache.mailet.MailAddress; 30 31 /** 32 * Adds or removes an email address to a listserv. 33 * 34 * <p>Sample configuration: 35 * <br><mailet match="CommandForListserv=james@list.working-dogs.com" class="AvalonListservManager"> 36 * <br><repositoryName>name of user repository configured in UsersStore block </repositoryName> 37 * <br></mailet> 38 * 39 * @version This is $Revision: 684466 $ 40 */ 41 public class AvalonListservManager extends GenericListservManager { 42 43 private UsersRepository members; 44 45 /** 46 * Initialize the mailet 47 */ 48 public void init() { 49 ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 50 try { 51 UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE); 52 String repName = getInitParameter("repositoryName"); 53 54 members = (UsersRepository) usersStore.getRepository(repName); 55 } catch (ServiceException cnfe) { 56 log("Failed to retrieve Store component:" + cnfe.getMessage()); 57 } catch (Exception e) { 58 log("Failed to retrieve Store component:" + e.getMessage()); 59 } 60 } 61 62 /** 63 * Add an address to the list. 64 * 65 * @param address the address to add 66 * 67 * @return true if successful, false otherwise 68 */ 69 public boolean addAddress(MailAddress address) { 70 members.addUser(address.toString(), ""); 71 return true; 72 } 73 74 /** 75 * Remove an address from the list. 76 * 77 * @param address the address to remove 78 * 79 * @return true if successful, false otherwise 80 */ 81 public boolean removeAddress(MailAddress address) { 82 members.removeUser(address.toString()); 83 return true; 84 } 85 86 public boolean existsAddress(MailAddress address) { 87 return members.contains(address.toString()); 88 } 89 90 /** 91 * Return a string describing this mailet. 92 * 93 * @return a string describing this mailet 94 */ 95 public String getMailetInfo() { 96 return "AvalonListservManager Mailet"; 97 } 98 } 99