enumType) {
46 | this.enumType = enumType;
47 | }
48 |
49 | /**
50 | * 转换
51 | *
52 | * @param source
53 | * @return
54 | */
55 | public T convert(String source) {
56 | if (StringUtils.isBlank(source)) {
57 | return null;
58 | }
59 | return EnumUtils.getEnum(enumType, source);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/office/ExcelCell.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.office;
2 |
3 | /**
4 | * row中的一列数据
5 | *
6 | * Created by liyd on 7/28/14.
7 | */
8 | public class ExcelCell {
9 |
10 | /** 列值 */
11 | private Object value;
12 |
13 | public ExcelCell() {
14 | }
15 |
16 | public ExcelCell(Object value) {
17 | this.value = value;
18 | }
19 |
20 | /**
21 | * 获取value值的字符串形式
22 | *
23 | * @return
24 | */
25 | public String getStringValue() {
26 | if (this.value != null) {
27 | return value.toString();
28 | }
29 | return "";
30 | }
31 |
32 | public Object getValue() {
33 | return value;
34 | }
35 |
36 | public void setValue(Object value) {
37 | this.value = value;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | if (this.value != null) {
43 | return value.toString();
44 | }
45 | return "null";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/office/ExcelRow.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.office;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.apache.commons.lang3.StringUtils;
7 |
8 | /**
9 | * sheet中的一行数据
10 | *
11 | * Created by liyd on 7/28/14.
12 | */
13 | public class ExcelRow {
14 |
15 | /**
16 | * 行中的列
17 | */
18 | private List cells;
19 |
20 | public ExcelRow() {
21 | cells = new ArrayList();
22 | }
23 |
24 | public List getCells() {
25 | return cells;
26 | }
27 |
28 | public void setCells(Object... values) {
29 | for (Object value : values) {
30 | this.addCell(value);
31 | }
32 | }
33 |
34 | public void setCells(List cells) {
35 | this.cells = cells;
36 | }
37 |
38 | public int getTotalCellsNum() {
39 | return cells.size();
40 | }
41 |
42 | public boolean hasCells() {
43 | return !cells.isEmpty();
44 | }
45 |
46 | public boolean isEmptyRow() {
47 | if (cells.isEmpty()) {
48 | return true;
49 | }
50 | for (ExcelCell excelCell : cells) {
51 | if (excelCell != null && excelCell.getValue() != null && StringUtils.isNotBlank(excelCell.getStringValue())) {
52 | return false;
53 | }
54 | }
55 | return true;
56 | }
57 |
58 | public void addCell(Object value) {
59 | ExcelCell excelCell = new ExcelCell(value);
60 | this.cells.add(excelCell);
61 | }
62 |
63 | public ExcelCell getFirstCell() {
64 | return cells.isEmpty() ? null : cells.iterator().next();
65 | }
66 |
67 | public ExcelCell getCell(int i) {
68 | return cells.isEmpty() ? null : cells.get(i);
69 | }
70 |
71 | public Object getCellValue(int i) {
72 | return cells.isEmpty() ? null : cells.get(i).getValue();
73 | }
74 |
75 | public String getCellStringValue(int i) {
76 | return cells.isEmpty() ? null : cells.get(i).getStringValue();
77 | }
78 |
79 | @Override
80 | public String toString() {
81 |
82 | if (cells == null) {
83 | return null;
84 | }
85 | StringBuilder sb = new StringBuilder();
86 | for (ExcelCell excelCell : cells) {
87 | sb.append(excelCell == null ? "null" : excelCell.toString()).append(" ");
88 | }
89 | return sb.toString();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/office/ExcelStyleCreator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Yolema.com Inc.
3 | * Copyright (c) 2011-2013 All Rights Reserved.
4 | */
5 | package com.dexcoder.commons.office;
6 |
7 | import org.apache.poi.hssf.usermodel.HSSFCell;
8 | import org.apache.poi.hssf.usermodel.HSSFRow;
9 | import org.apache.poi.hssf.usermodel.HSSFSheet;
10 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
11 |
12 | /**
13 | * Excel样式创建方法接口
14 | *
15 | * @author liyd
16 | * @version $Id: ExcelStyleCreate.java, v 0.1 2013-1-17 上午10:43:55 liyd Exp $
17 | */
18 | public interface ExcelStyleCreator {
19 |
20 | /**
21 | * 按指定的标题创建一个sheet
22 | *
23 | * @param workbook 工作薄对象
24 | * @param excelSheet the excel sheet
25 | * @return hSSF sheet
26 | */
27 | HSSFSheet createSheet(HSSFWorkbook workbook, ExcelSheet excelSheet);
28 |
29 | /**
30 | * 生成sheet列标题行
31 | *
32 | * @param workbook 工作薄对象
33 | * @param sheet sheet对象
34 | * @param excelSheet the excel sheet
35 | */
36 | void createTitle(HSSFWorkbook workbook, HSSFSheet sheet, ExcelSheet excelSheet);
37 |
38 | /**
39 | * 创建行
40 | *
41 | * @param workbook 工作薄
42 | * @param sheet sheet对象
43 | * @param excelRow the sheet row
44 | * @param rowIndex 行索引
45 | * @throws Exception
46 | */
47 | void createRow(HSSFWorkbook workbook, HSSFSheet sheet, ExcelRow excelRow, int rowIndex);
48 |
49 | /**
50 | * 创建列
51 | *
52 | * @param workbook 工作薄
53 | * @param sheet sheet对象
54 | * @param row 行对象
55 | * @param value 列值
56 | * @param rowIndex 行索引
57 | * @param cellIndex 列索引
58 | */
59 | void createCell(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row, Object value, int rowIndex, int cellIndex);
60 |
61 | /**
62 | * 创建日期列
63 | *
64 | * @param cell 列对象
65 | * @param value 列的值
66 | */
67 | void createDateCellStyle(HSSFCell cell, Object value);
68 |
69 | /**
70 | * 创建图片列
71 | *
72 | * @param workbook 工作薄
73 | * @param sheet sheet对象
74 | * @param row 行对象
75 | * @param rowIndex 行索引
76 | * @param cellIndex 列索引
77 | * @param value 列的值
78 | */
79 | void createPictureCellStyle(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row, int rowIndex, int cellIndex,
80 | byte[] value);
81 |
82 | /**
83 | * 创建下拉列表列
84 | *
85 | * @param sheet sheet对象
86 | * @param cell cell对象
87 | * @param firstRowIndex 开始行索引
88 | * @param lastRowIndex 结束行索引
89 | * @param firstCellIndex 开始列索引
90 | * @param lastCellIndex 结束列索引
91 | * @param cellValue 列的值
92 | */
93 | void createSelectCellStyle(HSSFSheet sheet, HSSFCell cell, int firstRowIndex, int lastRowIndex, int firstCellIndex,
94 | int lastCellIndex, String[] cellValue);
95 |
96 | /**
97 | * 创建默认格式列
98 | *
99 | * @param cell 列对象
100 | * @param value 列的值
101 | */
102 | void createDefaultCellStyle(HSSFCell cell, Object value);
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/result/RunResult.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.result;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * 执行消息返回结果
8 | *
9 | * User: liyd
10 | * Date: 2/13/14
11 | * Time: 4:34 PM
12 | */
13 | public class RunResult {
14 |
15 | /** is success */
16 | protected boolean success = true;
17 |
18 | /** result code */
19 | protected String code;
20 |
21 | /** error message */
22 | protected List errors;
23 |
24 | /** result message */
25 | protected List messages;
26 |
27 | /**
28 | * Constructor
29 | */
30 | public RunResult() {
31 | }
32 |
33 | /**
34 | * Constructor
35 | *
36 | * @param isSuccess the is success
37 | */
38 | public RunResult(boolean isSuccess) {
39 | this.success = isSuccess;
40 | }
41 |
42 | /**
43 | * Constructor
44 | *
45 | * @param isSuccess the is success
46 | * @param resultCode the result code
47 | */
48 | public RunResult(boolean isSuccess, String resultCode) {
49 | this.success = isSuccess;
50 | this.code = resultCode;
51 | }
52 |
53 | /**
54 | * 添加运行信息
55 | *
56 | * @param message
57 | */
58 | public void addMessage(String message) {
59 | if (this.messages == null) {
60 | messages = new ArrayList();
61 | }
62 | this.messages.add(message);
63 | }
64 |
65 | /**
66 | * 添加错误信息
67 | *
68 | * @param error
69 | */
70 | public void addError(String error) {
71 | if (this.errors == null) {
72 | errors = new ArrayList();
73 | }
74 | this.errors.add(error);
75 | }
76 |
77 | /**
78 | * 获取错误信息字符串,以;号分隔
79 | *
80 | * @return
81 | */
82 | public String getStrErrors() {
83 | if (this.errors == null || this.errors.isEmpty()) {
84 | return null;
85 | }
86 |
87 | StringBuilder sb = new StringBuilder();
88 | for (String error : errors) {
89 | sb.append(error);
90 | sb.append(";");
91 | }
92 | return sb.toString();
93 | }
94 |
95 | /**
96 | * 获取结果信息字符串,以;号分隔
97 | *
98 | * @return
99 | */
100 | public String getStrMessages() {
101 |
102 | if (this.messages == null || this.messages.isEmpty()) {
103 | return null;
104 | }
105 |
106 | StringBuilder sb = new StringBuilder();
107 | for (String resultMessage : messages) {
108 | sb.append(resultMessage);
109 | sb.append(";");
110 | }
111 | return sb.toString();
112 | }
113 |
114 | public boolean isSuccess() {
115 | return success;
116 | }
117 |
118 | public void setSuccess(boolean success) {
119 | this.success = success;
120 | }
121 |
122 | public List getMessages() {
123 | return messages;
124 | }
125 |
126 | public void setMessages(List messages) {
127 | this.messages = messages;
128 | }
129 |
130 | public String getCode() {
131 | return code;
132 | }
133 |
134 | public void setCode(String code) {
135 | this.code = code;
136 | }
137 |
138 | public List getErrors() {
139 | return errors;
140 | }
141 |
142 | public void setErrors(List errors) {
143 | this.errors = errors;
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/EncryptUtils.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.utils;
2 |
3 | import java.math.BigInteger;
4 | import java.security.MessageDigest;
5 |
6 | import javax.crypto.Mac;
7 | import javax.crypto.SecretKey;
8 | import javax.crypto.spec.SecretKeySpec;
9 |
10 | import com.dexcoder.commons.exceptions.CommonsAssistantException;
11 |
12 | /**
13 | * Created by liyd on 7/2/14.
14 | */
15 | public class EncryptUtils {
16 |
17 | /**
18 | * 对字符串md5加密
19 | *
20 | * @param str
21 | * @return
22 | */
23 | public static String getMD5(String str) {
24 |
25 | try {
26 |
27 | // 生成一个MD5加密计算摘要
28 | MessageDigest md = MessageDigest.getInstance("MD5");
29 | // 计算md5函数
30 | md.update(str.getBytes());
31 | // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
32 | // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
33 | String md5 = new BigInteger(1, md.digest()).toString(16);
34 | //当数字以0开头时会去掉0,补齐
35 | if (md5.length() < 32) {
36 | md5 = String.format("%32s", md5).replace(' ', '0');
37 | }
38 | return md5;
39 | } catch (Exception e) {
40 | throw new CommonsAssistantException("MD5加密出现错误", e);
41 | }
42 | }
43 |
44 | /**
45 | * 使用 HMAC-SHA1 签名方法对对encryptText进行签名
46 | * @param encryptText 被签名的字符串
47 | * @param encryptKey 密钥
48 | * @return
49 | * @throws Exception
50 | */
51 | public static byte[] hmacSHA1Encrypt(String encryptText, String encryptKey) {
52 | try {
53 | byte[] data = encryptKey.getBytes();
54 | //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
55 | SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
56 | //生成一个指定 Mac 算法 的 Mac 对象
57 | Mac mac = Mac.getInstance("HmacSHA1");
58 | //用给定密钥初始化 Mac 对象
59 | mac.init(secretKey);
60 |
61 | byte[] text = encryptText.getBytes();
62 | //完成 Mac 操作
63 | return mac.doFinal(text);
64 | } catch (Exception e) {
65 | throw new CommonsAssistantException("HmacSHA1加密出现错误", e);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/EnumUtils.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.utils;
2 |
3 | import com.dexcoder.commons.enums.IEnum;
4 | import com.dexcoder.commons.exceptions.CommonsAssistantException;
5 |
6 | /**
7 | * 枚举辅助类
8 | *
9 | * User: liyd
10 | * Date: 14-1-25
11 | * Time: 上午10:17
12 | */
13 | public final class EnumUtils {
14 |
15 | /**
16 | * 获取枚举的所有属性
17 | *
18 | * @param clazz
19 | * @return
20 | */
21 | public static IEnum[] getEnums(Class> clazz) {
22 | if (IEnum.class.isAssignableFrom(clazz)) {
23 | Object[] enumConstants = clazz.getEnumConstants();
24 | return (IEnum[]) enumConstants;
25 | }
26 | return null;
27 | }
28 |
29 | /**
30 | * 获取枚举的所有属性
31 | *
32 | * @param enumClass
33 | * @return
34 | */
35 | public static IEnum[] getEnums(String enumClass) {
36 | try {
37 | Class> clazz = Thread.currentThread().getContextClassLoader().loadClass(enumClass);
38 | return getEnums(clazz);
39 | } catch (ClassNotFoundException e) {
40 | throw new CommonsAssistantException("加载枚举类失败", e);
41 | }
42 | }
43 |
44 | /**
45 | * 获取枚举的所有属性
46 | *
47 | * @param clazz the clazz
48 | * @param code the code
49 | * @return enum
50 | */
51 | @SuppressWarnings("unchecked")
52 | public static T getEnum(Class clazz, String code) {
53 | if (!IEnum.class.isAssignableFrom(clazz)) {
54 | return null;
55 | }
56 | IEnum[] enumConstants = (IEnum[]) clazz.getEnumConstants();
57 | for (IEnum enumConstant : enumConstants) {
58 | if (enumConstant.getCode().equalsIgnoreCase(code)) {
59 | return (T) enumConstant;
60 | }
61 | }
62 | return null;
63 | }
64 |
65 | /**
66 | * 获取枚举的所有属性
67 | *
68 | * @param clazzName the clazzName
69 | * @param code the code
70 | * @return enum
71 | */
72 | @SuppressWarnings("unchecked")
73 | public static T getEnum(String clazzName, String code) {
74 | Class> clazz = ClassUtils.loadClass(clazzName);
75 | if (!IEnum.class.isAssignableFrom(clazz)) {
76 | return null;
77 | }
78 | IEnum[] enumConstants = (IEnum[]) clazz.getEnumConstants();
79 | for (IEnum enumConstant : enumConstants) {
80 | if (enumConstant.getCode().equalsIgnoreCase(code)) {
81 | return (T) enumConstant;
82 | }
83 | }
84 | return null;
85 | }
86 |
87 | public static String getEnumVal(String clazzName, String code) {
88 | Object anEnum = getEnum(clazzName, code);
89 | return anEnum == null ? code : ((IEnum) anEnum).getDesc();
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/SerializeUtils.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Yolema.com Inc.
3 | * Copyright (c) 2011-2012 All Rights Reserved.
4 | */
5 | package com.dexcoder.commons.utils;
6 |
7 | import java.io.*;
8 |
9 | import org.apache.commons.codec.binary.Base64;
10 | import org.apache.commons.io.IOUtils;
11 | import org.apache.commons.lang3.StringUtils;
12 |
13 | import com.dexcoder.commons.exceptions.CommonsAssistantException;
14 |
15 | /**
16 | * 序列化辅助类
17 | *
18 | * @author liyd
19 | * @version $Id: SerializerUtil.java, v 0.1 2012-8-16 下午4:11:08 liyd Exp $
20 | */
21 | public final class SerializeUtils {
22 |
23 | private SerializeUtils() {
24 | }
25 |
26 | /**
27 | * 将对象序列化成字符串
28 | *
29 | * @param obj
30 | * @return
31 | */
32 | public static String objectToString(Object obj) {
33 |
34 | if (obj == null) {
35 | return null;
36 | }
37 |
38 | ByteArrayOutputStream baops = null;
39 | ObjectOutputStream oos = null;
40 | try {
41 | baops = new ByteArrayOutputStream();
42 | oos = new ObjectOutputStream(baops);
43 | oos.writeObject(obj);
44 |
45 | //产生编码问题,用base64保证完整性
46 | return Base64.encodeBase64String(baops.toByteArray());
47 |
48 | } catch (IOException e) {
49 | throw new CommonsAssistantException("将对象序列化成字符串失败", e);
50 | } finally {
51 | IOUtils.closeQuietly(baops);
52 | IOUtils.closeQuietly(oos);
53 | }
54 | }
55 |
56 | /**
57 | * 将字符串反序列化成对象
58 | *
59 | * @param strObj
60 | * @return
61 | */
62 | public static Object stringToObject(String strObj) {
63 |
64 | if (StringUtils.isBlank(strObj)) {
65 | return null;
66 | }
67 |
68 | ObjectInputStream ois = null;
69 |
70 | try {
71 | byte[] bytes = strObj.getBytes();
72 | ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(Base64.decodeBase64(bytes))));
73 |
74 | Object obj = ois.readObject();
75 | return obj;
76 | } catch (Exception e) {
77 | throw new CommonsAssistantException("将字符串反序列化成对象失败", e);
78 | } finally {
79 | IOUtils.closeQuietly(ois);
80 | }
81 |
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/TextUtils.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.utils;
2 |
3 | import java.io.UnsupportedEncodingException;
4 |
5 | import org.apache.commons.lang3.ArrayUtils;
6 | import org.apache.commons.lang3.StringUtils;
7 |
8 | import com.dexcoder.commons.exceptions.CommonsAssistantException;
9 |
10 | /**
11 | * 字符文本操作
12 | * 太多的StringUtils了,命名为TextUtils
13 | *
14 | * Created by liyd on 2015-8-14.
15 | */
16 | public class TextUtils {
17 |
18 | /**
19 | * 转换特殊符号
20 | *
21 | * @param str
22 | * @return
23 | */
24 | public static String convertHtmlSpecialChars(String str) {
25 | if (StringUtils.isBlank(str)) {
26 | return null;
27 | }
28 | //最后一个中文全角空格换成英文,防止strin的trim方法失效
29 | String[][] chars = new String[][] { { "&", "&" }, { "<", "<" }, { ">", ">" }, { "\"", """ },
30 | { " ", " " } };
31 | return replaceChars(str, chars);
32 | }
33 |
34 | /**
35 | * 反转特殊符号,将转义后的符号转换回标签,以便缩进等格式化
36 | *
37 | * @param str
38 | * @return
39 | */
40 | public static String reverseHtmlSpecialChars(String str) {
41 | if (StringUtils.isBlank(str)) {
42 | return null;
43 | }
44 | String[][] chars = new String[][] { { "&", "&" }, { "<", "<" }, { ">", ">" }, { """, "\"" },
45 | { " ", " " } };
46 | return replaceChars(str, chars);
47 | }
48 |
49 | public static String replaceChars(String str, String[][] chars) {
50 | for (String[] cs : chars) {
51 | str = str.replace(cs[0], cs[1]);
52 | }
53 | return str;
54 | }
55 |
56 | /**
57 | * 截取字符串,按byte长度,可以避免直接按length截取中英文混合显示长短差很多的情况
58 | *
59 | * @param text
60 | * @param length
61 | * @return
62 | */
63 | public static String substringForByte(String text, int length) {
64 |
65 | return substringForByte(text, length, false);
66 | }
67 |
68 | /**
69 | * 截取字符串,按byte长度,可以避免直接按length截取中英文混合显示长短差很多的情况
70 | *
71 | * @param text
72 | * @param length
73 | * @return
74 | */
75 | public static String substringForByte(String text, int length, boolean isConvertSpecialChars) {
76 |
77 | if (StringUtils.isBlank(text) || length < 1) {
78 | return text;
79 | }
80 | //转换特殊字符,页面显示时非常有用
81 | if (isConvertSpecialChars) {
82 | text = convertHtmlSpecialChars(text);
83 | }
84 | try {
85 | //防止中英文有长有短,转换成byte截取
86 | byte[] bytes = text.getBytes("GBK");
87 |
88 | //截取
89 | byte[] contentNameBytes = ArrayUtils.subarray(bytes, 0, length);
90 |
91 | //处理截取了半个汉字的情况
92 | int count = 0;
93 | for (byte b : contentNameBytes) {
94 | if (b < 0) {
95 | count++;
96 | }
97 | }
98 | if (count % 2 != 0) {
99 | contentNameBytes = ArrayUtils.subarray(contentNameBytes, 0, contentNameBytes.length - 1);
100 | }
101 |
102 | String contentName = new String(contentNameBytes, "GBK");
103 |
104 | return contentName;
105 | } catch (UnsupportedEncodingException e) {
106 | throw new CommonsAssistantException("根据byte截取字符串失败", e);
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/ThreadExecutionUtils.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.utils;
2 |
3 | import java.util.concurrent.LinkedBlockingQueue;
4 | import java.util.concurrent.ThreadPoolExecutor;
5 | import java.util.concurrent.TimeUnit;
6 |
7 | /**
8 | * 线程执行工厂类
9 | *
10 | * @author liyd
11 | *
12 | */
13 | public class ThreadExecutionUtils {
14 |
15 | /** 核心线程数 */
16 | private static final int CORE_POOL_SIZE = 1;
17 |
18 | /** 最大线程数 */
19 | private static final int MAXIMUM_POOL_SIZE = 5;
20 |
21 | /** 线程生存时间,单位秒 */
22 | private static final long KEEP_ALIVE_TIME = 10;
23 |
24 | /** 线程池对象 */
25 | private static ThreadPoolExecutor threadPoolExecutor;
26 |
27 | /**
28 | * 添加线程执行任务,采用无界队列。 调用者添加线程任务完成之后,调用shutdown()方法关闭线程池
29 | *
30 | * @param runnable
31 | */
32 | public synchronized static void addTask(Runnable runnable) {
33 |
34 | if (threadPoolExecutor == null || threadPoolExecutor.isShutdown()) {
35 | threadPoolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE,
36 | KEEP_ALIVE_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue(),
37 | new ThreadPoolExecutor.AbortPolicy());
38 | }
39 | threadPoolExecutor.execute(runnable);
40 | // shutdown();
41 | }
42 |
43 | /**
44 | * 关闭线程池
45 | */
46 | public static void shutdown() {
47 | if (threadPoolExecutor == null || threadPoolExecutor.isShutdown()) {
48 | return;
49 | }
50 | threadPoolExecutor.shutdown();
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/UUIDUtils.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.utils;
2 |
3 | import java.util.UUID;
4 |
5 | /**
6 | * Created by liyd on 9/10/14.
7 | */
8 | public class UUIDUtils {
9 |
10 | public static final String[] CHARS = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
11 | "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6",
12 | "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
13 | "S", "T", "U", "V", "W", "X", "Y", "Z" };
14 |
15 | /**
16 | * 生成指定长度的uuid
17 | *
18 | * @param length
19 | * @return
20 | */
21 | private static String getUUID(int length, UUID uuid) {
22 | int groupLength = 32 / length;
23 | StringBuilder sb = new StringBuilder();
24 | String id = uuid.toString().replace("-", "");
25 | for (int i = 0; i < length; i++) {
26 | String str = id.substring(i * groupLength, i * groupLength + groupLength);
27 | int x = Integer.parseInt(str, 16);
28 | sb.append(CHARS[x % 0x3E]);
29 | }
30 | return sb.toString();
31 | }
32 |
33 | /**
34 | * 8位UUID
35 | *
36 | * @return
37 | */
38 | public static String getUUID8() {
39 | return getUUID(8, UUID.randomUUID());
40 | }
41 |
42 | /**
43 | * 8位UUID
44 | *
45 | * @return
46 | */
47 | public static String getUUID8(byte[] bytes) {
48 | return getUUID(8, UUID.nameUUIDFromBytes(bytes));
49 | }
50 |
51 | /**
52 | * 8位UUID
53 | *
54 | * @return
55 | */
56 | public static String getUUID8(String fromString) {
57 | return getUUID(8, UUID.fromString(fromString));
58 | }
59 |
60 | /**
61 | * 16位UUID
62 | *
63 | * @return
64 | */
65 | public static String getUUID16() {
66 | return getUUID(16, UUID.randomUUID());
67 | }
68 |
69 | /**
70 | * 16位UUID
71 | *
72 | * @return
73 | */
74 | public static String getUUID16(String fromString) {
75 | return getUUID(16, UUID.fromString(fromString));
76 | }
77 |
78 | /**
79 | * 16位UUID
80 | *
81 | * @return
82 | */
83 | public static String getUUID16(byte[] bytes) {
84 | return getUUID(16, UUID.nameUUIDFromBytes(bytes));
85 | }
86 |
87 | /**
88 | * 32位UUID
89 | *
90 | * @return
91 | */
92 | public static String getUUID32() {
93 | return UUID.randomUUID().toString().replace("-", "");
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/dexcoder-commons/src/main/java/com/dexcoder/commons/utils/ZipUtils.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.commons.utils;
2 |
3 | import java.io.BufferedOutputStream;
4 | import java.io.File;
5 | import java.io.FileOutputStream;
6 | import java.util.zip.ZipEntry;
7 | import java.util.zip.ZipOutputStream;
8 |
9 | import org.apache.commons.io.FileUtils;
10 | import org.apache.commons.io.IOUtils;
11 |
12 | import com.dexcoder.commons.exceptions.CommonsAssistantException;
13 |
14 | /**
15 | * zip包压缩工具类 jdk原生实现,不支持中文
16 | */
17 | public class ZipUtils {
18 |
19 | /**
20 | * 压缩打包文件成zip包
21 | *
22 | * @param srcFileName
23 | * @param targetFileName
24 | */
25 | public static void zip(String srcFileName, String targetFileName) {
26 |
27 | ZipOutputStream zos = null;
28 | BufferedOutputStream bos = null;
29 | try {
30 | zos = new ZipOutputStream(new FileOutputStream(targetFileName));
31 | bos = new BufferedOutputStream(zos);
32 | File srcFile = new File(srcFileName);
33 | zip(zos, srcFile, srcFile.getName(), bos);
34 | } catch (Exception e) {
35 | throw new CommonsAssistantException("压缩打包文件成zip包失败:" + srcFileName, e);
36 | } finally {
37 | IOUtils.closeQuietly(bos);
38 | IOUtils.closeQuietly(zos);
39 | }
40 | }
41 |
42 | /**
43 | * 打包压缩文件
44 | *
45 | * @param zos
46 | * @param file
47 | * @param base
48 | * @param bos
49 | * @throws Exception
50 | */
51 | private static void zip(ZipOutputStream zos, File file, String base, BufferedOutputStream bos) throws Exception {
52 | if (file.isDirectory()) {
53 | // 创建zip压缩进入点base
54 | zos.putNextEntry(new ZipEntry(base + "/"));
55 | zos.flush();
56 | File[] fl = file.listFiles();
57 | for (int i = 0; i < fl.length; i++) {
58 | // 递归遍历子文件夹
59 | zip(zos, fl[i], base + "/" + fl[i].getName(), bos);
60 | }
61 | } else {
62 | //创建zip压缩进入点base
63 | zos.putNextEntry(new ZipEntry(base));
64 | bos.write(FileUtils.readFileToByteArray(file));
65 | bos.flush();
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/dexcoder-dal-batis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dexcoder-assistant
7 | com.dexcoder
8 | 2.3.5
9 |
10 | 4.0.0
11 | dexcoder-dal-batis
12 | dexcoder-dal-batis
13 |
14 |
15 | com.dexcoder
16 | dexcoder-dal
17 | 2.3.5
18 |
19 |
20 | org.springframework
21 | spring-beans
22 |
23 |
24 | ognl
25 | ognl
26 |
27 |
28 | org.slf4j
29 | slf4j-api
30 |
31 |
32 |
--------------------------------------------------------------------------------
/dexcoder-dal-batis/src/main/java/com/dexcoder/dal/batis/BatisSqlFactory.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.dal.batis;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import org.apache.commons.lang3.ArrayUtils;
7 | import org.apache.commons.lang3.StringUtils;
8 |
9 | import com.dexcoder.dal.BoundSql;
10 | import com.dexcoder.dal.SqlFactory;
11 | import com.dexcoder.dal.batis.build.Configuration;
12 | import com.dexcoder.dal.batis.build.MappedStatement;
13 | import com.dexcoder.dal.exceptions.JdbcAssistantException;
14 |
15 | /**
16 | * Created by liyd on 2015-11-24.
17 | */
18 | public class BatisSqlFactory implements SqlFactory {
19 |
20 | /** 默认参数名 */
21 | private static final String DEFAULT_PARAMETERS_KEY = "parameters";
22 |
23 | private Configuration configuration;
24 |
25 | public BatisSqlFactory(Configuration configuration) {
26 | this.configuration = configuration;
27 | }
28 |
29 | public BoundSql getBoundSql(String refSql, String expectParamKey, Object[] parameters) {
30 |
31 | Map params = this.processParameters(expectParamKey, parameters);
32 | MappedStatement mappedStatement = this.configuration.getMappedStatements().get(refSql);
33 | if (mappedStatement == null) {
34 | throw new JdbcAssistantException("自定义sql没有找到,refSql=" + refSql);
35 | }
36 | return mappedStatement.getSqlSource().getBoundSql(params);
37 | }
38 |
39 | /**
40 | * 处理转换参数
41 | *
42 | * @param expectParamKey
43 | * @param parameters
44 | * @return
45 | */
46 | private Map processParameters(String expectParamKey, Object[] parameters) {
47 |
48 | if (ArrayUtils.isEmpty(parameters)) {
49 | return null;
50 | }
51 | String paramKey = StringUtils.isBlank(expectParamKey) ? DEFAULT_PARAMETERS_KEY : expectParamKey;
52 | Map map = new HashMap();
53 | if (parameters.length == 1) {
54 | map.put(paramKey, parameters[0]);
55 | } else {
56 | map.put(paramKey, parameters);
57 | }
58 | return map;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/dexcoder-dal-batis/src/main/java/com/dexcoder/dal/batis/BatisSqlFactoryBean.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.dal.batis;
2 |
3 | import com.dexcoder.dal.handler.DefaultMappingHandler;
4 | import com.dexcoder.dal.handler.MappingHandler;
5 | import org.apache.commons.lang3.StringUtils;
6 | import org.springframework.beans.factory.FactoryBean;
7 | import org.springframework.beans.factory.InitializingBean;
8 | import org.springframework.core.io.Resource;
9 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
10 | import org.springframework.core.io.support.ResourcePatternResolver;
11 |
12 | import com.dexcoder.dal.batis.build.Configuration;
13 | import com.dexcoder.dal.batis.xml.XMLMapperBuilder;
14 | import com.dexcoder.dal.exceptions.JdbcAssistantException;
15 |
16 | /**
17 | * Created by liyd on 2015-11-24.
18 | */
19 | public class BatisSqlFactoryBean implements FactoryBean, InitializingBean {
20 |
21 | private String sqlLocation;
22 |
23 | private Configuration configuration;
24 |
25 | private MappingHandler mappingHandler;
26 |
27 | private BatisSqlFactory sqlFactory;
28 |
29 | public void afterPropertiesSet() throws Exception {
30 | this.configuration = new Configuration();
31 | String[] sqlLocations = StringUtils.split(this.sqlLocation, ",");
32 | ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
33 | for (String location : sqlLocations) {
34 | try {
35 | Resource[] resources = resourcePatternResolver.getResources(location);
36 | this.readResource(resources);
37 | } catch (Exception e) {
38 | throw new JdbcAssistantException("读取sqlLocation失败:" + location, e);
39 | }
40 | }
41 | }
42 |
43 | /**
44 | * 读取resource
45 | *
46 | * @param resources
47 | */
48 | private void readResource(Resource[] resources) {
49 |
50 | for (Resource resource : resources) {
51 | try {
52 | XMLMapperBuilder mapperParser = new XMLMapperBuilder(resource, this.configuration,getMappingHandler());
53 | mapperParser.parse();
54 | } catch (Exception e) {
55 | throw new JdbcAssistantException("读取resource文件失败:" + resource.getFilename(), e);
56 | }
57 | }
58 | }
59 |
60 | /**
61 | * 获取名称处理器
62 | *
63 | * @return
64 | */
65 | protected MappingHandler getMappingHandler() {
66 |
67 | if (this.mappingHandler == null) {
68 | this.mappingHandler = new DefaultMappingHandler();
69 | }
70 | return this.mappingHandler;
71 | }
72 |
73 | public BatisSqlFactory getObject() throws Exception {
74 | this.sqlFactory = new BatisSqlFactory(this.configuration);
75 | return this.sqlFactory;
76 | }
77 |
78 | public Class> getObjectType() {
79 | return BatisSqlFactory.class;
80 | }
81 |
82 | public boolean isSingleton() {
83 | return true;
84 | }
85 |
86 | public void setMappingHandler(MappingHandler mappingHandler) {
87 | this.mappingHandler = mappingHandler;
88 | }
89 |
90 | public void setSqlLocation(String sqlLocation) {
91 | this.sqlLocation = sqlLocation;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/dexcoder-dal-batis/src/main/java/com/dexcoder/dal/batis/build/BaseBuilder.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.dal.batis.build;
2 |
3 | /**
4 | * Created by liyd on 2015-11-24.
5 | */
6 | public abstract class BaseBuilder {
7 |
8 | protected final Configuration configuration;
9 |
10 | public BaseBuilder(Configuration configuration) {
11 | this.configuration = configuration;
12 | }
13 |
14 | public Configuration getConfiguration() {
15 | return configuration;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/dexcoder-dal-batis/src/main/java/com/dexcoder/dal/batis/build/BatisBoundSql.java:
--------------------------------------------------------------------------------
1 | package com.dexcoder.dal.batis.build;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | import com.dexcoder.dal.BoundSql;
9 | import com.dexcoder.dal.batis.reflection.MetaObject;
10 |
11 | /**
12 | * Created by liyd on 2015-11-25.
13 | */
14 | public class BatisBoundSql implements BoundSql {
15 |
16 | private String sql;
17 | private List parameterMappings;
18 | private Object parameterObject;
19 | private Map additionalParameters;
20 | private MetaObject metaParameters;
21 |
22 | public BatisBoundSql(String sql, List parameterMappings, Object parameterObject) {
23 | this.sql = sql;
24 | this.parameterMappings = parameterMappings;
25 | this.parameterObject = parameterObject;
26 | this.additionalParameters = new HashMap();
27 | this.metaParameters = MetaObject.forObject(additionalParameters);
28 | }
29 |
30 | public List