P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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();
    }
    
}