提交 | 用户 | age
|
a6a76f
|
1 |
package com.yc.multiData; |
F |
2 |
|
3a6b5b
|
3 |
import com.mchange.v2.c3p0.ComboPooledDataSource; |
F |
4 |
import com.yc.action.panval.InvGet; |
d3a04a
|
5 |
import com.yc.entity.AttachmentConfig; |
3a6b5b
|
6 |
import com.yc.entity.DataSourceEntity; |
F |
7 |
import com.yc.entity.DemoConstant; |
8a20b4
|
8 |
import com.yc.exception.ApplicationException; |
3a6b5b
|
9 |
import com.yc.factory.FactoryBean; |
F |
10 |
import com.yc.sdk.password.action.ChangePassword; |
|
11 |
import com.yc.sdk.shopping.util.SettingKey; |
|
12 |
import com.yc.sdk.weixinmp.util.MD5Util; |
|
13 |
import com.yc.service.demo.DemoIfc; |
|
14 |
import com.yc.service.impl.DBHelper; |
|
15 |
import com.yc.utils.SessionKey; |
|
16 |
import org.apache.commons.lang3.StringUtils; |
|
17 |
import org.apache.commons.logging.Log; |
|
18 |
import org.apache.commons.logging.LogFactory; |
3c1697
|
19 |
import org.jetbrains.annotations.Nullable; |
3a6b5b
|
20 |
import org.springframework.beans.BeansException; |
526a72
|
21 |
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
3a6b5b
|
22 |
import org.springframework.context.ApplicationContext; |
F |
23 |
import org.springframework.context.ApplicationContextAware; |
|
24 |
import org.springframework.dao.DataAccessException; |
8a20b4
|
25 |
import org.springframework.dao.EmptyResultDataAccessException; |
3a6b5b
|
26 |
|
F |
27 |
import javax.servlet.http.HttpServletRequest; |
|
28 |
import javax.servlet.http.HttpSession; |
|
29 |
import javax.sql.DataSource; |
a6a76f
|
30 |
import java.io.PrintWriter; |
F |
31 |
import java.sql.Connection; |
|
32 |
import java.sql.SQLException; |
|
33 |
import java.sql.SQLFeatureNotSupportedException; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.HashMap; |
|
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.logging.Logger; |
|
39 |
import java.util.stream.Collectors; |
|
40 |
|
|
41 |
|
|
42 |
/** |
|
43 |
* @author pengbei |
|
44 |
*/ |
|
45 |
public class MultiDataSource implements DataSource, ApplicationContextAware { |
|
46 |
|
|
47 |
private static final Log log = LogFactory.getLog(MultiDataSource.class); |
|
48 |
private ApplicationContext applicationContext = null; |
484907
|
49 |
|
F |
50 |
public static Map<String, ComboPooledDataSource> getComMap() { |
|
51 |
return comMap; |
|
52 |
} |
|
53 |
|
|
54 |
public static void setComMap(Map<String, ComboPooledDataSource> comMap) { |
|
55 |
MultiDataSource.comMap = comMap; |
|
56 |
} |
|
57 |
|
d3a04a
|
58 |
/** |
F |
59 |
* 新增配置 |
|
60 |
*/ |
a6a76f
|
61 |
private static Map<String, ComboPooledDataSource> comMap = new HashMap<String, ComboPooledDataSource>(); |
F |
62 |
// private static Map<String, com.alibaba.druid.pool.DruidDataSource> comMap = new HashMap<String, com.alibaba.druid.pool.DruidDataSource>(); |
d3a04a
|
63 |
/** |
F |
64 |
* 新增配置是否停用 之后停用/启动在操作页控制,也更改下配置 |
|
65 |
*/ |
a6a76f
|
66 |
private static Map<String, DataSourceEntity> dataSourceMap = new HashMap<String, DataSourceEntity>(); |
F |
67 |
// /** 在登录时或者demo查询时是否查询下所有配置 */ |
|
68 |
// private boolean infoAllTo = true; |
d3a04a
|
69 |
/** |
F |
70 |
* 基础配置 |
|
71 |
*/ |
a6a76f
|
72 |
private static com.yc.dataAccess.ComboPooledDataSource demoDataSource = null; |
F |
73 |
|
|
74 |
public static boolean hasDemoDataSource() { |
d3a04a
|
75 |
if (demoDataSource != null) return true; |
F |
76 |
if (getDataSourceMap(SpObserver.DEMOXML) != null) return true; |
|
77 |
return false; |
a6a76f
|
78 |
} |
d3a04a
|
79 |
|
a6a76f
|
80 |
/** |
F |
81 |
* 按 dbid 获取数据源 DataSourceEntity 对象 |
d3a04a
|
82 |
* |
a6a76f
|
83 |
* @param dbid |
F |
84 |
* @return |
|
85 |
*/ |
|
86 |
public static DataSourceEntity getDataSourceMap(String dbId) { |
5af59e
|
87 |
DataSourceEntity dataSource = dataSourceMap.get(dbId); |
3c1697
|
88 |
DemoConstant demoConstant=new DemoConstant(); |
F |
89 |
demoConstant.setDbId(dbId); |
484907
|
90 |
if(dataSource!=null&&getComMap().get(dataSource.getDbId()+"")==null){ |
F |
91 |
//已有数据源实体,但没实际的数据连接 |
|
92 |
demoConstant.setDbId(null);//去掉dbid是为了可能跳过检查,都要加载数据库连接 |
|
93 |
MultiDataSource multiDataSource = (MultiDataSource) FactoryBean.getBean("multiDataSource"); |
|
94 |
multiDataSource.setDataSourceByMap(dataSource, demoConstant); |
|
95 |
}else if(dataSource==null) { |
|
96 |
//全新 |
|
97 |
demoConstant.setNotSkipCheck(false);//指明跳过检查,要加载数据源 |
|
98 |
dataSource = getDataSourceFromDemoDataBase(demoConstant, dataSource); |
|
99 |
} |
3c1697
|
100 |
return dataSource; |
F |
101 |
} |
|
102 |
|
|
103 |
@Nullable |
|
104 |
private static DataSourceEntity getDataSourceFromDemoDataBase(DemoConstant demoConstant, DataSourceEntity dataSource) { |
5af59e
|
105 |
if (dataSource == null) { |
F |
106 |
//由于现在数据源池不再是加载所有的数据源,当需要取不存在于当前服务器上的数据源信息,需要读取数据库重新从加载 by by danaus 2022/3/29 15:54 |
|
107 |
try { |
|
108 |
SpObserver.setDBtoDemo(); |
176de8
|
109 |
DemoIfc demoIfc = (DemoIfc) FactoryBean.getBean("demo"); |
3c1697
|
110 |
|
8a20b4
|
111 |
final List<DataSourceEntity> list = demoIfc.getDataSource(demoConstant); |
F |
112 |
if(list!=null&&list.size()>0) { |
0099c9
|
113 |
dataSource =list.get(0); |
F |
114 |
for(DataSourceEntity dataSourceEntity:list) { |
|
115 |
dataSourceMap.put(dataSourceEntity.getDbId() + "", dataSourceEntity);// 这里只是生成数据源实体对象 by danaus 2023-05-13 11:10 |
|
116 |
MultiDataSource multiDataSource = (MultiDataSource) FactoryBean.getBean("multiDataSource"); |
|
117 |
multiDataSource.setDataSourceByMap(dataSourceEntity, demoConstant); |
|
118 |
} |
8a20b4
|
119 |
} |
526a72
|
120 |
}catch (NoSuchBeanDefinitionException e1){ |
F |
121 |
//demo数据源不存在,这种情况是在新系统才出现,所以不需要处理 |
8a20b4
|
122 |
}catch (DataAccessException e){ |
F |
123 |
if(e instanceof EmptyResultDataAccessException){ |
3c1697
|
124 |
String error="没有找到数据源"; |
F |
125 |
if(StringUtils.isNotBlank(demoConstant.getDbId())){ |
|
126 |
error="没有找到dbid为【" + demoConstant.getDbId() + "】数据源【dbid:" + demoConstant.getDbId() + "】"; |
|
127 |
}else if(StringUtils.isNotBlank(demoConstant.getMiniAppId())){ |
|
128 |
error="没有找到小程序MiniAppId为【" + demoConstant.getMiniAppId() + "】数据源【MiniAppId:" + demoConstant.getMiniAppId() + "】"; |
|
129 |
}else if(StringUtils.isNotBlank(demoConstant.getCorpId())){ |
|
130 |
error="没有找到企业微信CorpId为【" + demoConstant.getCorpId() + "】数据源【CorpId:" + demoConstant.getCorpId() + "】"; |
|
131 |
}else if(StringUtils.isNotBlank(demoConstant.getMpAppId())){ |
|
132 |
error="没有找到公众号MpAppId为【" + demoConstant.getMpAppId() + "】数据源【MpAppId:" + demoConstant.getMpAppId() + "】"; |
|
133 |
} |
|
134 |
throw new ApplicationException(error); |
8a20b4
|
135 |
}else{ |
F |
136 |
throw e; |
|
137 |
} |
|
138 |
}catch (Exception ex) { |
5af59e
|
139 |
throw ex; |
F |
140 |
} finally { |
|
141 |
SpObserver.setDBtoInstance(); |
|
142 |
} |
|
143 |
} |
|
144 |
return dataSource; |
a6a76f
|
145 |
} |
d3a04a
|
146 |
|
a6a76f
|
147 |
/** |
d3a04a
|
148 |
* 按 corpid ,mpAppid ,maAppid,dbid, SHOPPING_DBID ,hosturl 顺序取数据源 |
F |
149 |
* |
a6a76f
|
150 |
* @param request |
F |
151 |
* @return |
|
152 |
* @throws Exception |
|
153 |
*/ |
|
154 |
public static DataSourceEntity getDataSourceMap(HttpServletRequest request) throws Exception { |
d3a04a
|
155 |
String wx = request.getParameter(SessionKey.WEIXIN_FROM); |
F |
156 |
String appId = request.getParameter(SessionKey.WEIXIN_CORPID); |
a6a76f
|
157 |
|
d3a04a
|
158 |
if (appId == null || "".equals(appId)) { |
F |
159 |
appId = request.getParameter(SessionKey.WEIXIN_APPID); |
|
160 |
} |
|
161 |
DataSourceEntity dataSourceEntity = null; |
|
162 |
//按企业号 corpid 查找数据源 |
|
163 |
if (wx != null && ("1".equals(wx) || "4".equals(wx)) && appId != null && !"".equals(appId)) { |
|
164 |
dataSourceEntity = getDataSourceMapByCorpId(appId); |
|
165 |
if (dataSourceEntity == null) { |
|
166 |
throw new Exception("没有找到微信企业号数据源【corpid:" + appId + "】"); |
|
167 |
} |
|
168 |
return dataSourceEntity; |
|
169 |
} |
|
170 |
//按公众号 appid 查找数据源 |
|
171 |
if (wx != null && "2".equals(wx) && appId != null && !"".equals(appId)) { |
|
172 |
dataSourceEntity = getDataSourceMapByMpAppId(appId); |
|
173 |
if (dataSourceEntity == null) { |
|
174 |
throw new Exception("没有找到微信公众号数据源【appid:" + appId + "】"); |
|
175 |
} |
|
176 |
return dataSourceEntity; |
|
177 |
} |
|
178 |
//按小程序 appid 查找数据源 |
|
179 |
if (wx != null && "3".equals(wx) && appId != null && !"".equals(appId)) { |
|
180 |
dataSourceEntity = getDataSourceMapByMaAppId(appId); |
|
181 |
if (dataSourceEntity == null) { |
|
182 |
throw new Exception("没有找到微信小程序数据源【appid:" + appId + "】"); |
|
183 |
} |
|
184 |
return dataSourceEntity; |
|
185 |
} |
|
186 |
HttpSession session = request.getSession(); |
|
187 |
String dbId = request.getParameter(SessionKey.DATA_BASE_ID); |
|
188 |
|
|
189 |
if (dbId == null || "".equals(dbId)) { |
|
190 |
dbId = (String) session.getAttribute(SessionKey.DATA_BASE_ID); |
|
191 |
} |
|
192 |
if (dbId == null || "".equals(dbId)) { |
|
193 |
dbId = (request.getAttribute(SessionKey.DATA_BASE_ID) == null ? null : |
|
194 |
(String) request.getAttribute(SessionKey.DATA_BASE_ID)); |
|
195 |
} |
|
196 |
if (dbId == null || "".equals(dbId)) { |
|
197 |
dbId = request.getParameter(SessionKey.SHOPPING_DBID); |
|
198 |
} |
|
199 |
if (dbId == null || "".equals(dbId)) { |
|
200 |
dbId = (request.getAttribute(SessionKey.SHOPPING_DBID) == null ? null : |
|
201 |
(String) request.getAttribute(SessionKey.SHOPPING_DBID)); |
|
202 |
} |
|
203 |
if (dbId == null || "".equals(dbId)) { |
|
204 |
dbId = (session.getAttribute(SessionKey.SHOPPING_DBID) == null ? null : |
|
205 |
(String) session.getAttribute(SessionKey.SHOPPING_DBID)); |
|
206 |
} |
|
207 |
//解决首页登录,在多公司中有部分数据源过期的情况,会导致里面正常的数据源也用不的问题,by danaus 2021/7/9 9:38 |
|
208 |
if (StringUtils.isBlank(dbId)) { |
|
209 |
dbId = request.getParameter("dataName") != null ? request.getParameter("dataName") : null; |
|
210 |
} |
|
211 |
//按已经存在的 会话 dbid查找 |
|
212 |
if (dbId != null && !"".equals(dbId)) { |
|
213 |
dataSourceEntity = getDataSourceMap(dbId); |
|
214 |
if (dataSourceEntity == null) { |
|
215 |
throw new Exception("没有找到dbid为【" + dbId + "】数据源【dbid:" + dbId + "】"); |
|
216 |
} |
|
217 |
return dataSourceEntity; |
|
218 |
} |
|
219 |
|
|
220 |
//按 唯一主机名 CorpUrl 查找 |
|
221 |
String hostUrl = SettingKey.getHostUrl(request); |
|
222 |
dataSourceEntity = getDataSourceMapByCorpURL(hostUrl); |
|
223 |
if (dataSourceEntity != null) { |
|
224 |
return dataSourceEntity; |
|
225 |
} |
|
226 |
|
|
227 |
//按主机域名查找 |
|
228 |
String domain = com.yc.utils.HtmlUtil.getDomain(hostUrl); |
|
229 |
List<DataSourceEntity> list = MultiDataSource.getDataSourceMapsByDomain(domain); |
|
230 |
if (list != null && list.size() > 0) { |
|
231 |
dataSourceEntity = list.get(0); |
|
232 |
} |
|
233 |
|
|
234 |
if (dataSourceEntity == null) { |
|
235 |
throw new Exception("没有找到hostUrl为【" + hostUrl + "】数据源【hostUrl:" + hostUrl + "】"); |
|
236 |
} |
|
237 |
return dataSourceEntity; |
|
238 |
} |
|
239 |
|
|
240 |
|
|
241 |
/** |
|
242 |
* 获取数据源ID |
|
243 |
* |
|
244 |
* @param request |
|
245 |
* @return |
|
246 |
*/ |
|
247 |
public static String getDatabaseId(HttpServletRequest request) { |
|
248 |
HttpSession session = request.getSession(); //必须传 false 值 |
|
249 |
String dbId = null; |
|
250 |
|
|
251 |
dbId = request.getParameter(SessionKey.DATA_BASE_ID); |
|
252 |
|
|
253 |
if ((dbId == null || "".equals(dbId)) && session != null) { |
|
254 |
dbId = (String) session.getAttribute(SessionKey.DATA_BASE_ID); |
|
255 |
} |
|
256 |
|
|
257 |
if (dbId == null || "".equals(dbId)) { |
|
258 |
dbId = request.getParameter(SessionKey.SHOPPING_DBID); |
|
259 |
} |
|
260 |
|
|
261 |
if (dbId == null || "".equals(dbId)) { |
|
262 |
// 如果是微信过来的,则要给 USERCODE 赋值 |
|
263 |
String corpId = request.getParameter(SessionKey.WEIXIN_CORPID); |
|
264 |
|
|
265 |
//微信点击来源: 1 从企业号点击 , 2 从公众号点击 |
|
266 |
String wx = request.getParameter(SessionKey.WEIXIN_FROM); |
|
267 |
if (corpId == null || "".equals(corpId)) { |
|
268 |
corpId = request.getParameter(SessionKey.WEIXIN_APPID); |
|
269 |
} |
|
270 |
if (corpId != null || !"".equals(corpId)) { |
|
271 |
DataSourceEntity dataSourceEntity = null; |
|
272 |
if (wx == null || "".equals(wx)) { |
|
273 |
dataSourceEntity = MultiDataSource.getDataSourceMapByMpAppId(corpId); |
|
274 |
wx = "2"; |
|
275 |
} else { |
|
276 |
dataSourceEntity = MultiDataSource.getDataSourceMapByCorpId(corpId); |
|
277 |
} |
|
278 |
if (dataSourceEntity != null) { |
|
279 |
dbId = dataSourceEntity.getDbId() + ""; |
|
280 |
} |
|
281 |
|
|
282 |
|
|
283 |
if (dbId == null || "".equals(dbId)) { |
|
284 |
//按 域名 重新取数据源信息 |
|
285 |
String hostUrl = SettingKey.getHostUrl(request); |
|
286 |
//String URL = (request.getRequestURL()+"").replace(request.getRequestURI(),"") + request.getContextPath(); |
|
287 |
String domain = com.yc.utils.HtmlUtil.getDomain(hostUrl); |
|
288 |
List<DataSourceEntity> list = MultiDataSource.getDataSourceMapsByDomain(domain); |
|
289 |
if (list != null && list.size() > 0) { |
|
290 |
dbId = (list.get(0).getDbId() + ""); |
|
291 |
} |
|
292 |
} |
|
293 |
|
|
294 |
|
|
295 |
} |
|
296 |
} |
|
297 |
return dbId; |
|
298 |
} |
|
299 |
|
a6a76f
|
300 |
/** |
F |
301 |
* 获取所有数据源 DataSourceEntity 对象 |
d3a04a
|
302 |
* |
a6a76f
|
303 |
* @return |
F |
304 |
*/ |
|
305 |
public static Map<String, DataSourceEntity> getDataSourceMaps() { |
d3a04a
|
306 |
return dataSourceMap; |
a6a76f
|
307 |
} |
d3a04a
|
308 |
|
a6a76f
|
309 |
/** |
F |
310 |
* 获取所有数据源 DataSourceEntity 对象 |
d3a04a
|
311 |
* |
a6a76f
|
312 |
* @return |
F |
313 |
*/ |
|
314 |
public static List<DataSourceEntity> getDataSourceMapsAll() { |
d3a04a
|
315 |
List<DataSourceEntity> list = new ArrayList<DataSourceEntity>(); |
F |
316 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
|
317 |
list.add(entry.getValue()); |
|
318 |
} |
|
319 |
return list; |
|
320 |
} |
a6a76f
|
321 |
|
F |
322 |
/** |
|
323 |
* 根据URL 地址中的域名 获取数据源 DataSourceEntity 对象列表 获取 isShowInLoginPage = true 的记录 ,主要用于登录主页面 login.jsp |
d3a04a
|
324 |
* |
a6a76f
|
325 |
* @param domain |
F |
326 |
* @return |
|
327 |
*/ |
|
328 |
public static List<DataSourceEntity> getDataSourceMapsByDomainByShowInLoginPage(String domain) { |
d3a04a
|
329 |
List<DataSourceEntity> list = getDataSourceMapsByDomain(domain); |
F |
330 |
if (list != null) { |
|
331 |
return list.stream().filter(x -> x.isShowInLoginPage()).collect(Collectors.toList()); |
|
332 |
} else { |
|
333 |
return list; |
|
334 |
} |
a6a76f
|
335 |
} |
F |
336 |
|
|
337 |
/** |
|
338 |
* 根据URL 地址中的域名 获取数据源 DataSourceEntity 对象列表 |
d3a04a
|
339 |
* |
a6a76f
|
340 |
* @param domain |
F |
341 |
* @return |
|
342 |
*/ |
|
343 |
public static List<DataSourceEntity> getDataSourceMapsByDomain(String domain) { |
d3a04a
|
344 |
if (domain == null) return null; |
F |
345 |
List<DataSourceEntity> hasDomainList = new ArrayList<DataSourceEntity>(); |
|
346 |
List<DataSourceEntity> hasNoDomainList = new ArrayList<DataSourceEntity>(); |
|
347 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
|
348 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
349 |
String dsDmain = null; |
|
350 |
if (dataSourceEntity.getDomain() != null) { |
|
351 |
dsDmain = dataSourceEntity.getDomain().toLowerCase().trim(); |
|
352 |
} |
|
353 |
|
|
354 |
if (dsDmain != null && !"".equals(dsDmain) |
|
355 |
&& (dsDmain.startsWith(domain.toLowerCase().trim() + ";") |
|
356 |
|| dsDmain.endsWith(";" + domain.toLowerCase().trim()) |
|
357 |
|| dsDmain.equals(domain.toLowerCase().trim()) |
|
358 |
|| dsDmain.contains(";" + domain.toLowerCase().trim() + ";"))) { |
|
359 |
hasDomainList.add(dataSourceEntity); |
|
360 |
} else { |
|
361 |
//hasNoDomainList.add(dataSourceEntity); //避免程序漏洞,把它注释掉,modified by Johns Wang, 2017-06-20 |
|
362 |
} |
|
363 |
} |
|
364 |
|
|
365 |
if (hasDomainList.size() > 0) { |
|
366 |
return hasDomainList; |
|
367 |
} else { |
0099c9
|
368 |
//---都没有符合,则重新加载数据库生成数据源 by danaus 2023-06-28 15:07 |
F |
369 |
DataSourceEntity dataSourceEntity=null; |
|
370 |
DemoConstant constant=new DemoConstant(); |
|
371 |
constant.setDomain(domain); |
|
372 |
dataSourceEntity=getDataSourceFromDemoDataBase(constant,dataSourceEntity); |
|
373 |
hasNoDomainList.add(dataSourceEntity); |
d3a04a
|
374 |
return hasNoDomainList; |
F |
375 |
} |
a6a76f
|
376 |
} |
d3a04a
|
377 |
|
a6a76f
|
378 |
/** |
d3a04a
|
379 |
* 把不同数据库主机IP和端口号的 数据源取出来,用于计算数据库使用空间 |
F |
380 |
* |
a6a76f
|
381 |
* @return |
F |
382 |
*/ |
d3a04a
|
383 |
public static Map<String, DataSourceEntity> getDataSourceMapsByHostAndPort() { |
F |
384 |
Map<String, DataSourceEntity> map = new HashMap<String, DataSourceEntity>(); |
|
385 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
|
386 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
387 |
if (dataSourceEntity.getHost() != null && !"".equals(dataSourceEntity.getHost()) |
|
388 |
&& dataSourceEntity.getPort() != null && !"".equals(dataSourceEntity.getPort()) |
|
389 |
&& dataSourceEntity.getDb() != null && !"".equals(dataSourceEntity.getDb())) { |
|
390 |
String hostAndPort = dataSourceEntity.getHost() + "_" + dataSourceEntity.getPort(); |
|
391 |
if (!map.containsKey(hostAndPort)) { |
|
392 |
map.put(hostAndPort, dataSourceEntity); |
|
393 |
} |
|
394 |
} |
|
395 |
|
|
396 |
} |
|
397 |
|
|
398 |
return map; |
a6a76f
|
399 |
} |
d3a04a
|
400 |
|
F |
401 |
|
a6a76f
|
402 |
/** |
d3a04a
|
403 |
* 把不同数据库主机IP和端口号的 数据源取出来,用于计算数据库使用空间 |
F |
404 |
* |
a6a76f
|
405 |
* @return |
F |
406 |
*/ |
d3a04a
|
407 |
public static List<DataSourceEntity> getDataSourceListByHostAndPort(String host, String port) { |
F |
408 |
List<DataSourceEntity> list = new ArrayList<DataSourceEntity>(); |
|
409 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
|
410 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
411 |
if (dataSourceEntity.getHost() != null && dataSourceEntity.getPort() != null && dataSourceEntity.getDb() != null |
|
412 |
&& !"".equals(dataSourceEntity.getHost()) |
|
413 |
&& !"".equals(dataSourceEntity.getPort()) |
|
414 |
&& !"".equals(dataSourceEntity.getDb()) |
|
415 |
&& dataSourceEntity.getHost().equals(host)) { |
|
416 |
//String hostAndPort = dataSourceEntity.getHost() + "_" + dataSourceEntity.getPort(); |
|
417 |
list.add(dataSourceEntity); |
|
418 |
} |
|
419 |
|
|
420 |
} |
|
421 |
|
|
422 |
return list; |
a6a76f
|
423 |
} |
d3a04a
|
424 |
|
a6a76f
|
425 |
/** |
F |
426 |
* 根据客户 cltid 获取数据源 DataSourceEntity 对象 |
d3a04a
|
427 |
* |
a6a76f
|
428 |
* @param cltId |
F |
429 |
* @return |
|
430 |
*/ |
|
431 |
public static DataSourceEntity getDataSourceMapByCltId(int cltId) { |
d3a04a
|
432 |
if (cltId == 0) return null; |
a6a76f
|
433 |
|
d3a04a
|
434 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
435 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
436 |
if (dataSourceEntity.getCltId() != 0 && dataSourceEntity.getCltId() == cltId) { |
|
437 |
return dataSourceEntity; |
|
438 |
} |
|
439 |
} |
|
440 |
return null; |
a6a76f
|
441 |
} |
F |
442 |
|
|
443 |
/** |
|
444 |
* 根据微信企业号 corpId 获取数据源 DataSourceEntity 对象 |
d3a04a
|
445 |
* |
3c1697
|
446 |
* @param corpId |
a6a76f
|
447 |
* @return |
F |
448 |
*/ |
|
449 |
public static DataSourceEntity getDataSourceMapByCorpId(String corpId) { |
d3a04a
|
450 |
if (corpId == null) return null; |
3c1697
|
451 |
DataSourceEntity dataSourceEntity=null; |
d3a04a
|
452 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
3c1697
|
453 |
dataSourceEntity = entry.getValue(); |
d3a04a
|
454 |
if (dataSourceEntity.getCorpId() != null && dataSourceEntity.getCorpId().toLowerCase().equals(corpId.toLowerCase())) { |
F |
455 |
return dataSourceEntity; |
|
456 |
} |
|
457 |
} |
3c1697
|
458 |
DemoConstant demoConstant=new DemoConstant(); |
F |
459 |
demoConstant.setCorpId(corpId); |
|
460 |
return getDataSourceFromDemoDataBase(demoConstant, dataSourceEntity); |
a6a76f
|
461 |
} |
F |
462 |
|
d3a04a
|
463 |
/** |
F |
464 |
* 根据accesske获得数据源 |
|
465 |
* |
|
466 |
* @param accesskey |
|
467 |
* @return |
|
468 |
*/ |
|
469 |
public static DataSourceEntity getDataSourceByAccesskey(String accesskey) { |
|
470 |
if (accesskey == null) return null; |
a6a76f
|
471 |
|
d3a04a
|
472 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
473 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
474 |
if (dataSourceEntity.getSystemAccessKey() != null && MD5Util.string2MD5(dataSourceEntity.getSystemAccessKey()).equals(accesskey)) { |
|
475 |
return dataSourceEntity; |
|
476 |
} |
|
477 |
} |
|
478 |
return null; |
|
479 |
} |
|
480 |
|
a6a76f
|
481 |
/** |
F |
482 |
* 获取微信企业号所有数据源 DataSourceEntity 对象 |
d3a04a
|
483 |
* |
a6a76f
|
484 |
* @return |
F |
485 |
*/ |
|
486 |
public static List<DataSourceEntity> getDataSourceMapsByCorpId() { |
d3a04a
|
487 |
List<DataSourceEntity> list = new ArrayList<DataSourceEntity>(); |
a6a76f
|
488 |
|
d3a04a
|
489 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
490 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
491 |
if (dataSourceEntity.getCorpId() != null && !dataSourceEntity.getCorpId().equals("") && dataSourceEntity.getActived()) { |
|
492 |
list.add(dataSourceEntity); |
|
493 |
} |
|
494 |
} |
|
495 |
return list; |
|
496 |
} |
|
497 |
|
a6a76f
|
498 |
/** |
F |
499 |
* 根据微信企公众号 MpAppId 获取数据源 DataSourceEntity 对象 |
d3a04a
|
500 |
* |
3c1697
|
501 |
* @param mpAppId |
a6a76f
|
502 |
* @return |
F |
503 |
*/ |
|
504 |
public static DataSourceEntity getDataSourceMapByMpAppId(String mpAppId) { |
d3a04a
|
505 |
if (mpAppId == null) return null; |
3c1697
|
506 |
DataSourceEntity dataSourceEntity=null; |
d3a04a
|
507 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
3c1697
|
508 |
dataSourceEntity = entry.getValue(); |
d3a04a
|
509 |
if (dataSourceEntity.getMpAppId() != null && dataSourceEntity.getMpAppId().toLowerCase().equals(mpAppId.toLowerCase())) { |
F |
510 |
return dataSourceEntity; |
|
511 |
} |
|
512 |
} |
3c1697
|
513 |
DemoConstant demoConstant=new DemoConstant(); |
F |
514 |
demoConstant.setMpAppId(mpAppId); |
|
515 |
return getDataSourceFromDemoDataBase(demoConstant, dataSourceEntity); |
d3a04a
|
516 |
} |
a6a76f
|
517 |
|
F |
518 |
/** |
|
519 |
* 根据微信小程序号 MaAppId 获取数据源 DataSourceEntity 对象 |
d3a04a
|
520 |
* |
3c1697
|
521 |
* @param maAppId |
a6a76f
|
522 |
* @return |
F |
523 |
*/ |
|
524 |
public static DataSourceEntity getDataSourceMapByMaAppId(String maAppId) { |
d3a04a
|
525 |
if (maAppId == null) return null; |
3c1697
|
526 |
DataSourceEntity dataSourceEntity=null; |
F |
527 |
List<Map.Entry<String, DataSourceEntity>> collect = dataSourceMap.entrySet().stream().filter(entry -> { |
|
528 |
DataSourceEntity dataSource = entry.getValue(); |
|
529 |
if (dataSource.getMiniAppId() != null && dataSource.getMiniAppId().toLowerCase().equals(maAppId.toLowerCase())) { |
|
530 |
return true; |
d3a04a
|
531 |
} |
3c1697
|
532 |
return false; |
F |
533 |
}).collect(Collectors.toList()); |
|
534 |
if(collect!=null&&collect.size()>0) { |
|
535 |
dataSourceEntity = collect.get(0).getValue(); |
d3a04a
|
536 |
} |
3c1697
|
537 |
DemoConstant demoConstant=new DemoConstant(); |
F |
538 |
demoConstant.setMiniAppId(maAppId); |
|
539 |
return getDataSourceFromDemoDataBase(demoConstant, dataSourceEntity); |
d3a04a
|
540 |
} |
a6a76f
|
541 |
|
F |
542 |
/** |
d3a04a
|
543 |
* 按小程序原始ID找数据源 |
F |
544 |
* |
a6a76f
|
545 |
* @param miniAppOrgId |
F |
546 |
* @return |
|
547 |
*/ |
|
548 |
public static DataSourceEntity getDataSourceMapByMaOrgId(String miniAppOrgId) { |
d3a04a
|
549 |
if (miniAppOrgId == null) return null; |
a6a76f
|
550 |
|
d3a04a
|
551 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
552 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
553 |
if (dataSourceEntity.getMiniAppOrgId() != null && dataSourceEntity.getMiniAppOrgId().toLowerCase().equals(miniAppOrgId.toLowerCase())) { |
|
554 |
return dataSourceEntity; |
|
555 |
} |
|
556 |
} |
|
557 |
return null; |
|
558 |
} |
|
559 |
|
a6a76f
|
560 |
/** |
F |
561 |
* 获取微信公众号所有数据源 DataSourceEntity 对象 |
d3a04a
|
562 |
* |
a6a76f
|
563 |
* @return |
F |
564 |
*/ |
|
565 |
public static List<DataSourceEntity> getDataSourceMapsByMpAppId() { |
d3a04a
|
566 |
List<DataSourceEntity> list = new ArrayList<DataSourceEntity>(); |
a6a76f
|
567 |
|
d3a04a
|
568 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
569 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
570 |
if (dataSourceEntity.getMpAppId() != null && !dataSourceEntity.getMpAppId().equals("")) { |
|
571 |
list.add(dataSourceEntity); |
|
572 |
} |
|
573 |
} |
|
574 |
return list; |
|
575 |
} |
|
576 |
|
a6a76f
|
577 |
/** |
F |
578 |
* 获取微信小程序所有数据源 DataSourceEntity 对象 |
d3a04a
|
579 |
* |
a6a76f
|
580 |
* @return |
F |
581 |
*/ |
|
582 |
public static List<DataSourceEntity> getDataSourceMapsByMaAppId() { |
d3a04a
|
583 |
List<DataSourceEntity> list = new ArrayList<DataSourceEntity>(); |
a6a76f
|
584 |
|
d3a04a
|
585 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
586 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
587 |
if (dataSourceEntity.getMiniAppId() != null && !dataSourceEntity.getMiniAppId().equals("")) { |
|
588 |
list.add(dataSourceEntity); |
|
589 |
} |
|
590 |
} |
|
591 |
return list; |
|
592 |
} |
|
593 |
|
a6a76f
|
594 |
/** |
F |
595 |
* 按唯一主机名访问,适用于 导购网店 shopping 模块,需要主机名来访问数据源, 获取数据源 DataSourceEntity 对象 |
d3a04a
|
596 |
* |
a6a76f
|
597 |
* @param cltId |
F |
598 |
* @return |
|
599 |
*/ |
|
600 |
public static DataSourceEntity getDataSourceMapByCorpURL(String corpURL) { |
d3a04a
|
601 |
if (corpURL == null || "".equals(corpURL) || "null".equals(corpURL)) { |
F |
602 |
return null; |
|
603 |
} |
|
604 |
corpURL = corpURL.replace("https://", ""); |
|
605 |
corpURL = corpURL.replace("http://", ""); |
|
606 |
// 去掉端口号,Added by Johns Wang, 2016-07-09 |
|
607 |
int i = corpURL.indexOf(":"); |
|
608 |
if (i > 0) { |
|
609 |
corpURL = corpURL.substring(0, i); |
|
610 |
} |
|
611 |
|
|
612 |
|
|
613 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
|
614 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
615 |
String dsCorpUrl = dataSourceEntity.getCorpURL(); |
|
616 |
if (dsCorpUrl != null) { |
|
617 |
dsCorpUrl = dsCorpUrl.replace("https://", ""); |
|
618 |
dsCorpUrl = dsCorpUrl.replace("http://", ""); |
|
619 |
|
|
620 |
// 去掉端口号,Added by Johns Wang, 2020-05-23 |
|
621 |
int k = dsCorpUrl.indexOf(":"); |
|
622 |
if (k > 0) { |
|
623 |
dsCorpUrl = dsCorpUrl.substring(0, k); |
|
624 |
} |
|
625 |
|
|
626 |
|
|
627 |
if (dsCorpUrl.toLowerCase().equals(corpURL.toLowerCase())) { |
|
628 |
return dataSourceEntity; |
|
629 |
} |
|
630 |
} |
|
631 |
} |
0099c9
|
632 |
//---都没有符合,则重新加载数据库生成数据源 by danaus 2023-06-28 15:08 |
F |
633 |
DataSourceEntity dataSourceEntity=null; |
|
634 |
DemoConstant constant=new DemoConstant(); |
|
635 |
constant.setHostUrl(corpURL); |
|
636 |
dataSourceEntity=getDataSourceFromDemoDataBase(constant,dataSourceEntity); |
|
637 |
return dataSourceEntity; |
d3a04a
|
638 |
} |
F |
639 |
|
|
640 |
|
a6a76f
|
641 |
/** |
F |
642 |
* 按高德地图 WebApi Key 获取数据源 DataSourceEntity 对象 |
d3a04a
|
643 |
* |
a6a76f
|
644 |
* @param cltId |
F |
645 |
* @return |
|
646 |
*/ |
|
647 |
public static DataSourceEntity getDataSourceMapByGeoWebApiKey(String geoWebApiKey) { |
d3a04a
|
648 |
if (geoWebApiKey == null) return null; |
a6a76f
|
649 |
|
d3a04a
|
650 |
for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
651 |
DataSourceEntity dataSourceEntity = entry.getValue(); |
|
652 |
if (dataSourceEntity.getGeoWebApiKey() != null && dataSourceEntity.getGeoWebApiKey().toLowerCase().equals(geoWebApiKey.toLowerCase())) { |
|
653 |
return dataSourceEntity; |
|
654 |
} |
|
655 |
} |
|
656 |
return null; |
|
657 |
} |
|
658 |
|
|
659 |
|
a6a76f
|
660 |
/* |
F |
661 |
* (non-Javadoc) |
d3a04a
|
662 |
* |
a6a76f
|
663 |
* @see javax.sql.DataSource#getConnection() |
F |
664 |
*/ |
|
665 |
public Connection getConnection() throws SQLException { |
d3a04a
|
666 |
try { |
F |
667 |
DataSource ds = getDataSource(); |
|
668 |
if (ds == null) { |
|
669 |
throw new SQLException("连接数据库" + SpObserver.getCurrentInstance() + "出错,原因:数据源为 null 值,解决办法:检查数据源的IP、用户名、密码是否正确"); |
|
670 |
} |
|
671 |
|
|
672 |
return getDataSource().getConnection(); |
|
673 |
} catch (SQLException e) { |
|
674 |
throw new SQLException("连接数据库" + SpObserver.getCurrentInstance() + "出错,原因:" + e.getMessage()); |
|
675 |
} catch (Exception e) { |
|
676 |
throw e; |
|
677 |
} |
a6a76f
|
678 |
} |
F |
679 |
|
|
680 |
/* |
|
681 |
* (non-Javadoc) |
d3a04a
|
682 |
* |
a6a76f
|
683 |
* @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) |
F |
684 |
*/ |
|
685 |
public Connection getConnection(String arg0, String arg1) throws SQLException { |
d3a04a
|
686 |
return getDataSource().getConnection(arg0, arg1); |
a6a76f
|
687 |
} |
F |
688 |
|
|
689 |
/* |
|
690 |
* (non-Javadoc) |
d3a04a
|
691 |
* |
a6a76f
|
692 |
* @see javax.sql.DataSource#getLogWriter() |
F |
693 |
*/ |
|
694 |
public PrintWriter getLogWriter() throws SQLException { |
d3a04a
|
695 |
return getDataSource().getLogWriter(); |
a6a76f
|
696 |
} |
F |
697 |
|
|
698 |
/* |
|
699 |
* (non-Javadoc) |
d3a04a
|
700 |
* |
a6a76f
|
701 |
* @see javax.sql.DataSource#getLoginTimeout() |
F |
702 |
*/ |
|
703 |
public int getLoginTimeout() throws SQLException { |
d3a04a
|
704 |
return getDataSource().getLoginTimeout(); |
a6a76f
|
705 |
} |
F |
706 |
|
|
707 |
/* |
|
708 |
* (non-Javadoc) |
d3a04a
|
709 |
* |
a6a76f
|
710 |
* @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter) |
F |
711 |
*/ |
|
712 |
public void setLogWriter(PrintWriter arg0) throws SQLException { |
d3a04a
|
713 |
getDataSource().setLogWriter(arg0); |
a6a76f
|
714 |
} |
F |
715 |
|
|
716 |
/* |
|
717 |
* (non-Javadoc) |
d3a04a
|
718 |
* |
a6a76f
|
719 |
* @see javax.sql.DataSource#setLoginTimeout(int) |
F |
720 |
*/ |
|
721 |
public void setLoginTimeout(int arg0) throws SQLException { |
d3a04a
|
722 |
getDataSource().setLoginTimeout(arg0); |
a6a76f
|
723 |
} |
F |
724 |
|
|
725 |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
d3a04a
|
726 |
this.applicationContext = applicationContext; |
a6a76f
|
727 |
} |
F |
728 |
|
d3a04a
|
729 |
public DataSource getDataSource(String dataSourceName) {// 数据id 例如:_12 |
F |
730 |
String dataSourceNameKey = dataSourceName.substring(1); |
|
731 |
ComboPooledDataSource ds = comMap.get(dataSourceNameKey); |
|
732 |
if (ds != null) { |
|
733 |
return (DataSource) ds; |
|
734 |
} |
|
735 |
|
|
736 |
if (dataSourceName.equals(SpObserver.DEMOXML)) { |
|
737 |
if (demoDataSource == null) { |
|
738 |
DataSourceEntity dataSourceEntity = dataSourceMap.get(SpObserver.DEMOXML); |
|
739 |
if (dataSourceEntity != null) { |
|
740 |
try { |
|
741 |
demoDataSource = new com.yc.dataAccess.ComboPooledDataSource(); |
|
742 |
demoDataSource.setDriverClass(DBHelper.encryptHelper.encrypt(InvGet.driverClass)); |
286e82
|
743 |
demoDataSource.setJdbcUrl(DBHelper.encryptHelper.encrypt("jdbc:sqlserver://" + dataSourceEntity.getHost() + ":" + dataSourceEntity.getPort() + ";encrypt=true;trustServerCertificate=true;databaseName=" + dataSourceEntity.getDb())); |
d3a04a
|
744 |
demoDataSource.setUser(DBHelper.encryptHelper.encrypt(dataSourceEntity.getUserid())); |
F |
745 |
demoDataSource.setPassword(dataSourceEntity.getPassword()); |
|
746 |
demoDataSource.setInitialPoolSize(InvGet.initialPoolSize); |
|
747 |
demoDataSource.setMinPoolSize(InvGet.minPoolSize); |
|
748 |
demoDataSource.setMaxPoolSize(InvGet.maxPoolSize); |
|
749 |
demoDataSource.setAcquireIncrement(InvGet.acquireIncrement); |
|
750 |
demoDataSource.setMaxIdleTime(InvGet.maxIdleTime); |
|
751 |
demoDataSource.setIdleConnectionTestPeriod(InvGet.idleConnectionTestPeriod); |
|
752 |
demoDataSource.setMaxStatements(InvGet.maxStatements); |
|
753 |
demoDataSource.resetPoolManager(); |
|
754 |
|
|
755 |
} catch (Exception e) { |
|
756 |
// TODO Auto-generated catch block |
|
757 |
e.printStackTrace(); |
|
758 |
return null; |
|
759 |
} |
|
760 |
} else { |
|
761 |
demoDataSource = (com.yc.dataAccess.ComboPooledDataSource) this.applicationContext.getBean(SpObserver.DEMOXML); |
|
762 |
} |
|
763 |
} |
|
764 |
return (DataSource) demoDataSource; |
|
765 |
} |
|
766 |
return null; |
a6a76f
|
767 |
} |
F |
768 |
|
|
769 |
/** |
d3a04a
|
770 |
* 按 dbid 刷新数据源 , 如果 dbid 参数为空值,则刷新所有数据源 |
F |
771 |
* |
a6a76f
|
772 |
* @param dbId |
F |
773 |
* @return |
|
774 |
* @throws SQLException |
|
775 |
*/ |
e98f5c
|
776 |
public List<DataSourceEntity> refreshDataSource(String dbId) throws SQLException { |
d3a04a
|
777 |
return refreshDataSource(dbId, null); |
a6a76f
|
778 |
} |
F |
779 |
|
|
780 |
/** |
d3a04a
|
781 |
* 按 dbid 和 domain 刷新数据源 ,如果2个参数全部为空值,则刷新所有数据源 |
F |
782 |
* |
a6a76f
|
783 |
* @param dbId |
F |
784 |
* @param domain |
|
785 |
* @throws SQLException |
|
786 |
*/ |
e98f5c
|
787 |
public List<DataSourceEntity> refreshDataSource(String dbId, String domain) throws SQLException { |
d3a04a
|
788 |
try { |
F |
789 |
if (demoDataSource == null) {// 判断是否加载,未加载就先赋值 |
|
790 |
demoDataSource = (com.yc.dataAccess.ComboPooledDataSource) this.applicationContext.getBean(SpObserver.DEMOXML); |
|
791 |
} |
|
792 |
//removeDataSource(dbId) ; //先删除数据源 |
|
793 |
//System.out.println(this.getClass()+" getInfoAll() 准备刷新数据源"); |
|
794 |
DemoIfc demoIfc = (DemoIfc) FactoryBean.getBean("demo"); |
|
795 |
|
|
796 |
//List<Map<String, Object>> infoList = null; |
|
797 |
List<DataSourceEntity> list = null; |
|
798 |
try { |
|
799 |
SpObserver.setDBtoDemo(); |
|
800 |
list = demoIfc.getDataSource(DemoConstant.getInstance().setDbId(dbId).setActived(null).setDomain(domain)); |
|
801 |
} finally { |
|
802 |
SpObserver.setDBtoInstance(); |
|
803 |
} |
|
804 |
|
|
805 |
//System.out.println(this.getClass()+" getInfoAll() 已经获取了 infoList is null : " + (infoList == null?"false":"true") + " infoList.size : " + infoList.size() ); |
|
806 |
if (list != null) { |
29126a
|
807 |
DemoConstant demoConstant=new DemoConstant(); |
d3a04a
|
808 |
for (DataSourceEntity dataSourceEntity : list) { |
29126a
|
809 |
demoConstant.setDbId(dataSourceEntity.getDbId()+""); |
F |
810 |
setDataSourceByMap(dataSourceEntity,demoConstant); |
d3a04a
|
811 |
|
F |
812 |
} |
|
813 |
} |
|
814 |
//test |
a6a76f
|
815 |
// for (Map.Entry<String, DataSourceEntity> entry : dataSourceMap.entrySet()) { |
F |
816 |
// System.out.println(this.getClass()+" 数据源:" + entry.getValue().getSystemID() + " dbid:" + entry.getValue().getDbId()); |
|
817 |
// } |
e98f5c
|
818 |
return list; |
d3a04a
|
819 |
} catch (Exception e) { |
F |
820 |
e.printStackTrace(); |
|
821 |
throw e; |
|
822 |
} |
a6a76f
|
823 |
} |
d3a04a
|
824 |
|
a6a76f
|
825 |
/** |
d3a04a
|
826 |
* 删除一个数据源 |
F |
827 |
* |
a6a76f
|
828 |
* @param dbid |
F |
829 |
* @return |
|
830 |
*/ |
|
831 |
public boolean removeDataSource(String dbid) { |
d3a04a
|
832 |
try { |
F |
833 |
if (dbid != null && !"".equals(dbid)) { |
|
834 |
ComboPooledDataSource cpds = comMap.get(dbid); |
|
835 |
if (cpds != null) { |
|
836 |
cpds.close(); //关闭数据源,使它不再使用 |
|
837 |
cpds.resetPoolManager(); |
|
838 |
comMap.remove(dbid); //先删除 |
|
839 |
dataSourceMap.remove(dbid); |
|
840 |
return true; |
|
841 |
} |
|
842 |
} |
|
843 |
} catch (Exception e) { |
|
844 |
e.printStackTrace(); |
|
845 |
throw e; |
|
846 |
} |
|
847 |
return false; |
a6a76f
|
848 |
} |
F |
849 |
|
d3a04a
|
850 |
/** |
F |
851 |
* 刷新当前数据源的过期时间 |
|
852 |
*/ |
|
853 |
public void refreshExpiredDataSource() { |
|
854 |
try { |
|
855 |
SpObserver.setDBtoDemo(); |
|
856 |
DemoConstant demoConstant = new DemoConstant(); |
|
857 |
DemoIfc demoIfc = (DemoIfc) FactoryBean.getBean("demo"); |
|
858 |
List<DataSourceEntity> list = demoIfc.getDataSource(demoConstant); |
|
859 |
for (int i = 0; list != null && i < list.size(); i++) { |
|
860 |
DataSourceEntity dataSourceEntity = dataSourceMap.get(list.get(i).getDbId() + ""); |
|
861 |
if (dataSourceEntity != null) { |
|
862 |
dataSourceEntity.setExpiredDate(list.get(i).getExpiredDate()); |
|
863 |
dataSourceEntity.setExpiredDays(list.get(i).getExpiredDays()); |
|
864 |
dataSourceEntity.setActived(list.get(i).getActived()); |
|
865 |
} |
|
866 |
|
|
867 |
} |
|
868 |
} catch (DataAccessException e) { |
|
869 |
e.printStackTrace(); |
|
870 |
return; |
|
871 |
} catch (Exception e) { |
|
872 |
e.printStackTrace(); |
|
873 |
return; |
|
874 |
} finally { |
|
875 |
SpObserver.setDBtoInstance(); |
|
876 |
} |
|
877 |
} |
|
878 |
|
|
879 |
/** |
|
880 |
* 根据attachment.config.properties的相关设置判断是否需要加载当前数据源 |
|
881 |
* |
|
882 |
* @param dataSourceEntity |
|
883 |
* @return |
|
884 |
*/ |
|
885 |
private boolean processDataSource(DataSourceEntity dataSourceEntity) { |
|
886 |
//attachment.config.properties中localHostBindIPs有配置,则只能加载匹配配置的IP的数据源 |
|
887 |
//只在设置了定时任务的服务器上运行 |
0eb813
|
888 |
String isCreateAllPool = AttachmentConfig.get("isCreateAllPool"); |
d3a04a
|
889 |
//定本机IP匹配数据源绑定的IP,未配置就匹配所有 |
F |
890 |
String localHostBindIPs = AttachmentConfig.get("localHostBindIPs"); |
|
891 |
//创建最少的连接数和超时时间,用于开发环境(9010)及定时任务服务,生产环境(9001)不配置 |
|
892 |
String isMiniConnectionSetting = AttachmentConfig.get("isMiniConnectionSetting"); |
|
893 |
boolean isMiniConection = false; |
|
894 |
|
0eb813
|
895 |
//----定时作业服务器,图片服务器 |
F |
896 |
if (StringUtils.isNotBlank(isCreateAllPool) && "1".equals(isCreateAllPool)) { |
d3a04a
|
897 |
//表示是定时作业的服务器,需要加载所有的数据源,localHostBindIPs不需要处理 |
F |
898 |
if (StringUtils.isNotBlank(isMiniConnectionSetting) && "1".equals(isMiniConnectionSetting)) { |
|
899 |
//表示需要创建最小的连接数,也就是jdbc.properties配置文件定义 |
|
900 |
isMiniConection = true; |
|
901 |
|
|
902 |
} |
|
903 |
} |
|
904 |
//----9010 |
|
905 |
else if (StringUtils.isNotBlank(isMiniConnectionSetting) && "1".equals(isMiniConnectionSetting)) { |
|
906 |
//表示需要创建最小的连接数,也就是jdbc.properties配置文件定义 |
|
907 |
// 且执行localHostBindIPs的过滤 |
|
908 |
if (StringUtils.isNotBlank(localHostBindIPs) && StringUtils.isNotBlank(dataSourceEntity.getDomainIpList())) { |
fc5b4f
|
909 |
//处理一个数据源对应多个域名情况,yingchen.onbus.cn;mp.onbus.cn |
644024
|
910 |
String domainIPlist=dataSourceEntity.getDomainIpList(); |
F |
911 |
if(StringUtils.isNotBlank(dataSourceEntity.getDomainStaticIpList())){ |
|
912 |
domainIPlist+=";"+dataSourceEntity.getDomainStaticIpList(); |
|
913 |
} |
|
914 |
String[] ipList=domainIPlist.split(";"); |
fc5b4f
|
915 |
for(String ip:ipList) { |
F |
916 |
if ((";" + localHostBindIPs + ";").contains(";" + ip + ";")) { |
|
917 |
//判断当前数据源所绑定的IP是否属于定义的IP名单里面 |
|
918 |
isMiniConection = true; |
|
919 |
break; |
|
920 |
} |
|
921 |
} |
|
922 |
if(!isMiniConection) { |
d3a04a
|
923 |
return false;//不属于当前服务器的不需要生成数据源 |
F |
924 |
} |
|
925 |
} else { |
|
926 |
//没设置则不需要过滤 |
|
927 |
isMiniConection = true; |
|
928 |
} |
|
929 |
} |
|
930 |
//---9001 |
4bb845
|
931 |
else if (StringUtils.isNotBlank(localHostBindIPs) && StringUtils.isNotBlank(dataSourceEntity.getDomainIpList())) { |
644024
|
932 |
String domainIPlist=dataSourceEntity.getDomainIpList(); |
F |
933 |
if(StringUtils.isNotBlank(dataSourceEntity.getDomainStaticIpList())){ |
|
934 |
domainIPlist+=";"+dataSourceEntity.getDomainStaticIpList(); |
|
935 |
} |
|
936 |
String[] ipList=domainIPlist.split(";"); |
fc5b4f
|
937 |
for(String ip:ipList) { |
F |
938 |
if ((";" + localHostBindIPs + ";").contains(";" + ip + ";")) { |
d3a04a
|
939 |
//默认取数据源定义的配置,如果没配置再取配置文件的值 |
29126a
|
940 |
setDataSourceInfo(dataSourceEntity);//取数据源配置 |
d3a04a
|
941 |
return true; |
F |
942 |
} |
fc5b4f
|
943 |
} |
4bb845
|
944 |
}else{ |
F |
945 |
//什么也不配置,加载所有 |
|
946 |
isMiniConection=true; |
d3a04a
|
947 |
} |
F |
948 |
if (isMiniConection) { |
29126a
|
949 |
//默认配置 |
d3a04a
|
950 |
dataSourceEntity.setDataSourceMaxIdleTime(InvGet.maxIdleTime); |
F |
951 |
dataSourceEntity.setDataSourceMinPoolSize(InvGet.minPoolSize); |
|
952 |
dataSourceEntity.setDataSourceMaxStatements(InvGet.maxStatements); |
|
953 |
dataSourceEntity.setDataSourceMaxPoolSize(InvGet.maxPoolSize); |
|
954 |
dataSourceEntity.setDataSourceAcquireIncrement(InvGet.acquireIncrement); |
|
955 |
dataSourceEntity.setDataSourceInitialPoolSize(InvGet.initialPoolSize); |
|
956 |
dataSourceEntity.setDataSourceIdleConnectionTestPeriod(InvGet.idleConnectionTestPeriod); |
|
957 |
return true; |
|
958 |
} |
|
959 |
return false; |
|
960 |
} |
|
961 |
|
a6a76f
|
962 |
/** |
484907
|
963 |
* 获得一个真实的数据源连接 |
d3a04a
|
964 |
* |
a6a76f
|
965 |
*/ |
29126a
|
966 |
public void setDataSourceByMap(DataSourceEntity dataSourceEntity,DemoConstant demoConstant) { |
a6a76f
|
967 |
|
d3a04a
|
968 |
try { |
484907
|
969 |
if(StringUtils.isNotBlank(demoConstant.getDbId())&&demoConstant.isNotSkipCheck()) {//存在id且设置要检查 |
29126a
|
970 |
if (!processDataSource(dataSourceEntity)) {return;}//不是当前服务器的数据源,不进行加载 |
F |
971 |
}else { |
|
972 |
//小程序,公众号需要获取数据连接池,不能调用processDataSource过滤掉 |
|
973 |
setDataSourceInfo(dataSourceEntity); |
|
974 |
} |
484907
|
975 |
boolean found = true; |
d3a04a
|
976 |
ComboPooledDataSource cpds = comMap.get(dataSourceEntity.getDbId() + ""); |
F |
977 |
if (cpds == null) { |
|
978 |
found = false; |
|
979 |
cpds = new ComboPooledDataSource(); |
|
980 |
} |
|
981 |
|
|
982 |
cpds.setDriverClass(InvGet.driverClass); |
286e82
|
983 |
cpds.setJdbcUrl("jdbc:sqlserver://" + dataSourceEntity.getHost() + ":" + dataSourceEntity.getPort() + ";encrypt=true;trustServerCertificate=true;databaseName=" + dataSourceEntity.getDb() + ""); |
d3a04a
|
984 |
|
F |
985 |
//加密 2次 |
|
986 |
String plainTextPassword = ChangePassword.getDecryptPassword(dataSourceEntity.getPassword()); |
|
987 |
|
|
988 |
cpds.setUser(dataSourceEntity.getUserid()); |
|
989 |
cpds.setPassword(plainTextPassword); |
|
990 |
//默认加载 |
|
991 |
cpds.setInitialPoolSize(dataSourceEntity.getDataSourceInitialPoolSize()); // demoDataSource.getInitialPoolSize()); |
|
992 |
cpds.setMinPoolSize(dataSourceEntity.getDataSourceMinPoolSize());// demoDataSource.getMinPoolSize()); |
|
993 |
cpds.setMaxPoolSize(dataSourceEntity.getDataSourceMaxPoolSize()); // demoDataSource.getMaxPoolSize()); |
|
994 |
cpds.setAcquireIncrement(dataSourceEntity.getDataSourceAcquireIncrement()); // demoDataSource.getAcquireIncrement()); |
|
995 |
cpds.setMaxIdleTime(dataSourceEntity.getDataSourceMaxIdleTime()); // demoDataSource.getMaxIdleTime()); |
|
996 |
cpds.setIdleConnectionTestPeriod(dataSourceEntity.getDataSourceIdleConnectionTestPeriod()); // demoDataSource.getIdleConnectionTestPeriod()); |
|
997 |
cpds.setMaxStatements(dataSourceEntity.getDataSourceMaxStatements()); // demoDataSource.getMaxStatements()); |
|
998 |
|
|
999 |
cpds.resetPoolManager(); |
|
1000 |
// TODO 连接需要增加信息 在此后面添加 暂时不做处理 |
|
1001 |
String dbId = dataSourceEntity.getDemoDataSource() != null && !"".equals(dataSourceEntity.getDemoDataSource()) ? dataSourceEntity.getDemoDataSource() : dataSourceEntity.getDbId() + ""; |
|
1002 |
if (!found) { |
|
1003 |
//comMap.remove(dataSourceEntity.getDbId()+"") ; //先删除 , 不能删除,因为这是数据源连接,如果删除了就变成死链接,数据源会提示: APP Dead Lock!!! |
|
1004 |
comMap.put(dbId, cpds); |
|
1005 |
} |
|
1006 |
dataSourceMap.remove(dbId); //这个只是个集合,可以先删除然后再新增 |
|
1007 |
dataSourceMap.put(dbId, dataSourceEntity); |
|
1008 |
} catch (Exception e) { |
|
1009 |
log.error(e.getMessage()); |
|
1010 |
e.printStackTrace(); |
|
1011 |
} |
a6a76f
|
1012 |
} |
29126a
|
1013 |
public DataSourceEntity setDataSourceInfo(DataSourceEntity dataSourceEntity) { |
3c1697
|
1014 |
|
F |
1015 |
try { |
|
1016 |
dataSourceEntity.setDataSourceMaxIdleTime( |
|
1017 |
dataSourceEntity.getDataSourceMaxIdleTime() != null ? dataSourceEntity.getDataSourceMaxIdleTime() : InvGet.maxIdleTime); |
|
1018 |
dataSourceEntity.setDataSourceMinPoolSize( |
|
1019 |
dataSourceEntity.getDataSourceMinPoolSize() != null ? dataSourceEntity.getDataSourceMinPoolSize() : InvGet.minPoolSize); |
|
1020 |
dataSourceEntity.setDataSourceMaxStatements( |
|
1021 |
dataSourceEntity.getDataSourceMaxStatements() != null ? dataSourceEntity.getDataSourceMaxStatements() : InvGet.maxStatements); |
|
1022 |
dataSourceEntity.setDataSourceMaxPoolSize( |
|
1023 |
dataSourceEntity.getDataSourceMaxPoolSize() != null ? dataSourceEntity.getDataSourceMaxPoolSize() : InvGet.maxPoolSize); |
|
1024 |
dataSourceEntity.setDataSourceAcquireIncrement( |
|
1025 |
dataSourceEntity.getDataSourceAcquireIncrement() != null ? dataSourceEntity.getDataSourceAcquireIncrement() : InvGet.acquireIncrement); |
|
1026 |
dataSourceEntity.setDataSourceInitialPoolSize( |
|
1027 |
dataSourceEntity.getDataSourceInitialPoolSize() != null ? dataSourceEntity.getDataSourceInitialPoolSize() : InvGet.initialPoolSize); |
|
1028 |
dataSourceEntity.setDataSourceIdleConnectionTestPeriod( |
|
1029 |
dataSourceEntity.getDataSourceIdleConnectionTestPeriod() != null ? dataSourceEntity.getDataSourceIdleConnectionTestPeriod() : InvGet.idleConnectionTestPeriod); |
|
1030 |
|
|
1031 |
} catch (Exception e) { |
|
1032 |
log.error(e.getMessage()); |
|
1033 |
e.printStackTrace(); |
29126a
|
1034 |
return null; |
3c1697
|
1035 |
} |
29126a
|
1036 |
return dataSourceEntity; |
3c1697
|
1037 |
} |
a6a76f
|
1038 |
|
F |
1039 |
public static Map<String, ComboPooledDataSource> getcomm() { |
d3a04a
|
1040 |
return comMap; |
a6a76f
|
1041 |
} |
F |
1042 |
|
|
1043 |
|
|
1044 |
public DataSource getDataSource() { |
d3a04a
|
1045 |
return getDataSource(SpObserver.getCurrentInstance()); |
a6a76f
|
1046 |
} |
F |
1047 |
|
|
1048 |
@Override |
|
1049 |
public boolean isWrapperFor(Class<?> iface) throws SQLException { |
d3a04a
|
1050 |
return false; |
a6a76f
|
1051 |
} |
F |
1052 |
|
|
1053 |
@Override |
|
1054 |
public <T> T unwrap(Class<T> iface) throws SQLException { |
d3a04a
|
1055 |
return null; |
a6a76f
|
1056 |
} |
F |
1057 |
|
|
1058 |
@Override |
|
1059 |
public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
d3a04a
|
1060 |
return null; |
a6a76f
|
1061 |
} |
F |
1062 |
} |