001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: Notifier.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.task;
022:
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.parameters.ParameterException;
027: import org.apache.avalon.framework.parameters.Parameters;
028: import org.apache.lenya.util.NamespaceMap;
029: import org.apache.log4j.Logger;
030:
031: /**
032: * Task Notification
033: * @deprecated Use the usecase framework instead.
034: */
035: public class Notifier extends ParameterWrapper {
036:
037: private static Logger log = Logger.getLogger(Notifier.class);
038:
039: /**
040: * <code>PREFIX</code> Notification namespace prefix
041: */
042: public static final String PREFIX = "notification";
043: /**
044: * <code>TARGET</code> Notification target
045: */
046: public static final String TARGET = "notification";
047: /**
048: * <code>PARAMETER_TO</code> To Parameter
049: */
050: public static final String PARAMETER_TO = "tolist";
051: /**
052: * <code>PARAMETER_FROM</code> From Parameter
053: */
054: public static final String PARAMETER_FROM = "from";
055:
056: private TaskManager taskManager;
057:
058: /**
059: * Ctor.
060: * @param _taskManager The task manager.
061: * @param parameters The task wrapper parameters.
062: */
063: public Notifier(TaskManager _taskManager, Map parameters) {
064: super (parameters);
065: this .taskManager = _taskManager;
066: }
067:
068: /**
069: * Sends the notification message.
070: * @param taskParameters The task parameters.
071: * @throws ExecutionException when something went wrong.
072: */
073: public void sendNotification(TaskParameters taskParameters)
074: throws ExecutionException {
075:
076: if (getMap().isEmpty()) {
077: log
078: .info("Not sending notification: no parameters provided.");
079: } else if ("".equals(get(PARAMETER_TO).trim())) {
080: log
081: .info("Not sending notification: empty notification.tolist parameter.");
082: } else {
083: log.info("Sending notification");
084:
085: Task task = this .taskManager.getTask(TaskManager.ANT_TASK);
086:
087: Parameters params = new Parameters();
088:
089: params.setParameter(AntTask.TARGET, TARGET);
090:
091: String[] keys = { Task.PARAMETER_PUBLICATION_ID,
092: Task.PARAMETER_CONTEXT_PREFIX,
093: Task.PARAMETER_SERVER_PORT,
094: Task.PARAMETER_SERVER_URI,
095: Task.PARAMETER_SERVLET_CONTEXT };
096:
097: for (int i = 0; i < keys.length; i++) {
098: params.setParameter(keys[i], taskParameters
099: .get(keys[i]));
100: }
101:
102: NamespaceMap mailMap = new NamespaceMap(PREFIX);
103: mailMap.putAll(getMap());
104: NamespaceMap propertiesMap = new NamespaceMap(
105: AntTask.PROPERTIES_PREFIX);
106: propertiesMap.putAll(mailMap.getPrefixedMap());
107:
108: Map prefixMap = propertiesMap.getPrefixedMap();
109: String key;
110: String value;
111: Map.Entry entry;
112:
113: for (Iterator iter = prefixMap.entrySet().iterator(); iter
114: .hasNext();) {
115: entry = (Map.Entry) iter.next();
116: key = (String) entry.getKey();
117: value = (String) entry.getValue();
118: String trimmedValue = value.replace((char) 160, ' ');
119: trimmedValue = trimmedValue.trim();
120: if (log.isDebugEnabled()) {
121: log.debug("Trimming value [" + value + "] to ["
122: + trimmedValue + "]");
123: log.debug("First character: [" + value.charAt(0)
124: + "] = [" + (int) value.charAt(0) + "]");
125: }
126: params.setParameter(key, trimmedValue);
127: }
128:
129: try {
130: task.parameterize(params);
131: } catch (ParameterException e) {
132: throw new ExecutionException(e);
133: }
134: log.info(" Executing notification target ...");
135: try {
136: task.execute(taskParameters
137: .get(Task.PARAMETER_SERVLET_CONTEXT));
138: } catch (Exception e) {
139: log.error("Error during notification: ", e);
140: }
141: log.info(" Notification target executed.");
142: }
143: }
144:
145: /**
146: * Returns the task manager.
147: * @return A task manager.
148: */
149: protected TaskManager getTaskManager() {
150: return this .taskManager;
151: }
152:
153: /**
154: * @see org.apache.lenya.cms.task.ParameterWrapper#getPrefix()
155: */
156: public String getPrefix() {
157: return PREFIX;
158: }
159:
160: /**
161: * @see org.apache.lenya.cms.task.ParameterWrapper#getRequiredKeys()
162: */
163: protected String[] getRequiredKeys() {
164: String[] requiredKeys = {};
165: return requiredKeys;
166: }
167:
168: }
|