1 17 18 19 20 package org.apache.lenya.cms.task; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.parameters.ParameterException; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.lenya.util.NamespaceMap; 28 import org.apache.log4j.Category; 29 30 public class Notifier extends ParameterWrapper { 31 32 private static Category log = Category.getInstance(Notifier.class); 33 34 public static final String PREFIX = "notification"; 35 public static final String TARGET = "notification"; 36 public static final String PARAMETER_TO = "tolist"; 37 public static final String PARAMETER_FROM = "from"; 38 39 private TaskManager taskManager; 40 41 46 public Notifier(TaskManager taskManager, Map parameters) { 47 super(parameters); 48 this.taskManager = taskManager; 49 } 50 51 56 public void sendNotification(TaskParameters taskParameters) throws ExecutionException { 57 58 if (getMap().isEmpty()) { 59 log.info("Not sending notification: no parameters provided."); 60 } else if ("".equals(get(PARAMETER_TO).trim())) { 61 log.info("Not sending notification: empty notification.tolist parameter."); 62 } 63 else { 64 log.info("Sending notification"); 65 66 Task task = taskManager.getTask(TaskManager.ANT_TASK); 67 68 Parameters params = new Parameters(); 69 70 params.setParameter(AntTask.TARGET, TARGET); 71 72 String [] keys = 73 { 74 Task.PARAMETER_PUBLICATION_ID, 75 Task.PARAMETER_CONTEXT_PREFIX, 76 Task.PARAMETER_SERVER_PORT, 77 Task.PARAMETER_SERVER_URI, 78 Task.PARAMETER_SERVLET_CONTEXT }; 79 80 for (int i = 0; i < keys.length; i++) { 81 params.setParameter(keys[i], taskParameters.get(keys[i])); 82 } 83 84 NamespaceMap mailMap = new NamespaceMap(PREFIX); 85 mailMap.putAll(getMap()); 86 NamespaceMap propertiesMap = new NamespaceMap(AntTask.PROPERTIES_PREFIX); 87 propertiesMap.putAll(mailMap.getPrefixedMap()); 88 89 Map prefixMap = propertiesMap.getPrefixedMap(); 90 for (Iterator i = prefixMap.keySet().iterator(); i.hasNext();) { 91 String key = (String ) i.next(); 92 String value = (String ) prefixMap.get(key); 93 String trimmedValue = value.replace((char) 160, ' '); 94 trimmedValue = trimmedValue.trim(); 95 if (log.isDebugEnabled()) { 96 log.debug("Trimming value [" + value + "] to [" + trimmedValue + "]"); 97 log.debug("First character: [" + value.charAt(0) + "] = [" + (int) value.charAt(0) + "]"); 98 } 99 params.setParameter(key, trimmedValue); 100 } 101 102 try { 103 task.parameterize(params); 104 } catch (ParameterException e) { 105 throw new ExecutionException(e); 106 } 107 log.info(" Executing notification target ..."); 108 try { 109 task.execute(taskParameters.get(Task.PARAMETER_SERVLET_CONTEXT)); 110 } 111 catch (Exception e) { 112 log.error("Error during notification: ", e); 113 } 114 log.info(" Notification target executed."); 115 } 116 } 117 118 122 protected TaskManager getTaskManager() { 123 return taskManager; 124 } 125 126 129 public String getPrefix() { 130 return PREFIX; 131 } 132 133 136 protected String [] getRequiredKeys() { 137 String [] requiredKeys = { }; 138 return requiredKeys; 139 } 140 141 } 142 | Popular Tags |