KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > Notifier


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: Notifier.java 43111 2004-07-10 23:15:54Z andreas $ */
19
20 package org.apache.lenya.cms.task;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc PREFIX = "notification";
35     public static final String JavaDoc TARGET = "notification";
36     public static final String JavaDoc PARAMETER_TO = "tolist";
37     public static final String JavaDoc PARAMETER_FROM = "from";
38
39     private TaskManager taskManager;
40
41     /**
42      * Ctor.
43      * @param taskManager The task manager.
44      * @param parameters The task wrapper parameters.
45      */

46     public Notifier(TaskManager taskManager, Map JavaDoc parameters) {
47         super(parameters);
48         this.taskManager = taskManager;
49     }
50
51     /**
52      * Sends the notification message.
53      * @param taskParameters The task parameters.
54      * @throws ExecutionException when something went wrong.
55      */

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 JavaDoc[] 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 JavaDoc prefixMap = propertiesMap.getPrefixedMap();
90             for (Iterator JavaDoc i = prefixMap.keySet().iterator(); i.hasNext();) {
91                 String JavaDoc key = (String JavaDoc) i.next();
92                 String JavaDoc value = (String JavaDoc) prefixMap.get(key);
93                 String JavaDoc 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 JavaDoc e) {
112                 log.error("Error during notification: ", e);
113             }
114             log.info(" Notification target executed.");
115         }
116     }
117
118     /**
119      * Returns the task manager.
120      * @return A task manager.
121      */

122     protected TaskManager getTaskManager() {
123         return taskManager;
124     }
125
126     /**
127      * @see org.apache.lenya.cms.task.ParameterWrapper#getPrefix()
128      */

129     public String JavaDoc getPrefix() {
130         return PREFIX;
131     }
132
133     /**
134      * @see org.apache.lenya.cms.task.ParameterWrapper#getRequiredKeys()
135      */

136     protected String JavaDoc[] getRequiredKeys() {
137         String JavaDoc[] requiredKeys = { };
138         return requiredKeys;
139     }
140
141 }
142
Popular Tags