📄 commandlistservmanager.java
字号:
try { File xmlFile = new File(getResourcesFile()); Properties props = getStandardProperties(); String listName = props.getProperty("LIST_NAME"); XMLResources[] xmlResources = new XMLResources[names.length]; for (int index = 0; index < names.length; index++) { xmlResources[index] = new XMLResources(); xmlResources[index].init(xmlFile, names[index], listName, props); } return xmlResources; } catch (Exception e) { log(e.getMessage(), e); throw new ConfigurationException("Can't initialize:", e); } } public void init() throws MessagingException { try { //Well, i want a more complex configuration structure //of my mailet, so i have to cheat... and cheat i will... Configuration configuration = (Configuration) getField(getMailetConfig(), "configuration"); //get name listName = configuration.getChild("listName").getValue(); displayName = configuration.getChild("displayName").getValue(); listOwner = configuration.getChild("listOwner").getValue(); listDomain = configuration.getChild("listDomain").getValue(); //initialize resources initializeResources(); //get users store initUsersRepository(); //get command packages loadCommandPackages(configuration); //load commands loadCommands(configuration); //register w/context getMailetContext().setAttribute(ICommandListservManager.ID + listName, this); } catch (Exception e) { throw new MessagingException(e.getMessage(), e); } } /** * Based on the to address get a valid or command or null * @param mailAddress * @return IListServCommand or null */ public IListServCommand getCommandTarget(MailAddress mailAddress) { String commandName = getCommandName(mailAddress); return getCommand(commandName); } /** * <p>Called by the mailet container to allow the mailet to process a * message.</p> * * <p>This method is declared abstract so subclasses must override it.</p> * * @param mail - the Mail object that contains the MimeMessage and * routing information * @throws MessagingException - if an exception occurs that interferes with the mailet's normal operation * occurred */ public void service(Mail mail) throws MessagingException { if (mail.getRecipients().size() != 1) { getMailetContext().bounce(mail, "You can only send one command at a time to this listserv manager."); return; } MailAddress mailAddress = (MailAddress) mail.getRecipients().iterator().next(); IListServCommand command = getCommandTarget(mailAddress); if (command == null) { //don't recognize the command Properties props = getStandardProperties(); props.setProperty("COMMAND", getCommandName(mailAddress)); onError(mail, "unknown command", xmlResources.getString("command.not.understood", props)); } else { command.onCommand(mail); } // onError or onCommand would have done the job, so regardless // of which get rid of this e-mail. This is something that we // should review, and decide if there is any reason to allow a // passthrough. mail.setState(Mail.GHOST); } /** * Get the name of the command * @param mailAddress * @return the name of the command */ protected String getCommandName(MailAddress mailAddress) { String user = mailAddress.getUser(); int index = user.indexOf('-', listName.length()); String commandName = user.substring(++index); return commandName; } /** * initialize the resources * @throws Exception */ protected void initializeResources() throws Exception { xmlResources = initXMLResources(new String[]{"List Manager"})[0]; } /** * Fetch the repository of users */ protected void initUsersRepository() { ServiceManager compMgr = (ServiceManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); try { UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE); String repName = getInitParameter("repositoryName"); usersRepository = usersStore.getRepository(repName); } catch (Exception e) { log("Failed to retrieve Store component:" + e.getMessage()); } } /** * Load an initialize all of the available commands * @param configuration * @throws ConfigurationException */ protected void loadCommands(Configuration configuration) throws Exception { final Configuration commandConfigurations = configuration.getChild("commands"); final Configuration[] commandConfs = commandConfigurations.getChildren("command"); for (int index = 0; index < commandConfs.length; index++) { Configuration commandConf = commandConfs[index]; String commandName = commandConf.getAttribute("name").toLowerCase(); String className = commandConf.getAttribute("class"); loadCommand(commandName, className, commandConf); } } /** * Loads and initializes a single command * * @param commandName * @param className * @param configuration * @throws ConfigurationException */ protected void loadCommand(String commandName, String className, Configuration configuration) throws ConfigurationException, ClassNotFoundException, IllegalAccessException, InstantiationException { ClassLoader theClassLoader = getClass().getClassLoader(); for (Iterator it = commandPackages.iterator(); it.hasNext();) { String packageName = (String) it.next(); IListServCommand listServCommand = null; try { listServCommand = (IListServCommand) theClassLoader.loadClass(packageName + className).newInstance(); } catch (Exception e) { //ignore continue; } listServCommand.init(this, configuration); commandMap.put(commandName, listServCommand); return; } throw new ConfigurationException("Unable to load listservcommand: " + commandName); } /** * loads all of the packages for the commands * * @param configuration * @throws ConfigurationException */ protected void loadCommandPackages(Configuration configuration) throws ConfigurationException { commandPackages.add(""); final Configuration packageConfiguration = configuration.getChild("commandpackages"); final Configuration[] pkgConfs = packageConfiguration.getChildren("commandpackage"); for (int index = 0; index < pkgConfs.length; index++) { Configuration conf = pkgConfs[index]; String packageName = conf.getValue().trim(); if (!packageName.endsWith(".")) { packageName += "."; } commandPackages.add(packageName); } } /** * Retrieves a data field, potentially defined by a super class. * @return null if not found, the object otherwise */ protected static Object getField(Object instance, String name) throws IllegalAccessException { Class clazz = instance.getClass(); Field[] fields; while (clazz != null) { fields = clazz.getDeclaredFields(); for (int index = 0; index < fields.length; index++) { Field field = fields[index]; if (field.getName().equals(name)) { field.setAccessible(true); return field.get(instance); } } clazz = clazz.getSuperclass(); } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -