1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.postage.configuration;
22
23 import org.apache.james.postage.mail.MailFactory;
24 import org.apache.james.postage.mail.DefaultMailFactory;
25 import org.apache.james.postage.result.MailProcessingRecord;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import javax.mail.Session;
30 import javax.mail.Message;
31
32 /***
33 * specifies, how mail is to be generated and sent, as coming from the configuration (<send> element)<br/>
34 * the source and target are specified by the parent SendProfile instance<br/>
35 * <br/>
36 * TODO init increaseSendPerMinute & maxSendPerMinute from config
37 */
38 public class MailSender {
39
40 private static Log log = LogFactory.getLog(MailSender.class);
41
42 private int sendPerMinute = 1;
43 private double increaseSendPerMinute = 0.0;
44 private int maxSendPerMinute = -1;
45 private String subject = "test";
46 private int sizeMinText = 0;
47 private int sizeMaxText = 1000;
48 private int sizeMinBinary = -1;
49 private int sizeMaxBinary = -1;
50 private SendProfile m_parentProfile;
51
52 private String m_mailFactoryClassname = null;
53 private Class m_mailFactoryClass = null;
54
55 public MailSender(SendProfile parent) {
56 m_parentProfile = parent;
57 }
58
59 public int getSendPerMinute() {
60 return sendPerMinute;
61 }
62
63 public void setSendPerMinute(int sendPerMinute) {
64 this.sendPerMinute = sendPerMinute;
65 }
66
67 public double getIncreaseSendPerMinute() {
68 return increaseSendPerMinute;
69 }
70
71 public void setIncreaseSendPerMinute(double increaseSendPerMinute) {
72 this.increaseSendPerMinute = increaseSendPerMinute;
73 }
74
75 public double getMaxSendPerMinute() {
76 return maxSendPerMinute;
77 }
78
79 public void setMaxSendPerMinute(int maxSendPerMinute) {
80 this.maxSendPerMinute = maxSendPerMinute;
81 }
82
83 /***
84 * how much emails are to be sent in the specified minute
85 * (taking into account the increase and max values)
86 * @param minute
87 * @return mails to be sent
88 */
89 public int getSendPerMinute(int minute) {
90 int increased = sendPerMinute + (int)(increaseSendPerMinute * minute);
91 if (maxSendPerMinute > 0) {
92 if (increased > maxSendPerMinute) increased = maxSendPerMinute;
93 }
94 return increased;
95 }
96
97 public String getSubject() {
98 return subject;
99 }
100
101 public void setSubject(String subject) {
102 this.subject = subject;
103 }
104
105 public int getSizeMinText() {
106 return sizeMinText;
107 }
108
109 public void setSizeMinText(int sizeMinText) {
110 this.sizeMinText = sizeMinText;
111 }
112
113 public int getSizeMaxText() {
114 return sizeMaxText;
115 }
116
117 public void setSizeMaxText(int sizeMaxText) {
118 this.sizeMaxText = sizeMaxText;
119 }
120
121 public int getSizeMinBinary() {
122 return sizeMinBinary;
123 }
124
125 public void setSizeMinBinary(int sizeMinBinary) {
126 this.sizeMinBinary = sizeMinBinary;
127 }
128
129 public int getSizeMaxBinary() {
130 return sizeMaxBinary;
131 }
132
133 public void setSizeMaxBinary(int sizeMaxBinary) {
134 this.sizeMaxBinary = sizeMaxBinary;
135 }
136
137 public SendProfile getParentProfile() {
138 return m_parentProfile;
139 }
140
141 public String getMailFactoryClassname() {
142 return m_mailFactoryClassname;
143 }
144
145 public void setMailFactoryClassname(String mailFactoryClassname) {
146 this.m_mailFactoryClassname = mailFactoryClassname;
147 }
148
149 public boolean sendTextPart() {
150 return getSizeMinText() >= 0 && getSizeMaxText() >= 1;
151 }
152
153 public boolean sendBinaryPart() {
154 return getSizeMinBinary() >= 0 && getSizeMaxBinary() >= 1;
155 }
156
157 public Message createMail(Session mailSession, MailProcessingRecord mailProcessingRecord) {
158 return getMailFactory().createMail(mailSession, this, mailProcessingRecord);
159
160 }
161
162 public MailFactory getMailFactory() {
163 MailFactory mailFactory = null;
164
165
166 if (m_mailFactoryClassname != null && m_mailFactoryClass == null) {
167 try {
168 m_mailFactoryClass = Class.forName(m_mailFactoryClassname);
169 } catch (ClassNotFoundException e) {
170 log.error("failed to load MailFactory class " + m_mailFactoryClassname, e);
171 }
172 }
173
174
175 if (m_mailFactoryClass != null) {
176 try {
177 mailFactory = (MailFactory)m_mailFactoryClass.newInstance();
178 } catch (Exception e) {
179 log.error("failed to create instance if MailFactory class " + m_mailFactoryClassname, e);
180 }
181 }
182
183 if (mailFactory == null) mailFactory = new DefaultMailFactory();
184 return mailFactory;
185 }
186
187
188 }