KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > executer > MailActionExecuter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action.executer;
18
19 import java.util.List JavaDoc;
20
21 import org.alfresco.repo.action.ParameterDefinitionImpl;
22 import org.alfresco.service.cmr.action.Action;
23 import org.alfresco.service.cmr.action.ParameterDefinition;
24 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.mail.SimpleMailMessage;
29 import org.springframework.mail.javamail.JavaMailSender;
30
31 /**
32  * Mail action executor implementation.
33  *
34  * @author Roy Wetherall
35  */

36 public class MailActionExecuter extends ActionExecuterAbstractBase
37 {
38     private static Log logger = LogFactory.getLog(MailActionExecuter.class);
39     
40     /**
41      * Action executor constants
42      */

43     public static final String JavaDoc NAME = "mail";
44     public static final String JavaDoc PARAM_TO = "to";
45     public static final String JavaDoc PARAM_SUBJECT = "subject";
46     public static final String JavaDoc PARAM_TEXT = "text";
47     
48     /**
49      * From address
50      */

51     public static final String JavaDoc FROM_ADDRESS = "alfresco_repository@alfresco.org";
52     
53     /**
54      * The java mail sender
55      */

56     private JavaMailSender javaMailSender;
57     
58     /**
59      * Set the java mail sender
60      *
61      * @param javaMailSender the java mail sender
62      */

63     public void setMailService(JavaMailSender javaMailSender)
64     {
65         this.javaMailSender = javaMailSender;
66     }
67     
68     /**
69      * Execute the rule action
70      */

71     @Override JavaDoc
72     protected void executeImpl(
73             Action ruleAction,
74             NodeRef actionedUponNodeRef)
75     {
76         // Create the simple mail message
77
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
78         simpleMailMessage.setTo((String JavaDoc)ruleAction.getParameterValue(PARAM_TO));
79         simpleMailMessage.setSubject((String JavaDoc)ruleAction.getParameterValue(PARAM_SUBJECT));
80         simpleMailMessage.setText((String JavaDoc)ruleAction.getParameterValue(PARAM_TEXT));
81         simpleMailMessage.setFrom(FROM_ADDRESS);
82             
83         try
84         {
85            // Send the message
86
javaMailSender.send(simpleMailMessage);
87         }
88         catch (Throwable JavaDoc e)
89         {
90            // don't stop the action but let admins know email is not getting sent
91
logger.error("Failed to send email to " + (String JavaDoc)ruleAction.getParameterValue(PARAM_TO), e);
92         }
93     }
94
95     /**
96      * Add the parameter definitions
97      */

98     @Override JavaDoc
99     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> paramList)
100     {
101         paramList.add(new ParameterDefinitionImpl(PARAM_TO, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_TO)));
102         paramList.add(new ParameterDefinitionImpl(PARAM_SUBJECT, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_SUBJECT)));
103         paramList.add(new ParameterDefinitionImpl(PARAM_TEXT, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_TEXT)));
104     }
105
106 }
107
Popular Tags