de6945502ebc107c4ce9d2b45ce0874c909c5d35.svn-base
4.87 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package org.apache.ibatis.thread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.Configuration;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.NestedIOException;
/**
* 刷新使用进程
*
* @author liubaoquan
*
*/
public class Runnable implements java.lang.Runnable {
public static Logger log = Logger.getLogger(Runnable.class);
private String location;
private Configuration configuration;
private Long beforeTime = 0L; // 上一次刷新时间
private static boolean refresh = false; // 是否执行刷新
private static String mappingPath = "mappings"; // xml文件夹匹配字符串,需要根据需要修改
private static int delaySeconds = 10;// 延迟刷新秒数
private static int sleepSeconds = 1;// 休眠时间
private static boolean enabled = false;
static {
delaySeconds = PropertiesUtil.getInt("delaySeconds");
sleepSeconds = PropertiesUtil.getInt("sleepSeconds");
mappingPath = PropertiesUtil.getString("mappingPath");
enabled = "true".equals(PropertiesUtil.getString("enabled"));
delaySeconds = delaySeconds == 0 ? 50 : delaySeconds;
sleepSeconds = sleepSeconds == 0 ? 1 : sleepSeconds;
mappingPath = StringUtils.isBlank(mappingPath) ? "mappings"
: mappingPath;
log.debug("[delaySeconds] " + delaySeconds);
log.debug("[sleepSeconds] " + sleepSeconds);
log.debug("[mappingPath] " + mappingPath);
}
public static boolean isRefresh() {
return refresh;
}
public Runnable(String location, Configuration configuration) {
this.location = location.replaceAll("\\\\", "/");
this.configuration = configuration;
}
@Override
public void run() {
location = location.substring("file [".length(),
location.lastIndexOf(mappingPath) + mappingPath.length());
beforeTime = System.currentTimeMillis();
log.debug("[location] " + location);
log.debug("[configuration] " + configuration);
if (enabled){
start(this);
}
}
public void start(final Runnable runnable) {
new Thread(new java.lang.Runnable() {
@Override
public void run() {
try {
Thread.sleep(delaySeconds * 1000);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
refresh = true;
System.out.println("========= Enabled refresh mybatis mapper =========");
while (true) {
try {
runnable.refresh(location, beforeTime);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
Thread.sleep(sleepSeconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
/**
* 执行刷新
*
* @param filePath
* 刷新目录
* @param beforeTime
* 上次刷新时间
* @throws NestedIOException
* 解析异常
* @throws FileNotFoundException
* 文件未找到
*/
public void refresh(String filePath, Long beforeTime) throws Exception {
// 本次刷新时间
Long refrehTime = System.currentTimeMillis();
List<File> refreshs = this.getRefreshFile(new File(filePath),
beforeTime);
if (refreshs.size() > 0) {
log.debug("refresh files:" + refreshs.size());
}
for (int i = 0; i < refreshs.size(); i++) {
System.out.println("Refresh file: "
+ mappingPath
+ StringUtils.substringAfterLast(refreshs.get(i)
.getAbsolutePath(), mappingPath));
log.debug("refresh file:" + refreshs.get(i).getAbsolutePath());
log.debug("refresh filename:" + refreshs.get(i).getName());
SqlSessionFactoryBean.refresh(new FileInputStream(refreshs.get(i)),
refreshs.get(i).getAbsolutePath(), configuration);
}
// 如果刷新了文件,则修改刷新时间,否则不修改
if (refreshs.size() > 0) {
this.beforeTime = refrehTime;
}
}
/**
* 获取需要刷新的文件列表
*
* @param dir
* 目录
* @param beforeTime
* 上次刷新时间
* @return 刷新文件列表
*/
public List<File> getRefreshFile(File dir, Long beforeTime) {
List<File> refreshs = new ArrayList<File>();
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
refreshs.addAll(this.getRefreshFile(file, beforeTime));
} else if (file.isFile()) {
if (this.check(file, beforeTime)) {
refreshs.add(file);
}
} else {
System.out.println("error file." + file.getName());
}
}
return refreshs;
}
/**
* 判断文件是否需要刷新
*
* @param file
* 文件
* @param beforeTime
* 上次刷新时间
* @return 需要刷新返回true,否则返回false
*/
public boolean check(File file, Long beforeTime) {
if (file.lastModified() > beforeTime) {
return true;
}
return false;
}
}