mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-11 09:20:30 +08:00
chore: 工作区反编译与 Maven/文档/脚本同步到发布分支
- artifacts/decompiled 树与相关源码变更 - maven-cw-elevator-application 业务 docs 与 package-info - scripts 下 formatter 校验与辅助脚本 - 其他子工程/接口与发布线一并纳入版本控制 Made-with: Cursor Former-commit-id: e102e8cab64e575bcd23c9a66a598aa1892bb492
This commit is contained in:
+4
-6
@@ -6,13 +6,11 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@Target({ ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface CloudwalkParamsValidate {
|
||||
int[] argsIndexs() default {0};
|
||||
|
||||
Class<?>[] groups() default {javax.validation.groups.Default.class};
|
||||
int[] argsIndexs() default { 0 };
|
||||
|
||||
Class<?>[] groups() default { javax.validation.groups.Default.class };
|
||||
}
|
||||
|
||||
|
||||
|
||||
+6
-8
@@ -6,15 +6,13 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Target({ ElementType.PARAMETER, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface CloudwalkTrace {
|
||||
String module() default "";
|
||||
|
||||
String operation() default "";
|
||||
|
||||
boolean code() default false;
|
||||
String module() default "";
|
||||
|
||||
String operation() default "";
|
||||
|
||||
boolean code() default false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-4
@@ -6,9 +6,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Target({ ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataFieldEncrypt {}
|
||||
|
||||
|
||||
public @interface DataFieldEncrypt {
|
||||
}
|
||||
|
||||
+3
-4
@@ -6,9 +6,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.TYPE})
|
||||
@Target({ ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataObjectEncrypt {}
|
||||
|
||||
|
||||
public @interface DataObjectEncrypt {
|
||||
}
|
||||
|
||||
+3
-4
@@ -8,10 +8,9 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@Target({ ElementType.FIELD, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@JsonSerialize(using = I8ndescribedSerialize.class)
|
||||
@JacksonAnnotationsInside
|
||||
public @interface I8ndescribed {}
|
||||
|
||||
|
||||
public @interface I8ndescribed {
|
||||
}
|
||||
|
||||
+8
-10
@@ -9,18 +9,16 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||
@Target({ ElementType.FIELD, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@JsonSerialize(using = SensitiveInfoSerialize.class)
|
||||
@JacksonAnnotationsInside
|
||||
public @interface SensitiveField {
|
||||
SensitiveType type() default SensitiveType.NULL;
|
||||
|
||||
int prefix() default 4;
|
||||
|
||||
int suffix() default 3;
|
||||
|
||||
int split() default 3;
|
||||
SensitiveType type() default SensitiveType.NULL;
|
||||
|
||||
int prefix() default 4;
|
||||
|
||||
int suffix() default 3;
|
||||
|
||||
int split() default 3;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+65
-112
@@ -7,134 +7,87 @@ import cn.cloudwalk.cloud.session.user.UserContext;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public final class CloudwalkCallContext implements Serializable {
|
||||
private static final long serialVersionUID = -6192221637110412715L;
|
||||
private String applicationId;
|
||||
private String serviceCode;
|
||||
private String deviceId;
|
||||
private String logId;
|
||||
private Date callTime;
|
||||
private UserContext user;
|
||||
private ExtendContext ext;
|
||||
private NotesContext notes;
|
||||
private CompanyContext company;
|
||||
|
||||
public String getApplicationId() {
|
||||
return this.applicationId;
|
||||
}
|
||||
|
||||
public void setApplicationId(String applicationId) {
|
||||
this.applicationId = applicationId;
|
||||
}
|
||||
|
||||
public String getDeviceId() {
|
||||
return this.deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceId(String deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getLogId() {
|
||||
return this.logId;
|
||||
}
|
||||
|
||||
public void setLogId(String logId) {
|
||||
this.logId = logId;
|
||||
}
|
||||
|
||||
public Date getCallTime() {
|
||||
return this.callTime;
|
||||
}
|
||||
|
||||
public void setCallTime(Date callTime) {
|
||||
this.callTime = callTime;
|
||||
}
|
||||
|
||||
public UserContext getUser() {
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public void setUser(UserContext user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public CompanyContext getCompany() {
|
||||
return this.company;
|
||||
}
|
||||
|
||||
public void setCompany(CompanyContext company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public ExtendContext getExt() {
|
||||
return this.ext;
|
||||
}
|
||||
|
||||
public void setExt(ExtendContext ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public NotesContext getNotes() {
|
||||
return this.notes;
|
||||
}
|
||||
|
||||
public void setNotes(NotesContext notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public String getServiceCode() {
|
||||
return this.serviceCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public final class CloudwalkCallContext
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = -6192221637110412715L;
|
||||
private String applicationId;
|
||||
private String serviceCode;
|
||||
private String deviceId;
|
||||
private String logId;
|
||||
private Date callTime;
|
||||
private UserContext user;
|
||||
private ExtendContext ext;
|
||||
private NotesContext notes;
|
||||
private CompanyContext company;
|
||||
|
||||
public String getApplicationId() {
|
||||
/* 68 */ return this.applicationId;
|
||||
public void setServiceCode(String serviceCode) {
|
||||
this.serviceCode = serviceCode;
|
||||
}
|
||||
}
|
||||
|
||||
public void setApplicationId(String applicationId) {
|
||||
/* 72 */ this.applicationId = applicationId;
|
||||
}
|
||||
|
||||
public String getDeviceId() {
|
||||
/* 76 */ return this.deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceId(String deviceId) {
|
||||
/* 80 */ this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getLogId() {
|
||||
/* 84 */ return this.logId;
|
||||
}
|
||||
|
||||
public void setLogId(String logId) {
|
||||
/* 88 */ this.logId = logId;
|
||||
}
|
||||
|
||||
public Date getCallTime() {
|
||||
/* 92 */ return this.callTime;
|
||||
}
|
||||
|
||||
public void setCallTime(Date callTime) {
|
||||
/* 96 */ this.callTime = callTime;
|
||||
}
|
||||
|
||||
public UserContext getUser() {
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public void setUser(UserContext user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public CompanyContext getCompany() {
|
||||
return this.company;
|
||||
}
|
||||
|
||||
public void setCompany(CompanyContext company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public ExtendContext getExt() {
|
||||
return this.ext;
|
||||
}
|
||||
|
||||
public void setExt(ExtendContext ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public NotesContext getNotes() {
|
||||
return this.notes;
|
||||
}
|
||||
|
||||
public void setNotes(NotesContext notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public String getServiceCode() {
|
||||
return this.serviceCode;
|
||||
}
|
||||
|
||||
public void setServiceCode(String serviceCode) {
|
||||
this.serviceCode = serviceCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-39
@@ -1,46 +1,23 @@
|
||||
package cn.cloudwalk.cloud.context;
|
||||
|
||||
public final class CloudwalkCallContextBuilder {
|
||||
public static CloudwalkCallContext buildContext(CloudwalkSessionContextHolder sessionContextHolder) {
|
||||
CloudwalkSessionObject session = sessionContextHolder.getSession();
|
||||
CloudwalkCallContext context = new CloudwalkCallContext();
|
||||
|
||||
context.setUser(session.getUser());
|
||||
context.setCompany(session.getCompany());
|
||||
context.setExt(session.getExt());
|
||||
context.setNotes(session.getNotes());
|
||||
|
||||
context.setCallTime(session.getCallTime());
|
||||
context.setApplicationId(session.getApplicationId());
|
||||
context.setServiceCode(session.getServiceCode());
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public final class CloudwalkCallContextBuilder
|
||||
{
|
||||
public static CloudwalkCallContext buildContext(CloudwalkSessionContextHolder sessionContextHolder) {
|
||||
CloudwalkSessionObject session = sessionContextHolder.getSession();
|
||||
CloudwalkCallContext context = new CloudwalkCallContext();
|
||||
|
||||
context.setUser(session.getUser());
|
||||
context.setCompany(session.getCompany());
|
||||
context.setExt(session.getExt());
|
||||
context.setNotes(session.getNotes());
|
||||
|
||||
context.setCallTime(session.getCallTime());
|
||||
context.setApplicationId(session.getApplicationId());
|
||||
context.setServiceCode(session.getServiceCode());
|
||||
|
||||
return context;
|
||||
public static void cleanContext(CloudwalkSessionContextHolder sessionContextHolder) {
|
||||
sessionContextHolder.clearSession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void cleanContext(CloudwalkSessionContextHolder sessionContextHolder) {
|
||||
sessionContextHolder.clearSession();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+11
-39
@@ -1,45 +1,17 @@
|
||||
package cn.cloudwalk.cloud.context;
|
||||
|
||||
public final class CloudwalkSessionContextHolder {
|
||||
private static ThreadLocal<CloudwalkSessionObject> SESSION_OBJECT = new ThreadLocal<>();
|
||||
|
||||
public CloudwalkSessionObject getSession() {
|
||||
return SESSION_OBJECT.get();
|
||||
}
|
||||
|
||||
public void putSession(CloudwalkSessionObject cloudWalkSessionObject) {
|
||||
SESSION_OBJECT.set(cloudWalkSessionObject);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public final class CloudwalkSessionContextHolder
|
||||
{
|
||||
private static ThreadLocal<CloudwalkSessionObject> SESSION_OBJECT = new ThreadLocal<>();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public CloudwalkSessionObject getSession() {
|
||||
return SESSION_OBJECT.get();
|
||||
public void clearSession() {
|
||||
SESSION_OBJECT.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void putSession(CloudwalkSessionObject cloudWalkSessionObject) {
|
||||
SESSION_OBJECT.set(cloudWalkSessionObject);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void clearSession() {
|
||||
SESSION_OBJECT.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+81
-144
@@ -10,170 +10,107 @@ import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public final class CloudwalkSessionObject implements Serializable {
|
||||
private static final long serialVersionUID = -5960069764782340892L;
|
||||
public static final String CLOUDWALK_SESSION_OBJECT_KEY = "cloudSessionObject";
|
||||
private String applicationId;
|
||||
private String serviceCode;
|
||||
private Date callTime;
|
||||
private UserContext user;
|
||||
private CompanyContext company;
|
||||
private ExtendContext ext;
|
||||
private NotesContext notes;
|
||||
|
||||
public CloudwalkSessionObject() {
|
||||
this.notes = (NotesContext) new ErrorContext();
|
||||
}
|
||||
|
||||
public CloudwalkSessionObject(String[] values) {
|
||||
Assert.notNull(values, "values is not null");
|
||||
if (values.length != 4) {
|
||||
throw new IllegalArgumentException("values is not vaild");
|
||||
}
|
||||
|
||||
this.user = new UserContext();
|
||||
this.company = new CompanyContext();
|
||||
this.notes = (NotesContext) new ErrorContext();
|
||||
|
||||
this.user.setCaller(values[0]);
|
||||
this.user.setCallerName(values[2]);
|
||||
this.company.setCompanyId(values[1]);
|
||||
|
||||
setApplicationId(values[3]);
|
||||
setCallTime(CloudwalkDateUtils.getCurrentDate());
|
||||
}
|
||||
|
||||
public CloudwalkSessionObject(String userId, String serviceCode, String businessId, String corpCode,
|
||||
String username) {
|
||||
this.user = new UserContext();
|
||||
this.company = new CompanyContext();
|
||||
this.notes = (NotesContext) new ErrorContext();
|
||||
|
||||
this.user.setCaller(userId);
|
||||
this.user.setCallerName(username);
|
||||
this.company.setCompanyId(businessId);
|
||||
this.company.setCorpCode(corpCode);
|
||||
|
||||
setServiceCode(serviceCode);
|
||||
setCallTime(CloudwalkDateUtils.getCurrentDate());
|
||||
}
|
||||
|
||||
public String getApplicationId() {
|
||||
return this.applicationId;
|
||||
}
|
||||
|
||||
public void setApplicationId(String applicationId) {
|
||||
this.applicationId = applicationId;
|
||||
}
|
||||
|
||||
public Date getCallTime() {
|
||||
return this.callTime;
|
||||
}
|
||||
|
||||
public void setCallTime(Date callTime) {
|
||||
this.callTime = callTime;
|
||||
}
|
||||
|
||||
public UserContext getUser() {
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public void setUser(UserContext user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public CompanyContext getCompany() {
|
||||
return this.company;
|
||||
}
|
||||
|
||||
public void setCompany(CompanyContext company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public ExtendContext getExt() {
|
||||
return this.ext;
|
||||
}
|
||||
|
||||
public void setExt(ExtendContext ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public NotesContext getNotes() {
|
||||
return this.notes;
|
||||
}
|
||||
|
||||
public void setNotes(NotesContext notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public String getServiceCode() {
|
||||
return this.serviceCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public final class CloudwalkSessionObject
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = -5960069764782340892L;
|
||||
public static final String CLOUDWALK_SESSION_OBJECT_KEY = "cloudSessionObject";
|
||||
private String applicationId;
|
||||
private String serviceCode;
|
||||
private Date callTime;
|
||||
private UserContext user;
|
||||
private CompanyContext company;
|
||||
private ExtendContext ext;
|
||||
private NotesContext notes;
|
||||
|
||||
public CloudwalkSessionObject() {
|
||||
/* 71 */ this.notes = (NotesContext)new ErrorContext();
|
||||
public void setServiceCode(String serviceCode) {
|
||||
this.serviceCode = serviceCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public CloudwalkSessionObject(String[] values) {
|
||||
/* 81 */ Assert.notNull(values, "values is not null");
|
||||
/* 82 */ if (values.length != 4) {
|
||||
/* 83 */ throw new IllegalArgumentException("values is not vaild");
|
||||
}
|
||||
|
||||
/* 86 */ this.user = new UserContext();
|
||||
/* 87 */ this.company = new CompanyContext();
|
||||
/* 88 */ this.notes = (NotesContext)new ErrorContext();
|
||||
|
||||
/* 90 */ this.user.setCaller(values[0]);
|
||||
/* 91 */ this.user.setCallerName(values[2]);
|
||||
/* 92 */ this.company.setCompanyId(values[1]);
|
||||
|
||||
/* 94 */ setApplicationId(values[3]);
|
||||
/* 95 */ setCallTime(CloudwalkDateUtils.getCurrentDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public CloudwalkSessionObject(String userId, String serviceCode, String businessId, String corpCode, String username) {
|
||||
this.user = new UserContext();
|
||||
this.company = new CompanyContext();
|
||||
this.notes = (NotesContext)new ErrorContext();
|
||||
|
||||
this.user.setCaller(userId);
|
||||
this.user.setCallerName(username);
|
||||
this.company.setCompanyId(businessId);
|
||||
this.company.setCorpCode(corpCode);
|
||||
|
||||
setServiceCode(serviceCode);
|
||||
setCallTime(CloudwalkDateUtils.getCurrentDate());
|
||||
}
|
||||
|
||||
public String getApplicationId() {
|
||||
return this.applicationId;
|
||||
}
|
||||
|
||||
public void setApplicationId(String applicationId) {
|
||||
this.applicationId = applicationId;
|
||||
}
|
||||
|
||||
public Date getCallTime() {
|
||||
return this.callTime;
|
||||
}
|
||||
|
||||
public void setCallTime(Date callTime) {
|
||||
this.callTime = callTime;
|
||||
}
|
||||
|
||||
public UserContext getUser() {
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public void setUser(UserContext user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public CompanyContext getCompany() {
|
||||
return this.company;
|
||||
}
|
||||
|
||||
public void setCompany(CompanyContext company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public ExtendContext getExt() {
|
||||
return this.ext;
|
||||
}
|
||||
|
||||
public void setExt(ExtendContext ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public NotesContext getNotes() {
|
||||
return this.notes;
|
||||
}
|
||||
|
||||
public void setNotes(NotesContext notes) {
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
public String getServiceCode() {
|
||||
return this.serviceCode;
|
||||
}
|
||||
|
||||
public void setServiceCode(String serviceCode) {
|
||||
this.serviceCode = serviceCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+23
-44
@@ -2,54 +2,33 @@ package cn.cloudwalk.cloud.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CloudwalkBaseIdentify implements Serializable {
|
||||
private static final long serialVersionUID = -2923446725609856732L;
|
||||
private String id;
|
||||
private String createUserId;
|
||||
private String lastUpdateUserId;
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getCreateUserId() {
|
||||
return this.createUserId;
|
||||
}
|
||||
|
||||
public String getLastUpdateUserId() {
|
||||
return this.lastUpdateUserId;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setCreateUserId(String createUserId) {
|
||||
this.createUserId = createUserId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkBaseIdentify
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = -2923446725609856732L;
|
||||
private String id;
|
||||
private String createUserId;
|
||||
private String lastUpdateUserId;
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
public void setLastUpdateUserId(String lastUpdateUserId) {
|
||||
this.lastUpdateUserId = lastUpdateUserId;
|
||||
}
|
||||
}
|
||||
|
||||
public String getCreateUserId() {
|
||||
return this.createUserId;
|
||||
}
|
||||
|
||||
public String getLastUpdateUserId() {
|
||||
return this.lastUpdateUserId;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setCreateUserId(String createUserId) {
|
||||
this.createUserId = createUserId;
|
||||
}
|
||||
|
||||
public void setLastUpdateUserId(String lastUpdateUserId) {
|
||||
this.lastUpdateUserId = lastUpdateUserId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+30
-57
@@ -1,68 +1,41 @@
|
||||
package cn.cloudwalk.cloud.entity;
|
||||
|
||||
public class CloudwalkBasePeriod extends CloudwalkBaseIdentify {
|
||||
private static final long serialVersionUID = 4257604668089816087L;
|
||||
private Long createBeginTime;
|
||||
private Long createEndTime;
|
||||
private Long lastUpdateBeginTime;
|
||||
private Long lastUpdateEndTime;
|
||||
|
||||
public Long getCreateBeginTime() {
|
||||
return this.createBeginTime;
|
||||
}
|
||||
|
||||
public Long getCreateEndTime() {
|
||||
return this.createEndTime;
|
||||
}
|
||||
|
||||
public Long getLastUpdateBeginTime() {
|
||||
return this.lastUpdateBeginTime;
|
||||
}
|
||||
|
||||
public Long getLastUpdateEndTime() {
|
||||
return this.lastUpdateEndTime;
|
||||
}
|
||||
|
||||
public void setCreateBeginTime(Long createBeginTime) {
|
||||
this.createBeginTime = createBeginTime;
|
||||
}
|
||||
|
||||
public void setCreateEndTime(Long createEndTime) {
|
||||
this.createEndTime = createEndTime;
|
||||
}
|
||||
|
||||
public void setLastUpdateBeginTime(Long lastUpdateBeginTime) {
|
||||
this.lastUpdateBeginTime = lastUpdateBeginTime;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkBasePeriod
|
||||
extends CloudwalkBaseIdentify
|
||||
{
|
||||
private static final long serialVersionUID = 4257604668089816087L;
|
||||
private Long createBeginTime;
|
||||
private Long createEndTime;
|
||||
private Long lastUpdateBeginTime;
|
||||
private Long lastUpdateEndTime;
|
||||
|
||||
public Long getCreateBeginTime() {
|
||||
return this.createBeginTime;
|
||||
public void setLastUpdateEndTime(Long lastUpdateEndTime) {
|
||||
this.lastUpdateEndTime = lastUpdateEndTime;
|
||||
}
|
||||
}
|
||||
|
||||
public Long getCreateEndTime() {
|
||||
return this.createEndTime;
|
||||
}
|
||||
|
||||
public Long getLastUpdateBeginTime() {
|
||||
return this.lastUpdateBeginTime;
|
||||
}
|
||||
|
||||
public Long getLastUpdateEndTime() {
|
||||
return this.lastUpdateEndTime;
|
||||
}
|
||||
|
||||
public void setCreateBeginTime(Long createBeginTime) {
|
||||
this.createBeginTime = createBeginTime;
|
||||
}
|
||||
|
||||
public void setCreateEndTime(Long createEndTime) {
|
||||
this.createEndTime = createEndTime;
|
||||
}
|
||||
|
||||
public void setLastUpdateBeginTime(Long lastUpdateBeginTime) {
|
||||
this.lastUpdateBeginTime = lastUpdateBeginTime;
|
||||
}
|
||||
|
||||
public void setLastUpdateEndTime(Long lastUpdateEndTime) {
|
||||
this.lastUpdateEndTime = lastUpdateEndTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-35
@@ -2,43 +2,24 @@ package cn.cloudwalk.cloud.entity;
|
||||
|
||||
import cn.cloudwalk.cloud.enums.SortEnum;
|
||||
|
||||
public class CloudwalkBaseSort extends CloudwalkBasePeriod {
|
||||
private static final long serialVersionUID = 2250338703453500458L;
|
||||
private String sortField;
|
||||
private SortEnum sortValue;
|
||||
|
||||
public String getSortField() {
|
||||
return this.sortField;
|
||||
}
|
||||
|
||||
public void setSortField(String sortField) {
|
||||
this.sortField = sortField;
|
||||
}
|
||||
|
||||
public SortEnum getSortValue() {
|
||||
return (this.sortValue == null) ? SortEnum.AES : this.sortValue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkBaseSort
|
||||
extends CloudwalkBasePeriod
|
||||
{
|
||||
private static final long serialVersionUID = 2250338703453500458L;
|
||||
private String sortField;
|
||||
private SortEnum sortValue;
|
||||
|
||||
public String getSortField() {
|
||||
return this.sortField;
|
||||
public void setSortValue(SortEnum sortValue) {
|
||||
this.sortValue = sortValue;
|
||||
}
|
||||
}
|
||||
|
||||
public void setSortField(String sortField) {
|
||||
this.sortField = sortField;
|
||||
}
|
||||
|
||||
public SortEnum getSortValue() {
|
||||
return (this.sortValue == null) ? SortEnum.AES : this.sortValue;
|
||||
}
|
||||
|
||||
public void setSortValue(SortEnum sortValue) {
|
||||
this.sortValue = sortValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-35
@@ -1,42 +1,23 @@
|
||||
package cn.cloudwalk.cloud.entity;
|
||||
|
||||
public class CloudwalkBaseTimes extends CloudwalkBaseIdentify {
|
||||
private static final long serialVersionUID = -1915432938815132522L;
|
||||
private Long createTime;
|
||||
private Long lastUpdateTime;
|
||||
|
||||
public Long getCreateTime() {
|
||||
return this.createTime;
|
||||
}
|
||||
|
||||
public Long getLastUpdateTime() {
|
||||
return this.lastUpdateTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkBaseTimes
|
||||
extends CloudwalkBaseIdentify
|
||||
{
|
||||
private static final long serialVersionUID = -1915432938815132522L;
|
||||
private Long createTime;
|
||||
private Long lastUpdateTime;
|
||||
|
||||
public Long getCreateTime() {
|
||||
return this.createTime;
|
||||
public void setLastUpdateTime(Long lastUpdateTime) {
|
||||
this.lastUpdateTime = lastUpdateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public Long getLastUpdateTime() {
|
||||
return this.lastUpdateTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public void setLastUpdateTime(Long lastUpdateTime) {
|
||||
this.lastUpdateTime = lastUpdateTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+5
-28
@@ -1,34 +1,11 @@
|
||||
package cn.cloudwalk.cloud.enums;
|
||||
|
||||
public enum DataFieldEncryptType {
|
||||
SM4,
|
||||
|
||||
AES,
|
||||
|
||||
DES,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum DataFieldEncryptType
|
||||
{
|
||||
SM4,
|
||||
|
||||
|
||||
|
||||
|
||||
AES,
|
||||
|
||||
|
||||
|
||||
|
||||
DES,
|
||||
|
||||
|
||||
|
||||
|
||||
JAS;
|
||||
JAS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-44
@@ -1,54 +1,19 @@
|
||||
package cn.cloudwalk.cloud.enums;
|
||||
|
||||
public enum SensitiveType {
|
||||
CHINESE_NAME,
|
||||
|
||||
ID_CARD,
|
||||
|
||||
FIXED_PHONE,
|
||||
|
||||
MOBILE_PHONE,
|
||||
|
||||
EMAIL,
|
||||
|
||||
BANK_CARD,
|
||||
|
||||
CNAPS_CODE,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum SensitiveType
|
||||
{
|
||||
CHINESE_NAME,
|
||||
|
||||
|
||||
|
||||
|
||||
ID_CARD,
|
||||
|
||||
|
||||
|
||||
|
||||
FIXED_PHONE,
|
||||
|
||||
|
||||
|
||||
|
||||
MOBILE_PHONE,
|
||||
|
||||
|
||||
|
||||
|
||||
EMAIL,
|
||||
|
||||
|
||||
|
||||
|
||||
BANK_CARD,
|
||||
|
||||
|
||||
|
||||
|
||||
CNAPS_CODE,
|
||||
|
||||
|
||||
|
||||
|
||||
NULL;
|
||||
NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+2
-16
@@ -1,19 +1,5 @@
|
||||
package cn.cloudwalk.cloud.enums;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum SortEnum
|
||||
{
|
||||
AES, DES;
|
||||
public enum SortEnum {
|
||||
AES, DES;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+11
-34
@@ -1,40 +1,17 @@
|
||||
package cn.cloudwalk.cloud.exception;
|
||||
|
||||
public class DataAccessException extends Exception {
|
||||
private static final long serialVersionUID = -1219262335729891920L;
|
||||
|
||||
public DataAccessException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DataAccessException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class DataAccessException
|
||||
extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = -1219262335729891920L;
|
||||
|
||||
public DataAccessException(String message) {
|
||||
super(message);
|
||||
public DataAccessException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public DataAccessException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public DataAccessException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+22
-55
@@ -1,64 +1,31 @@
|
||||
package cn.cloudwalk.cloud.exception;
|
||||
|
||||
public class ServiceException extends Exception {
|
||||
private static final long serialVersionUID = -1219262335729891920L;
|
||||
private String code;
|
||||
|
||||
public ServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ServiceException(String code, String message) {
|
||||
this(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public ServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ServiceException
|
||||
extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = -1219262335729891920L;
|
||||
private String code;
|
||||
|
||||
public ServiceException(String message) {
|
||||
super(message);
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ServiceException(String code, String message) {
|
||||
this(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+19
-37
@@ -2,46 +2,28 @@ package cn.cloudwalk.cloud.page;
|
||||
|
||||
import cn.cloudwalk.cloud.entity.CloudwalkBasePeriod;
|
||||
|
||||
public class CloudwalkBasePageForm extends CloudwalkBasePeriod {
|
||||
private static final long serialVersionUID = -4212911809130413023L;
|
||||
private int rowsOfPage = 10;
|
||||
|
||||
private int currentPage = 1;
|
||||
|
||||
public int getCurrentPage() {
|
||||
return this.currentPage;
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) {
|
||||
if (currentPage >= 1) {
|
||||
this.currentPage = currentPage;
|
||||
}
|
||||
}
|
||||
|
||||
public int getRowsOfPage() {
|
||||
return this.rowsOfPage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkBasePageForm
|
||||
extends CloudwalkBasePeriod
|
||||
{
|
||||
private static final long serialVersionUID = -4212911809130413023L;
|
||||
private int rowsOfPage = 10;
|
||||
|
||||
|
||||
|
||||
|
||||
private int currentPage = 1;
|
||||
|
||||
public int getCurrentPage() {
|
||||
return this.currentPage;
|
||||
public void setRowsOfPage(int rowsOfPage) {
|
||||
if (rowsOfPage >= 1)
|
||||
this.rowsOfPage = rowsOfPage;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCurrentPage(int currentPage) {
|
||||
if (currentPage >= 1) {
|
||||
this.currentPage = currentPage;
|
||||
}
|
||||
}
|
||||
|
||||
public int getRowsOfPage() {
|
||||
return this.rowsOfPage;
|
||||
}
|
||||
|
||||
public void setRowsOfPage(int rowsOfPage) {
|
||||
if (rowsOfPage >= 1)
|
||||
this.rowsOfPage = rowsOfPage;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+58
-111
@@ -3,129 +3,76 @@ package cn.cloudwalk.cloud.page;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
|
||||
public class CloudwalkPageAble<T> implements Serializable {
|
||||
private static final long serialVersionUID = -3628865867907230918L;
|
||||
private long pageSize;
|
||||
private long currentPage;
|
||||
private long totalPages;
|
||||
private long totalRows;
|
||||
private long minRowNumber;
|
||||
private long maxRowNumber;
|
||||
private Collection<T> datas;
|
||||
|
||||
public CloudwalkPageAble() {
|
||||
}
|
||||
|
||||
public CloudwalkPageAble(Collection<T> list, CloudwalkPageInfo page, long totalCount) {
|
||||
if (totalCount < 0L || page == null || page.getPageSize() <= 0 || page.getCurrentPage() < 0) {
|
||||
throw new IllegalArgumentException("totalCount must more than 0");
|
||||
}
|
||||
|
||||
this.datas = list;
|
||||
this.totalRows = totalCount;
|
||||
|
||||
this.pageSize = page.getPageSize();
|
||||
this.currentPage = page.getCurrentPage();
|
||||
|
||||
if (totalCount == 0L) {
|
||||
this.totalPages = 0L;
|
||||
} else if (totalCount % page.getPageSize() > 0L) {
|
||||
this.totalPages = totalCount / page.getPageSize() + 1L;
|
||||
} else {
|
||||
this.totalPages = totalCount / page.getPageSize();
|
||||
}
|
||||
|
||||
if (this.totalRows == 0L) {
|
||||
this.minRowNumber = 0L;
|
||||
} else {
|
||||
this.minRowNumber = (this.currentPage - 1L) * page.getPageSize() + 1L;
|
||||
}
|
||||
|
||||
if (this.currentPage * page.getPageSize() > this.totalRows) {
|
||||
this.maxRowNumber = this.totalRows;
|
||||
} else {
|
||||
this.maxRowNumber = this.currentPage * page.getPageSize();
|
||||
}
|
||||
}
|
||||
|
||||
public long getCurrentPage() {
|
||||
return this.currentPage;
|
||||
}
|
||||
|
||||
public long getTotalPages() {
|
||||
return this.totalPages;
|
||||
}
|
||||
|
||||
public long getTotalRows() {
|
||||
return this.totalRows;
|
||||
}
|
||||
|
||||
public Collection<T> getDatas() {
|
||||
return this.datas;
|
||||
}
|
||||
|
||||
public long getMinRowNumber() {
|
||||
return this.minRowNumber;
|
||||
}
|
||||
|
||||
public long getMaxRowNumber() {
|
||||
return this.maxRowNumber;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkPageAble<T>
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = -3628865867907230918L;
|
||||
private long pageSize;
|
||||
private long currentPage;
|
||||
private long totalPages;
|
||||
private long totalRows;
|
||||
private long minRowNumber;
|
||||
private long maxRowNumber;
|
||||
private Collection<T> datas;
|
||||
|
||||
public CloudwalkPageAble() {}
|
||||
|
||||
public CloudwalkPageAble(Collection<T> list, CloudwalkPageInfo page, long totalCount) {
|
||||
/* 67 */ if (totalCount < 0L || page == null || page.getPageSize() <= 0 || page.getCurrentPage() < 0) {
|
||||
/* 68 */ throw new IllegalArgumentException("totalCount must more than 0");
|
||||
public long getPageSize() {
|
||||
return this.pageSize;
|
||||
}
|
||||
}
|
||||
|
||||
/* 71 */ this.datas = list;
|
||||
/* 72 */ this.totalRows = totalCount;
|
||||
|
||||
/* 74 */ this.pageSize = page.getPageSize();
|
||||
/* 75 */ this.currentPage = page.getCurrentPage();
|
||||
|
||||
/* 77 */ if (totalCount == 0L) {
|
||||
/* 78 */ this.totalPages = 0L;
|
||||
}
|
||||
/* 80 */ else if (totalCount % page.getPageSize() > 0L) {
|
||||
/* 81 */ this.totalPages = totalCount / page.getPageSize() + 1L;
|
||||
} else {
|
||||
/* 83 */ this.totalPages = totalCount / page.getPageSize();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* 88 */ if (this.totalRows == 0L) {
|
||||
/* 89 */ this.minRowNumber = 0L;
|
||||
} else {
|
||||
/* 91 */ this.minRowNumber = (this.currentPage - 1L) * page.getPageSize() + 1L;
|
||||
}
|
||||
|
||||
|
||||
/* 95 */ if (this.currentPage * page.getPageSize() > this.totalRows) {
|
||||
/* 96 */ this.maxRowNumber = this.totalRows;
|
||||
} else {
|
||||
/* 98 */ this.maxRowNumber = this.currentPage * page.getPageSize();
|
||||
}
|
||||
}
|
||||
|
||||
public long getCurrentPage() {
|
||||
return this.currentPage;
|
||||
}
|
||||
|
||||
public long getTotalPages() {
|
||||
return this.totalPages;
|
||||
}
|
||||
|
||||
public long getTotalRows() {
|
||||
return this.totalRows;
|
||||
}
|
||||
|
||||
public Collection<T> getDatas() {
|
||||
return this.datas;
|
||||
}
|
||||
|
||||
public long getMinRowNumber() {
|
||||
return this.minRowNumber;
|
||||
}
|
||||
|
||||
public long getMaxRowNumber() {
|
||||
return this.maxRowNumber;
|
||||
}
|
||||
|
||||
public long getPageSize() {
|
||||
return this.pageSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+25
-50
@@ -2,61 +2,36 @@ package cn.cloudwalk.cloud.page;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CloudwalkPageDTO<T> implements Serializable {
|
||||
private static final long serialVersionUID = -1351931333484421041L;
|
||||
private CloudwalkPageInfo page;
|
||||
private T dto;
|
||||
|
||||
public CloudwalkPageDTO() {
|
||||
}
|
||||
|
||||
public CloudwalkPageDTO(CloudwalkPageInfo page, T dto) {
|
||||
this.page = page;
|
||||
this.dto = dto;
|
||||
}
|
||||
|
||||
public CloudwalkPageInfo getPage() {
|
||||
return this.page;
|
||||
}
|
||||
|
||||
public void setPage(CloudwalkPageInfo page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
public T getDto() {
|
||||
return this.dto;
|
||||
}
|
||||
|
||||
public void setDto(T dto) {
|
||||
this.dto = dto;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkPageDTO<T>
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = -1351931333484421041L;
|
||||
private CloudwalkPageInfo page;
|
||||
private T dto;
|
||||
|
||||
public CloudwalkPageDTO() {}
|
||||
|
||||
public CloudwalkPageDTO(CloudwalkPageInfo page, T dto) {
|
||||
this.page = page;
|
||||
this.dto = dto;
|
||||
public String toString() {
|
||||
return "CloudwalkPageDTO [page=" + this.page + ", dto=" + this.dto + "]";
|
||||
}
|
||||
}
|
||||
|
||||
public CloudwalkPageInfo getPage() {
|
||||
return this.page;
|
||||
}
|
||||
|
||||
public void setPage(CloudwalkPageInfo page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
public T getDto() {
|
||||
return this.dto;
|
||||
}
|
||||
|
||||
public void setDto(T dto) {
|
||||
this.dto = dto;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return "CloudwalkPageDTO [page=" + this.page + ", dto=" + this.dto + "]";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+27
-64
@@ -2,75 +2,38 @@ package cn.cloudwalk.cloud.page;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CloudwalkPageInfo implements Serializable {
|
||||
private static final long serialVersionUID = -9065614200385450712L;
|
||||
private int pageSize;
|
||||
private int currentPage;
|
||||
|
||||
public CloudwalkPageInfo(int currentPage, int pageSize) {
|
||||
if (currentPage < 1 || pageSize < 1) {
|
||||
throw new IllegalArgumentException("currentPage and pageSize must more than 0.");
|
||||
}
|
||||
|
||||
this.currentPage = currentPage;
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public CloudwalkPageInfo() {
|
||||
this.currentPage = 1;
|
||||
this.pageSize = 20;
|
||||
}
|
||||
|
||||
public int getPageStart() {
|
||||
if (this.currentPage < 1) {
|
||||
this.currentPage = 1;
|
||||
}
|
||||
|
||||
return (this.currentPage - 1) * this.pageSize;
|
||||
}
|
||||
|
||||
public int getPageSize() {
|
||||
return this.pageSize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkPageInfo
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = -9065614200385450712L;
|
||||
private int pageSize;
|
||||
private int currentPage;
|
||||
|
||||
public CloudwalkPageInfo(int currentPage, int pageSize) {
|
||||
if (currentPage < 1 || pageSize < 1) {
|
||||
throw new IllegalArgumentException("currentPage and pageSize must more than 0.");
|
||||
public int getCurrentPage() {
|
||||
return this.currentPage;
|
||||
}
|
||||
}
|
||||
|
||||
this.currentPage = currentPage;
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public CloudwalkPageInfo() {
|
||||
this.currentPage = 1;
|
||||
this.pageSize = 20;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int getPageStart() {
|
||||
if (this.currentPage < 1) {
|
||||
this.currentPage = 1;
|
||||
}
|
||||
|
||||
return (this.currentPage - 1) * this.pageSize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int getPageSize() {
|
||||
return this.pageSize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public int getCurrentPage() {
|
||||
return this.currentPage;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+56
-108
@@ -2,124 +2,72 @@ package cn.cloudwalk.cloud.result;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CloudwalkResult<T> implements Serializable {
|
||||
private static final long serialVersionUID = 9191892693219217387L;
|
||||
public static final String RESP_CODE_SUCCESS = "00000000";
|
||||
public static final String RESP_MESG_SUCCESS = "success";
|
||||
private String code;
|
||||
private boolean success;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return this.success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public static <T> CloudwalkResult<T> success(T data) {
|
||||
CloudwalkResult<T> result = new CloudwalkResult<>();
|
||||
result.setCode("00000000");
|
||||
result.setMessage("success");
|
||||
result.setSuccess(true);
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> CloudwalkResult<T> fail(String code, String message, T data) {
|
||||
CloudwalkResult<T> result = new CloudwalkResult<>();
|
||||
result.setCode(code);
|
||||
result.setData(data);
|
||||
result.setMessage(message);
|
||||
result.setSuccess(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> CloudwalkResult<T> fail(String code, String message) {
|
||||
CloudwalkResult<T> result = new CloudwalkResult<>();
|
||||
result.setCode(code);
|
||||
result.setMessage(message);
|
||||
result.setSuccess(false);
|
||||
result.setData(null);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkResult<T>
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 9191892693219217387L;
|
||||
public static final String RESP_CODE_SUCCESS = "00000000";
|
||||
public static final String RESP_MESG_SUCCESS = "success";
|
||||
private String code;
|
||||
private boolean success;
|
||||
private String message;
|
||||
private T data;
|
||||
|
||||
public String getCode() {
|
||||
/* 47 */ return this.code;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
/* 51 */ this.code = code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
/* 55 */ return this.message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
/* 59 */ this.message = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
/* 63 */ return this.data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
/* 67 */ this.data = data;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
/* 71 */ return this.success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
/* 75 */ this.success = success;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> CloudwalkResult<T> success(T data) {
|
||||
/* 84 */ CloudwalkResult<T> result = new CloudwalkResult<>();
|
||||
/* 85 */ result.setCode("00000000");
|
||||
/* 86 */ result.setMessage("success");
|
||||
/* 87 */ result.setSuccess(true);
|
||||
/* 88 */ result.setData(data);
|
||||
/* 89 */ return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> CloudwalkResult<T> fail(String code, String message, T data) {
|
||||
/* 99 */ CloudwalkResult<T> result = new CloudwalkResult<>();
|
||||
result.setCode(code);
|
||||
result.setData(data);
|
||||
result.setMessage(message);
|
||||
result.setSuccess(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> CloudwalkResult<T> fail(String code, String message) {
|
||||
CloudwalkResult<T> result = new CloudwalkResult<>();
|
||||
result.setCode(code);
|
||||
result.setMessage(message);
|
||||
result.setSuccess(false);
|
||||
result.setData(null);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+49
-127
@@ -5,148 +5,70 @@ import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
public class AESUtils {
|
||||
private static final String KEY_ALGORITHM = "AES";
|
||||
private static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
|
||||
|
||||
public static String encrypt(String content, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(1, getSecretKey(password));
|
||||
|
||||
byte[] byteContent = content.getBytes(CHARSET);
|
||||
byte[] result = cipher.doFinal(byteContent);
|
||||
|
||||
return HexUtils.bytesToHex(result);
|
||||
}
|
||||
|
||||
public static String encryptBase64(String content, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(1, getSecretKey(password));
|
||||
|
||||
byte[] byteContent = content.getBytes(CHARSET);
|
||||
byte[] result = cipher.doFinal(byteContent);
|
||||
|
||||
return Base64.encodeBase64String(result);
|
||||
}
|
||||
|
||||
public static String decrypt(String content, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(2, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(HexUtils.hexStringToByte(content));
|
||||
|
||||
return new String(result, CHARSET);
|
||||
}
|
||||
|
||||
public static String decryptBase64(String content, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(2, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
|
||||
|
||||
return new String(result, CHARSET);
|
||||
}
|
||||
|
||||
public static byte[] getEncCode(byte[] bytes, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(1, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] getDecCode(byte[] bytes, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(2, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class AESUtils
|
||||
{
|
||||
private static final String KEY_ALGORITHM = "AES";
|
||||
/* 30 */ private static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String encrypt(String content, String password) throws Exception {
|
||||
/* 46 */ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
/* 47 */ cipher.init(1, getSecretKey(password));
|
||||
|
||||
/* 49 */ byte[] byteContent = content.getBytes(CHARSET);
|
||||
/* 50 */ byte[] result = cipher.doFinal(byteContent);
|
||||
|
||||
/* 52 */ return HexUtils.bytesToHex(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String encryptBase64(String content, String password) throws Exception {
|
||||
/* 64 */ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
/* 65 */ cipher.init(1, getSecretKey(password));
|
||||
|
||||
/* 67 */ byte[] byteContent = content.getBytes(CHARSET);
|
||||
/* 68 */ byte[] result = cipher.doFinal(byteContent);
|
||||
|
||||
/* 70 */ return Base64.encodeBase64String(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String decrypt(String content, String password) throws Exception {
|
||||
/* 83 */ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
/* 84 */ cipher.init(2, getSecretKey(password));
|
||||
/* 85 */ byte[] result = cipher.doFinal(HexUtils.hexStringToByte(content));
|
||||
|
||||
/* 87 */ return new String(result, CHARSET);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String decryptBase64(String content, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(2, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
|
||||
|
||||
return new String(result, CHARSET);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] getEncCode(byte[] bytes, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(1, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] getDecCode(byte[] bytes, String password) throws Exception {
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(2, getSecretKey(password));
|
||||
byte[] result = cipher.doFinal(bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static SecretKeySpec getSecretKey(String password) {
|
||||
try {
|
||||
return new SecretKeySpec(password.getBytes(CHARSET), "AES");
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encode init error");
|
||||
}
|
||||
}
|
||||
private static SecretKeySpec getSecretKey(String password) {
|
||||
try {
|
||||
return new SecretKeySpec(password.getBytes(CHARSET), "AES");
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("encode init error");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+39
-134
@@ -8,151 +8,56 @@ import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.DESKeySpec;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
public class DESUtils {
|
||||
private static final String DES = "DES";
|
||||
private static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
public static String encrypt(String data, String key) throws Exception {
|
||||
return HexUtils.bytesToHex(encrypt(data.getBytes(CHARSET), key.getBytes(CHARSET)));
|
||||
}
|
||||
|
||||
public static byte[] encrypt(byte[] data, String key) throws Exception {
|
||||
return encrypt(data, key.getBytes(CHARSET));
|
||||
}
|
||||
|
||||
public static String encryptBase64(String data, String key) throws Exception {
|
||||
return Base64.encodeBase64String(encrypt(data.getBytes(CHARSET), key.getBytes(CHARSET)));
|
||||
}
|
||||
|
||||
public static String decrypt(String data, String key) throws Exception {
|
||||
return new String(decrypt(HexUtils.hexStringToByte(data), key.getBytes(CHARSET)), CHARSET);
|
||||
}
|
||||
|
||||
public static byte[] decrypt(byte[] data, String key) throws Exception {
|
||||
return decrypt(data, key.getBytes(CHARSET));
|
||||
}
|
||||
|
||||
public static String decryptBase64(String data, String key) throws Exception {
|
||||
return new String(decrypt(Base64.decodeBase64(data), key.getBytes(CHARSET)), CHARSET);
|
||||
}
|
||||
|
||||
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
SecretKey securekey = getSecretKey(key);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("DES");
|
||||
cipher.init(1, securekey, sr);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
SecretKey securekey = getSecretKey(key);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("DES");
|
||||
cipher.init(2, securekey, sr);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class DESUtils
|
||||
{
|
||||
private static final String DES = "DES";
|
||||
/* 31 */ private static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String encrypt(String data, String key) throws Exception {
|
||||
/* 42 */ return HexUtils.bytesToHex(encrypt(data.getBytes(CHARSET), key.getBytes(CHARSET)));
|
||||
private static SecretKey getSecretKey(byte[] key) throws Exception {
|
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||||
return keyFactory.generateSecret(new DESKeySpec(key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encrypt(byte[] data, String key) throws Exception {
|
||||
/* 54 */ return encrypt(data, key.getBytes(CHARSET));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String encryptBase64(String data, String key) throws Exception {
|
||||
/* 66 */ return Base64.encodeBase64String(encrypt(data.getBytes(CHARSET), key.getBytes(CHARSET)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String decrypt(String data, String key) throws Exception {
|
||||
/* 78 */ return new String(decrypt(HexUtils.hexStringToByte(data), key.getBytes(CHARSET)), CHARSET);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] decrypt(byte[] data, String key) throws Exception {
|
||||
/* 90 */ return decrypt(data, key.getBytes(CHARSET));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String decryptBase64(String data, String key) throws Exception {
|
||||
return new String(decrypt(Base64.decodeBase64(data), key.getBytes(CHARSET)), CHARSET);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
SecretKey securekey = getSecretKey(key);
|
||||
|
||||
|
||||
Cipher cipher = Cipher.getInstance("DES");
|
||||
cipher.init(1, securekey, sr);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
SecretKey securekey = getSecretKey(key);
|
||||
|
||||
|
||||
Cipher cipher = Cipher.getInstance("DES");
|
||||
cipher.init(2, securekey, sr);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static SecretKey getSecretKey(byte[] key) throws Exception {
|
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||||
return keyFactory.generateSecret(new DESKeySpec(key));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+24
-50
@@ -1,58 +1,32 @@
|
||||
package cn.cloudwalk.cloud.security;
|
||||
|
||||
public class HexUtils {
|
||||
public static String bytesToHex(byte[] src) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < src.length; i++) {
|
||||
String hex = Integer.toHexString(src[i] & 0xFF);
|
||||
if (hex.length() == 1) {
|
||||
hex = '0' + hex;
|
||||
}
|
||||
|
||||
sb.append(hex.toUpperCase());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static byte[] hexStringToByte(String hexStr) {
|
||||
if (hexStr.length() < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] result = new byte[hexStr.length() / 2];
|
||||
for (int i = 0; i < hexStr.length() / 2; i++) {
|
||||
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
|
||||
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
|
||||
result[i] = (byte) (high * 16 + low);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class HexUtils
|
||||
{
|
||||
public static String bytesToHex(byte[] src) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < src.length; i++) {
|
||||
String hex = Integer.toHexString(src[i] & 0xFF);
|
||||
if (hex.length() == 1) {
|
||||
hex = '0' + hex;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
sb.append(hex.toUpperCase());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] hexStringToByte(String hexStr) {
|
||||
if (hexStr.length() < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] result = new byte[hexStr.length() / 2];
|
||||
for (int i = 0; i < hexStr.length() / 2; i++) {
|
||||
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
|
||||
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
|
||||
result[i] = (byte)(high * 16 + low);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+73
-181
@@ -17,213 +17,105 @@ import java.util.Map;
|
||||
import javax.crypto.Cipher;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
public class RSAUtils {
|
||||
private static final int KEY_SIZE = 1024;
|
||||
public static final String KEY_RSA = "RSA";
|
||||
public static final String KEY_RSA_PUBLICKEY = "RSAPublicKey";
|
||||
public static final String KEY_RSA_PRIVATEKEY = "RSAPrivateKey";
|
||||
private static final String KEY_RSA_SIGNATURE = "MD5withRSA";
|
||||
|
||||
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGenerator.initialize(1024);
|
||||
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
|
||||
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
||||
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("RSAPublicKey", publicKey);
|
||||
map.put("RSAPrivateKey", privateKey);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static byte[] getPublicKey(Map<String, Object> map) {
|
||||
return ((Key) map.get("RSAPublicKey")).getEncoded();
|
||||
}
|
||||
|
||||
public static byte[] getPrivateKey(Map<String, Object> map) {
|
||||
return ((Key) map.get("RSAPrivateKey")).getEncoded();
|
||||
}
|
||||
|
||||
public static byte[] encryptByPublic(byte[] data, byte[] keyBytes) throws Exception {
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicKey = factory.generatePublic(keySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(1, publicKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
public static byte[] decryptByPrivate(byte[] data, byte[] keyBytes) throws Exception {
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = factory.generatePrivate(keySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(2, privateKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
public static byte[] encryptByPrivate(byte[] data, byte[] keyBytes) throws Exception {
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = factory.generatePrivate(keySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(1, privateKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
public static byte[] decryptByPublic(byte[] data, byte[] keyBytes) throws Exception {
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicKey = factory.generatePublic(keySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(2, publicKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
public static String sign(byte[] data, byte[] privateKey) throws Exception {
|
||||
PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(privateKey);
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey key = factory.generatePrivate(pkcs);
|
||||
|
||||
Signature signature = Signature.getInstance("MD5withRSA");
|
||||
signature.initSign(key);
|
||||
signature.update(data);
|
||||
|
||||
return Base64.encodeBase64String(signature.sign());
|
||||
}
|
||||
|
||||
public static boolean verify(byte[] data, byte[] publicKey, String sign) throws Exception {
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PublicKey key = factory.generatePublic(keySpec);
|
||||
|
||||
Signature signature = Signature.getInstance("MD5withRSA");
|
||||
signature.initVerify(key);
|
||||
signature.update(data);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class RSAUtils
|
||||
{
|
||||
private static final int KEY_SIZE = 1024;
|
||||
public static final String KEY_RSA = "RSA";
|
||||
public static final String KEY_RSA_PUBLICKEY = "RSAPublicKey";
|
||||
public static final String KEY_RSA_PRIVATEKEY = "RSAPrivateKey";
|
||||
private static final String KEY_RSA_SIGNATURE = "MD5withRSA";
|
||||
|
||||
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {
|
||||
/* 66 */ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||
/* 67 */ keyPairGenerator.initialize(1024);
|
||||
/* 68 */ KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
|
||||
|
||||
/* 71 */ RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
|
||||
/* 72 */ RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
|
||||
|
||||
|
||||
/* 75 */ Map<String, Object> map = new HashMap<>();
|
||||
/* 76 */ map.put("RSAPublicKey", publicKey);
|
||||
/* 77 */ map.put("RSAPrivateKey", privateKey);
|
||||
|
||||
/* 79 */ return map;
|
||||
return signature.verify(Base64.decodeBase64(sign));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] getPublicKey(Map<String, Object> map) {
|
||||
/* 88 */ return ((Key)map.get("RSAPublicKey")).getEncoded();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] getPrivateKey(Map<String, Object> map) {
|
||||
/* 97 */ return ((Key)map.get("RSAPrivateKey")).getEncoded();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encryptByPublic(byte[] data, byte[] keyBytes) throws Exception {
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
||||
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicKey = factory.generatePublic(keySpec);
|
||||
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(1, publicKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] decryptByPrivate(byte[] data, byte[] keyBytes) throws Exception {
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = factory.generatePrivate(keySpec);
|
||||
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(2, privateKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encryptByPrivate(byte[] data, byte[] keyBytes) throws Exception {
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = factory.generatePrivate(keySpec);
|
||||
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(1, privateKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] decryptByPublic(byte[] data, byte[] keyBytes) throws Exception {
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
||||
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicKey = factory.generatePublic(keySpec);
|
||||
|
||||
|
||||
Cipher cipher = Cipher.getInstance(factory.getAlgorithm());
|
||||
cipher.init(2, publicKey);
|
||||
|
||||
return cipher.doFinal(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String sign(byte[] data, byte[] privateKey) throws Exception {
|
||||
PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(privateKey);
|
||||
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey key = factory.generatePrivate(pkcs);
|
||||
|
||||
|
||||
Signature signature = Signature.getInstance("MD5withRSA");
|
||||
signature.initSign(key);
|
||||
signature.update(data);
|
||||
|
||||
return Base64.encodeBase64String(signature.sign());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static boolean verify(byte[] data, byte[] publicKey, String sign) throws Exception {
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
|
||||
|
||||
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
PublicKey key = factory.generatePublic(keySpec);
|
||||
|
||||
|
||||
Signature signature = Signature.getInstance("MD5withRSA");
|
||||
signature.initVerify(key);
|
||||
signature.update(data);
|
||||
|
||||
return signature.verify(Base64.decodeBase64(sign));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+208
-333
@@ -4,384 +4,259 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
public class SM4Utils {
|
||||
private static final String ENCODE = "UTF-8";
|
||||
private static final int ENCRYPT = 1;
|
||||
private static final int DECRYPT = 0;
|
||||
public static final int ROUND = 32;
|
||||
private static final int BLOCK = 16;
|
||||
private static byte[] Sbox = new byte[] { -42, -112, -23, -2, -52, -31, 61, -73, 22, -74, 20, -62, 40, -5, 44, 5,
|
||||
43, 103, -102, 118, 42, -66, 4, -61, -86, 68, 19, 38, 73, -122, 6, -103, -100, 66, 80, -12, -111, -17, -104,
|
||||
122, 51, 84, 11, 67, -19, -49, -84, 98, -28, -77, 28, -87, -55, 8, -24, -107, Byte.MIN_VALUE, -33, -108, -6,
|
||||
117, -113, 63, -90, 71, 7, -89, -4, -13, 115, 23, -70, -125, 89, 60, 25, -26, -123, 79, -88, 104, 107, -127,
|
||||
-78, 113, 100, -38, -117, -8, -21, 15, 75, 112, 86, -99, 53, 30, 36, 14, 94, 99, 88, -47, -94, 37, 34, 124,
|
||||
59, 1, 33, 120, -121, -44, 0, 70, 87, -97, -45, 39, 82, 76, 54, 2, -25, -96, -60, -56, -98, -22, -65, -118,
|
||||
-46, 64, -57, 56, -75, -93, -9, -14, -50, -7, 97, 21, -95, -32, -82, 93, -92, -101, 52, 26, 85, -83, -109,
|
||||
50, 48, -11, -116, -79, -29, 29, -10, -30, 46, -126, 102, -54, 96, -64, 41, 35, -85, 13, 83, 78, 111, -43,
|
||||
-37, 55, 69, -34, -3, -114, 47, 3, -1, 106, 114, 109, 108, 91, 81, -115, 27, -81, -110, -69, -35, -68,
|
||||
Byte.MAX_VALUE, 17, -39, 92, 65, 31, 16, 90, -40, 10, -63, 49, -120, -91, -51, 123, -67, 45, 116, -48, 18,
|
||||
-72, -27, -76, -80, -119, 105, -105, 74, 12, -106, 119, 126, 101, -71, -15, 9, -59, 110, -58, -124, 24, -16,
|
||||
125, -20, 58, -36, 77, 32, 121, -18, 95, 62, -41, -53, 57, 72 };
|
||||
|
||||
private static int[] CK = new int[] { 462357, 472066609, 943670861, 1415275113, 1886879365, -1936483679,
|
||||
-1464879427, -993275175, -521670923, -66909679, 404694573, 876298825, 1347903077, 1819507329, -2003855715,
|
||||
-1532251463, -1060647211, -589042959, -117504499, 337322537, 808926789, 1280531041, 1752135293, -2071227751,
|
||||
-1599623499, -1128019247, -656414995, -184876535, 269950501, 741554753, 1213159005, 1684763257 };
|
||||
|
||||
private static int rotl(int x, int y) {
|
||||
return x << y | x >>> 32 - y;
|
||||
}
|
||||
|
||||
private static int byteSub(int a) {
|
||||
return (Sbox[a >>> 24 & 0xFF] & 0xFF) << 24 | (Sbox[a >>> 16 & 0xFF] & 0xFF) << 16
|
||||
| (Sbox[a >>> 8 & 0xFF] & 0xFF) << 8 | Sbox[a & 0xFF] & 0xFF;
|
||||
}
|
||||
|
||||
private static int left1(int b) {
|
||||
return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
|
||||
}
|
||||
|
||||
private static int left2(int b) {
|
||||
return b ^ rotl(b, 13) ^ rotl(b, 23);
|
||||
}
|
||||
|
||||
private static void sms4Crypt(byte[] input, byte[] output, int[] rk) {
|
||||
if (input == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
int[] x = new int[4];
|
||||
int[] tmp = new int[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
tmp[0] = input[0 + 4 * i] & 0xFF;
|
||||
tmp[1] = input[1 + 4 * i] & 0xFF;
|
||||
tmp[2] = input[2 + 4 * i] & 0xFF;
|
||||
tmp[3] = input[3 + 4 * i] & 0xFF;
|
||||
x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
|
||||
}
|
||||
for (int r = 0; r < 32; r += 4) {
|
||||
int mid = x[1] ^ x[2] ^ x[3] ^ rk[r + 0];
|
||||
mid = byteSub(mid);
|
||||
x[0] = x[0] ^ left1(mid);
|
||||
|
||||
mid = x[2] ^ x[3] ^ x[0] ^ rk[r + 1];
|
||||
mid = byteSub(mid);
|
||||
x[1] = x[1] ^ left1(mid);
|
||||
|
||||
mid = x[3] ^ x[0] ^ x[1] ^ rk[r + 2];
|
||||
mid = byteSub(mid);
|
||||
x[2] = x[2] ^ left1(mid);
|
||||
|
||||
mid = x[0] ^ x[1] ^ x[2] ^ rk[r + 3];
|
||||
mid = byteSub(mid);
|
||||
x[3] = x[3] ^ left1(mid);
|
||||
}
|
||||
|
||||
for (int j = 0; j < 16; j += 4) {
|
||||
output[j] = (byte) (x[3 - j / 4] >>> 24 & 0xFF);
|
||||
output[j + 1] = (byte) (x[3 - j / 4] >>> 16 & 0xFF);
|
||||
output[j + 2] = (byte) (x[3 - j / 4] >>> 8 & 0xFF);
|
||||
output[j + 3] = (byte) (x[3 - j / 4] & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
private static void sms4KeyExt(byte[] key, int[] rk, int cryptFlag) {
|
||||
int[] x = new int[4];
|
||||
int[] tmp = new int[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
tmp[0] = key[0 + 4 * i] & 0xFF;
|
||||
tmp[1] = key[1 + 4 * i] & 0xFF;
|
||||
tmp[2] = key[2 + 4 * i] & 0xFF;
|
||||
tmp[3] = key[3 + 4 * i] & 0xFF;
|
||||
x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
|
||||
}
|
||||
x[0] = x[0] ^ 0xA3B1BAC6;
|
||||
x[1] = x[1] ^ 0x56AA3350;
|
||||
x[2] = x[2] ^ 0x677D9197;
|
||||
x[3] = x[3] ^ 0xB27022DC;
|
||||
int r;
|
||||
for (r = 0; r < 32; r += 4) {
|
||||
int mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
|
||||
mid = byteSub(mid);
|
||||
x[0] = x[0] ^ left2(mid);
|
||||
rk[r + 0] = x[0] ^ left2(mid);
|
||||
|
||||
mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
|
||||
mid = byteSub(mid);
|
||||
x[1] = x[1] ^ left2(mid);
|
||||
rk[r + 1] = x[1] ^ left2(mid);
|
||||
|
||||
mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
|
||||
mid = byteSub(mid);
|
||||
x[2] = x[2] ^ left2(mid);
|
||||
rk[r + 2] = x[2] ^ left2(mid);
|
||||
|
||||
mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
|
||||
mid = byteSub(mid);
|
||||
x[3] = x[3] ^ left2(mid);
|
||||
rk[r + 3] = x[3] ^ left2(mid);
|
||||
}
|
||||
|
||||
if (cryptFlag == 0) {
|
||||
for (r = 0; r < 16; r++) {
|
||||
int mid = rk[r];
|
||||
rk[r] = rk[31 - r];
|
||||
rk[31 - r] = mid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int sms4(byte[] in, int inLen, byte[] key, byte[] out, int cryptFlag) {
|
||||
int point = 0;
|
||||
int[] roundKey = new int[32];
|
||||
|
||||
sms4KeyExt(key, roundKey, cryptFlag);
|
||||
byte[] input = null;
|
||||
byte[] output = new byte[16];
|
||||
|
||||
while (inLen >= 16) {
|
||||
input = Arrays.copyOfRange(in, point, point + 16);
|
||||
sms4Crypt(input, output, roundKey);
|
||||
System.arraycopy(output, 0, out, point, 16);
|
||||
inLen -= 16;
|
||||
point += 16;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static String encodeSMS4(String plaintext, String key) throws UnsupportedEncodingException {
|
||||
for (int i = (plaintext.getBytes("UTF-8")).length % 16; i < 16; i++) {
|
||||
plaintext = plaintext + Character.MIN_VALUE;
|
||||
}
|
||||
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
return Base64.encodeBase64String(encodeSMS4(plaintext.getBytes("UTF-8"), keyHex));
|
||||
}
|
||||
|
||||
public static byte[] encodeSMS4(String plaintext, byte[] key) throws UnsupportedEncodingException {
|
||||
if (plaintext == null || plaintext.equals("")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (int i = (plaintext.getBytes("UTF-8")).length % 16; i < 16; i++) {
|
||||
plaintext = plaintext + Character.MIN_VALUE;
|
||||
}
|
||||
|
||||
return encodeSMS4(plaintext.getBytes("UTF-8"), key);
|
||||
}
|
||||
|
||||
public static byte[] encodeSMS4(byte[] data, String key) throws UnsupportedEncodingException {
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
return encodeSMS4(data, keyHex);
|
||||
}
|
||||
|
||||
public static byte[] encodeSMS4(byte[] plaintext, byte[] key) {
|
||||
byte[] ciphertext = new byte[plaintext.length];
|
||||
|
||||
int k = 0;
|
||||
int plainLen = plaintext.length;
|
||||
while (k + 16 <= plainLen) {
|
||||
byte[] cellPlain = new byte[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
cellPlain[i] = plaintext[k + i];
|
||||
}
|
||||
|
||||
byte[] cellCipher = encode16(cellPlain, key);
|
||||
for (int j = 0; j < cellCipher.length; j++) {
|
||||
ciphertext[k + j] = cellCipher[j];
|
||||
}
|
||||
|
||||
k += 16;
|
||||
}
|
||||
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
public class SM4Utils
|
||||
{
|
||||
private static final String ENCODE = "UTF-8";
|
||||
private static final int ENCRYPT = 1;
|
||||
private static final int DECRYPT = 0;
|
||||
public static final int ROUND = 32;
|
||||
private static final int BLOCK = 16;
|
||||
/* 46 */ private static byte[] Sbox = new byte[] { -42, -112, -23, -2, -52, -31, 61, -73, 22, -74, 20, -62, 40, -5, 44, 5, 43, 103, -102, 118, 42, -66, 4, -61, -86, 68, 19, 38, 73, -122, 6, -103, -100, 66, 80, -12, -111, -17, -104, 122, 51, 84, 11, 67, -19, -49, -84, 98, -28, -77, 28, -87, -55, 8, -24, -107, Byte.MIN_VALUE, -33, -108, -6, 117, -113, 63, -90, 71, 7, -89, -4, -13, 115, 23, -70, -125, 89, 60, 25, -26, -123, 79, -88, 104, 107, -127, -78, 113, 100, -38, -117, -8, -21, 15, 75, 112, 86, -99, 53, 30, 36, 14, 94, 99, 88, -47, -94, 37, 34, 124, 59, 1, 33, 120, -121, -44, 0, 70, 87, -97, -45, 39, 82, 76, 54, 2, -25, -96, -60, -56, -98, -22, -65, -118, -46, 64, -57, 56, -75, -93, -9, -14, -50, -7, 97, 21, -95, -32, -82, 93, -92, -101, 52, 26, 85, -83, -109, 50, 48, -11, -116, -79, -29, 29, -10, -30, 46, -126, 102, -54, 96, -64, 41, 35, -85, 13, 83, 78, 111, -43, -37, 55, 69, -34, -3, -114, 47, 3, -1, 106, 114, 109, 108, 91, 81, -115, 27, -81, -110, -69, -35, -68, Byte.MAX_VALUE, 17, -39, 92, 65, 31, 16, 90, -40, 10, -63, 49, -120, -91, -51, 123, -67, 45, 116, -48, 18, -72, -27, -76, -80, -119, 105, -105, 74, 12, -106, 119, 126, 101, -71, -15, 9, -59, 110, -58, -124, 24, -16, 125, -20, 58, -36, 77, 32, 121, -18, 95, 62, -41, -53, 57, 72 };
|
||||
public static String decodeSMS4(String cipherString, String key) throws UnsupportedEncodingException {
|
||||
byte[] ciphertext = Base64.decodeBase64(cipherString);
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
|
||||
return decodeSMS4toString(ciphertext, keyHex);
|
||||
}
|
||||
|
||||
public static byte[] decodeSMS4(byte[] data, String key) throws UnsupportedEncodingException {
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
|
||||
return decodeSMS4(data, keyHex);
|
||||
}
|
||||
|
||||
public static byte[] decodeSMS4(byte[] ciphertext, byte[] key) {
|
||||
if (ciphertext == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] plaintext = new byte[ciphertext.length];
|
||||
|
||||
int k = 0;
|
||||
int cipherLen = ciphertext.length;
|
||||
while (k + 16 <= cipherLen) {
|
||||
byte[] cellCipher = new byte[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
cellCipher[i] = ciphertext[k + i];
|
||||
}
|
||||
|
||||
byte[] cellPlain = decode16(cellCipher, key);
|
||||
for (int j = 0; j < cellPlain.length; j++) {
|
||||
plaintext[k + j] = cellPlain[j];
|
||||
}
|
||||
|
||||
k += 16;
|
||||
}
|
||||
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
public static String decodeSMS4toString(byte[] ciphertext, byte[] key) {
|
||||
byte[] plaintext = decodeSMS4(ciphertext, key);
|
||||
if (plaintext == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return (new String(plaintext)).replaceAll("\000", "");
|
||||
}
|
||||
|
||||
private static byte[] encode16(byte[] plaintext, byte[] key) {
|
||||
byte[] cipher = new byte[16];
|
||||
sms4(plaintext, 16, key, cipher, 1);
|
||||
|
||||
return cipher;
|
||||
}
|
||||
|
||||
private static byte[] decode16(byte[] ciphertext, byte[] key) {
|
||||
byte[] plain = new byte[16];
|
||||
sms4(ciphertext, 16, key, plain, 0);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 71 */ private static int[] CK = new int[] { 462357, 472066609, 943670861, 1415275113, 1886879365, -1936483679, -1464879427, -993275175, -521670923, -66909679, 404694573, 876298825, 1347903077, 1819507329, -2003855715, -1532251463, -1060647211, -589042959, -117504499, 337322537, 808926789, 1280531041, 1752135293, -2071227751, -1599623499, -1128019247, -656414995, -184876535, 269950501, 741554753, 1213159005, 1684763257 };
|
||||
|
||||
|
||||
|
||||
|
||||
private static int rotl(int x, int y) {
|
||||
/* 77 */ return x << y | x >>> 32 - y;
|
||||
return plain;
|
||||
}
|
||||
}
|
||||
|
||||
private static int byteSub(int a) {
|
||||
/* 81 */ return (Sbox[a >>> 24 & 0xFF] & 0xFF) << 24 | (Sbox[a >>> 16 & 0xFF] & 0xFF) << 16 | (Sbox[a >>> 8 & 0xFF] & 0xFF) << 8 | Sbox[a & 0xFF] & 0xFF;
|
||||
}
|
||||
|
||||
|
||||
private static int left1(int b) {
|
||||
/* 86 */ return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
|
||||
}
|
||||
|
||||
private static int left2(int b) {
|
||||
/* 90 */ return b ^ rotl(b, 13) ^ rotl(b, 23);
|
||||
}
|
||||
|
||||
|
||||
private static void sms4Crypt(byte[] input, byte[] output, int[] rk) {
|
||||
/* 95 */ if (input == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int[] x = new int[4];
|
||||
int[] tmp = new int[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
tmp[0] = input[0 + 4 * i] & 0xFF;
|
||||
tmp[1] = input[1 + 4 * i] & 0xFF;
|
||||
tmp[2] = input[2 + 4 * i] & 0xFF;
|
||||
tmp[3] = input[3 + 4 * i] & 0xFF;
|
||||
x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
|
||||
}
|
||||
for (int r = 0; r < 32; r += 4) {
|
||||
int mid = x[1] ^ x[2] ^ x[3] ^ rk[r + 0];
|
||||
mid = byteSub(mid);
|
||||
x[0] = x[0] ^ left1(mid);
|
||||
|
||||
mid = x[2] ^ x[3] ^ x[0] ^ rk[r + 1];
|
||||
mid = byteSub(mid);
|
||||
x[1] = x[1] ^ left1(mid);
|
||||
|
||||
mid = x[3] ^ x[0] ^ x[1] ^ rk[r + 2];
|
||||
mid = byteSub(mid);
|
||||
x[2] = x[2] ^ left1(mid);
|
||||
|
||||
mid = x[0] ^ x[1] ^ x[2] ^ rk[r + 3];
|
||||
mid = byteSub(mid);
|
||||
x[3] = x[3] ^ left1(mid);
|
||||
}
|
||||
|
||||
|
||||
for (int j = 0; j < 16; j += 4) {
|
||||
output[j] = (byte)(x[3 - j / 4] >>> 24 & 0xFF);
|
||||
output[j + 1] = (byte)(x[3 - j / 4] >>> 16 & 0xFF);
|
||||
output[j + 2] = (byte)(x[3 - j / 4] >>> 8 & 0xFF);
|
||||
output[j + 3] = (byte)(x[3 - j / 4] & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void sms4KeyExt(byte[] key, int[] rk, int cryptFlag) {
|
||||
int[] x = new int[4];
|
||||
int[] tmp = new int[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
tmp[0] = key[0 + 4 * i] & 0xFF;
|
||||
tmp[1] = key[1 + 4 * i] & 0xFF;
|
||||
tmp[2] = key[2 + 4 * i] & 0xFF;
|
||||
tmp[3] = key[3 + 4 * i] & 0xFF;
|
||||
x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
|
||||
}
|
||||
x[0] = x[0] ^ 0xA3B1BAC6;
|
||||
x[1] = x[1] ^ 0x56AA3350;
|
||||
x[2] = x[2] ^ 0x677D9197;
|
||||
x[3] = x[3] ^ 0xB27022DC; int r;
|
||||
for (r = 0; r < 32; r += 4) {
|
||||
int mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
|
||||
mid = byteSub(mid);
|
||||
x[0] = x[0] ^ left2(mid); rk[r + 0] = x[0] ^ left2(mid);
|
||||
|
||||
mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
|
||||
mid = byteSub(mid);
|
||||
x[1] = x[1] ^ left2(mid); rk[r + 1] = x[1] ^ left2(mid);
|
||||
|
||||
mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
|
||||
mid = byteSub(mid);
|
||||
x[2] = x[2] ^ left2(mid); rk[r + 2] = x[2] ^ left2(mid);
|
||||
|
||||
mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
|
||||
mid = byteSub(mid);
|
||||
x[3] = x[3] ^ left2(mid); rk[r + 3] = x[3] ^ left2(mid);
|
||||
}
|
||||
|
||||
|
||||
if (cryptFlag == 0) {
|
||||
for (r = 0; r < 16; r++) {
|
||||
int mid = rk[r];
|
||||
rk[r] = rk[31 - r];
|
||||
rk[31 - r] = mid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int sms4(byte[] in, int inLen, byte[] key, byte[] out, int cryptFlag) {
|
||||
int point = 0;
|
||||
int[] roundKey = new int[32];
|
||||
|
||||
sms4KeyExt(key, roundKey, cryptFlag);
|
||||
byte[] input = null;
|
||||
byte[] output = new byte[16];
|
||||
|
||||
while (inLen >= 16) {
|
||||
input = Arrays.copyOfRange(in, point, point + 16);
|
||||
sms4Crypt(input, output, roundKey);
|
||||
System.arraycopy(output, 0, out, point, 16);
|
||||
inLen -= 16;
|
||||
point += 16;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String encodeSMS4(String plaintext, String key) throws UnsupportedEncodingException {
|
||||
for (int i = (plaintext.getBytes("UTF-8")).length % 16; i < 16; i++) {
|
||||
plaintext = plaintext + Character.MIN_VALUE;
|
||||
}
|
||||
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
return Base64.encodeBase64String(encodeSMS4(plaintext.getBytes("UTF-8"), keyHex));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encodeSMS4(String plaintext, byte[] key) throws UnsupportedEncodingException {
|
||||
if (plaintext == null || plaintext.equals("")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (int i = (plaintext.getBytes("UTF-8")).length % 16; i < 16; i++) {
|
||||
plaintext = plaintext + Character.MIN_VALUE;
|
||||
}
|
||||
|
||||
return encodeSMS4(plaintext.getBytes("UTF-8"), key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encodeSMS4(byte[] data, String key) throws UnsupportedEncodingException {
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
return encodeSMS4(data, keyHex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] encodeSMS4(byte[] plaintext, byte[] key) {
|
||||
byte[] ciphertext = new byte[plaintext.length];
|
||||
|
||||
int k = 0;
|
||||
int plainLen = plaintext.length;
|
||||
while (k + 16 <= plainLen) {
|
||||
byte[] cellPlain = new byte[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
cellPlain[i] = plaintext[k + i];
|
||||
}
|
||||
|
||||
byte[] cellCipher = encode16(cellPlain, key);
|
||||
for (int j = 0; j < cellCipher.length; j++) {
|
||||
ciphertext[k + j] = cellCipher[j];
|
||||
}
|
||||
|
||||
k += 16;
|
||||
}
|
||||
|
||||
return ciphertext;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String decodeSMS4(String cipherString, String key) throws UnsupportedEncodingException {
|
||||
byte[] ciphertext = Base64.decodeBase64(cipherString);
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
|
||||
return decodeSMS4toString(ciphertext, keyHex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] decodeSMS4(byte[] data, String key) throws UnsupportedEncodingException {
|
||||
byte[] keyHex = key.getBytes("UTF-8");
|
||||
|
||||
return decodeSMS4(data, keyHex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] decodeSMS4(byte[] ciphertext, byte[] key) {
|
||||
if (ciphertext == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] plaintext = new byte[ciphertext.length];
|
||||
|
||||
int k = 0;
|
||||
int cipherLen = ciphertext.length;
|
||||
while (k + 16 <= cipherLen) {
|
||||
byte[] cellCipher = new byte[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
cellCipher[i] = ciphertext[k + i];
|
||||
}
|
||||
|
||||
byte[] cellPlain = decode16(cellCipher, key);
|
||||
for (int j = 0; j < cellPlain.length; j++) {
|
||||
plaintext[k + j] = cellPlain[j];
|
||||
}
|
||||
|
||||
k += 16;
|
||||
}
|
||||
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String decodeSMS4toString(byte[] ciphertext, byte[] key) {
|
||||
byte[] plaintext = decodeSMS4(ciphertext, key);
|
||||
if (plaintext == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return (new String(plaintext)).replaceAll("\000", "");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static byte[] encode16(byte[] plaintext, byte[] key) {
|
||||
byte[] cipher = new byte[16];
|
||||
sms4(plaintext, 16, key, cipher, 1);
|
||||
|
||||
return cipher;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static byte[] decode16(byte[] ciphertext, byte[] key) {
|
||||
byte[] plain = new byte[16];
|
||||
sms4(ciphertext, 16, key, plain, 0);
|
||||
|
||||
return plain;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-22
@@ -2,29 +2,16 @@ package cn.cloudwalk.cloud.sensitive;
|
||||
|
||||
import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
|
||||
public abstract class AbstractSensitiveHandler implements SensitiveHandler {
|
||||
public String sensitive(String text, SensitiveType type) {
|
||||
if (type.equals(strategy())) {
|
||||
return sensitive(text);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract SensitiveType strategy();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public abstract class AbstractSensitiveHandler
|
||||
implements SensitiveHandler
|
||||
{
|
||||
public String sensitive(String text, SensitiveType type) {
|
||||
if (type.equals(strategy())) {
|
||||
return sensitive(text);
|
||||
public abstract String sensitive(String paramString);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract SensitiveType strategy();
|
||||
|
||||
public abstract String sensitive(String paramString);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+20
-39
@@ -11,46 +11,27 @@ import cn.cloudwalk.cloud.sensitive.handler.MobilePhoneSensitiveHandler;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CompositeSensitiveHandler implements SensitiveHandler {
|
||||
private static List<SensitiveHandler> SENSITIVE_HANDLER_LIST = new ArrayList<>();
|
||||
|
||||
static {
|
||||
SENSITIVE_HANDLER_LIST.add(new IdCardSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new EmailSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new BankCardSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new CnapsCodeSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new FixedPhoneSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new ChineseNameSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new MobilePhoneSensitiveHandler());
|
||||
}
|
||||
|
||||
public String sensitive(String text, SensitiveType type) {
|
||||
for (SensitiveHandler handler : SENSITIVE_HANDLER_LIST) {
|
||||
String result = handler.sensitive(text, type);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CompositeSensitiveHandler
|
||||
implements SensitiveHandler
|
||||
{
|
||||
private static List<SensitiveHandler> SENSITIVE_HANDLER_LIST = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
|
||||
static {
|
||||
SENSITIVE_HANDLER_LIST.add(new IdCardSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new EmailSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new BankCardSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new CnapsCodeSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new FixedPhoneSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new ChineseNameSensitiveHandler());
|
||||
SENSITIVE_HANDLER_LIST.add(new MobilePhoneSensitiveHandler());
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text, SensitiveType type) {
|
||||
for (SensitiveHandler handler : SENSITIVE_HANDLER_LIST) {
|
||||
String result = handler.sensitive(text, type);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+31
-59
@@ -12,71 +12,43 @@ import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class I8ndescribedSerialize extends JsonSerializer<String> implements ContextualSerializer {
|
||||
private I8ndescribed i8ndescribed;
|
||||
|
||||
public I8ndescribedSerialize() {
|
||||
}
|
||||
|
||||
public I8ndescribedSerialize(I8ndescribed i8ndescribed) {
|
||||
this.i8ndescribed = i8ndescribed;
|
||||
}
|
||||
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
|
||||
throws JsonMappingException {
|
||||
if (property != null) {
|
||||
if (Objects.equals(property.getType().getRawClass(), String.class)) {
|
||||
|
||||
I8ndescribed cloudwalkI8N = (I8ndescribed) property.getAnnotation(I8ndescribed.class);
|
||||
if (cloudwalkI8N == null) {
|
||||
cloudwalkI8N = (I8ndescribed) property.getContextAnnotation(I8ndescribed.class);
|
||||
}
|
||||
|
||||
if (cloudwalkI8N != null) {
|
||||
return new I8ndescribedSerialize(cloudwalkI8N);
|
||||
}
|
||||
}
|
||||
|
||||
return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
|
||||
return prov.findNullValueSerializer(property);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class I8ndescribedSerialize
|
||||
extends JsonSerializer<String>
|
||||
implements ContextualSerializer
|
||||
{
|
||||
private I8ndescribed i8ndescribed;
|
||||
|
||||
public I8ndescribedSerialize() {}
|
||||
|
||||
public I8ndescribedSerialize(I8ndescribed i8ndescribed) {
|
||||
this.i8ndescribed = i8ndescribed;
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers)
|
||||
throws IOException, JsonProcessingException {
|
||||
if (this.i8ndescribed == null) {
|
||||
gen.writeString(value);
|
||||
} else {
|
||||
gen.writeString(ApplicationContextUtils.getMessage(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
||||
if (property != null) {
|
||||
if (Objects.equals(property.getType().getRawClass(), String.class)) {
|
||||
|
||||
I8ndescribed cloudwalkI8N = (I8ndescribed)property.getAnnotation(I8ndescribed.class);
|
||||
if (cloudwalkI8N == null) {
|
||||
cloudwalkI8N = (I8ndescribed)property.getContextAnnotation(I8ndescribed.class);
|
||||
}
|
||||
|
||||
if (cloudwalkI8N != null) {
|
||||
return new I8ndescribedSerialize(cloudwalkI8N);
|
||||
}
|
||||
}
|
||||
|
||||
return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
|
||||
return prov.findNullValueSerializer(property);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
|
||||
if (this.i8ndescribed == null) {
|
||||
gen.writeString(value);
|
||||
} else {
|
||||
gen.writeString(ApplicationContextUtils.getMessage(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -3,7 +3,5 @@ package cn.cloudwalk.cloud.sensitive;
|
||||
import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
|
||||
public interface SensitiveHandler {
|
||||
String sensitive(String paramString, SensitiveType paramSensitiveType);
|
||||
String sensitive(String paramString, SensitiveType paramSensitiveType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+50
-94
@@ -15,114 +15,70 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SensitiveInfoSerialize extends JsonSerializer<String> implements ContextualSerializer {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SensitiveInfoSerialize.class);
|
||||
|
||||
private static CompositeSensitiveHandler handler = new CompositeSensitiveHandler();
|
||||
|
||||
private SensitiveField sensitiveField;
|
||||
|
||||
public SensitiveInfoSerialize() {
|
||||
}
|
||||
|
||||
public SensitiveInfoSerialize(SensitiveField sensitiveField) {
|
||||
this.sensitiveField = sensitiveField;
|
||||
}
|
||||
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
|
||||
throws JsonMappingException {
|
||||
if (property != null) {
|
||||
|
||||
if (Objects.equals(property.getType().getRawClass(), String.class)) {
|
||||
|
||||
SensitiveField sensitiveField = (SensitiveField) property.getAnnotation(SensitiveField.class);
|
||||
if (sensitiveField == null) {
|
||||
sensitiveField = (SensitiveField) property.getContextAnnotation(SensitiveField.class);
|
||||
}
|
||||
|
||||
if (sensitiveField != null) {
|
||||
return new SensitiveInfoSerialize(sensitiveField);
|
||||
}
|
||||
} else {
|
||||
|
||||
logger.debug("未对字段进行序列化,因为数据类型非String");
|
||||
}
|
||||
|
||||
return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
|
||||
return prov.findNullValueSerializer(property);
|
||||
}
|
||||
|
||||
public class SensitiveInfoSerialize
|
||||
extends JsonSerializer<String>
|
||||
implements ContextualSerializer
|
||||
{
|
||||
/* 34 */ private static final Logger logger = LoggerFactory.getLogger(SensitiveInfoSerialize.class);
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers)
|
||||
throws IOException, JsonProcessingException {
|
||||
SensitiveType type = this.sensitiveField.type();
|
||||
if (!SensitiveType.NULL.equals(type)) {
|
||||
String ret = handler.sensitive(value, type);
|
||||
gen.writeString(ret);
|
||||
} else {
|
||||
gen.writeString(split(value));
|
||||
}
|
||||
}
|
||||
|
||||
private String split(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = text.length();
|
||||
if (this.sensitiveField.prefix() + this.sensitiveField.suffix() >= length) {
|
||||
return text;
|
||||
}
|
||||
|
||||
String left = StringUtils.left(text, this.sensitiveField.prefix());
|
||||
String right = StringUtils.right(text, this.sensitiveField.suffix());
|
||||
|
||||
/* 39 */ private static CompositeSensitiveHandler handler = new CompositeSensitiveHandler();
|
||||
|
||||
|
||||
|
||||
|
||||
private SensitiveField sensitiveField;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public SensitiveInfoSerialize() {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public SensitiveInfoSerialize(SensitiveField sensitiveField) {
|
||||
/* 57 */ this.sensitiveField = sensitiveField;
|
||||
length = this.sensitiveField.suffix() + this.sensitiveField.split();
|
||||
return left.concat(StringUtils.leftPad(right, length, '*'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
||||
/* 64 */ if (property != null) {
|
||||
|
||||
|
||||
/* 67 */ if (Objects.equals(property.getType().getRawClass(), String.class)) {
|
||||
|
||||
|
||||
/* 70 */ SensitiveField sensitiveField = (SensitiveField)property.getAnnotation(SensitiveField.class);
|
||||
/* 71 */ if (sensitiveField == null) {
|
||||
/* 72 */ sensitiveField = (SensitiveField)property.getContextAnnotation(SensitiveField.class);
|
||||
}
|
||||
|
||||
|
||||
/* 76 */ if (sensitiveField != null) {
|
||||
/* 77 */ return new SensitiveInfoSerialize(sensitiveField);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
/* 82 */ logger.debug("未对字段进行序列化,因为数据类型非String");
|
||||
}
|
||||
|
||||
/* 85 */ return prov.findValueSerializer(property.getType(), property);
|
||||
}
|
||||
|
||||
/* 88 */ return prov.findNullValueSerializer(property);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
|
||||
/* 95 */ SensitiveType type = this.sensitiveField.type();
|
||||
/* 96 */ if (!SensitiveType.NULL.equals(type)) {
|
||||
/* 97 */ String ret = handler.sensitive(value, type);
|
||||
/* 98 */ gen.writeString(ret);
|
||||
} else {
|
||||
gen.writeString(split(value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private String split(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = text.length();
|
||||
if (this.sensitiveField.prefix() + this.sensitiveField.suffix() >= length) {
|
||||
return text;
|
||||
}
|
||||
|
||||
String left = StringUtils.left(text, this.sensitiveField.prefix());
|
||||
String right = StringUtils.right(text, this.sensitiveField.suffix());
|
||||
|
||||
length = this.sensitiveField.suffix() + this.sensitiveField.split();
|
||||
return left.concat(StringUtils.leftPad(right, length, '*'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-30
@@ -9,37 +9,19 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({I8ndescribedProperties.class})
|
||||
@ConditionalOnProperty(name = {"disable"}, havingValue = "true", matchIfMissing = false, prefix = "cloudwalk.i8ndescribed")
|
||||
public class I8ndescribedConfiguration
|
||||
{
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
@EnableConfigurationProperties({ I8ndescribedProperties.class })
|
||||
@ConditionalOnProperty(name = {
|
||||
"disable" }, havingValue = "true", matchIfMissing = false, prefix = "cloudwalk.i8ndescribed")
|
||||
public class I8ndescribedConfiguration {
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Bean
|
||||
public DisableI8ndescribedIntrospector disableI8ndescribedIntrospector() {
|
||||
DisableI8ndescribedIntrospector intrspector = new DisableI8ndescribedIntrospector();
|
||||
this.objectMapper.setAnnotationIntrospector((AnnotationIntrospector)intrspector);
|
||||
@Bean
|
||||
public DisableI8ndescribedIntrospector disableI8ndescribedIntrospector() {
|
||||
DisableI8ndescribedIntrospector intrspector = new DisableI8ndescribedIntrospector();
|
||||
this.objectMapper.setAnnotationIntrospector((AnnotationIntrospector) intrspector);
|
||||
|
||||
return intrspector;
|
||||
return intrspector;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-38
@@ -2,44 +2,18 @@ package cn.cloudwalk.cloud.sensitive.configuration;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ConfigurationProperties(prefix = "cloudwalk.sensitive")
|
||||
public class I8ndescribedProperties
|
||||
{
|
||||
public static final String I8NDESCRIBED_PREFIX = "cloudwalk.i8ndescribed";
|
||||
public static final String I8NDESCRIBED_DISABLE_VALUE = "true";
|
||||
public static final String I8NDESCRIBED_KEY = "disable";
|
||||
private String disable = "true";
|
||||
public class I8ndescribedProperties {
|
||||
public static final String I8NDESCRIBED_PREFIX = "cloudwalk.i8ndescribed";
|
||||
public static final String I8NDESCRIBED_DISABLE_VALUE = "true";
|
||||
public static final String I8NDESCRIBED_KEY = "disable";
|
||||
private String disable = "true";
|
||||
|
||||
public String getDisable() {
|
||||
return this.disable;
|
||||
public String getDisable() {
|
||||
return this.disable;
|
||||
}
|
||||
|
||||
public void setDisable(String disable) {
|
||||
this.disable = disable;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDisable(String disable) {
|
||||
this.disable = disable;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-29
@@ -9,36 +9,19 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({SensitiveInfoProperties.class})
|
||||
@ConditionalOnProperty(name = {"disable"}, havingValue = "true", matchIfMissing = false, prefix = "cloudwalk.sensitive")
|
||||
public class SensitiveInfoConfiguration
|
||||
{
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
@EnableConfigurationProperties({ SensitiveInfoProperties.class })
|
||||
@ConditionalOnProperty(name = {
|
||||
"disable" }, havingValue = "true", matchIfMissing = false, prefix = "cloudwalk.sensitive")
|
||||
public class SensitiveInfoConfiguration {
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Bean
|
||||
public DisableSensitiveInfoIntrospector disableSensitiveInfoIntrospector() {
|
||||
DisableSensitiveInfoIntrospector intrspector = new DisableSensitiveInfoIntrospector();
|
||||
this.objectMapper.setAnnotationIntrospector((AnnotationIntrospector)intrspector);
|
||||
@Bean
|
||||
public DisableSensitiveInfoIntrospector disableSensitiveInfoIntrospector() {
|
||||
DisableSensitiveInfoIntrospector intrspector = new DisableSensitiveInfoIntrospector();
|
||||
this.objectMapper.setAnnotationIntrospector((AnnotationIntrospector) intrspector);
|
||||
|
||||
return intrspector;
|
||||
return intrspector;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-37
@@ -2,43 +2,18 @@ package cn.cloudwalk.cloud.sensitive.configuration;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ConfigurationProperties(prefix = "cloudwalk.sensitive")
|
||||
public class SensitiveInfoProperties
|
||||
{
|
||||
public static final String FIELD_SENSITIVE_PREFIX = "cloudwalk.sensitive";
|
||||
public static final String FIELD_SENSITIVE_DISABLE_VALUE = "true";
|
||||
public static final String FIELD_SENSITIVE_KEY = "disable";
|
||||
private String disable = "true";
|
||||
public class SensitiveInfoProperties {
|
||||
public static final String FIELD_SENSITIVE_PREFIX = "cloudwalk.sensitive";
|
||||
public static final String FIELD_SENSITIVE_DISABLE_VALUE = "true";
|
||||
public static final String FIELD_SENSITIVE_KEY = "disable";
|
||||
private String disable = "true";
|
||||
|
||||
public String getDisable() {
|
||||
return this.disable;
|
||||
public String getDisable() {
|
||||
return this.disable;
|
||||
}
|
||||
|
||||
public void setDisable(String disable) {
|
||||
this.disable = disable;
|
||||
}
|
||||
}
|
||||
|
||||
public void setDisable(String disable) {
|
||||
this.disable = disable;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-30
@@ -4,37 +4,23 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class BankCardSensitiveHandler extends AbstractSensitiveHandler {
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.BANK_CARD;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 10) {
|
||||
return text;
|
||||
}
|
||||
String left = StringUtils.left(text, 6);
|
||||
String right = StringUtils.right(text, 4);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class BankCardSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.BANK_CARD;
|
||||
return left.concat(StringUtils.removeStart(StringUtils.leftPad(right, length, '*'), "******"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 10) {
|
||||
return text;
|
||||
}
|
||||
String left = StringUtils.left(text, 6);
|
||||
String right = StringUtils.right(text, 4);
|
||||
|
||||
return left.concat(StringUtils.removeStart(StringUtils.leftPad(right, length, '*'), "******"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-26
@@ -4,33 +4,19 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ChineseNameSensitiveHandler extends AbstractSensitiveHandler {
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.CHINESE_NAME;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
String w = StringUtils.left(text, 1);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ChineseNameSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.CHINESE_NAME;
|
||||
return StringUtils.rightPad(w, (length > 4) ? 4 : length, '*');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
String w = StringUtils.left(text, 1);
|
||||
|
||||
return StringUtils.rightPad(w, (length > 4) ? 4 : length, '*');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-29
@@ -4,35 +4,21 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class CnapsCodeSensitiveHandler extends AbstractSensitiveHandler {
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.CNAPS_CODE;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CnapsCodeSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.CNAPS_CODE;
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 2) {
|
||||
return text;
|
||||
}
|
||||
String left = StringUtils.left(text, 2);
|
||||
return StringUtils.rightPad(left, length, '*');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 2) {
|
||||
return text;
|
||||
}
|
||||
String left = StringUtils.left(text, 2);
|
||||
return StringUtils.rightPad(left, length, '*');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-29
@@ -4,35 +4,21 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class EmailSensitiveHandler extends AbstractSensitiveHandler {
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.EMAIL;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class EmailSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.EMAIL;
|
||||
int index = StringUtils.indexOf(text, 64);
|
||||
if (index <= 1) {
|
||||
return text;
|
||||
}
|
||||
return StringUtils.rightPad(StringUtils.left(text, 1), index, '*')
|
||||
.concat(StringUtils.mid(text, index, StringUtils.length(text)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int index = StringUtils.indexOf(text, 64);
|
||||
if (index <= 1) {
|
||||
return text;
|
||||
}
|
||||
return StringUtils.rightPad(StringUtils.left(text, 1), index, '*')
|
||||
.concat(StringUtils.mid(text, index, StringUtils.length(text)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-29
@@ -4,35 +4,21 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class FixedPhoneSensitiveHandler extends AbstractSensitiveHandler {
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.FIXED_PHONE;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class FixedPhoneSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.FIXED_PHONE;
|
||||
int length = StringUtils.length(text);
|
||||
if (length <= 4) {
|
||||
return text;
|
||||
}
|
||||
String last = StringUtils.right(text, 4);
|
||||
return StringUtils.leftPad(last, length, '*');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length <= 4) {
|
||||
return text;
|
||||
}
|
||||
String last = StringUtils.right(text, 4);
|
||||
return StringUtils.leftPad(last, length, '*');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+19
-45
@@ -4,53 +4,27 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class IdCardSensitiveHandler extends AbstractSensitiveHandler {
|
||||
private static final int ID_CARD_LENGTH = 8;
|
||||
private static final int PRE_CHAR_LENGTH = 3;
|
||||
private static final int LST_CHAR_LENGTH = 4;
|
||||
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.ID_CARD;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 8) {
|
||||
return text;
|
||||
}
|
||||
String pre = StringUtils.left(text, 3);
|
||||
String num = StringUtils.right(text, 4);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class IdCardSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
private static final int ID_CARD_LENGTH = 8;
|
||||
private static final int PRE_CHAR_LENGTH = 3;
|
||||
private static final int LST_CHAR_LENGTH = 4;
|
||||
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.ID_CARD;
|
||||
return pre.concat(StringUtils.leftPad(num, 8, '*'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 8) {
|
||||
return text;
|
||||
}
|
||||
String pre = StringUtils.left(text, 3);
|
||||
String num = StringUtils.right(text, 4);
|
||||
|
||||
return pre.concat(StringUtils.leftPad(num, 8, '*'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-30
@@ -4,37 +4,23 @@ import cn.cloudwalk.cloud.enums.SensitiveType;
|
||||
import cn.cloudwalk.cloud.sensitive.AbstractSensitiveHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class MobilePhoneSensitiveHandler extends AbstractSensitiveHandler {
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.MOBILE_PHONE;
|
||||
}
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 7) {
|
||||
return text;
|
||||
}
|
||||
String left = StringUtils.left(text, 3);
|
||||
String right = StringUtils.right(text, 4);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class MobilePhoneSensitiveHandler
|
||||
extends AbstractSensitiveHandler
|
||||
{
|
||||
public SensitiveType strategy() {
|
||||
return SensitiveType.MOBILE_PHONE;
|
||||
return left.concat(StringUtils.leftPad(right, 8, '*'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String sensitive(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
int length = StringUtils.length(text);
|
||||
if (length < 7) {
|
||||
return text;
|
||||
}
|
||||
String left = StringUtils.left(text, 3);
|
||||
String right = StringUtils.right(text, 4);
|
||||
|
||||
return left.concat(StringUtils.leftPad(right, 8, '*'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+8
-22
@@ -4,27 +4,13 @@ import cn.cloudwalk.cloud.annotation.I8ndescribed;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
public class DisableI8ndescribedIntrospector extends JacksonAnnotationIntrospector {
|
||||
private static final long serialVersionUID = 5915688650237481024L;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class DisableI8ndescribedIntrospector
|
||||
extends JacksonAnnotationIntrospector
|
||||
{
|
||||
private static final long serialVersionUID = 5915688650237481024L;
|
||||
|
||||
public boolean isAnnotationBundle(Annotation ann) {
|
||||
if (ann.annotationType().equals(I8ndescribed.class)) {
|
||||
return false;
|
||||
public boolean isAnnotationBundle(Annotation ann) {
|
||||
if (ann.annotationType().equals(I8ndescribed.class)) {
|
||||
return false;
|
||||
}
|
||||
return super.isAnnotationBundle(ann);
|
||||
}
|
||||
}
|
||||
return super.isAnnotationBundle(ann);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+8
-22
@@ -4,27 +4,13 @@ import cn.cloudwalk.cloud.annotation.SensitiveField;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
public class DisableSensitiveInfoIntrospector extends JacksonAnnotationIntrospector {
|
||||
private static final long serialVersionUID = 5915688650237481024L;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class DisableSensitiveInfoIntrospector
|
||||
extends JacksonAnnotationIntrospector
|
||||
{
|
||||
private static final long serialVersionUID = 5915688650237481024L;
|
||||
|
||||
public boolean isAnnotationBundle(Annotation ann) {
|
||||
if (ann.annotationType().equals(SensitiveField.class)) {
|
||||
return false;
|
||||
public boolean isAnnotationBundle(Annotation ann) {
|
||||
if (ann.annotationType().equals(SensitiveField.class)) {
|
||||
return false;
|
||||
}
|
||||
return super.isAnnotationBundle(ann);
|
||||
}
|
||||
}
|
||||
return super.isAnnotationBundle(ann);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-17
@@ -2,21 +2,8 @@ package cn.cloudwalk.cloud.serial;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class UUIDGeneralSerial
|
||||
implements UUIDSerial
|
||||
{
|
||||
public String uuid() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
public class UUIDGeneralSerial implements UUIDSerial {
|
||||
public String uuid() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
package cn.cloudwalk.cloud.serial;
|
||||
|
||||
public interface UUIDSerial {
|
||||
String uuid();
|
||||
String uuid();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+23
-74
@@ -2,84 +2,33 @@ package cn.cloudwalk.cloud.session.company;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CompanyContext implements Serializable {
|
||||
private static final long serialVersionUID = 7852895769123192530L;
|
||||
private String companyId;
|
||||
private String corpCode;
|
||||
private String companyName;
|
||||
|
||||
public String getCompanyId() {
|
||||
return this.companyId;
|
||||
}
|
||||
|
||||
public void setCompanyId(String companyId) {
|
||||
this.companyId = companyId;
|
||||
}
|
||||
|
||||
public String getCompanyName() {
|
||||
return this.companyName;
|
||||
}
|
||||
|
||||
public void setCompanyName(String companyName) {
|
||||
this.companyName = companyName;
|
||||
}
|
||||
|
||||
public String getCorpCode() {
|
||||
return this.corpCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CompanyContext
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 7852895769123192530L;
|
||||
private String companyId;
|
||||
private String corpCode;
|
||||
private String companyName;
|
||||
|
||||
public String getCompanyId() {
|
||||
return this.companyId;
|
||||
public void setCorpCode(String corpCode) {
|
||||
this.corpCode = corpCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setCompanyId(String companyId) {
|
||||
this.companyId = companyId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String getCompanyName() {
|
||||
return this.companyName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setCompanyName(String companyName) {
|
||||
this.companyName = companyName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String getCorpCode() {
|
||||
return this.corpCode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setCorpCode(String corpCode) {
|
||||
this.corpCode = corpCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-24
@@ -1,29 +1,14 @@
|
||||
package cn.cloudwalk.cloud.session.extend;
|
||||
|
||||
public class DefaultExtendContext<T> implements ExtendContext {
|
||||
private static final long serialVersionUID = -4446456929803517034L;
|
||||
private T value;
|
||||
|
||||
public T getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class DefaultExtendContext<T>
|
||||
implements ExtendContext
|
||||
{
|
||||
private static final long serialVersionUID = -4446456929803517034L;
|
||||
private T value;
|
||||
|
||||
public T getValue() {
|
||||
return this.value;
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+2
-3
@@ -2,6 +2,5 @@ package cn.cloudwalk.cloud.session.extend;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public interface ExtendContext extends Serializable {}
|
||||
|
||||
|
||||
public interface ExtendContext extends Serializable {
|
||||
}
|
||||
|
||||
+9
-24
@@ -1,29 +1,14 @@
|
||||
package cn.cloudwalk.cloud.session.note;
|
||||
|
||||
public class ErrorContext implements NotesContext {
|
||||
private static final long serialVersionUID = -4931497254293052617L;
|
||||
private String message;
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ErrorContext
|
||||
implements NotesContext
|
||||
{
|
||||
private static final long serialVersionUID = -4931497254293052617L;
|
||||
private String message;
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -3,7 +3,5 @@ package cn.cloudwalk.cloud.session.note;
|
||||
import java.io.Serializable;
|
||||
|
||||
public interface NotesContext extends Serializable {
|
||||
String getMessage();
|
||||
String getMessage();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+24
-53
@@ -4,63 +4,34 @@ import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class UserContext implements Serializable {
|
||||
private static final long serialVersionUID = 1937298343860044405L;
|
||||
@NotNull(message = "10000013")
|
||||
private String caller;
|
||||
private String callerName;
|
||||
private Set<String> roles;
|
||||
|
||||
public String getCaller() {
|
||||
return this.caller;
|
||||
}
|
||||
|
||||
public void setCaller(String caller) {
|
||||
this.caller = caller;
|
||||
}
|
||||
|
||||
public String getCallerName() {
|
||||
return this.callerName;
|
||||
}
|
||||
|
||||
public void setCallerName(String callerName) {
|
||||
this.callerName = callerName;
|
||||
}
|
||||
|
||||
public Set<String> getRoles() {
|
||||
return this.roles;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class UserContext
|
||||
implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1937298343860044405L;
|
||||
@NotNull(message = "10000013")
|
||||
private String caller;
|
||||
private String callerName;
|
||||
private Set<String> roles;
|
||||
|
||||
public String getCaller() {
|
||||
return this.caller;
|
||||
public void setRoles(Set<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCaller(String caller) {
|
||||
this.caller = caller;
|
||||
}
|
||||
|
||||
public String getCallerName() {
|
||||
return this.callerName;
|
||||
}
|
||||
|
||||
public void setCallerName(String callerName) {
|
||||
this.callerName = callerName;
|
||||
}
|
||||
|
||||
public Set<String> getRoles() {
|
||||
return this.roles;
|
||||
}
|
||||
|
||||
public void setRoles(Set<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+24
-55
@@ -8,66 +8,35 @@ import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Component
|
||||
public class ApplicationContextUtils
|
||||
implements ApplicationContextAware
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApplicationContextUtils.class);
|
||||
public class ApplicationContextUtils implements ApplicationContextAware {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApplicationContextUtils.class);
|
||||
|
||||
private static ApplicationContext context;
|
||||
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
ApplicationContextUtils.context = context;
|
||||
}
|
||||
|
||||
private static ApplicationContext context;
|
||||
public static ApplicationContext getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public static String getMessage(String code) {
|
||||
try {
|
||||
return context.getMessage(code, null, LocaleContextHolder.getLocale());
|
||||
} catch (Exception e) {
|
||||
logger.error("Try to resolve the message falid");
|
||||
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
ApplicationContextUtils.context = context;
|
||||
public static <T> T getBean(String name) {
|
||||
return (T) context.getBean(name);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> requiredType) {
|
||||
return (T) context.getBean(requiredType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static ApplicationContext getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getMessage(String code) {
|
||||
try {
|
||||
return context.getMessage(code, null, LocaleContextHolder.getLocale());
|
||||
} catch (Exception e) {
|
||||
logger.error("Try to resolve the message falid");
|
||||
|
||||
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T getBean(String name) {
|
||||
return (T)context.getBean(name);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> requiredType) {
|
||||
return (T)context.getBean(requiredType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+47
-96
@@ -8,110 +8,61 @@ import java.util.List;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
public class BeanCopyUtils {
|
||||
public static <T> T copyProperties(Object source, Class<T> targetClazz) {
|
||||
T target = null;
|
||||
try {
|
||||
target = targetClazz.getDeclaredConstructor().newInstance();
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
BeanUtils.copyProperties(source, target);
|
||||
return target;
|
||||
}
|
||||
|
||||
public static <T> T copyProperties(Object source, T target) {
|
||||
BeanUtils.copyProperties(source, target);
|
||||
return target;
|
||||
}
|
||||
|
||||
public static <T> T copyProperties(Object source, CloudwalkCallContext context, T target) {
|
||||
BeanUtils.copyProperties(source, target);
|
||||
if (CloudwalkBaseTimes.class.isAssignableFrom(target.getClass())) {
|
||||
copyProperties((CloudwalkBaseTimes) target, context);
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
public static void copyProperties(CloudwalkBaseTimes dto, CloudwalkCallContext context) {
|
||||
if (context == null || dto == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
dto.setCreateTime(Long.valueOf(context.getCallTime().getTime()));
|
||||
dto.setCreateUserId(context.getUser().getCaller());
|
||||
|
||||
dto.setLastUpdateTime(Long.valueOf(context.getCallTime().getTime()));
|
||||
dto.setLastUpdateUserId(context.getUser().getCaller());
|
||||
}
|
||||
|
||||
public static <V, E> List<E> copy(Collection<V> list, Class<E> clazz) {
|
||||
List<E> result = new ArrayList<>(12);
|
||||
|
||||
if (!CollectionUtils.isEmpty(list)) {
|
||||
for (V source : list) {
|
||||
E target = null;
|
||||
try {
|
||||
target = clazz.getDeclaredConstructor().newInstance();
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
BeanUtils.copyProperties(source, target);
|
||||
|
||||
result.add(target);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class BeanCopyUtils
|
||||
{
|
||||
public static <T> T copyProperties(Object source, Class<T> targetClazz) {
|
||||
/* 34 */ T target = null;
|
||||
try {
|
||||
/* 36 */ target = targetClazz.getDeclaredConstructor().newInstance();
|
||||
/* 37 */ } catch (ReflectiveOperationException e) {
|
||||
/* 38 */ throw new RuntimeException(e);
|
||||
}
|
||||
/* 40 */ BeanUtils.copyProperties(source, target);
|
||||
/* 41 */ return target;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> T copyProperties(Object source, T target) {
|
||||
/* 52 */ BeanUtils.copyProperties(source, target);
|
||||
/* 53 */ return target;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> T copyProperties(Object source, CloudwalkCallContext context, T target) {
|
||||
/* 65 */ BeanUtils.copyProperties(source, target);
|
||||
/* 66 */ if (CloudwalkBaseTimes.class.isAssignableFrom(target.getClass())) {
|
||||
/* 67 */ copyProperties((CloudwalkBaseTimes)target, context);
|
||||
}
|
||||
|
||||
/* 70 */ return target;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void copyProperties(CloudwalkBaseTimes dto, CloudwalkCallContext context) {
|
||||
/* 79 */ if (context == null || dto == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* 83 */ dto.setCreateTime(Long.valueOf(context.getCallTime().getTime()));
|
||||
/* 84 */ dto.setCreateUserId(context.getUser().getCaller());
|
||||
|
||||
/* 86 */ dto.setLastUpdateTime(Long.valueOf(context.getCallTime().getTime()));
|
||||
/* 87 */ dto.setLastUpdateUserId(context.getUser().getCaller());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <V, E> List<E> copy(Collection<V> list, Class<E> clazz) {
|
||||
/* 98 */ List<E> result = new ArrayList<>(12);
|
||||
|
||||
if (!CollectionUtils.isEmpty(list)) {
|
||||
for (V source : list) {
|
||||
E target = null;
|
||||
try {
|
||||
target = clazz.getDeclaredConstructor().newInstance();
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
BeanUtils.copyProperties(source, target);
|
||||
|
||||
result.add(target);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+33
-96
@@ -8,110 +8,47 @@ import org.joda.time.DateTime;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CloudwalkDateUtils {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CloudwalkDateUtils.class);
|
||||
|
||||
private static final String YYYYMMDD = "yyyyMMdd";
|
||||
|
||||
private static final String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
|
||||
private static final String HH_MM_SS = "HHmmss";
|
||||
|
||||
public static Date getCurrentDate() {
|
||||
return new Date(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static String formatDate(Date date, String pattern) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
public static Date toDate(String str, String pattern) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
try {
|
||||
return sdf.parse(str);
|
||||
} catch (ParseException e) {
|
||||
LOG.error("日期格式转换失败,原因:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDate8YMD() {
|
||||
return (new DateTime()).toString("yyyyMMdd");
|
||||
}
|
||||
|
||||
public static String getTime8() {
|
||||
return (new DateTime()).toString("HHmmss");
|
||||
}
|
||||
|
||||
public static String getDate10YMD() {
|
||||
return (new DateTime()).toString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkDateUtils
|
||||
{
|
||||
/* 26 */ private static final Logger LOG = LoggerFactory.getLogger(CloudwalkDateUtils.class);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static final String YYYYMMDD = "yyyyMMdd";
|
||||
|
||||
|
||||
|
||||
|
||||
private static final String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
|
||||
|
||||
|
||||
|
||||
private static final String HH_MM_SS = "HHmmss";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static Date getCurrentDate() {
|
||||
/* 49 */ return new Date(System.currentTimeMillis());
|
||||
public static String getUUID() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String formatDate(Date date, String pattern) {
|
||||
/* 60 */ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
/* 61 */ return sdf.format(date);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static Date toDate(String str, String pattern) {
|
||||
/* 72 */ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
try {
|
||||
/* 74 */ return sdf.parse(str);
|
||||
/* 75 */ } catch (ParseException e) {
|
||||
/* 76 */ LOG.error("日期格式转换失败,原因:", e);
|
||||
/* 77 */ return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getDate8YMD() {
|
||||
/* 87 */ return (new DateTime()).toString("yyyyMMdd");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getTime8() {
|
||||
/* 95 */ return (new DateTime()).toString("HHmmss");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getDate10YMD() {
|
||||
return (new DateTime()).toString("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getUUID() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+21
-59
@@ -3,68 +3,30 @@ package cn.cloudwalk.cloud.utils;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class CloudwalkListUtils {
|
||||
private static final String COMMA = ",";
|
||||
private static final String SINGLE_QUOTES = "'";
|
||||
|
||||
public static <T> String join(List<T> list) {
|
||||
return join(list, ",");
|
||||
}
|
||||
|
||||
public static <T> String join(List<T> list, String separator) {
|
||||
return join(list, "'", separator);
|
||||
}
|
||||
|
||||
public static <T> String join(List<T> list, String chr, String separator) {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
if (list != null && list.size() > 0) {
|
||||
for (T t : list) {
|
||||
if (StringUtils.isNoneEmpty(new CharSequence[] { ret })) {
|
||||
ret.append(separator);
|
||||
}
|
||||
|
||||
ret.append(chr).append(t).append(chr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkListUtils
|
||||
{
|
||||
private static final String COMMA = ",";
|
||||
private static final String SINGLE_QUOTES = "'";
|
||||
|
||||
public static <T> String join(List<T> list) {
|
||||
return join(list, ",");
|
||||
return ret.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> String join(List<T> list, String separator) {
|
||||
return join(list, "'", separator);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> String join(List<T> list, String chr, String separator) {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
if (list != null && list.size() > 0) {
|
||||
for (T t : list) {
|
||||
if (StringUtils.isNoneEmpty(new CharSequence[] { ret })) {
|
||||
ret.append(separator);
|
||||
}
|
||||
|
||||
ret.append(chr).append(t).append(chr);
|
||||
}
|
||||
}
|
||||
|
||||
return ret.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+127
-219
@@ -7,251 +7,159 @@ import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class NetworkUtils {
|
||||
public enum Filter {
|
||||
ALL,
|
||||
|
||||
UP,
|
||||
|
||||
VIRTUAL,
|
||||
|
||||
LOOPBACK,
|
||||
|
||||
PHYICAL_ONLY;
|
||||
|
||||
public boolean apply(NetworkInterface input) {
|
||||
if (null == input) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
byte[] hardwareAddress;
|
||||
switch (this) {
|
||||
case UP:
|
||||
return input.isUp();
|
||||
case VIRTUAL:
|
||||
return input.isVirtual();
|
||||
case LOOPBACK:
|
||||
return input.isLoopback();
|
||||
case PHYICAL_ONLY:
|
||||
hardwareAddress = input.getHardwareAddress();
|
||||
return (null != hardwareAddress && hardwareAddress.length > 0 &&
|
||||
|
||||
!input.isVirtual() && !NetworkUtils.isVMMac(hardwareAddress));
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static Set<NetworkInterface> getNICs(Filter... filters) {
|
||||
if (null == filters) {
|
||||
filters = new Filter[] { Filter.ALL };
|
||||
}
|
||||
|
||||
Set<NetworkInterface> ret = new HashSet<>();
|
||||
Enumeration<NetworkInterface> networks = null;
|
||||
try {
|
||||
networks = NetworkInterface.getNetworkInterfaces();
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
while (networks.hasMoreElements()) {
|
||||
boolean match = false;
|
||||
NetworkInterface temp = networks.nextElement();
|
||||
for (Filter v : filters) {
|
||||
if (v.apply(temp)) {
|
||||
|
||||
public class NetworkUtils
|
||||
{
|
||||
public enum Filter
|
||||
{
|
||||
/* 25 */ ALL,
|
||||
match = true;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
ret.add(temp);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Set<NetworkInterface> getPhysicalNICs() {
|
||||
return getNICs(new Filter[] { Filter.PHYICAL_ONLY, Filter.UP });
|
||||
}
|
||||
|
||||
public static final String format(byte[] source, String separator, Radix radix) {
|
||||
if (null == source) {
|
||||
return "";
|
||||
}
|
||||
|
||||
/* 30 */ UP,
|
||||
if (null == separator) {
|
||||
separator = "";
|
||||
}
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (byte b : source) {
|
||||
buf.append(separator).append(apply(Byte.valueOf(b), radix));
|
||||
}
|
||||
|
||||
return (buf.length() > 0) ? buf.substring(separator.length()) : "";
|
||||
}
|
||||
|
||||
private static String apply(Byte input, Radix radix) {
|
||||
return String.copyValueOf(new char[] { Character.forDigit((input.byteValue() & 0xF0) >> 4, radix.value),
|
||||
Character.forDigit(input.byteValue() & 0xF, radix.value) });
|
||||
}
|
||||
|
||||
/* 35 */ VIRTUAL,
|
||||
public static final String formatMac(byte[] source, String separator) {
|
||||
return format(source, separator, Radix.HEX);
|
||||
}
|
||||
|
||||
public static final String formatIp(byte[] source) {
|
||||
return format(source, ".", Radix.DEC);
|
||||
}
|
||||
|
||||
public static byte[] getMacAddress(InetAddress address) {
|
||||
try {
|
||||
NetworkInterface nic = NetworkInterface.getByInetAddress(address);
|
||||
return (null == nic) ? null : nic.getHardwareAddress();
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getMacAddress(NetworkInterface nic, String separator) {
|
||||
try {
|
||||
return format(nic.getHardwareAddress(), separator, Radix.HEX);
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/* 40 */ LOOPBACK,
|
||||
public static String getMacAddress(InetAddress address, String separator) {
|
||||
return format(getMacAddress(address), separator, Radix.HEX);
|
||||
}
|
||||
|
||||
private static byte[][] invalidMacs = new byte[][] { { 0, 5, 105 }, { 0, 28, 20 }, { 0, 12, 41 }, { 0, 80, 86 },
|
||||
{ 8, 0, 39 }, { 10, 0, 39 }, { 0, 3, -1 }, { 0, 21, 93 } };
|
||||
|
||||
private static boolean isVMMac(byte[] mac) {
|
||||
if (null == mac) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (byte[] invalid : invalidMacs) {
|
||||
if (invalid[0] == mac[0] && invalid[1] == mac[1] && invalid[2] == mac[2]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* 45 */ PHYICAL_ONLY;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean apply(NetworkInterface input) {
|
||||
/* 48 */ if (null == input) {
|
||||
/* 49 */ return false;
|
||||
public enum Radix {
|
||||
BIN(2),
|
||||
|
||||
DEC(10),
|
||||
|
||||
HEX(16);
|
||||
|
||||
final int value;
|
||||
|
||||
Radix(int radix) {
|
||||
this.value = radix;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
byte[] hardwareAddress;
|
||||
/* 53 */ switch (this) {
|
||||
case UP:
|
||||
/* 55 */ return input.isUp();
|
||||
case VIRTUAL:
|
||||
/* 57 */ return input.isVirtual();
|
||||
case LOOPBACK:
|
||||
/* 59 */ return input.isLoopback();
|
||||
case PHYICAL_ONLY:
|
||||
/* 61 */ hardwareAddress = input.getHardwareAddress();
|
||||
/* 62 */ return (null != hardwareAddress && hardwareAddress.length > 0 &&
|
||||
|
||||
/* 64 */ !input.isVirtual() &&
|
||||
/* 65 */ !NetworkUtils.isVMMac(hardwareAddress));
|
||||
}
|
||||
|
||||
/* 68 */ return true;
|
||||
}
|
||||
/* 70 */ catch (SocketException e) {
|
||||
/* 71 */ throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@SafeVarargs
|
||||
public static Set<NetworkInterface> getNICs(Filter... filters) {
|
||||
/* 83 */ if (null == filters) {
|
||||
/* 84 */ filters = new Filter[] { Filter.ALL };
|
||||
}
|
||||
|
||||
/* 87 */ Set<NetworkInterface> ret = new HashSet<>();
|
||||
/* 88 */ Enumeration<NetworkInterface> networks = null;
|
||||
try {
|
||||
/* 90 */ networks = NetworkInterface.getNetworkInterfaces();
|
||||
/* 91 */ } catch (SocketException e) {
|
||||
/* 92 */ throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
/* 95 */ while (networks.hasMoreElements()) {
|
||||
/* 96 */ boolean match = false;
|
||||
/* 97 */ NetworkInterface temp = networks.nextElement();
|
||||
/* 98 */ for (Filter v : filters) {
|
||||
/* 99 */ if (v.apply(temp))
|
||||
{
|
||||
|
||||
match = true;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
ret.add(temp);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Set<NetworkInterface> getPhysicalNICs() {
|
||||
return getNICs(new Filter[] { Filter.PHYICAL_ONLY, Filter.UP });
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static final String format(byte[] source, String separator, Radix radix) {
|
||||
if (null == source) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (null == separator) {
|
||||
separator = "";
|
||||
}
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (byte b : source) {
|
||||
buf.append(separator).append(apply(Byte.valueOf(b), radix));
|
||||
}
|
||||
|
||||
return (buf.length() > 0) ? buf.substring(separator.length()) : "";
|
||||
}
|
||||
|
||||
private static String apply(Byte input, Radix radix) {
|
||||
return String.copyValueOf(new char[] {
|
||||
Character.forDigit((input.byteValue() & 0xF0) >> 4, radix.value),
|
||||
Character.forDigit(input.byteValue() & 0xF, radix.value)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static final String formatMac(byte[] source, String separator) {
|
||||
return format(source, separator, Radix.HEX);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static final String formatIp(byte[] source) {
|
||||
return format(source, ".", Radix.DEC);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] getMacAddress(InetAddress address) {
|
||||
try {
|
||||
NetworkInterface nic = NetworkInterface.getByInetAddress(address);
|
||||
return (null == nic) ? null : nic.getHardwareAddress();
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getMacAddress(NetworkInterface nic, String separator) {
|
||||
try {
|
||||
return format(nic.getHardwareAddress(), separator, Radix.HEX);
|
||||
} catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String getMacAddress(InetAddress address, String separator) {
|
||||
return format(getMacAddress(address), separator, Radix.HEX);
|
||||
}
|
||||
|
||||
private static byte[][] invalidMacs = new byte[][] { { 0, 5, 105 }, { 0, 28, 20 }, { 0, 12, 41 }, { 0, 80, 86 }, { 8, 0, 39 }, { 10, 0, 39 }, { 0, 3, -1 }, { 0, 21, 93 } };
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private static boolean isVMMac(byte[] mac) {
|
||||
if (null == mac) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (byte[] invalid : invalidMacs) {
|
||||
if (invalid[0] == mac[0] && invalid[1] == mac[1] && invalid[2] == mac[2]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public enum Radix
|
||||
{
|
||||
BIN(2),
|
||||
|
||||
|
||||
|
||||
|
||||
DEC(10),
|
||||
|
||||
|
||||
|
||||
|
||||
HEX(16);
|
||||
|
||||
final int value;
|
||||
|
||||
Radix(int radix) {
|
||||
this.value = radix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+33
-44
@@ -14,52 +14,41 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({CloudwalkSerialProperties.class})
|
||||
@ConditionalOnProperty(prefix = "cloudwalk.serial", value = {"enabled"}, havingValue = "true", matchIfMissing = true)
|
||||
public class CloudwalkSerialAutoConfiguration
|
||||
{
|
||||
@Autowired(required = false)
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Autowired
|
||||
private CloudwalkSerialProperties serialProperties;
|
||||
@EnableConfigurationProperties({ CloudwalkSerialProperties.class })
|
||||
@ConditionalOnProperty(prefix = "cloudwalk.serial", value = { "enabled" }, havingValue = "true", matchIfMissing = true)
|
||||
public class CloudwalkSerialAutoConfiguration {
|
||||
@Autowired(required = false)
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Autowired
|
||||
private CloudwalkSerialProperties serialProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CloudwalkRedisService cloudwalkRedisService() {
|
||||
if ("redis".equals(this.serialProperties.getSerialType()));
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CloudwalkRedisService cloudwalkRedisService() {
|
||||
if ("redis".equals(this.serialProperties.getSerialType()))
|
||||
;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
@Bean
|
||||
public GeneralSerial generalSerial() {
|
||||
if ("redis".equals(this.serialProperties.getSerialType())) {
|
||||
CloudwalkRedisService redisService = new CloudwalkRedisService();
|
||||
redisService.setRedisTemplate(this.redisTemplate);
|
||||
RedisGeneralCode redisGeneralCode = new RedisGeneralCode(this.serialProperties.getSerialRedisKey());
|
||||
redisGeneralCode.setRedisService(redisService);
|
||||
return (GeneralSerial) redisGeneralCode;
|
||||
}
|
||||
|
||||
return (GeneralSerial) new MacGeneralSerial();
|
||||
}
|
||||
|
||||
@Bean({ "generalSerialCode" })
|
||||
public AbstractGeneralCode abstractGeneralCode() {
|
||||
GeneralSerialCode generalSerialCode = new GeneralSerialCode(generalSerial());
|
||||
generalSerialCode.setSerialLength(this.serialProperties.getSerialLength());
|
||||
return (AbstractGeneralCode) generalSerialCode;
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GeneralSerial generalSerial() {
|
||||
if ("redis".equals(this.serialProperties.getSerialType())) {
|
||||
CloudwalkRedisService redisService = new CloudwalkRedisService();
|
||||
redisService.setRedisTemplate(this.redisTemplate);
|
||||
RedisGeneralCode redisGeneralCode = new RedisGeneralCode(this.serialProperties.getSerialRedisKey());
|
||||
redisGeneralCode.setRedisService(redisService);
|
||||
return (GeneralSerial)redisGeneralCode;
|
||||
}
|
||||
|
||||
return (GeneralSerial)new MacGeneralSerial();
|
||||
}
|
||||
|
||||
|
||||
@Bean({"generalSerialCode"})
|
||||
public AbstractGeneralCode abstractGeneralCode() {
|
||||
GeneralSerialCode generalSerialCode = new GeneralSerialCode(generalSerial());
|
||||
generalSerialCode.setSerialLength(this.serialProperties.getSerialLength());
|
||||
return (AbstractGeneralCode)generalSerialCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+43
-59
@@ -2,69 +2,53 @@ package cn.cloudwalk.serial.autoconfig.serial;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ConfigurationProperties(prefix = "cloudwalk.serial")
|
||||
public class CloudwalkSerialProperties
|
||||
{
|
||||
public static final String TARGET_NAME = "cloudwalk.serial";
|
||||
private boolean enable = true;
|
||||
private String serialRedisKey;
|
||||
private String serialType;
|
||||
private int serialLength = 8;
|
||||
public class CloudwalkSerialProperties {
|
||||
public static final String TARGET_NAME = "cloudwalk.serial";
|
||||
private boolean enable = true;
|
||||
private String serialRedisKey;
|
||||
private String serialType;
|
||||
private int serialLength = 8;
|
||||
|
||||
private ServerIdStrategyConf serverIdStrategy;
|
||||
|
||||
private ServerIdStrategyConf serverIdStrategy;
|
||||
public void setSerialRedisKey(String serialRedisKey) {
|
||||
this.serialRedisKey = serialRedisKey;
|
||||
}
|
||||
|
||||
public String getSerialRedisKey() {
|
||||
return this.serialRedisKey;
|
||||
}
|
||||
|
||||
public void setSerialType(String serialType) {
|
||||
this.serialType = serialType;
|
||||
}
|
||||
|
||||
public void setSerialRedisKey(String serialRedisKey) {
|
||||
this.serialRedisKey = serialRedisKey;
|
||||
public String getSerialType() {
|
||||
return this.serialType;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
return this.enable;
|
||||
}
|
||||
|
||||
public void setSerialLength(int serialLength) {
|
||||
this.serialLength = serialLength;
|
||||
}
|
||||
|
||||
public int getSerialLength() {
|
||||
return this.serialLength;
|
||||
}
|
||||
|
||||
public ServerIdStrategyConf getServerIdStrategy() {
|
||||
return this.serverIdStrategy;
|
||||
}
|
||||
|
||||
public void setServerIdStrategy(ServerIdStrategyConf serverIdStrategy) {
|
||||
this.serverIdStrategy = serverIdStrategy;
|
||||
}
|
||||
}
|
||||
|
||||
public String getSerialRedisKey() {
|
||||
return this.serialRedisKey;
|
||||
}
|
||||
|
||||
public void setSerialType(String serialType) {
|
||||
this.serialType = serialType;
|
||||
}
|
||||
|
||||
public String getSerialType() {
|
||||
return this.serialType;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
return this.enable;
|
||||
}
|
||||
|
||||
public void setSerialLength(int serialLength) {
|
||||
this.serialLength = serialLength;
|
||||
}
|
||||
|
||||
public int getSerialLength() {
|
||||
return this.serialLength;
|
||||
}
|
||||
|
||||
public ServerIdStrategyConf getServerIdStrategy() {
|
||||
return this.serverIdStrategy;
|
||||
}
|
||||
|
||||
public void setServerIdStrategy(ServerIdStrategyConf serverIdStrategy) {
|
||||
this.serverIdStrategy = serverIdStrategy;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+8
-23
@@ -9,29 +9,14 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
public class CloudwalkSnowflakeConfiguration
|
||||
{
|
||||
@Autowired(required = false)
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
public class CloudwalkSnowflakeConfiguration {
|
||||
@Autowired(required = false)
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean({UUIDSerial.class})
|
||||
public UUIDSerial snowFlakeSerialCode(ServerIdStrategy serverIdStrategy) {
|
||||
return (UUIDSerial)new SnowFlakeSerialCode(this.redisTemplate, serverIdStrategy);
|
||||
@Bean
|
||||
@ConditionalOnMissingBean({ UUIDSerial.class })
|
||||
public UUIDSerial snowFlakeSerialCode(ServerIdStrategy serverIdStrategy) {
|
||||
return (UUIDSerial) new SnowFlakeSerialCode(this.redisTemplate, serverIdStrategy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-34
@@ -1,41 +1,22 @@
|
||||
package cn.cloudwalk.serial.autoconfig.serial;
|
||||
|
||||
public class ServerIdStrategyConf {
|
||||
private String code;
|
||||
private String className;
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return this.className;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ServerIdStrategyConf
|
||||
{
|
||||
private String code;
|
||||
private String className;
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return this.className;
|
||||
}
|
||||
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+18
-53
@@ -1,61 +1,26 @@
|
||||
package cn.cloudwalk.serial.code;
|
||||
|
||||
public abstract class AbstractGeneralCode {
|
||||
private GeneralSerial generalSerial;
|
||||
private int serialLength;
|
||||
|
||||
public void setSerialLength(int serialLength) {
|
||||
this.serialLength = serialLength;
|
||||
}
|
||||
|
||||
public void setGeneralBusinessCode(GeneralSerial generalSerial) {
|
||||
this.generalSerial = generalSerial;
|
||||
}
|
||||
|
||||
public String generalCode(String prefix, Integer length) {
|
||||
Integer serial = this.generalSerial.generalSerial();
|
||||
return prefix + warpSerial(serial, length);
|
||||
}
|
||||
|
||||
public String generalCode() {
|
||||
Integer serial = this.generalSerial.generalSerial();
|
||||
return warpSerial(serial, Integer.valueOf(this.serialLength));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public abstract class AbstractGeneralCode
|
||||
{
|
||||
private GeneralSerial generalSerial;
|
||||
private int serialLength;
|
||||
|
||||
public void setSerialLength(int serialLength) {
|
||||
this.serialLength = serialLength;
|
||||
protected abstract String warpSerial(Integer paramInteger1, Integer paramInteger2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setGeneralBusinessCode(GeneralSerial generalSerial) {
|
||||
this.generalSerial = generalSerial;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String generalCode(String prefix, Integer length) {
|
||||
Integer serial = this.generalSerial.generalSerial();
|
||||
return prefix + warpSerial(serial, length);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String generalCode() {
|
||||
Integer serial = this.generalSerial.generalSerial();
|
||||
return warpSerial(serial, Integer.valueOf(this.serialLength));
|
||||
}
|
||||
|
||||
protected abstract String warpSerial(Integer paramInteger1, Integer paramInteger2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
package cn.cloudwalk.serial.code;
|
||||
|
||||
public interface GeneralSerial {
|
||||
Integer generalSerial();
|
||||
Integer generalSerial();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+27
-46
@@ -2,53 +2,34 @@ package cn.cloudwalk.serial.code;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class GeneralSerialCode extends AbstractGeneralCode {
|
||||
public GeneralSerialCode(GeneralSerial generalSerial) {
|
||||
Assert.notNull(generalSerial, "generalBusinessCode is not null");
|
||||
setGeneralBusinessCode(generalSerial);
|
||||
}
|
||||
|
||||
protected String warpSerial(Integer serial, Integer length) {
|
||||
if (length.intValue() <= 0) {
|
||||
throw new IllegalArgumentException("length must more than 0");
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (serial.intValue() == 0) {
|
||||
for (int i = 0; i < length.intValue(); i++) {
|
||||
sb.append("0");
|
||||
}
|
||||
} else {
|
||||
String ser = String.valueOf(serial);
|
||||
if (ser.length() >= length.intValue()) {
|
||||
sb.append(ser);
|
||||
} else {
|
||||
for (int i = ser.length(); i < length.intValue(); i++) {
|
||||
sb.append("0");
|
||||
}
|
||||
sb.append(ser);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class GeneralSerialCode
|
||||
extends AbstractGeneralCode
|
||||
{
|
||||
public GeneralSerialCode(GeneralSerial generalSerial) {
|
||||
Assert.notNull(generalSerial, "generalBusinessCode is not null");
|
||||
setGeneralBusinessCode(generalSerial);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected String warpSerial(Integer serial, Integer length) {
|
||||
if (length.intValue() <= 0) {
|
||||
throw new IllegalArgumentException("length must more than 0");
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (serial.intValue() == 0) {
|
||||
for (int i = 0; i < length.intValue(); i++) {
|
||||
sb.append("0");
|
||||
}
|
||||
} else {
|
||||
String ser = String.valueOf(serial);
|
||||
if (ser.length() >= length.intValue()) {
|
||||
sb.append(ser);
|
||||
} else {
|
||||
for (int i = ser.length(); i < length.intValue(); i++) {
|
||||
sb.append("0");
|
||||
}
|
||||
sb.append(ser);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-38
@@ -3,45 +3,22 @@ package cn.cloudwalk.serial.code;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class MacGeneralSerial implements GeneralSerial {
|
||||
private Lock lock = new ReentrantLock();
|
||||
|
||||
private volatile Integer serial = Integer.valueOf(0);
|
||||
|
||||
public Integer generalSerial() {
|
||||
return serial();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class MacGeneralSerial
|
||||
implements GeneralSerial
|
||||
{
|
||||
private Lock lock = new ReentrantLock();
|
||||
|
||||
|
||||
|
||||
|
||||
private volatile Integer serial = Integer.valueOf(0);
|
||||
|
||||
|
||||
public Integer generalSerial() {
|
||||
return serial();
|
||||
private Integer serial() {
|
||||
try {
|
||||
this.lock.lock();
|
||||
Integer integer1 = this.serial, integer2 = this.serial = Integer.valueOf(this.serial.intValue() + 1);
|
||||
} finally {
|
||||
this.lock.unlock();
|
||||
}
|
||||
return this.serial;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private Integer serial() {
|
||||
try {
|
||||
this.lock.lock();
|
||||
Integer integer1 = this.serial, integer2 = this.serial = Integer.valueOf(this.serial.intValue() + 1);
|
||||
} finally {
|
||||
this.lock.unlock();
|
||||
}
|
||||
return this.serial;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+20
-45
@@ -3,53 +3,28 @@ package cn.cloudwalk.serial.code;
|
||||
import cn.cloudwalk.serial.redis.CloudwalkRedisService;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class RedisGeneralCode implements GeneralSerial {
|
||||
private CloudwalkRedisService redisService;
|
||||
private String redisKey;
|
||||
|
||||
public RedisGeneralCode(String redisKey) {
|
||||
if (StringUtils.isEmpty(redisKey)) {
|
||||
this.redisKey = "CLODWALK-DEFAULT-GENERAL-SERIAL-KEY";
|
||||
} else {
|
||||
this.redisKey = redisKey;
|
||||
}
|
||||
}
|
||||
|
||||
public void setRedisService(CloudwalkRedisService redisService) {
|
||||
this.redisService = redisService;
|
||||
}
|
||||
|
||||
public Integer generalSerial() {
|
||||
return serial();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class RedisGeneralCode
|
||||
implements GeneralSerial
|
||||
{
|
||||
private CloudwalkRedisService redisService;
|
||||
private String redisKey;
|
||||
|
||||
public RedisGeneralCode(String redisKey) {
|
||||
if (StringUtils.isEmpty(redisKey)) {
|
||||
this.redisKey = "CLODWALK-DEFAULT-GENERAL-SERIAL-KEY";
|
||||
} else {
|
||||
this.redisKey = redisKey;
|
||||
}
|
||||
private Integer serial() {
|
||||
long serial = this.redisService.incr(this.redisKey);
|
||||
return Integer.valueOf((int) serial);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRedisService(CloudwalkRedisService redisService) {
|
||||
this.redisService = redisService;
|
||||
}
|
||||
|
||||
|
||||
public Integer generalSerial() {
|
||||
return serial();
|
||||
}
|
||||
|
||||
private Integer serial() {
|
||||
long serial = this.redisService.incr(this.redisKey);
|
||||
return Integer.valueOf((int)serial);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+29
-91
@@ -3,104 +3,42 @@ package cn.cloudwalk.serial.redis;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
public class CloudwalkRedisService {
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
return (String) this.redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public void set(String key, String value, long timeout) {
|
||||
this.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void set(String key, String value) {
|
||||
set(key, value, 86400L);
|
||||
}
|
||||
|
||||
public void setBlank(String key) {
|
||||
set(key, "", 86400L);
|
||||
}
|
||||
|
||||
public long incr(String key) {
|
||||
return incrBy(key, 1L);
|
||||
}
|
||||
|
||||
public long incrBy(String key, long delta) {
|
||||
return this.redisTemplate.opsForValue().increment(key, delta).longValue();
|
||||
}
|
||||
|
||||
public boolean lock(String key, String value) {
|
||||
return this.redisTemplate.opsForValue().setIfAbsent(key, value).booleanValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class CloudwalkRedisService
|
||||
{
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
|
||||
/* 26 */ this.redisTemplate = redisTemplate;
|
||||
public void unLock(String key) {
|
||||
this.redisTemplate.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String get(String key) {
|
||||
/* 36 */ return (String)this.redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void set(String key, String value, long timeout) {
|
||||
/* 47 */ this.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void set(String key, String value) {
|
||||
/* 57 */ set(key, value, 86400L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setBlank(String key) {
|
||||
/* 65 */ set(key, "", 86400L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long incr(String key) {
|
||||
/* 74 */ return incrBy(key, 1L);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long incrBy(String key, long delta) {
|
||||
/* 83 */ return this.redisTemplate.opsForValue().increment(key, delta).longValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean lock(String key, String value) {
|
||||
/* 94 */ return this.redisTemplate.opsForValue().setIfAbsent(key, value).booleanValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void unLock(String key) {
|
||||
this.redisTemplate.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+302
-537
@@ -10,552 +10,317 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Component
|
||||
public final class RedisComponent
|
||||
{
|
||||
/* 28 */ private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
public final class RedisComponent {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
|
||||
public Set<Object> keys(Object prefix) {
|
||||
return this.redisTemplate.keys(prefix);
|
||||
}
|
||||
|
||||
public List<Object> multiGet(Set<Object> keys) {
|
||||
return this.redisTemplate.opsForValue().multiGet(keys);
|
||||
}
|
||||
|
||||
public boolean expire(String key, long time) {
|
||||
try {
|
||||
if (time > 0L) {
|
||||
this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("expire error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
public long getExpire(String key) {
|
||||
return this.redisTemplate.getExpire(key, TimeUnit.SECONDS).longValue();
|
||||
}
|
||||
|
||||
public boolean hasKey(String key) {
|
||||
try {
|
||||
return this.redisTemplate.hasKey(key).booleanValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hasKey error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void del(String... key) {
|
||||
if (key != null && key.length > 0) {
|
||||
if (key.length == 1) {
|
||||
this.redisTemplate.delete(key[0]);
|
||||
} else {
|
||||
this.redisTemplate.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
return (key == null) ? null : this.redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public boolean set(String key, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForValue().set(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("set error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Object> keys(Object prefix) {
|
||||
/* 42 */ return this.redisTemplate.keys(prefix);
|
||||
public boolean set(String key, Object value, long time) {
|
||||
try {
|
||||
if (time > 0L) {
|
||||
this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||
} else {
|
||||
set(key, value);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("set error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public long incr(String key, long delta) {
|
||||
if (delta < 0L) {
|
||||
throw new RuntimeException("递增因子必须大于0");
|
||||
}
|
||||
return this.redisTemplate.opsForValue().increment(key, delta).longValue();
|
||||
}
|
||||
|
||||
public long decr(String key, long delta) {
|
||||
if (delta < 0L) {
|
||||
throw new RuntimeException("递减因子必须大于0");
|
||||
}
|
||||
return this.redisTemplate.opsForValue().increment(key, -delta).longValue();
|
||||
}
|
||||
|
||||
public Object hget(String key, String item) {
|
||||
return this.redisTemplate.opsForHash().get(key, item);
|
||||
}
|
||||
|
||||
public Map<Object, Object> hmget(String key) {
|
||||
return this.redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
public boolean hmset(String key, Map<String, Object> map) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().putAll(key, map);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hmset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hmset(String key, Map<String, Object> map, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().putAll(key, map);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hmset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hset(String key, String item, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().put(key, item, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hset(String key, String item, Object value, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().put(key, item, value);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void hdel(String key, Object... item) {
|
||||
this.redisTemplate.opsForHash().delete(key, item);
|
||||
}
|
||||
|
||||
public boolean hHasKey(String key, String item) {
|
||||
return this.redisTemplate.opsForHash().hasKey(key, item).booleanValue();
|
||||
}
|
||||
|
||||
public double hincr(String key, String item, double by) {
|
||||
return this.redisTemplate.opsForHash().increment(key, item, by).doubleValue();
|
||||
}
|
||||
|
||||
public double hdecr(String key, String item, double by) {
|
||||
return this.redisTemplate.opsForHash().increment(key, item, -by).doubleValue();
|
||||
}
|
||||
|
||||
public Set<Object> sGet(String key) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().members(key);
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sGet error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sHasKey(String key, Object value) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().isMember(key, value).booleanValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sHasKey error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public long sSet(String key, Object... values) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().add(key, values).longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sSet error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public long sSetAndTime(String key, long time, Object... values) {
|
||||
try {
|
||||
Long count = this.redisTemplate.opsForSet().add(key, values);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return count.longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sSetAndTime error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public long sGetSetSize(String key) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().size(key).longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sGetSetSize error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public long setRemove(String key, Object... values) {
|
||||
try {
|
||||
Long count = this.redisTemplate.opsForSet().remove(key, values);
|
||||
return count.longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("setRemove error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Object> lGet(String key, long start, long end) {
|
||||
try {
|
||||
return this.redisTemplate.opsForList().range(key, start, end);
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lGet error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public long lGetListSize(String key) {
|
||||
try {
|
||||
return this.redisTemplate.opsForList().size(key).longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lGetListSize error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
public Object lGetIndex(String key, long index) {
|
||||
try {
|
||||
return this.redisTemplate.opsForList().index(key, index);
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lGetIndex error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lSet(String key, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPush(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lSet(String key, Object value, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPush(key, value);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lSet(String key, List<Object> value) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPushAll(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lSet(String key, List<Object> value, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPushAll(key, value);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean lUpdateIndex(String key, long index, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().set(key, index, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lUpdateIndex error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public long lRemove(String key, long count, Object value) {
|
||||
try {
|
||||
Long remove = this.redisTemplate.opsForList().remove(key, count, value);
|
||||
return remove.longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lRemove error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public List<Object> multiGet(Set<Object> keys) {
|
||||
/* 51 */ return this.redisTemplate.opsForValue().multiGet(keys);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean expire(String key, long time) {
|
||||
try {
|
||||
/* 62 */ if (time > 0L) {
|
||||
/* 63 */ this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
||||
}
|
||||
/* 65 */ return true;
|
||||
/* 66 */ } catch (Exception e) {
|
||||
/* 67 */ this.logger.error("expire error:", e);
|
||||
/* 68 */ return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long getExpire(String key) {
|
||||
/* 78 */ return this.redisTemplate.getExpire(key, TimeUnit.SECONDS).longValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hasKey(String key) {
|
||||
try {
|
||||
/* 88 */ return this.redisTemplate.hasKey(key).booleanValue();
|
||||
/* 89 */ } catch (Exception e) {
|
||||
/* 90 */ this.logger.error("hasKey error:", e);
|
||||
/* 91 */ return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void del(String... key) {
|
||||
if (key != null && key.length > 0) {
|
||||
if (key.length == 1) {
|
||||
this.redisTemplate.delete(key[0]);
|
||||
} else {
|
||||
this.redisTemplate.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Object get(String key) {
|
||||
return (key == null) ? null : this.redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean set(String key, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForValue().set(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("set error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean set(String key, Object value, long time) {
|
||||
try {
|
||||
if (time > 0L) {
|
||||
this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||
} else {
|
||||
set(key, value);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("set error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long incr(String key, long delta) {
|
||||
if (delta < 0L) {
|
||||
throw new RuntimeException("递增因子必须大于0");
|
||||
}
|
||||
return this.redisTemplate.opsForValue().increment(key, delta).longValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long decr(String key, long delta) {
|
||||
if (delta < 0L) {
|
||||
throw new RuntimeException("递减因子必须大于0");
|
||||
}
|
||||
return this.redisTemplate.opsForValue().increment(key, -delta).longValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Object hget(String key, String item) {
|
||||
return this.redisTemplate.opsForHash().get(key, item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Map<Object, Object> hmget(String key) {
|
||||
return this.redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hmset(String key, Map<String, Object> map) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().putAll(key, map);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hmset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hmset(String key, Map<String, Object> map, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().putAll(key, map);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hmset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hset(String key, String item, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().put(key, item, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hset(String key, String item, Object value, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForHash().put(key, item, value);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("hset error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void hdel(String key, Object... item) {
|
||||
this.redisTemplate.opsForHash().delete(key, item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hHasKey(String key, String item) {
|
||||
return this.redisTemplate.opsForHash().hasKey(key, item).booleanValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public double hincr(String key, String item, double by) {
|
||||
return this.redisTemplate.opsForHash().increment(key, item, by).doubleValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public double hdecr(String key, String item, double by) {
|
||||
return this.redisTemplate.opsForHash().increment(key, item, -by).doubleValue();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Set<Object> sGet(String key) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().members(key);
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sGet error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean sHasKey(String key, Object value) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().isMember(key, value).booleanValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sHasKey error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long sSet(String key, Object... values) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().add(key, values).longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sSet error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long sSetAndTime(String key, long time, Object... values) {
|
||||
try {
|
||||
Long count = this.redisTemplate.opsForSet().add(key, values);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return count.longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sSetAndTime error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long sGetSetSize(String key) {
|
||||
try {
|
||||
return this.redisTemplate.opsForSet().size(key).longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("sGetSetSize error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long setRemove(String key, Object... values) {
|
||||
try {
|
||||
Long count = this.redisTemplate.opsForSet().remove(key, values);
|
||||
return count.longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("setRemove error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public List<Object> lGet(String key, long start, long end) {
|
||||
try {
|
||||
return this.redisTemplate.opsForList().range(key, start, end);
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lGet error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long lGetListSize(String key) {
|
||||
try {
|
||||
return this.redisTemplate.opsForList().size(key).longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lGetListSize error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Object lGetIndex(String key, long index) {
|
||||
try {
|
||||
return this.redisTemplate.opsForList().index(key, index);
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lGetIndex error:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean lSet(String key, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPush(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean lSet(String key, Object value, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPush(key, value);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean lSet(String key, List<Object> value) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPushAll(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean lSet(String key, List<Object> value, long time) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().rightPushAll(key, value);
|
||||
if (time > 0L) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lSet error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean lUpdateIndex(String key, long index, Object value) {
|
||||
try {
|
||||
this.redisTemplate.opsForList().set(key, index, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lUpdateIndex error:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public long lRemove(String key, long count, Object value) {
|
||||
try {
|
||||
Long remove = this.redisTemplate.opsForList().remove(key, count, value);
|
||||
return remove.longValue();
|
||||
} catch (Exception e) {
|
||||
this.logger.error("lRemove error:", e);
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
package cn.cloudwalk.serial.strategy;
|
||||
|
||||
public interface ServerIdStrategy {
|
||||
String getServerId();
|
||||
String getServerId();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+20
-33
@@ -5,38 +5,25 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
public class ServerIdStrategyBeanConfig
|
||||
{
|
||||
@Bean
|
||||
public ServerIdStrategy createServerIdStrategy(CloudwalkSerialProperties serialProperties) {
|
||||
Class<?> beanClassName = ServerIdStrategyEnum.MAC.getClazz();
|
||||
try {
|
||||
if (serialProperties != null && serialProperties.getServerIdStrategy() != null) {
|
||||
if (StringUtils.isNotBlank(serialProperties.getServerIdStrategy().getClassName())) {
|
||||
beanClassName = Class.forName(serialProperties.getServerIdStrategy().getClassName());
|
||||
} else if (StringUtils.isNotBlank(serialProperties.getServerIdStrategy().getCode()) || null !=
|
||||
ServerIdStrategyEnum.getServerIdStrategyEnum(serialProperties.getServerIdStrategy().getCode())) {
|
||||
beanClassName = Class.forName(ServerIdStrategyEnum.getServerIdStrategyEnum(serialProperties.getServerIdStrategy().getCode()).getBeanName());
|
||||
}
|
||||
public class ServerIdStrategyBeanConfig {
|
||||
@Bean
|
||||
public ServerIdStrategy createServerIdStrategy(CloudwalkSerialProperties serialProperties) {
|
||||
Class<?> beanClassName = ServerIdStrategyEnum.MAC.getClazz();
|
||||
try {
|
||||
if (serialProperties != null && serialProperties.getServerIdStrategy() != null) {
|
||||
if (StringUtils.isNotBlank(serialProperties.getServerIdStrategy().getClassName())) {
|
||||
beanClassName = Class.forName(serialProperties.getServerIdStrategy().getClassName());
|
||||
} else if (StringUtils.isNotBlank(serialProperties.getServerIdStrategy().getCode())
|
||||
|| null != ServerIdStrategyEnum
|
||||
.getServerIdStrategyEnum(serialProperties.getServerIdStrategy().getCode())) {
|
||||
beanClassName = Class.forName(ServerIdStrategyEnum
|
||||
.getServerIdStrategyEnum(serialProperties.getServerIdStrategy().getCode()).getBeanName());
|
||||
}
|
||||
}
|
||||
return (ServerIdStrategy) beanClassName.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (ServerIdStrategy)beanClassName.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+38
-70
@@ -2,86 +2,54 @@ package cn.cloudwalk.serial.strategy;
|
||||
|
||||
import cn.cloudwalk.serial.strategy.impl.MacServerIdStrategy;
|
||||
|
||||
public enum ServerIdStrategyEnum {
|
||||
MAC("mac", "mac地址策略", MacServerIdStrategy.class, "macServerIdStrategy");
|
||||
|
||||
public Class<?> clazz;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private String name;
|
||||
|
||||
private String code;
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
ServerIdStrategyEnum(String code, String name, Class<?> clazz, String beanName) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.clazz = clazz;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public Class<?> getClazz() {
|
||||
return this.clazz;
|
||||
}
|
||||
|
||||
public void setClazz(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return this.beanName;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public enum ServerIdStrategyEnum
|
||||
{
|
||||
MAC("mac", "mac地址策略", MacServerIdStrategy.class, "macServerIdStrategy");
|
||||
|
||||
|
||||
|
||||
|
||||
public Class<?> clazz;
|
||||
|
||||
|
||||
|
||||
private String beanName;
|
||||
|
||||
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
|
||||
private String code;
|
||||
|
||||
|
||||
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
public static ServerIdStrategyEnum getServerIdStrategyEnum(String code) {
|
||||
for (ServerIdStrategyEnum item : values()) {
|
||||
if (item.getCode().equals(code)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
ServerIdStrategyEnum(String code, String name, Class<?> clazz, String beanName) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.clazz = clazz;
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
public Class<?> getClazz() {
|
||||
return this.clazz;
|
||||
}
|
||||
|
||||
public void setClazz(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return this.beanName;
|
||||
}
|
||||
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static ServerIdStrategyEnum getServerIdStrategyEnum(String code) {
|
||||
for (ServerIdStrategyEnum item : values()) {
|
||||
if (item.getCode().equals(code)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+15
-34
@@ -7,41 +7,22 @@ import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class MacServerIdStrategy implements ServerIdStrategy {
|
||||
private static final String CENTER_LINE = "-";
|
||||
|
||||
public String getServerId() {
|
||||
Set<NetworkInterface> nicSets = NetworkUtils
|
||||
.getNICs(new NetworkUtils.Filter[] { NetworkUtils.Filter.PHYICAL_ONLY, NetworkUtils.Filter.UP });
|
||||
|
||||
Iterator<NetworkInterface> it = nicSets.iterator();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class MacServerIdStrategy
|
||||
implements ServerIdStrategy
|
||||
{
|
||||
private static final String CENTER_LINE = "-";
|
||||
|
||||
public String getServerId() {
|
||||
Set<NetworkInterface> nicSets = NetworkUtils.getNICs(new NetworkUtils.Filter[] { NetworkUtils.Filter.PHYICAL_ONLY, NetworkUtils.Filter.UP });
|
||||
|
||||
|
||||
Iterator<NetworkInterface> it = nicSets.iterator();
|
||||
|
||||
String mac = "";
|
||||
while (it.hasNext()) {
|
||||
mac = NetworkUtils.getMacAddress(it.next(), "-");
|
||||
if (StringUtils.isNotEmpty(mac)) {
|
||||
break;
|
||||
String mac = "";
|
||||
while (it.hasNext()) {
|
||||
mac = NetworkUtils.getMacAddress(it.next(), "-");
|
||||
if (StringUtils.isNotEmpty(mac)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return mac.toUpperCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
return mac.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+123
-237
@@ -17,272 +17,158 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
public class SnowFlakeSerialCode implements UUIDSerial {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private static final int LOCK_TRY_MAX_TIME = 5;
|
||||
private static final int THREAD_SLEEP_TIME = 5;
|
||||
private static final Integer MAX_NODE_ID = Integer.valueOf(31);
|
||||
|
||||
private static final Integer MIN_NODE_ID = Integer.valueOf(0);
|
||||
|
||||
private static String SNOW_FLAKE_REDIS_KEY = "SNOW_FLAKE_SERIAL_CODE";
|
||||
|
||||
private static String SNOW_FLAKE_KEY_LOCK = SNOW_FLAKE_REDIS_KEY + ":LOCK";
|
||||
|
||||
private SnowflakeIdWorker idWorker;
|
||||
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
public SnowFlakeSerialCode(RedisTemplate<String, String> redisTemplate, ServerIdStrategy serverIdStrategy) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
if (redisTemplate == null) {
|
||||
this.idWorker = new SnowflakeIdWorker(MIN_NODE_ID.intValue(), MIN_NODE_ID.intValue());
|
||||
|
||||
} else if (null == this.idWorker) {
|
||||
ClusterNode tag = getClusterNode(serverIdStrategy.getServerId());
|
||||
this.idWorker = new SnowflakeIdWorker(tag.getWorkId(), tag.getCenterId());
|
||||
}
|
||||
}
|
||||
|
||||
public String uuid() {
|
||||
return Long.toString(this.idWorker.nextId());
|
||||
}
|
||||
|
||||
private ClusterNode getClusterNode(String serverId) {
|
||||
if (StringUtils.isEmpty(serverId)) {
|
||||
throw new RuntimeException("无法获取服务器的唯一标志异常,serverId为空!");
|
||||
}
|
||||
|
||||
if (this.redisTemplate.opsForHash().hasKey(SNOW_FLAKE_REDIS_KEY, serverId).booleanValue()) {
|
||||
Object value = this.redisTemplate.opsForHash().get(SNOW_FLAKE_REDIS_KEY, serverId);
|
||||
return (ClusterNode) JSONObject.parseObject(value.toString(), ClusterNode.class);
|
||||
}
|
||||
return genNewClusterNode(serverId);
|
||||
}
|
||||
|
||||
private ClusterNode getMaxClusterNode(Map<Object, Object> entries) {
|
||||
ClusterNode maxClusterNode = null;
|
||||
for (Map.Entry<Object, Object> entry : entries.entrySet()) {
|
||||
ClusterNode its = (ClusterNode) JSONObject.parseObject(entry.getValue().toString(), ClusterNode.class);
|
||||
if (maxClusterNode == null) {
|
||||
maxClusterNode = its;
|
||||
|
||||
continue;
|
||||
}
|
||||
if (maxClusterNode.getCenterId() < its.getCenterId()) {
|
||||
maxClusterNode = its;
|
||||
continue;
|
||||
}
|
||||
if (maxClusterNode.getCenterId() == its.getCenterId() && maxClusterNode.getWorkId() < its.getWorkId()) {
|
||||
maxClusterNode = its;
|
||||
}
|
||||
}
|
||||
|
||||
return maxClusterNode;
|
||||
}
|
||||
|
||||
private ClusterNode getNextClusterNode(Map<Object, Object> entries) {
|
||||
ClusterNode max = getMaxClusterNode(entries);
|
||||
|
||||
int centerId = max.getCenterId(), workId = max.getWorkId() + 1;
|
||||
if (workId > MAX_NODE_ID.intValue()) {
|
||||
if (++centerId > MAX_NODE_ID.intValue()) {
|
||||
throw new RuntimeException("从redis获取主键生成机器节点数据已饱满.");
|
||||
}
|
||||
|
||||
workId = MIN_NODE_ID.intValue();
|
||||
}
|
||||
|
||||
return new ClusterNode(centerId, workId);
|
||||
}
|
||||
|
||||
private ClusterNode genNewClusterNode(String serverId) {
|
||||
if (getLock(serverId, Integer.valueOf(5))) {
|
||||
Map<Object, Object> entries = this.redisTemplate.opsForHash().entries(SNOW_FLAKE_REDIS_KEY);
|
||||
|
||||
ClusterNode nextClusterNode = null;
|
||||
if (entries == null || entries.isEmpty()) {
|
||||
nextClusterNode = new ClusterNode(MIN_NODE_ID.intValue(), MIN_NODE_ID.intValue());
|
||||
} else {
|
||||
nextClusterNode = getNextClusterNode(entries);
|
||||
}
|
||||
|
||||
this.redisTemplate.opsForHash().put(SNOW_FLAKE_REDIS_KEY, serverId, JSON.toJSONString(nextClusterNode));
|
||||
return nextClusterNode;
|
||||
}
|
||||
throw new RuntimeException("从redis获取主键生成机器id和数据中心id繁忙错误!");
|
||||
}
|
||||
|
||||
public class SnowFlakeSerialCode
|
||||
implements UUIDSerial
|
||||
{
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private static final int LOCK_TRY_MAX_TIME = 5;
|
||||
private static final int THREAD_SLEEP_TIME = 5;
|
||||
/* 44 */ private static final Integer MAX_NODE_ID = Integer.valueOf(31);
|
||||
private boolean getLock(String serverId, Integer maxTimes) {
|
||||
boolean ret = false;
|
||||
while (true) {
|
||||
ret = this.redisTemplate.opsForHash()
|
||||
.putIfAbsent(SNOW_FLAKE_KEY_LOCK, serverId, Long.toString(System.currentTimeMillis()))
|
||||
.booleanValue();
|
||||
if (ret) {
|
||||
this.redisTemplate.expire(SNOW_FLAKE_KEY_LOCK, 5L, TimeUnit.SECONDS);
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(5L);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
this.log.warn("获取雪花 Redis 锁等待被中断", e);
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
Integer integer1 = maxTimes, integer2 = maxTimes = Integer.valueOf(maxTimes.intValue() - 1);
|
||||
if (integer1.intValue() <= 0)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static class ClusterNode {
|
||||
private int centerId;
|
||||
|
||||
private int workId;
|
||||
|
||||
/* 49 */ private static final Integer MIN_NODE_ID = Integer.valueOf(0);
|
||||
|
||||
|
||||
|
||||
|
||||
/* 54 */ private static String SNOW_FLAKE_REDIS_KEY = "SNOW_FLAKE_SERIAL_CODE";
|
||||
|
||||
|
||||
|
||||
|
||||
/* 59 */ private static String SNOW_FLAKE_KEY_LOCK = SNOW_FLAKE_REDIS_KEY + ":LOCK";
|
||||
|
||||
|
||||
|
||||
|
||||
private SnowflakeIdWorker idWorker;
|
||||
|
||||
|
||||
|
||||
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public SnowFlakeSerialCode(RedisTemplate<String, String> redisTemplate, ServerIdStrategy serverIdStrategy) {
|
||||
/* 76 */ this.redisTemplate = redisTemplate;
|
||||
/* 77 */ if (redisTemplate == null) {
|
||||
/* 78 */ this.idWorker = new SnowflakeIdWorker(MIN_NODE_ID.intValue(), MIN_NODE_ID.intValue());
|
||||
public ClusterNode() {
|
||||
}
|
||||
|
||||
public ClusterNode(int centerId, int workId) {
|
||||
this.centerId = centerId;
|
||||
this.workId = workId;
|
||||
}
|
||||
|
||||
public int getCenterId() {
|
||||
return this.centerId;
|
||||
}
|
||||
|
||||
public int getWorkId() {
|
||||
return this.workId;
|
||||
}
|
||||
|
||||
public void setCenterId(int centerId) {
|
||||
this.centerId = centerId;
|
||||
}
|
||||
|
||||
public void setWorkId(int workId) {
|
||||
this.workId = workId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/* 86 */ else if (null == this.idWorker) {
|
||||
/* 87 */ ClusterNode tag = getClusterNode(serverIdStrategy.getServerId());
|
||||
/* 88 */ this.idWorker = new SnowflakeIdWorker(tag.getWorkId(), tag.getCenterId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String uuid() {
|
||||
return Long.toString(this.idWorker.nextId());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private ClusterNode getClusterNode(String serverId) {
|
||||
if (StringUtils.isEmpty(serverId)) {
|
||||
throw new RuntimeException("无法获取服务器的唯一标志异常,serverId为空!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if (this.redisTemplate.opsForHash().hasKey(SNOW_FLAKE_REDIS_KEY, serverId).booleanValue()) {
|
||||
Object value = this.redisTemplate.opsForHash().get(SNOW_FLAKE_REDIS_KEY, serverId);
|
||||
return (ClusterNode)JSONObject.parseObject(value.toString(), ClusterNode.class);
|
||||
}
|
||||
return genNewClusterNode(serverId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private ClusterNode getMaxClusterNode(Map<Object, Object> entries) {
|
||||
ClusterNode maxClusterNode = null;
|
||||
for (Map.Entry<Object, Object> entry : entries.entrySet()) {
|
||||
ClusterNode its = (ClusterNode)JSONObject.parseObject(entry
|
||||
.getValue().toString(), ClusterNode.class);
|
||||
if (maxClusterNode == null) {
|
||||
maxClusterNode = its;
|
||||
|
||||
continue;
|
||||
}
|
||||
if (maxClusterNode.getCenterId() < its.getCenterId()) {
|
||||
maxClusterNode = its; continue;
|
||||
}
|
||||
if (maxClusterNode.getCenterId() == its.getCenterId() &&
|
||||
maxClusterNode.getWorkId() < its.getWorkId()) {
|
||||
maxClusterNode = its;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return maxClusterNode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private ClusterNode getNextClusterNode(Map<Object, Object> entries) {
|
||||
ClusterNode max = getMaxClusterNode(entries);
|
||||
|
||||
int centerId = max.getCenterId(), workId = max.getWorkId() + 1;
|
||||
if (workId > MAX_NODE_ID.intValue()) {
|
||||
if (++centerId > MAX_NODE_ID.intValue()) {
|
||||
throw new RuntimeException("从redis获取主键生成机器节点数据已饱满.");
|
||||
}
|
||||
|
||||
workId = MIN_NODE_ID.intValue();
|
||||
}
|
||||
|
||||
return new ClusterNode(centerId, workId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private ClusterNode genNewClusterNode(String serverId) {
|
||||
if (getLock(serverId, Integer.valueOf(5))) {
|
||||
Map<Object, Object> entries = this.redisTemplate.opsForHash().entries(SNOW_FLAKE_REDIS_KEY);
|
||||
|
||||
ClusterNode nextClusterNode = null;
|
||||
if (entries == null || entries.isEmpty()) {
|
||||
nextClusterNode = new ClusterNode(MIN_NODE_ID.intValue(), MIN_NODE_ID.intValue());
|
||||
} else {
|
||||
nextClusterNode = getNextClusterNode(entries);
|
||||
}
|
||||
|
||||
this.redisTemplate.opsForHash().put(SNOW_FLAKE_REDIS_KEY, serverId, JSON.toJSONString(nextClusterNode));
|
||||
return nextClusterNode;
|
||||
}
|
||||
throw new RuntimeException("从redis获取主键生成机器id和数据中心id繁忙错误!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private boolean getLock(String serverId, Integer maxTimes) {
|
||||
boolean ret = false;
|
||||
while (true) {
|
||||
ret = this.redisTemplate.opsForHash().putIfAbsent(SNOW_FLAKE_KEY_LOCK, serverId,
|
||||
Long.toString(System.currentTimeMillis())).booleanValue();
|
||||
if (ret) {
|
||||
this.redisTemplate.expire(SNOW_FLAKE_KEY_LOCK, 5L, TimeUnit.SECONDS);
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(5L);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
this.log.warn("获取雪花 Redis 锁等待被中断", e);
|
||||
}
|
||||
|
||||
|
||||
if (!ret) { Integer integer1 = maxTimes, integer2 = maxTimes = Integer.valueOf(maxTimes.intValue() - 1); if (integer1.intValue() <= 0)
|
||||
break; continue; } break;
|
||||
} return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static class ClusterNode
|
||||
{
|
||||
private int centerId;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private int workId;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ClusterNode() {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public ClusterNode(int centerId, int workId) {
|
||||
this.centerId = centerId;
|
||||
this.workId = workId;
|
||||
}
|
||||
|
||||
public int getCenterId() {
|
||||
return this.centerId;
|
||||
}
|
||||
|
||||
public int getWorkId() {
|
||||
return this.workId;
|
||||
}
|
||||
|
||||
public void setCenterId(int centerId) {
|
||||
this.centerId = centerId;
|
||||
}
|
||||
|
||||
public void setWorkId(int workId) {
|
||||
this.workId = workId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+55
-144
@@ -1,172 +1,83 @@
|
||||
package cn.cloudwalk.serial.worker;
|
||||
|
||||
public class SnowflakeIdWorker {
|
||||
private final long twepoch = 1514736000000L;
|
||||
|
||||
private final long workerIdBits = 5L;
|
||||
|
||||
private final long datacenterIdBits = 5L;
|
||||
|
||||
private final long maxWorkerId = 31L;
|
||||
|
||||
private final long maxDatacenterId = 31L;
|
||||
|
||||
private final long sequenceBits = 12L;
|
||||
|
||||
private final long workerIdShift = 12L;
|
||||
|
||||
private final long datacenterIdShift = 17L;
|
||||
|
||||
private final long timestampLeftShift = 22L;
|
||||
|
||||
private final long sequenceMask = 4095L;
|
||||
|
||||
private long workerId;
|
||||
|
||||
private long datacenterId;
|
||||
|
||||
private long sequence = 0L;
|
||||
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
public SnowflakeIdWorker(long workerId, long datacenterId) {
|
||||
if (workerId > 31L || workerId < 0L) {
|
||||
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0",
|
||||
new Object[] { Long.valueOf(31L) }));
|
||||
}
|
||||
|
||||
if (datacenterId > 31L || datacenterId < 0L) {
|
||||
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0",
|
||||
new Object[] { Long.valueOf(31L) }));
|
||||
}
|
||||
|
||||
this.workerId = workerId;
|
||||
this.datacenterId = datacenterId;
|
||||
}
|
||||
|
||||
public synchronized long nextId() {
|
||||
long timestamp = timeGen();
|
||||
|
||||
if (timestamp < this.lastTimestamp) {
|
||||
throw new RuntimeException(
|
||||
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
|
||||
new Object[] { Long.valueOf(this.lastTimestamp - timestamp) }));
|
||||
}
|
||||
|
||||
if (this.lastTimestamp == timestamp) {
|
||||
this.sequence = this.sequence + 1L & 0xFFFL;
|
||||
|
||||
if (this.sequence == 0L) {
|
||||
timestamp = tilNextMillis(this.lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
|
||||
public class SnowflakeIdWorker
|
||||
{
|
||||
/* 26 */ private final long twepoch = 1514736000000L;
|
||||
this.sequence = 0L;
|
||||
}
|
||||
|
||||
this.lastTimestamp = timestamp;
|
||||
|
||||
return timestamp - 1514736000000L << 22L | this.datacenterId << 17L | this.workerId << 12L | this.sequence;
|
||||
}
|
||||
|
||||
protected long tilNextMillis(long lastTimestamp) {
|
||||
long timestamp = timeGen();
|
||||
while (timestamp <= lastTimestamp) {
|
||||
timestamp = timeGen();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/* 31 */ private final long workerIdBits = 5L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 36 */ private final long datacenterIdBits = 5L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 41 */ private final long maxWorkerId = 31L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 46 */ private final long maxDatacenterId = 31L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 51 */ private final long sequenceBits = 12L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 56 */ private final long workerIdShift = 12L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 61 */ private final long datacenterIdShift = 17L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 66 */ private final long timestampLeftShift = 22L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 71 */ private final long sequenceMask = 4095L;
|
||||
|
||||
|
||||
|
||||
|
||||
private long workerId;
|
||||
|
||||
|
||||
|
||||
|
||||
private long datacenterId;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 86 */ private long sequence = 0L;
|
||||
|
||||
|
||||
|
||||
|
||||
/* 91 */ private long lastTimestamp = -1L;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public SnowflakeIdWorker(long workerId, long datacenterId) {
|
||||
/* 99 */ if (workerId > 31L || workerId < 0L) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("worker Id can't be greater than %d or less than 0", new Object[] { Long.valueOf(31L) }));
|
||||
protected long timeGen() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
if (datacenterId > 31L || datacenterId < 0L) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("datacenter Id can't be greater than %d or less than 0", new Object[] { Long.valueOf(31L) }));
|
||||
}
|
||||
|
||||
this.workerId = workerId;
|
||||
this.datacenterId = datacenterId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public synchronized long nextId() {
|
||||
long timestamp = timeGen();
|
||||
|
||||
|
||||
if (timestamp < this.lastTimestamp) {
|
||||
throw new RuntimeException(
|
||||
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", new Object[] { Long.valueOf(this.lastTimestamp - timestamp) }));
|
||||
}
|
||||
|
||||
|
||||
if (this.lastTimestamp == timestamp) {
|
||||
this.sequence = this.sequence + 1L & 0xFFFL;
|
||||
|
||||
if (this.sequence == 0L)
|
||||
{
|
||||
timestamp = tilNextMillis(this.lastTimestamp);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
this.sequence = 0L;
|
||||
}
|
||||
|
||||
|
||||
this.lastTimestamp = timestamp;
|
||||
|
||||
|
||||
return timestamp - 1514736000000L << 22L | this.datacenterId << 17L | this.workerId << 12L | this.sequence;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
protected long tilNextMillis(long lastTimestamp) {
|
||||
long timestamp = timeGen();
|
||||
while (timestamp <= lastTimestamp) {
|
||||
timestamp = timeGen();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
protected long timeGen() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+25
-52
@@ -15,67 +15,40 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Component
|
||||
@Order(2147483547)
|
||||
public class CloudwalkContextParameterFilter
|
||||
extends OncePerRequestFilter
|
||||
{
|
||||
public class CloudwalkContextParameterFilter extends OncePerRequestFilter {
|
||||
|
||||
@Autowired
|
||||
private CloudwalkSessionContextHolder cloudwalkSessionContextHolder;
|
||||
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
String businessId = request.getHeader("businessId");
|
||||
|
||||
String userId = request.getHeader("platformUserId");
|
||||
if (StringUtils.isEmpty(userId)) {
|
||||
userId = "platformUserId";
|
||||
}
|
||||
|
||||
String username = request.getHeader("username");
|
||||
if (StringUtils.isEmpty(username)) {
|
||||
username = "username";
|
||||
}
|
||||
|
||||
String serviceCode = request.getHeader("serviceCode");
|
||||
|
||||
@Autowired
|
||||
private CloudwalkSessionContextHolder cloudwalkSessionContextHolder;
|
||||
String corpCode = request.getHeader("corpCode");
|
||||
|
||||
CloudwalkSessionObject sessionObject = new CloudwalkSessionObject(userId, serviceCode, businessId, corpCode,
|
||||
username);
|
||||
|
||||
this.cloudwalkSessionContextHolder.putSession(sessionObject);
|
||||
|
||||
|
||||
|
||||
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
|
||||
String businessId = request.getHeader("businessId");
|
||||
|
||||
String userId = request.getHeader("platformUserId");
|
||||
if (StringUtils.isEmpty(userId)) {
|
||||
userId = "platformUserId";
|
||||
try {
|
||||
chain.doFilter((ServletRequest) request, (ServletResponse) response);
|
||||
} finally {
|
||||
this.cloudwalkSessionContextHolder.clearSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String username = request.getHeader("username");
|
||||
if (StringUtils.isEmpty(username)) {
|
||||
username = "username";
|
||||
}
|
||||
|
||||
|
||||
String serviceCode = request.getHeader("serviceCode");
|
||||
|
||||
String corpCode = request.getHeader("corpCode");
|
||||
|
||||
|
||||
CloudwalkSessionObject sessionObject = new CloudwalkSessionObject(userId, serviceCode, businessId, corpCode, username);
|
||||
|
||||
this.cloudwalkSessionContextHolder.putSession(sessionObject);
|
||||
|
||||
try {
|
||||
chain.doFilter((ServletRequest)request, (ServletResponse)response);
|
||||
} finally {
|
||||
this.cloudwalkSessionContextHolder.clearSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+13
-15
@@ -1,19 +1,17 @@
|
||||
package cn.cloudwalk.web.filter;
|
||||
|
||||
public interface GeneralParameterConst {
|
||||
public static final String BUSINESS_ID = "businessId";
|
||||
|
||||
public static final String CORP_CODE = "corpCode";
|
||||
|
||||
public static final String SERVICE_CODE = "serviceCode";
|
||||
|
||||
public static final String USER_ID = "platformUserId";
|
||||
|
||||
public static final String USER_NAME = "username";
|
||||
|
||||
public static final String DEFAULT_USER_ID = "0";
|
||||
|
||||
public static final String DEFAULT_USER_NAME = "默认用户";
|
||||
public static final String BUSINESS_ID = "businessId";
|
||||
|
||||
public static final String CORP_CODE = "corpCode";
|
||||
|
||||
public static final String SERVICE_CODE = "serviceCode";
|
||||
|
||||
public static final String USER_ID = "platformUserId";
|
||||
|
||||
public static final String USER_NAME = "username";
|
||||
|
||||
public static final String DEFAULT_USER_ID = "0";
|
||||
|
||||
public static final String DEFAULT_USER_NAME = "默认用户";
|
||||
}
|
||||
|
||||
|
||||
|
||||
+23
-37
@@ -11,42 +11,28 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
public class LocaleConfiguration
|
||||
extends WebMvcConfigurerAdapter
|
||||
{
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
|
||||
return (LocaleResolver)slr;
|
||||
public class LocaleConfiguration extends WebMvcConfigurerAdapter {
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
|
||||
return (LocaleResolver) slr;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
|
||||
interceptor.setParamName("lang");
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor((HandlerInterceptor) localeChangeInterceptor());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloudwalkSessionContextHolder cloudwalkSessionContextHolder() {
|
||||
return new CloudwalkSessionContextHolder();
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
|
||||
interceptor.setParamName("lang");
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor((HandlerInterceptor)localeChangeInterceptor());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloudwalkSessionContextHolder cloudwalkSessionContextHolder() {
|
||||
return new CloudwalkSessionContextHolder();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+9
-25
@@ -10,31 +10,15 @@ import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class XssFilter implements Filter {
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
chain.doFilter((ServletRequest) new XssHttpServletRequestWrapper((HttpServletRequest) request), response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class XssFilter
|
||||
implements Filter
|
||||
{
|
||||
public void init(FilterConfig filterConfig) throws ServletException {}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
chain.doFilter((ServletRequest)new XssHttpServletRequestWrapper((HttpServletRequest)request), response);
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+29
-111
@@ -3,136 +3,54 @@ package cn.cloudwalk.web.xss.util;
|
||||
import java.util.regex.Pattern;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class CustomXssUtil {
|
||||
private static final Pattern SCRIPT_PATTERN = Pattern.compile("<script>(.*?)</script>", 2);
|
||||
|
||||
private static final Pattern SRC_PATTERN1 = Pattern.compile("src[\r\n]*=[\r\n]*\\'(.*?)\\'", 42);
|
||||
|
||||
private static final Pattern SRC_PATTERN2 = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", 42);
|
||||
|
||||
private static final Pattern SINGLE_SCRIPT_PATTERN1 = Pattern.compile("</script>", 2);
|
||||
private static final Pattern SINGLE_SCRIPT_PATTERN2 = Pattern.compile("<script(.*?)>", 42);
|
||||
|
||||
private static final Pattern IMG_SCRIPT_PATTERN = Pattern.compile("<img.*?on.*?=.*?>", 2);
|
||||
|
||||
private static final Pattern EVAL_PATTERN = Pattern.compile("eval\\((.*?)\\)", 42);
|
||||
|
||||
private static final Pattern EXPRESSION_PATTERN = Pattern.compile("expression\\((.*?)\\)", 42);
|
||||
|
||||
private static final Pattern JS_PATTERN = Pattern.compile("javascript:", 2);
|
||||
|
||||
private static final Pattern VBS_PATTERN = Pattern.compile("vbscript:", 2);
|
||||
|
||||
private static final Pattern ONLOAD_PATTERN = Pattern.compile("onload(.*?)=", 42);
|
||||
|
||||
public static String stripXSS(String value) {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
value = SCRIPT_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
value = SRC_PATTERN1.matcher(value).replaceAll("");
|
||||
value = SRC_PATTERN2.matcher(value).replaceAll("");
|
||||
|
||||
public class CustomXssUtil
|
||||
{
|
||||
/* 21 */ private static final Pattern SCRIPT_PATTERN = Pattern.compile("<script>(.*?)</script>", 2);
|
||||
value = SINGLE_SCRIPT_PATTERN1.matcher(value).replaceAll("");
|
||||
|
||||
value = SINGLE_SCRIPT_PATTERN2.matcher(value).replaceAll("");
|
||||
|
||||
value = IMG_SCRIPT_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
value = EVAL_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
/* 26 */ private static final Pattern SRC_PATTERN1 = Pattern.compile("src[\r\n]*=[\r\n]*\\'(.*?)\\'", 42);
|
||||
value = EXPRESSION_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
/* 28 */ private static final Pattern SRC_PATTERN2 = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", 42);
|
||||
value = JS_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
value = VBS_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
value = ONLOAD_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
|
||||
/* 34 */ private static final Pattern SINGLE_SCRIPT_PATTERN1 = Pattern.compile("</script>", 2);
|
||||
/* 35 */ private static final Pattern SINGLE_SCRIPT_PATTERN2 = Pattern.compile("<script(.*?)>", 42);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 41 */ private static final Pattern IMG_SCRIPT_PATTERN = Pattern.compile("<img.*?on.*?=.*?>", 2);
|
||||
|
||||
|
||||
|
||||
|
||||
/* 46 */ private static final Pattern EVAL_PATTERN = Pattern.compile("eval\\((.*?)\\)", 42);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 52 */ private static final Pattern EXPRESSION_PATTERN = Pattern.compile("expression\\((.*?)\\)", 42);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* 58 */ private static final Pattern JS_PATTERN = Pattern.compile("javascript:", 2);
|
||||
|
||||
|
||||
|
||||
|
||||
/* 63 */ private static final Pattern VBS_PATTERN = Pattern.compile("vbscript:", 2);
|
||||
|
||||
|
||||
|
||||
|
||||
/* 68 */ private static final Pattern ONLOAD_PATTERN = Pattern.compile("onload(.*?)=", 42);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String stripXSS(String value) {
|
||||
/* 96 */ if (StringUtils.isBlank(value)) {
|
||||
/* 97 */ return value;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
value = SCRIPT_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = SRC_PATTERN1.matcher(value).replaceAll("");
|
||||
value = SRC_PATTERN2.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = SINGLE_SCRIPT_PATTERN1.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = SINGLE_SCRIPT_PATTERN2.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = IMG_SCRIPT_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = EVAL_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = EXPRESSION_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = JS_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = VBS_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
|
||||
value = ONLOAD_PATTERN.matcher(value).replaceAll("");
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+80
-123
@@ -11,141 +11,98 @@ import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletRequestWrapper;
|
||||
|
||||
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
|
||||
private final String body;
|
||||
|
||||
public XssHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
|
||||
super(request);
|
||||
|
||||
if (request instanceof org.springframework.web.multipart.MultipartHttpServletRequest) {
|
||||
this.body = null;
|
||||
|
||||
return;
|
||||
}
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
ServletInputStream servletInputStream = request.getInputStream();
|
||||
if (servletInputStream != null) {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader((InputStream) servletInputStream));
|
||||
char[] charBuffer = new char[1024];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException ex) {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.body = CustomXssUtil.stripXSS(stringBuilder.toString());
|
||||
}
|
||||
|
||||
public String getHeader(String name) {
|
||||
String value = super.getHeader(name);
|
||||
return CustomXssUtil.stripXSS(value);
|
||||
}
|
||||
|
||||
public String getParameter(String name) {
|
||||
String value = super.getParameter(name);
|
||||
return CustomXssUtil.stripXSS(value);
|
||||
}
|
||||
|
||||
public String[] getParameterValues(String name) {
|
||||
String[] values = super.getParameterValues(name);
|
||||
if (values == null) {
|
||||
return null;
|
||||
}
|
||||
int count = values.length;
|
||||
String[] encodedValues = new String[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
encodedValues[i] = CustomXssUtil.stripXSS(values[i]);
|
||||
}
|
||||
return encodedValues;
|
||||
}
|
||||
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
HttpServletRequest request = (HttpServletRequest) getRequest();
|
||||
if (request instanceof org.springframework.web.multipart.MultipartHttpServletRequest) {
|
||||
return super.getInputStream();
|
||||
}
|
||||
|
||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.body.getBytes());
|
||||
ServletInputStream servletInputStream = new ServletInputStream() {
|
||||
public int read() throws IOException {
|
||||
return byteArrayInputStream.read();
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
}
|
||||
};
|
||||
return servletInputStream;
|
||||
}
|
||||
|
||||
public BufferedReader getReader() throws IOException {
|
||||
return new BufferedReader(new InputStreamReader((InputStream) getInputStream()));
|
||||
}
|
||||
|
||||
|
||||
public class XssHttpServletRequestWrapper
|
||||
extends HttpServletRequestWrapper
|
||||
{
|
||||
private final String body;
|
||||
|
||||
public XssHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
|
||||
/* 35 */ super(request);
|
||||
|
||||
|
||||
/* 38 */ if (request instanceof org.springframework.web.multipart.MultipartHttpServletRequest) {
|
||||
/* 39 */ this.body = null;
|
||||
|
||||
return;
|
||||
}
|
||||
/* 43 */ StringBuilder stringBuilder = new StringBuilder();
|
||||
/* 44 */ BufferedReader bufferedReader = null;
|
||||
try {
|
||||
/* 46 */ ServletInputStream servletInputStream = request.getInputStream();
|
||||
/* 47 */ if (servletInputStream != null) {
|
||||
/* 48 */ bufferedReader = new BufferedReader(new InputStreamReader((InputStream)servletInputStream));
|
||||
/* 49 */ char[] charBuffer = new char[1024];
|
||||
/* 50 */ int bytesRead = -1;
|
||||
/* 51 */ while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||
/* 52 */ stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
public String getBody() {
|
||||
return this.body;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* 55 */ } catch (IOException ex) {
|
||||
/* 56 */ throw ex;
|
||||
} finally {
|
||||
/* 58 */ if (bufferedReader != null) {
|
||||
try {
|
||||
/* 60 */ bufferedReader.close();
|
||||
/* 61 */ } catch (IOException ex) {
|
||||
/* 62 */ throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* 68 */ this.body = CustomXssUtil.stripXSS(stringBuilder.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String getHeader(String name) {
|
||||
/* 77 */ String value = super.getHeader(name);
|
||||
/* 78 */ return CustomXssUtil.stripXSS(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String getParameter(String name) {
|
||||
/* 87 */ String value = super.getParameter(name);
|
||||
/* 88 */ return CustomXssUtil.stripXSS(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String[] getParameterValues(String name) {
|
||||
/* 97 */ String[] values = super.getParameterValues(name);
|
||||
/* 98 */ if (values == null) {
|
||||
/* 99 */ return null;
|
||||
}
|
||||
int count = values.length;
|
||||
String[] encodedValues = new String[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
encodedValues[i] = CustomXssUtil.stripXSS(values[i]);
|
||||
}
|
||||
return encodedValues;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
HttpServletRequest request = (HttpServletRequest)getRequest();
|
||||
if (request instanceof org.springframework.web.multipart.MultipartHttpServletRequest) {
|
||||
return super.getInputStream();
|
||||
}
|
||||
|
||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.body.getBytes());
|
||||
ServletInputStream servletInputStream = new ServletInputStream() {
|
||||
public int read() throws IOException {
|
||||
return byteArrayInputStream.read();
|
||||
}
|
||||
|
||||
|
||||
public boolean isFinished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setReadListener(ReadListener readListener) {}
|
||||
};
|
||||
return servletInputStream;
|
||||
}
|
||||
|
||||
|
||||
public BufferedReader getReader() throws IOException {
|
||||
return new BufferedReader(new InputStreamReader((InputStream)getInputStream()));
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return this.body;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+7
-19
@@ -7,24 +7,12 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties({LockProperties.class})
|
||||
@ConditionalOnProperty(prefix = "intelligent.lock", name = {"enable"}, havingValue = "true")
|
||||
public class IntelligentLockConfiguration
|
||||
{
|
||||
@Bean
|
||||
public LockFactory lockFactory() {
|
||||
return new LockFactory();
|
||||
@EnableConfigurationProperties({ LockProperties.class })
|
||||
@ConditionalOnProperty(prefix = "intelligent.lock", name = { "enable" }, havingValue = "true")
|
||||
public class IntelligentLockConfiguration {
|
||||
@Bean
|
||||
public LockFactory lockFactory() {
|
||||
return new LockFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+12
-14
@@ -10,19 +10,17 @@ import java.lang.annotation.Target;
|
||||
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
@Target({ ElementType.METHOD })
|
||||
public @interface RequiredLock {
|
||||
String name() default "DEFAULT-LOCK";
|
||||
|
||||
LockType lockType() default LockType.REENTRANT;
|
||||
|
||||
long lockWaitTime() default -1L;
|
||||
|
||||
long leaseTime() default -1L;
|
||||
|
||||
LockAcquireTimeoutStrategy lockAcquireTimeoutStrategy() default LockAcquireTimeoutStrategy.FAIL_FAST;
|
||||
|
||||
String customAcquireTimeoutHandleMethod() default "";
|
||||
String name() default "DEFAULT-LOCK";
|
||||
|
||||
LockType lockType() default LockType.REENTRANT;
|
||||
|
||||
long lockWaitTime() default -1L;
|
||||
|
||||
long leaseTime() default -1L;
|
||||
|
||||
LockAcquireTimeoutStrategy lockAcquireTimeoutStrategy() default LockAcquireTimeoutStrategy.FAIL_FAST;
|
||||
|
||||
String customAcquireTimeoutHandleMethod() default "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
+93
-154
@@ -20,174 +20,113 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(-999)
|
||||
public class LockAspect
|
||||
{
|
||||
/* 37 */ private static final Logger logger = LoggerFactory.getLogger(LockAspect.class);
|
||||
public class LockAspect {
|
||||
private static final Logger logger = LoggerFactory.getLogger(LockAspect.class);
|
||||
|
||||
@Autowired
|
||||
private LockFactory lockFactory;
|
||||
@Autowired
|
||||
private LockFactory lockFactory;
|
||||
|
||||
@Autowired
|
||||
private LockProperties lockProperties;
|
||||
@Autowired
|
||||
private LockProperties lockProperties;
|
||||
|
||||
/* 45 */ private ThreadLocal<Lock> currentThreadLock = new ThreadLocal<>();
|
||||
/* 46 */ private ThreadLocal<LockResult> currentThreadLockResult = new ThreadLocal<>();
|
||||
private ThreadLocal<Lock> currentThreadLock = new ThreadLocal<>();
|
||||
private ThreadLocal<LockResult> currentThreadLockResult = new ThreadLocal<>();
|
||||
|
||||
@Around("@annotation(requiredLock)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
LockInfo lockInfo = getLockInfo(joinPoint, requiredLock);
|
||||
this.currentThreadLockResult.set(new LockResult(lockInfo, Boolean.valueOf(false)));
|
||||
Lock lock = this.lockFactory.create(lockInfo);
|
||||
boolean isAcquired = lock.acquire();
|
||||
|
||||
if (!isAcquired) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("获取锁等待超时({})", lockInfo.getName());
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(requiredLock.customAcquireTimeoutHandleMethod())) {
|
||||
|
||||
requiredLock.lockAcquireTimeoutStrategy().handle(lockInfo, lock, (JoinPoint) joinPoint);
|
||||
}
|
||||
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)",
|
||||
new Object[] { lockInfo.getName(), Long.valueOf(lockInfo.getLockWaitTime()) }));
|
||||
}
|
||||
|
||||
this.currentThreadLock.set(lock);
|
||||
((LockResult) this.currentThreadLockResult.get()).setLocked(Boolean.valueOf(true));
|
||||
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
@AfterReturning("@annotation(requiredLock)")
|
||||
public void afterReturning(JoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
releaseLock();
|
||||
flushThreadLocal();
|
||||
}
|
||||
|
||||
@Around("@annotation(requiredLock)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
/* 58 */ LockInfo lockInfo = getLockInfo(joinPoint, requiredLock);
|
||||
/* 59 */ this.currentThreadLockResult.set(new LockResult(lockInfo, Boolean.valueOf(false)));
|
||||
/* 60 */ Lock lock = this.lockFactory.create(lockInfo);
|
||||
/* 61 */ boolean isAcquired = lock.acquire();
|
||||
@AfterThrowing(value = "@annotation(requiredLock)", throwing = "throwable")
|
||||
public void afterThrowing(JoinPoint joinPoint, RequiredLock requiredLock, Throwable throwable) throws Throwable {
|
||||
releaseLock();
|
||||
flushThreadLocal();
|
||||
throw throwable;
|
||||
}
|
||||
|
||||
/* 63 */ if (!isAcquired) {
|
||||
/* 64 */ if (logger.isWarnEnabled()) {
|
||||
/* 65 */ logger.warn("获取锁等待超时({})", lockInfo.getName());
|
||||
private void flushThreadLocal() {
|
||||
this.currentThreadLock.remove();
|
||||
this.currentThreadLockResult.remove();
|
||||
}
|
||||
|
||||
private LockInfo getLockInfo(ProceedingJoinPoint joinPoint, RequiredLock lock) {
|
||||
long lockWaitTime = (lock.lockWaitTime() < 0L) ? this.lockProperties.getDefaultWaitTime().longValue()
|
||||
: lock.lockWaitTime();
|
||||
|
||||
long leaseTime = (lock.leaseTime() <= 0L) ? -1L : lock.leaseTime();
|
||||
String lockName = parseLockName(joinPoint, lock.name());
|
||||
return new LockInfo(lock.lockType(), lockName, lockWaitTime, leaseTime);
|
||||
}
|
||||
|
||||
private String parseLockName(ProceedingJoinPoint joinPoint, String name) {
|
||||
return (String) ExpressionParseUtil.parse(joinPoint, name, String.class);
|
||||
}
|
||||
|
||||
private void releaseLock() throws Throwable {
|
||||
LockResult lockResult = this.currentThreadLockResult.get();
|
||||
if (lockResult.getLocked().booleanValue()) {
|
||||
boolean releaseRes = ((Lock) this.currentThreadLock.get()).unlock();
|
||||
lockResult.setLocked(Boolean.valueOf(false));
|
||||
if (!releaseRes)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
private class LockResult {
|
||||
private LockInfo lockInfo;
|
||||
|
||||
private Boolean isLocked;
|
||||
|
||||
public LockResult(LockInfo lockInfo, Boolean isLocked) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.isLocked = isLocked;
|
||||
}
|
||||
|
||||
public LockInfo getLockInfo() {
|
||||
return this.lockInfo;
|
||||
}
|
||||
|
||||
public void setLockInfo(LockInfo lockInfo) {
|
||||
this.lockInfo = lockInfo;
|
||||
}
|
||||
|
||||
public Boolean getLocked() {
|
||||
return this.isLocked;
|
||||
}
|
||||
|
||||
public void setLocked(Boolean locked) {
|
||||
this.isLocked = locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 68 */ if (StringUtils.isEmpty(requiredLock.customAcquireTimeoutHandleMethod()))
|
||||
{
|
||||
|
||||
|
||||
/* 72 */ requiredLock.lockAcquireTimeoutStrategy().handle(lockInfo, lock, (JoinPoint)joinPoint);
|
||||
}
|
||||
|
||||
/* 75 */ throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)", new Object[] { lockInfo.getName(),
|
||||
/* 76 */ Long.valueOf(lockInfo.getLockWaitTime()) }));
|
||||
}
|
||||
|
||||
|
||||
/* 80 */ this.currentThreadLock.set(lock);
|
||||
/* 81 */ ((LockResult)this.currentThreadLockResult.get()).setLocked(Boolean.valueOf(true));
|
||||
|
||||
/* 83 */ return joinPoint.proceed();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@AfterReturning("@annotation(requiredLock)")
|
||||
public void afterReturning(JoinPoint joinPoint, RequiredLock requiredLock) throws Throwable {
|
||||
/* 95 */ releaseLock();
|
||||
/* 96 */ flushThreadLocal();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@AfterThrowing(value = "@annotation(requiredLock)", throwing = "throwable")
|
||||
public void afterThrowing(JoinPoint joinPoint, RequiredLock requiredLock, Throwable throwable) throws Throwable {
|
||||
releaseLock();
|
||||
flushThreadLocal();
|
||||
throw throwable;
|
||||
}
|
||||
|
||||
private void flushThreadLocal() {
|
||||
this.currentThreadLock.remove();
|
||||
this.currentThreadLockResult.remove();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private LockInfo getLockInfo(ProceedingJoinPoint joinPoint, RequiredLock lock) {
|
||||
long lockWaitTime = (lock.lockWaitTime() < 0L) ? this.lockProperties.getDefaultWaitTime().longValue() : lock.lockWaitTime();
|
||||
|
||||
|
||||
long leaseTime = (lock.leaseTime() <= 0L) ? -1L : lock.leaseTime();
|
||||
String lockName = parseLockName(joinPoint, lock.name());
|
||||
return new LockInfo(lock.lockType(), lockName, lockWaitTime, leaseTime);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private String parseLockName(ProceedingJoinPoint joinPoint, String name) {
|
||||
return (String)ExpressionParseUtil.parse(joinPoint, name, String.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void releaseLock() throws Throwable {
|
||||
LockResult lockResult = this.currentThreadLockResult.get();
|
||||
if (lockResult.getLocked().booleanValue()) {
|
||||
boolean releaseRes = ((Lock)this.currentThreadLock.get()).unlock();
|
||||
lockResult.setLocked(Boolean.valueOf(false));
|
||||
if (!releaseRes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class LockResult
|
||||
{
|
||||
private LockInfo lockInfo;
|
||||
|
||||
|
||||
private Boolean isLocked;
|
||||
|
||||
|
||||
public LockResult(LockInfo lockInfo, Boolean isLocked) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.isLocked = isLocked;
|
||||
}
|
||||
|
||||
public LockInfo getLockInfo() {
|
||||
return this.lockInfo;
|
||||
}
|
||||
|
||||
public void setLockInfo(LockInfo lockInfo) {
|
||||
this.lockInfo = lockInfo;
|
||||
}
|
||||
|
||||
public Boolean getLocked() {
|
||||
return this.isLocked;
|
||||
}
|
||||
|
||||
public void setLocked(Boolean locked) {
|
||||
this.isLocked = locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-22
@@ -1,26 +1,7 @@
|
||||
package cn.cloudwalk.intelligent.lock.common.enums;
|
||||
|
||||
public enum LockType {
|
||||
REENTRANT,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum LockType
|
||||
{
|
||||
REENTRANT,
|
||||
|
||||
|
||||
|
||||
|
||||
REDLOCK;
|
||||
REDLOCK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-16
@@ -1,19 +1,7 @@
|
||||
package cn.cloudwalk.intelligent.lock.common.exception;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class AcquireLockTimeoutException
|
||||
extends RuntimeException
|
||||
{
|
||||
public AcquireLockTimeoutException(String message) {
|
||||
super(message);
|
||||
public class AcquireLockTimeoutException extends RuntimeException {
|
||||
public AcquireLockTimeoutException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+7
-19
@@ -1,23 +1,11 @@
|
||||
package cn.cloudwalk.intelligent.lock.common.exception;
|
||||
|
||||
public class LockOperationException extends RuntimeException {
|
||||
public LockOperationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class LockOperationException
|
||||
extends RuntimeException
|
||||
{
|
||||
public LockOperationException(String message) {
|
||||
super(message);
|
||||
public LockOperationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
public LockOperationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+35
-62
@@ -2,75 +2,48 @@ package cn.cloudwalk.intelligent.lock.common.model;
|
||||
|
||||
import cn.cloudwalk.intelligent.lock.common.enums.LockType;
|
||||
|
||||
public class LockInfo {
|
||||
private LockType type;
|
||||
private String name;
|
||||
private long lockWaitTime;
|
||||
private long leaseTime;
|
||||
|
||||
public LockInfo(LockType type, String name, long lockWaitTime, long leaseTime) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
this.leaseTime = leaseTime;
|
||||
}
|
||||
|
||||
public LockType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(LockType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getLockWaitTime() {
|
||||
return this.lockWaitTime;
|
||||
}
|
||||
|
||||
public void setLockWaitTime(long lockWaitTime) {
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
}
|
||||
|
||||
public long getLeaseTime() {
|
||||
return this.leaseTime;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class LockInfo
|
||||
{
|
||||
private LockType type;
|
||||
private String name;
|
||||
private long lockWaitTime;
|
||||
private long leaseTime;
|
||||
|
||||
public LockInfo(LockType type, String name, long lockWaitTime, long leaseTime) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
this.leaseTime = leaseTime;
|
||||
public void setLeaseTime(long leaseTime) {
|
||||
this.leaseTime = leaseTime;
|
||||
}
|
||||
}
|
||||
|
||||
public LockType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public void setType(LockType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getLockWaitTime() {
|
||||
return this.lockWaitTime;
|
||||
}
|
||||
|
||||
public void setLockWaitTime(long lockWaitTime) {
|
||||
this.lockWaitTime = lockWaitTime;
|
||||
}
|
||||
|
||||
public long getLeaseTime() {
|
||||
return this.leaseTime;
|
||||
}
|
||||
|
||||
public void setLeaseTime(long leaseTime) {
|
||||
this.leaseTime = leaseTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+16
-44
@@ -10,51 +10,23 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class ExpressionParseUtil {
|
||||
private static ExpressionParser parser = (ExpressionParser) new SpelExpressionParser();
|
||||
|
||||
public static <T> T parse(ProceedingJoinPoint joinPoint, String spel, Class<T> clazz) {
|
||||
String[] parameterNames = (new LocalVariableTableParameterNameDiscoverer())
|
||||
.getParameterNames(((MethodSignature) joinPoint.getSignature()).getMethod());
|
||||
Object[] values = joinPoint.getArgs();
|
||||
|
||||
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
standardEvaluationContext.setVariable(parameterNames[i], values[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ExpressionParseUtil
|
||||
{
|
||||
private static ExpressionParser parser = (ExpressionParser)new SpelExpressionParser();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static <T> T parse(ProceedingJoinPoint joinPoint, String spel, Class<T> clazz) {
|
||||
String[] parameterNames = (new LocalVariableTableParameterNameDiscoverer()).getParameterNames(((MethodSignature)joinPoint.getSignature()).getMethod());
|
||||
Object[] values = joinPoint.getArgs();
|
||||
|
||||
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
standardEvaluationContext.setVariable(parameterNames[i], values[i]);
|
||||
if (StringUtils.isEmpty(spel)) {
|
||||
return null;
|
||||
}
|
||||
Expression expression = parser.parseExpression(spel);
|
||||
return (T) expression.getValue((EvaluationContext) standardEvaluationContext, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(spel)) {
|
||||
return null;
|
||||
}
|
||||
Expression expression = parser.parseExpression(spel);
|
||||
return (T)expression.getValue((EvaluationContext)standardEvaluationContext, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-5
@@ -1,9 +1,7 @@
|
||||
package cn.cloudwalk.intelligent.lock.locks;
|
||||
|
||||
public interface Lock {
|
||||
boolean acquire();
|
||||
|
||||
boolean unlock();
|
||||
boolean acquire();
|
||||
|
||||
boolean unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+6
-18
@@ -4,23 +4,11 @@ import cn.cloudwalk.intelligent.lock.common.model.LockInfo;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class LockFactory {
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class LockFactory
|
||||
{
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public Lock create(LockInfo lockInfo) {
|
||||
return new ReentrantLock(lockInfo, this.redissonClient);
|
||||
public Lock create(LockInfo lockInfo) {
|
||||
return new ReentrantLock(lockInfo, this.redissonClient);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+27
-44
@@ -6,51 +6,34 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
public class ReentrantLock implements Lock {
|
||||
private LockInfo lockInfo;
|
||||
private RLock rLock;
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public ReentrantLock(LockInfo lockInfo, RedissonClient redissonClient) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
public boolean acquire() {
|
||||
try {
|
||||
this.rLock = this.redissonClient.getLock(this.lockInfo.getName());
|
||||
return this.rLock.tryLock(this.lockInfo.getLockWaitTime(), this.lockInfo.getLeaseTime(),
|
||||
TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("获取锁操作异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class ReentrantLock
|
||||
implements Lock
|
||||
{
|
||||
private LockInfo lockInfo;
|
||||
private RLock rLock;
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public ReentrantLock(LockInfo lockInfo, RedissonClient redissonClient) {
|
||||
this.lockInfo = lockInfo;
|
||||
this.redissonClient = redissonClient;
|
||||
public boolean unlock() {
|
||||
if (this.rLock.isHeldByCurrentThread()) {
|
||||
try {
|
||||
return ((Boolean) this.rLock.forceUnlockAsync().get()).booleanValue();
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("释放锁操作异常", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean acquire() {
|
||||
try {
|
||||
this.rLock = this.redissonClient.getLock(this.lockInfo.getName());
|
||||
return this.rLock.tryLock(this.lockInfo.getLockWaitTime(), this.lockInfo.getLeaseTime(), TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("获取锁操作异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean unlock() {
|
||||
if (this.rLock.isHeldByCurrentThread()) {
|
||||
try {
|
||||
return ((Boolean)this.rLock.forceUnlockAsync().get()).booleanValue();
|
||||
} catch (Exception e) {
|
||||
throw new LockOperationException("释放锁操作异常", e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -5,7 +5,5 @@ import cn.cloudwalk.intelligent.lock.locks.Lock;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
||||
public interface LockAcquireTimeoutHandler {
|
||||
void handle(LockInfo paramLockInfo, Lock paramLock, JoinPoint paramJoinPoint);
|
||||
void handle(LockInfo paramLockInfo, Lock paramLock, JoinPoint paramJoinPoint);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+31
-56
@@ -6,66 +6,41 @@ import cn.cloudwalk.intelligent.lock.locks.Lock;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
||||
public enum LockAcquireTimeoutStrategy implements LockAcquireTimeoutHandler {
|
||||
NO_OPERATION {
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
}
|
||||
},
|
||||
FAIL_FAST {
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)",
|
||||
new Object[] { lockInfo.getName(), Long.valueOf(lockInfo.getLockWaitTime())
|
||||
|
||||
}));
|
||||
}
|
||||
},
|
||||
KEEP_ACQUIRE {
|
||||
private static final long DEFAULT_INTERVAL = 100L;
|
||||
|
||||
private static final long DEFAULT_MAX_INTERVAL = 180000L;
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
long interval = 100L;
|
||||
while (!lock.acquire()) {
|
||||
|
||||
if (interval > 180000L) {
|
||||
throw new AcquireLockTimeoutException(
|
||||
String.format("获取锁(%s)阻塞时间过长", new Object[] { lockInfo.getName() }));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public enum LockAcquireTimeoutStrategy
|
||||
implements LockAcquireTimeoutHandler
|
||||
{
|
||||
NO_OPERATION
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {}
|
||||
},
|
||||
FAIL_FAST
|
||||
{
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)超时(%dms)", new Object[] { lockInfo.getName(),
|
||||
Long.valueOf(lockInfo.getLockWaitTime())
|
||||
|
||||
|
||||
|
||||
}));
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(interval);
|
||||
interval <<= 1L;
|
||||
} catch (InterruptedException e) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)失)", new Object[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
KEEP_ACQUIRE
|
||||
{
|
||||
private static final long DEFAULT_INTERVAL = 100L;
|
||||
|
||||
private static final long DEFAULT_MAX_INTERVAL = 180000L;
|
||||
|
||||
|
||||
public void handle(LockInfo lockInfo, Lock lock, JoinPoint joinPoint) {
|
||||
long interval = 100L;
|
||||
while (!lock.acquire()) {
|
||||
|
||||
if (interval > 180000L) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)阻塞时间过长", new Object[] { lockInfo.getName() }));
|
||||
}
|
||||
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(interval);
|
||||
interval <<= 1L;
|
||||
} catch (InterruptedException e) {
|
||||
throw new AcquireLockTimeoutException(String.format("获取锁(%s)失)", new Object[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
+10
-25
@@ -2,31 +2,16 @@ package cn.cloudwalk.intelligent.lock.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ConfigurationProperties(prefix = "intelligent.lock.config")
|
||||
public class LockProperties
|
||||
{
|
||||
public static final String PREFIX = "intelligent.lock.config";
|
||||
private Long defaultWaitTime = Long.valueOf(15000L);
|
||||
public class LockProperties {
|
||||
public static final String PREFIX = "intelligent.lock.config";
|
||||
private Long defaultWaitTime = Long.valueOf(15000L);
|
||||
|
||||
public void setDefaultWaitTime(Long defaultWaitTime) {
|
||||
this.defaultWaitTime = defaultWaitTime;
|
||||
public void setDefaultWaitTime(Long defaultWaitTime) {
|
||||
this.defaultWaitTime = defaultWaitTime;
|
||||
}
|
||||
|
||||
public Long getDefaultWaitTime() {
|
||||
return this.defaultWaitTime;
|
||||
}
|
||||
}
|
||||
|
||||
public Long getDefaultWaitTime() {
|
||||
return this.defaultWaitTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+67
-118
@@ -13,133 +13,82 @@ import cn.cloudwalk.cwos.client.event.service.EventService;
|
||||
import cn.cloudwalk.cwos.client.event.service.impl.EventServiceImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class EventClient {
|
||||
private String bootstrapServers;
|
||||
private String groupId;
|
||||
private boolean init;
|
||||
private EventService eventService;
|
||||
private Class listenerClass;
|
||||
|
||||
public static EventClient getInstance(String bootstrapServers, String groupId) {
|
||||
if (StringUtils.isEmpty(bootstrapServers)) {
|
||||
throw new ParamException("bootstrapServers 不能为空");
|
||||
}
|
||||
if (StringUtils.isEmpty(groupId)) {
|
||||
return new EventClient(bootstrapServers);
|
||||
}
|
||||
return new EventClient(bootstrapServers, groupId);
|
||||
}
|
||||
|
||||
public EventClient init() {
|
||||
this.eventService = (EventService) new EventServiceImpl(this.bootstrapServers, this.groupId);
|
||||
this.init = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventClient setWorkNum(Integer workThread) {
|
||||
this.eventService.setWorkNum(workThread);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventClient setListenerClass(Class<?> cls) {
|
||||
if (!EventListener.class.isAssignableFrom(cls)) {
|
||||
throw new ListenerClassException("ListenerClass must implements ");
|
||||
}
|
||||
this.listenerClass = cls;
|
||||
this.eventService.setListenerClass(this.listenerClass);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void subEvent(EventType eventType, String serviceCode) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
this.eventService.subEvent(eventType, serviceCode);
|
||||
}
|
||||
|
||||
public void subEvent(String topic, String serviceCode, CustomEvent eventClass) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
this.eventService.subEvent(topic, serviceCode, eventClass);
|
||||
}
|
||||
|
||||
public void subEvent(EventType eventType, String serviceCode,
|
||||
EventSingleSubEventListener eventSingleSubEventListener) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
if (!eventSingleSubEventListener.returnClass().isAssignableFrom(eventType.getEventClass().getClass())) {
|
||||
throw new EventNotMathEventTypeException("订阅类型与实现类不一致");
|
||||
}
|
||||
|
||||
this.eventService.subEvent(eventType, serviceCode, eventSingleSubEventListener);
|
||||
}
|
||||
|
||||
public void pubEvent(BaseEvent event) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
this.eventService.pubEvent(event);
|
||||
}
|
||||
|
||||
private EventClient(String bootstrapServers, String groupId) {
|
||||
this.bootstrapServers = bootstrapServers;
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public class EventClient
|
||||
{
|
||||
private String bootstrapServers;
|
||||
private String groupId;
|
||||
private boolean init;
|
||||
private EventService eventService;
|
||||
private Class listenerClass;
|
||||
|
||||
public static EventClient getInstance(String bootstrapServers, String groupId) {
|
||||
/* 56 */ if (StringUtils.isEmpty(bootstrapServers)) {
|
||||
/* 57 */ throw new ParamException("bootstrapServers 不能为空");
|
||||
private EventClient(String bootstrapServers) {
|
||||
this.bootstrapServers = bootstrapServers;
|
||||
}
|
||||
}
|
||||
/* 59 */ if (StringUtils.isEmpty(groupId)) {
|
||||
/* 60 */ return new EventClient(bootstrapServers);
|
||||
}
|
||||
/* 62 */ return new EventClient(bootstrapServers, groupId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public EventClient init() {
|
||||
/* 71 */ this.eventService = (EventService)new EventServiceImpl(this.bootstrapServers, this.groupId);
|
||||
/* 72 */ this.init = true;
|
||||
/* 73 */ return this;
|
||||
}
|
||||
|
||||
public EventClient setWorkNum(Integer workThread) {
|
||||
/* 77 */ this.eventService.setWorkNum(workThread);
|
||||
/* 78 */ return this;
|
||||
}
|
||||
|
||||
|
||||
public EventClient setListenerClass(Class<?> cls) {
|
||||
/* 83 */ if (!EventListener.class.isAssignableFrom(cls)) {
|
||||
/* 84 */ throw new ListenerClassException("ListenerClass must implements ");
|
||||
}
|
||||
/* 86 */ this.listenerClass = cls;
|
||||
/* 87 */ this.eventService.setListenerClass(this.listenerClass);
|
||||
/* 88 */ return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void subEvent(EventType eventType, String serviceCode) {
|
||||
/* 99 */ if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
this.eventService.subEvent(eventType, serviceCode);
|
||||
}
|
||||
|
||||
public void subEvent(String topic, String serviceCode, CustomEvent eventClass) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
this.eventService.subEvent(topic, serviceCode, eventClass);
|
||||
}
|
||||
|
||||
|
||||
public void subEvent(EventType eventType, String serviceCode, EventSingleSubEventListener eventSingleSubEventListener) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
if (!eventSingleSubEventListener.returnClass().isAssignableFrom(eventType.getEventClass().getClass())) {
|
||||
throw new EventNotMathEventTypeException("订阅类型与实现类不一致");
|
||||
}
|
||||
|
||||
this.eventService.subEvent(eventType, serviceCode, eventSingleSubEventListener);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void pubEvent(BaseEvent event) {
|
||||
if (!this.init) {
|
||||
throw new ClientNotInitException("client not init");
|
||||
}
|
||||
this.eventService.pubEvent(event);
|
||||
}
|
||||
|
||||
private EventClient(String bootstrapServers, String groupId) {
|
||||
this.bootstrapServers = bootstrapServers;
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
private EventClient(String bootstrapServers) {
|
||||
this.bootstrapServers = bootstrapServers;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+22
-32
@@ -2,42 +2,32 @@ package cn.cloudwalk.cwos.client.event.config;
|
||||
|
||||
import cn.cloudwalk.cwos.client.event.properties.KafkaProperties;
|
||||
|
||||
public class Config {
|
||||
private String messageStore = "KAFKA_STORE";
|
||||
private KafkaProperties kafkaProperties = new KafkaProperties();
|
||||
|
||||
public static Config getInstance(String bootstrapServers, String groupId) {
|
||||
return new Config(bootstrapServers, groupId);
|
||||
}
|
||||
|
||||
private Config(String bootstrapServers, String groupId) {
|
||||
this.kafkaProperties.setBootstrapServers(bootstrapServers);
|
||||
this.kafkaProperties.setGroupId(groupId);
|
||||
}
|
||||
|
||||
public String getMessageStore() {
|
||||
return this.messageStore;
|
||||
}
|
||||
|
||||
public void setMessageStore(String messageStore) {
|
||||
this.messageStore = messageStore;
|
||||
}
|
||||
|
||||
public KafkaProperties getKafkaProperties() {
|
||||
return this.kafkaProperties;
|
||||
}
|
||||
|
||||
|
||||
public class Config
|
||||
{
|
||||
private String messageStore = "KAFKA_STORE";
|
||||
private KafkaProperties kafkaProperties = new KafkaProperties();
|
||||
|
||||
public static Config getInstance(String bootstrapServers, String groupId) {
|
||||
return new Config(bootstrapServers, groupId);
|
||||
public void setKafkaProperties(KafkaProperties kafkaProperties) {
|
||||
this.kafkaProperties = kafkaProperties;
|
||||
}
|
||||
}
|
||||
|
||||
private Config(String bootstrapServers, String groupId) {
|
||||
this.kafkaProperties.setBootstrapServers(bootstrapServers);
|
||||
this.kafkaProperties.setGroupId(groupId);
|
||||
}
|
||||
|
||||
public String getMessageStore() {
|
||||
return this.messageStore;
|
||||
}
|
||||
|
||||
public void setMessageStore(String messageStore) {
|
||||
this.messageStore = messageStore;
|
||||
}
|
||||
|
||||
public KafkaProperties getKafkaProperties() {
|
||||
return this.kafkaProperties;
|
||||
}
|
||||
|
||||
public void setKafkaProperties(KafkaProperties kafkaProperties) {
|
||||
this.kafkaProperties = kafkaProperties;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
package cn.cloudwalk.cwos.client.event.config;
|
||||
|
||||
public class Constants {
|
||||
public static final String KAFKA_MESSAGE_STORE = "KAFKA_STORE";
|
||||
public static final String KAFKA_MESSAGE_STORE = "KAFKA_STORE";
|
||||
}
|
||||
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user