1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.configuration.Configuration; 22 import org.apache.avalon.framework.configuration.ConfigurationException; 23 import org.apache.james.Constants; 24 import org.apache.james.services.UsersRepository; 25 import org.apache.james.services.UsersStore; 26 import org.apache.james.transport.mailets.listservcommands.ErrorCommand; 27 import org.apache.james.transport.mailets.listservcommands.IListServCommand; 28 import org.apache.james.util.XMLResources; 29 import org.apache.mailet.GenericMailet; 30 import org.apache.mailet.Mail; 31 import org.apache.mailet.MailAddress; 32 33 import javax.mail.MessagingException ; 34 import java.io.File ; 35 import java.lang.reflect.Field ; 36 import java.util.*; 37 38 91 public class CommandListservManager extends GenericMailet implements ICommandListservManager { 92 93 protected Map commandMap = new HashMap(); 94 protected List commandPackages = new ArrayList(); 95 protected UsersRepository usersRepository; 96 protected String listName; 97 protected String displayName; 98 protected String listOwner; 99 protected String listDomain; 100 protected XMLResources xmlResources; 101 102 110 public String getListName(boolean displayFormat) { 111 return displayFormat ? displayName : listName; 112 } 113 114 121 public String getListOwner() { 122 return listOwner; 123 } 124 125 132 public String getListDomain() { 133 return listDomain; 134 } 135 136 153 public IListServCommand getCommand(String name) { 154 return (IListServCommand) commandMap.get(name.toLowerCase()); 155 } 156 157 162 public Map getCommands() { 163 return commandMap; 164 } 165 166 170 public UsersRepository getUsersRepository() { 171 return usersRepository; 172 } 173 174 180 public void onError(Mail mail, String subject, String errorMessage) throws MessagingException { 181 ErrorCommand errorCommand = (ErrorCommand) getCommand("error"); 182 errorCommand.onError(mail, subject, errorMessage); 183 } 184 185 188 public String getResourcesFile() { 189 return getInitParameter("resources"); 190 } 191 192 196 public Properties getStandardProperties() { 197 Properties standardProperties = new Properties(); 198 standardProperties.put("LIST_NAME", getListName(false)); 199 standardProperties.put("DISPLAY_NAME", getListName(true)); 200 standardProperties.put("DOMAIN_NAME", getListDomain()); 201 return standardProperties; 202 } 203 204 210 public XMLResources[] initXMLResources(String [] names) throws ConfigurationException { 211 try { 212 File xmlFile = new File (getResourcesFile()); 213 214 Properties props = getStandardProperties(); 215 String listName = props.getProperty("LIST_NAME"); 216 217 XMLResources[] xmlResources = new XMLResources[names.length]; 218 for (int index = 0; index < names.length; index++) { 219 xmlResources[index] = new XMLResources(); 220 xmlResources[index].init(xmlFile, names[index], listName, props); 221 } 222 return xmlResources; 223 } catch (Exception e) { 224 log(e.getMessage(), e); 225 throw new ConfigurationException("Can't initialize:", e); 226 } 227 } 228 229 public void init() throws MessagingException { 230 231 try { 232 Configuration configuration = (Configuration) getField(getMailetConfig(), "configuration"); 235 236 listName = configuration.getChild("listName").getValue(); 238 displayName = configuration.getChild("displayName").getValue(); 239 listOwner = configuration.getChild("listOwner").getValue(); 240 listDomain = configuration.getChild("listDomain").getValue(); 241 242 initializeResources(); 244 245 initUsersRepository(); 247 248 loadCommandPackages(configuration); 250 251 loadCommands(configuration); 253 254 getMailetContext().setAttribute(ICommandListservManager.ID + listName, this); 256 } catch (Exception e) { 257 throw new MessagingException (e.getMessage(), e); 258 } 259 } 260 261 266 public IListServCommand getCommandTarget(MailAddress mailAddress) { 267 String commandName = getCommandName(mailAddress); 268 return getCommand(commandName); 269 } 270 271 282 public void service(Mail mail) throws MessagingException { 283 if (mail.getRecipients().size() != 1) { 284 getMailetContext().bounce(mail, "You can only send one command at a time to this listserv manager."); 285 return; 286 } 287 MailAddress mailAddress = (MailAddress) mail.getRecipients().iterator().next(); 288 IListServCommand command = getCommandTarget(mailAddress); 289 290 if (command == null) { 291 onError(mail, 293 "unkown command", 294 xmlResources.getString("command.not.understood", getStandardProperties())); 295 } 296 command.onCommand(mail); 297 298 mail.setState(Mail.GHOST); 299 } 300 301 306 protected String getCommandName(MailAddress mailAddress) { 307 String user = mailAddress.getUser(); 308 int index = user.indexOf('-'); 309 String commandName = user.substring(++index); 310 return commandName; 311 } 312 313 317 protected void initializeResources() throws Exception { 318 xmlResources = initXMLResources(new String []{"List Manager"})[0]; 319 } 320 321 324 protected void initUsersRepository() { 325 ComponentManager compMgr = (ComponentManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 326 try { 327 UsersStore usersStore = (UsersStore) compMgr.lookup("org.apache.james.services.UsersStore"); 328 String repName = getInitParameter("repositoryName"); 329 330 usersRepository = usersStore.getRepository(repName); 331 } catch (Exception e) { 332 log("Failed to retrieve Store component:" + e.getMessage()); 333 } 334 } 335 336 341 protected void loadCommands(Configuration configuration) throws Exception { 342 final Configuration commandConfigurations = configuration.getChild("commands"); 343 final Configuration[] commandConfs = commandConfigurations.getChildren("command"); 344 for (int index = 0; index < commandConfs.length; index++) { 345 Configuration commandConf = commandConfs[index]; 346 String commandName = commandConf.getAttribute("name").toLowerCase(); 347 String className = commandConf.getAttribute("class"); 348 loadCommand(commandName, className, commandConf); 349 } 350 } 351 352 360 protected void loadCommand(String commandName, 361 String className, 362 Configuration configuration) 363 throws ConfigurationException, ClassNotFoundException , IllegalAccessException , InstantiationException { 364 ClassLoader theClassLoader = getClass().getClassLoader(); 365 for (Iterator it = commandPackages.iterator(); it.hasNext();) { 366 String packageName = (String ) it.next(); 367 368 IListServCommand listServCommand = null; 369 try { 370 listServCommand = (IListServCommand) theClassLoader.loadClass(packageName + className).newInstance(); 371 } catch (Exception e) { 372 continue; 374 } 375 listServCommand.init(this, configuration); 376 commandMap.put(commandName, listServCommand); 377 return; 378 } 379 380 throw new ConfigurationException("Unable to load listservcommand: " + commandName); 381 } 382 383 389 protected void loadCommandPackages(Configuration configuration) throws ConfigurationException { 390 commandPackages.add(""); 391 final Configuration packageConfiguration = configuration.getChild("commandpackages"); 392 final Configuration[] pkgConfs = packageConfiguration.getChildren("commandpackage"); 393 for (int index = 0; index < pkgConfs.length; index++) { 394 Configuration conf = pkgConfs[index]; 395 String packageName = conf.getValue().trim(); 396 if (!packageName.endsWith(".")) { 397 packageName += "."; 398 } 399 commandPackages.add(packageName); 400 } 401 } 402 403 407 protected static Object getField(Object instance, String name) throws IllegalAccessException { 408 Class clazz = instance.getClass(); 409 Field [] fields; 410 while (clazz != null) { 411 fields = clazz.getDeclaredFields(); 412 for (int index = 0; index < fields.length; index++) { 413 Field field = fields[index]; 414 if (field.getName().equals(name)) { 415 field.setAccessible(true); 416 return field.get(instance); 417 } 418 } 419 clazz = clazz.getSuperclass(); 420 } 421 422 return null; 423 } 424 } 425 | Popular Tags |