package foundation.send.mail;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
|
import foundation.io.FileRepository;
|
import foundation.server.config.ServerStatus;
|
import foundation.variant.expression.VariantExpression;
|
|
public class MailTemplate {
|
|
private String fileName;
|
private File file;
|
private long lastloadTime;
|
private VariantExpression content;
|
|
|
public MailTemplate(String fileName) {
|
this.fileName = fileName;
|
this.lastloadTime = 0;
|
|
content = new VariantExpression();
|
content.setFlag_Variant('$');
|
}
|
|
public boolean isDirty() {
|
//1. 如果已经加载过,且不需要检查配置更新, 不需要重新加载
|
if (lastloadTime > 0 && !ServerStatus.isCheckMetaUpdateOnTime()) {
|
return false;
|
}
|
|
//2. 如果文件最后更新时间小于等于加载时间,不需要重新加载
|
if (file != null) {
|
long now = file.lastModified();
|
if (lastloadTime >= now) {
|
return false;
|
}
|
}
|
|
//3. 其他情况,需要重新加载
|
return true;
|
}
|
|
public void load() throws Exception {
|
File path = FileRepository.getTemplatePath();
|
file = new File(path, fileName);
|
|
if (!file.exists()) {
|
throw new Exception("mail template not exists: " + file);
|
}
|
|
lastloadTime = file.lastModified();
|
content.clear();
|
|
InputStream inputStream = new FileInputStream(file);
|
try {
|
InputStreamReader reader = null; BufferedReader bufferedReader = null;
|
|
try {
|
reader = new InputStreamReader(inputStream, "UTF-8");
|
bufferedReader = new BufferedReader(reader);
|
|
String line = null;
|
while ((line = bufferedReader.readLine()) != null) {
|
content.parseOneLine(line);
|
}
|
}
|
finally {
|
if (bufferedReader != null) {
|
try {
|
bufferedReader.close();
|
}
|
catch (Exception e) {
|
}
|
}
|
if (reader != null) {
|
try {
|
reader.close();
|
}
|
catch (Exception e) {
|
}
|
}
|
}
|
}
|
finally {
|
try {
|
inputStream.close();
|
}
|
catch (Exception e) {
|
}
|
}
|
}
|
|
public VariantExpression getContent() {
|
return content;
|
}
|
|
public String getContentValue() {
|
return content.getString();
|
}
|
|
}
|