系统配置类抽象化

This commit is contained in:
jinyu
2016-05-24 20:57:51 +08:00
parent f6c12e07f3
commit 18303642f2
20 changed files with 527 additions and 523 deletions
@@ -41,7 +41,7 @@ public class WeixinException extends Exception {
return code;
}
public String getErrorMsg() {
public String getErrorDesc() {
return desc;
}
@@ -51,8 +51,8 @@ public class WeixinException extends Exception {
@Override
public String getMessage() {
StringBuilder buf = new StringBuilder();
if (StringUtil.isNotBlank(code)) {
StringBuilder buf = new StringBuilder();
buf.append(code).append(" >> ").append(desc);
String text = getErrorText();
if (StringUtil.isNotBlank(text)) {
@@ -36,11 +36,11 @@ import com.foxinmy.weixin4j.payment.mch.RedpacketSendResult;
import com.foxinmy.weixin4j.payment.mch.RefundRecord;
import com.foxinmy.weixin4j.payment.mch.RefundResult;
import com.foxinmy.weixin4j.payment.mch.SettlementRecord;
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
import com.foxinmy.weixin4j.type.BillType;
import com.foxinmy.weixin4j.type.CurrencyType;
import com.foxinmy.weixin4j.type.CustomsCity;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.util.Weixin4jSettings;
/**
* 微信支付接口实现
@@ -85,14 +85,14 @@ public class WeixinPayProxy {
*
* @param settings
* 支付相关配置信息
* @see com.foxinmy.weixin4j.util.Weixin4jSettings
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
*/
public WeixinPayProxy(Weixin4jSettings settings) {
this.settings = settings;
this.payApi = new PayApi(settings.getWeixinPayAccount());
this.couponApi = new CouponApi(settings.getWeixinPayAccount());
this.cashApi = new CashApi(settings.getWeixinPayAccount());
this.customsApi = new CustomsApi(settings.getWeixinPayAccount());
this.payApi = new PayApi(settings.getPayAccount());
this.couponApi = new CouponApi(settings.getPayAccount());
this.cashApi = new CashApi(settings.getPayAccount());
this.customsApi = new CustomsApi(settings.getPayAccount());
}
/**
@@ -100,8 +100,8 @@ public class WeixinPayProxy {
*
* @return
*/
public WeixinPayAccount getPayAccount() {
return this.settings.getWeixinPayAccount();
public WeixinPayAccount getWeixinPayAccount() {
return this.settings.getPayAccount();
}
/**
@@ -0,0 +1,90 @@
package com.foxinmy.weixin4j.setting;
import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenStorager;
/**
* 系统配置相关
*
* @className SystemSettings
* @author jinyu(foxinmy@gmail.com)
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public abstract class SystemSettings<T> {
/**
* 账号信息
*/
private T account;
/**
* Http参数
*/
private HttpParams httpParams;
/**
* token存储方式 默认为FileTokenStorager
*/
private TokenStorager tokenStorager;
/**
* 系统临时目录
*/
private String tmpdir;
/**
* @param account
*/
public SystemSettings(T account) {
this.account = account;
}
public T getAccount() {
return account;
}
public HttpParams getHttpParams() {
return httpParams;
}
public HttpParams getHttpParams0() {
if (httpParams == null) {
return new HttpParams();
}
return httpParams;
}
public String getTmpdir() {
return tmpdir;
}
public abstract String getTmpdir0();
public TokenStorager getTokenStorager() {
return tokenStorager;
}
public TokenStorager getTokenStorager0() {
if (tokenStorager == null) {
return new FileTokenStorager(getTmpdir0());
}
return tokenStorager;
}
public void setHttpParams(HttpParams httpParams) {
this.httpParams = httpParams;
}
public void setTmpdir(String tmpdir) {
this.tmpdir = tmpdir;
}
public void setTokenStorager(TokenStorager tokenStorager) {
this.tokenStorager = tokenStorager;
}
@Override
public String toString() {
return "account=" + account + ", httpParams=" + httpParams
+ ",tokenStorager=" + tokenStorager + ", tmpdir=" + tmpdir;
}
}
@@ -0,0 +1,106 @@
package com.foxinmy.weixin4j.setting;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 微信配置相关
*
* @className Weixin4jSettings
* @author jinyu(foxinmy@gmail.com)
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jSettings extends SystemSettings<WeixinAccount> {
/**
* 微信支付账号信息
*/
private WeixinPayAccount weixinPayAccount;
/**
* 支付接口需要的证书文件(*.p12)
*/
private String certificateFile;
/**
* 默认使用weixin4j.properties配置的信息
*/
public Weixin4jSettings() {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinPayAccount.class), null);
}
/**
* 支付代理接口
*
* @param weixinPayAccount
* 商户信息
* @param certificateFile
* 支付接口需要的证书文件(*.p12),比如退款接口
*/
public Weixin4jSettings(WeixinPayAccount weixinPayAccount,
String certificateFile) {
this(weixinPayAccount);
this.certificateFile = certificateFile;
}
/**
* 支付代理接口
*
* @param weixinPayAccount
* 商户信息
*/
public Weixin4jSettings(WeixinPayAccount weixinPayAccount) {
this(new WeixinAccount(weixinPayAccount.getId(),
weixinPayAccount.getSecret()));
this.weixinPayAccount = weixinPayAccount;
}
/**
* 账号信息
*
* @param account
*/
public Weixin4jSettings(WeixinAccount account) {
super(account);
}
public WeixinPayAccount getPayAccount() {
return weixinPayAccount;
}
@Override
public String getTmpdir0() {
if (StringUtil.isBlank(getTmpdir())) {
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
System.getProperty("java.io.tmpdir"));
}
return getTmpdir();
}
public String getCertificateFile() {
return certificateFile;
}
public String getCertificateFile0() {
if (StringUtil.isBlank(certificateFile)) {
return Weixin4jConfigUtil.getClassPathValue(
"weixin4j.certificate.file", "classpath:ca.p12");
}
return certificateFile;
}
public void setCertificateFile(String certificateFile) {
this.certificateFile = certificateFile;
}
@Override
public String toString() {
return "Weixin4jSettings [weixinPayAccount=" + weixinPayAccount
+ ", certificateFile=" + certificateFile + ", "
+ super.toString() + "]";
}
}
@@ -22,7 +22,8 @@ public interface CacheCreator<T> {
/**
* 创建Cache
*
* @return
* @throws WeixinException
* @return 缓存对象
*/
public T create() throws WeixinException;
}
@@ -1,7 +1,5 @@
package com.foxinmy.weixin4j.token;
import com.foxinmy.weixin4j.exception.WeixinException;
/**
* Cache的存储
*
@@ -18,9 +16,8 @@ public interface CacheStorager<T> {
* @param key
* 缓存key
* @return 缓存对象
* @throws WeixinException
*/
T lookup(String key) throws WeixinException;
T lookup(String key);
/**
* 缓存新的对象
@@ -30,9 +27,8 @@ public interface CacheStorager<T> {
*
* @param cache
* 将要缓存的对象
* @throws WeixinException
*/
void caching(String key, T cache) throws WeixinException;
void caching(String key, T cache);
/**
* 移除缓存对象
@@ -41,10 +37,10 @@ public interface CacheStorager<T> {
* 缓存key
* @return 移除的对象
*/
T evict(String key) throws WeixinException;
T evict(String key);
/**
* 清除所有缓存对象(<font color="red">请慎重</font>)
*/
void clear() throws WeixinException;
void clear();
}
@@ -6,14 +6,13 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.util.FileUtil;
import com.foxinmy.weixin4j.xml.XmlStream;
/**
* 用File形式保存Token信息
*
*
* @className FileTokenStorager
* @author jinyu(foxinmy@gmail.com)
* @date 2015年1月9日
@@ -28,7 +27,7 @@ public class FileTokenStorager implements TokenStorager {
}
@Override
public Token lookup(String cacheKey) throws WeixinException {
public Token lookup(String cacheKey) {
File token_file = new File(String.format("%s/%s.xml", cachePath,
cacheKey));
try {
@@ -45,24 +44,24 @@ public class FileTokenStorager implements TokenStorager {
}
return null;
} catch (IOException e) {
throw new WeixinException(e);
throw new RuntimeException(e);
}
}
@Override
public void caching(String cacheKey, Token token) throws WeixinException {
public void caching(String cacheKey, Token token) {
try {
XmlStream.toXML(
token,
new FileOutputStream(new File(String.format("%s/%s.xml",
cachePath, cacheKey))));
} catch (IOException e) {
throw new WeixinException(e);
throw new RuntimeException(e);
}
}
@Override
public Token evict(String cacheKey) throws WeixinException {
public Token evict(String cacheKey) {
Token token = null;
File token_file = new File(String.format("%s/%s.xml", cachePath,
cacheKey));
@@ -79,7 +78,7 @@ public class FileTokenStorager implements TokenStorager {
}
@Override
public void clear() throws WeixinException {
public void clear() {
File[] files = new File(cachePath).listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
@@ -28,12 +27,12 @@ public class MemcacheTokenStorager implements TokenStorager {
}
@Override
public Token lookup(String cacheKey) throws WeixinException {
public Token lookup(String cacheKey) {
return (Token) mc.get(cacheKey);
}
@Override
public void caching(String cacheKey, Token token) throws WeixinException {
public void caching(String cacheKey, Token token) {
if (token.getExpiresIn() > 0) {
mc.set(cacheKey, token,
new Date(token.getCreateTime() + token.getExpiresIn()
@@ -44,14 +43,14 @@ public class MemcacheTokenStorager implements TokenStorager {
}
@Override
public Token evict(String cacheKey) throws WeixinException {
public Token evict(String cacheKey) {
Token token = lookup(cacheKey);
mc.delete(cacheKey);
return token;
}
@Override
public void clear() throws WeixinException {
public void clear() {
throw new UnsupportedOperationException();
}
@@ -3,12 +3,11 @@ package com.foxinmy.weixin4j.token;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
/**
* 用内存保存Token信息(不推荐使用)
*
*
* @className MemoryTokenStorager
* @author jinyu(foxinmy@gmail.com)
* @date 2016年1月24日
@@ -24,7 +23,7 @@ public class MemoryTokenStorager implements TokenStorager {
}
@Override
public Token lookup(String cacheKey) throws WeixinException {
public Token lookup(String cacheKey) {
Token token = this.CONMAP.get(cacheKey);
if (token != null) {
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
@@ -36,17 +35,17 @@ public class MemoryTokenStorager implements TokenStorager {
}
@Override
public void caching(String cacheKey, Token token) throws WeixinException {
public void caching(String cacheKey, Token token) {
this.CONMAP.put(cacheKey, token);
}
@Override
public Token evict(String cacheKey) throws WeixinException {
public Token evict(String cacheKey) {
return this.CONMAP.remove(cacheKey);
}
@Override
public void clear() throws WeixinException {
public void clear() {
this.CONMAP.clear();
}
}
@@ -9,7 +9,6 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
/**
@@ -56,7 +55,7 @@ public class RedisTokenStorager implements TokenStorager {
}
@Override
public Token lookup(String cacheKey) throws WeixinException {
public Token lookup(String cacheKey) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
@@ -73,7 +72,7 @@ public class RedisTokenStorager implements TokenStorager {
}
@Override
public void caching(String cacheKey, Token token) throws WeixinException {
public void caching(String cacheKey, Token token) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
@@ -112,7 +111,7 @@ public class RedisTokenStorager implements TokenStorager {
}
@Override
public Token evict(String cacheKey) throws WeixinException {
public Token evict(String cacheKey) {
Token token = lookup(cacheKey);
Jedis jedis = null;
try {
@@ -127,7 +126,7 @@ public class RedisTokenStorager implements TokenStorager {
}
@Override
public void clear() throws WeixinException {
public void clear() {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
@@ -1,158 +0,0 @@
package com.foxinmy.weixin4j.util;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenStorager;
/**
* 微信配置相关
*
* @className Weixin4jSettings
* @author jinyu(foxinmy@gmail.com)
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jSettings {
/**
* 微信支付账号信息
*/
private WeixinPayAccount weixinPayAccount;
/**
* 微信账号信息
*/
private WeixinAccount weixinAccount;
/**
* Http参数
*/
private HttpParams httpParams;
/**
* token存储方式 默认为FileTokenStorager
*/
private TokenStorager tokenStorager;
/**
* 系统临时目录
*/
private String tmpdir;
/**
* 支付接口需要的证书文件(*.p12)
*/
private String certificateFile;
/**
* 默认使用weixin4j.properties配置的信息
*/
public Weixin4jSettings() {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"), WeixinPayAccount.class), null);
}
/**
* 支付代理接口
*
* @param weixinPayAccount
* 商户信息
* @param certificateFile
* 支付接口需要的证书文件(*.p12),比如退款接口
*/
public Weixin4jSettings(WeixinPayAccount weixinPayAccount, String certificateFile) {
this(weixinPayAccount);
this.certificateFile = certificateFile;
}
/**
* 支付代理接口
*
* @param weixinPayAccount
* 商户信息
*/
public Weixin4jSettings(WeixinPayAccount weixinPayAccount) {
this.weixinPayAccount = weixinPayAccount;
this.weixinAccount = new WeixinAccount(weixinPayAccount.getId(), weixinPayAccount.getSecret());
}
/**
* 普通代理接口
*
* @param weixinAccount
*/
public Weixin4jSettings(WeixinAccount weixinAccount) {
this.weixinAccount = weixinAccount;
}
public WeixinPayAccount getWeixinPayAccount() {
return weixinPayAccount;
}
public WeixinAccount getWeixinAccount() {
return weixinAccount;
}
public HttpParams getHttpParams() {
return httpParams;
}
public HttpParams getHttpParams0() {
if (httpParams == null) {
return new HttpParams();
}
return httpParams;
}
public String getTmpdir() {
return tmpdir;
}
public String getTmpdir0() {
if (StringUtil.isBlank(tmpdir)) {
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir", System.getProperty("java.io.tmpdir"));
}
return tmpdir;
}
public TokenStorager getTokenStorager() {
return tokenStorager;
}
public TokenStorager getTokenStorager0() {
if (tokenStorager == null) {
return new FileTokenStorager(getTmpdir0());
}
return tokenStorager;
}
public String getCertificateFile() {
return certificateFile;
}
public String getCertificateFile0() {
if (StringUtil.isBlank(certificateFile)) {
return Weixin4jConfigUtil.getClassPathValue("weixin4j.certificate.file", "classpath:ca.p12");
}
return certificateFile;
}
public void setHttpParams(HttpParams httpParams) {
this.httpParams = httpParams;
}
public void setTmpdir(String tmpdir) {
this.tmpdir = tmpdir;
}
public void setTokenStorager(TokenStorager tokenStorager) {
this.tokenStorager = tokenStorager;
}
public void setCertificateFile(String certificateFile) {
this.certificateFile = certificateFile;
}
@Override
public String toString() {
return "Weixin4jSettings [weixinAccount=" + weixinAccount + ", httpParams=" + httpParams + ",tokenStorager="
+ tokenStorager + ", tmpdir=" + tmpdir + ", certificateFile= " + certificateFile + "]";
}
}