KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > jonasbase > Mail


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: Mail.java,v 1.1 2004/06/11 09:27:47 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.ant.jonasbase;
28
29 import java.io.File JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.apache.tools.ant.BuildException;
33 import org.objectweb.jonas.ant.JOnASBaseTask;
34
35 /**
36  * Allow to create mail factory
37  * @author Florent Benoit
38  */

39 public class Mail extends JTask implements BaseTaskItf {
40
41     /**
42      * Info for the logger
43      */

44     private static final String JavaDoc INFO = "[Mail] ";
45
46     /**
47      * Property for Mail factories
48      */

49     private static final String JavaDoc MAILFACTORY_PROPERTY = "jonas.service.mail.factories";
50
51     /**
52      * Session factory
53      */

54     private static final String JavaDoc SESSION_FACTORY = "Session";
55
56     /**
57      * MimePartDataSource factory
58      */

59     private static final String JavaDoc MIMEPARTDATASOURCE_FACTORY = "MimePartDataSource";
60
61     /**
62      * Session factory class
63      */

64     private static final String JavaDoc SESSION_FACTORY_CLASS = "javax.mail.Session";
65
66     /**
67      * MimePartDataSource factory class
68      */

69     private static final String JavaDoc MIMEPARTDATASOURCE_FACTORY_CLASS = "javax.mail.internet.MimePartDataSource";
70
71     /**
72      * Type of factory (Session or MimePartDataSource)
73      */

74     private String JavaDoc type = null;
75
76     /**
77      * Name of the factory
78      */

79     private String JavaDoc name = null;
80
81     /**
82      * Recipient (TO) of MimePartDataSource factory
83      */

84     private String JavaDoc mailTo = null;
85
86     /**
87      * Subject of MimePartDataSource factory
88      */

89     private String JavaDoc subject = null;
90
91     /**
92      * Sets the recipient (MimePartDataSource)
93      * @param mailTo recipient.
94      */

95     public void setMailTo(String JavaDoc mailTo) {
96         this.mailTo = mailTo;
97     }
98
99     /**
100      * Sets the name
101      * @param name name of the factory
102      */

103     public void setName(String JavaDoc name) {
104         this.name = name;
105     }
106
107     /**
108      * Sets the subject (MimePartDataSource)
109      * @param subject of the mail
110      */

111     public void setSubject(String JavaDoc subject) {
112         this.subject = subject;
113     }
114
115     /**
116      * Sets the type of factory
117      * @param type of factory
118      */

119     public void setType(String JavaDoc type) {
120         this.type = type;
121     }
122
123     /**
124      * Check the properties
125      */

126     private void checkProperties() {
127         if (name == null) {
128             throw new BuildException(INFO + "Property 'name' is missing.");
129         } else if (type == null) {
130             throw new BuildException(INFO + "Property 'type' is missing.");
131         }
132     }
133
134     /**
135      * Execute this task
136      */

137     public void execute() {
138         checkProperties();
139
140         Properties JavaDoc props = new Properties JavaDoc();
141         props.put("mail.factory.name", name);
142         String JavaDoc className = null;
143
144         String JavaDoc infoTxt = "Generating a MailFactory with type '" + type + "' and name '" + name + "'";
145
146         if (type.equalsIgnoreCase(SESSION_FACTORY)) {
147             className = SESSION_FACTORY_CLASS;
148         } else if (type.equalsIgnoreCase(MIMEPARTDATASOURCE_FACTORY)) {
149             className = MIMEPARTDATASOURCE_FACTORY_CLASS;
150             // Set mailTo
151
if (mailTo != null) {
152                 props.put("mail.to", mailTo);
153                 infoTxt += ", mailTo field '" + mailTo + "'";
154             }
155             // Subject
156
if (subject != null) {
157                 props.put("mail.subject", subject);
158                 infoTxt += ", subject '" + subject + "'";
159             }
160
161         } else {
162             throw new BuildException(INFO + "Invalid type '" + type + "'.");
163         }
164
165         // Display info
166
log(INFO + infoTxt + "...");
167
168         // Set type
169
props.put("mail.factory.type", className);
170
171         // Build new mail factory
172
String JavaDoc jBaseConf = getDestDir().getPath() + File.separator + "conf";
173
174         String JavaDoc propsFileName = jBaseConf + File.separator + name + ".properties";
175         File JavaDoc tmpFile = new File JavaDoc(propsFileName);
176
177         writePropsToFile(INFO, props, tmpFile);
178
179         // Now add name to the existing list for the property
180
changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, MAILFACTORY_PROPERTY, name, true);
181     }
182
183 }
Popular Tags