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 package org.apache.james.transport.mailets; 21 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.james.Constants; 25 import org.apache.james.services.UsersRepository; 26 import org.apache.james.services.UsersStore; 27 import org.apache.mailet.MailAddress; 28 29 /*** 30 * Adds or removes an email address to a listserv. 31 * 32 * <p>Sample configuration: 33 * <br><mailet match="CommandForListserv=james@list.working-dogs.com" class="AvalonListservManager"> 34 * <br><repositoryName>name of user repository configured in UsersStore block </repositoryName> 35 * <br></mailet> 36 * 37 * @version This is $Revision: 494012 $ 38 */ 39 public class AvalonListservManager extends GenericListservManager { 40 41 private UsersRepository members; 42 43 /*** 44 * Initialize the mailet 45 */ 46 public void init() { 47 ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 48 try { 49 UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE); 50 String repName = getInitParameter("repositoryName"); 51 52 members = (UsersRepository) usersStore.getRepository(repName); 53 } catch (ServiceException cnfe) { 54 log("Failed to retrieve Store component:" + cnfe.getMessage()); 55 } catch (Exception e) { 56 log("Failed to retrieve Store component:" + e.getMessage()); 57 } 58 } 59 60 /*** 61 * Add an address to the list. 62 * 63 * @param address the address to add 64 * 65 * @return true if successful, false otherwise 66 */ 67 public boolean addAddress(MailAddress address) { 68 members.addUser(address.toString(), ""); 69 return true; 70 } 71 72 /*** 73 * Remove an address from the list. 74 * 75 * @param address the address to remove 76 * 77 * @return true if successful, false otherwise 78 */ 79 public boolean removeAddress(MailAddress address) { 80 members.removeUser(address.toString()); 81 return true; 82 } 83 84 public boolean existsAddress(MailAddress address) { 85 return members.contains(address.toString()); 86 } 87 88 /*** 89 * Return a string describing this mailet. 90 * 91 * @return a string describing this mailet 92 */ 93 public String getMailetInfo() { 94 return "AvalonListservManager Mailet"; 95 } 96 } 97