Skip to content

Commit 9d52d8c

Browse files
refactor: apply @UtilityClass annotation to utility classes and remove redundant static modifiers (fixes #4254)
- Added Lombok's @UtilityClass annotation to all eligible utility classes across the codebase - Removed explicit static keyword from all member methods and fields, since @UtilityClass promotes them automatically - Removed manual private constructors and abstract/final class qualifiers replaced by @UtilityClass - Affected 31 files across eventmesh-common, eventmesh-runtime, eventmesh-runtime-v2, eventmesh-openconnect, eventmesh-connectors, and eventmesh-admin-server modules
1 parent c3d6548 commit 9d52d8c

31 files changed

Lines changed: 327 additions & 260 deletions

File tree

eventmesh-admin-server/src/main/java/org/apache/eventmesh/admin/server/web/utils/Base64Utils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,31 @@
2525
import java.io.InputStream;
2626
import java.io.OutputStream;
2727

28-
public class Base64Utils {
29-
private static final int CACHE_SIZE = 1024;
28+
import lombok.experimental.UtilityClass;
3029

31-
public Base64Utils() {
32-
}
30+
@UtilityClass
31+
public class Base64Utils {
32+
private final int CACHE_SIZE = 1024;
3333

34-
public static byte[] decode(String base64) throws Exception {
34+
public byte[] decode(String base64) throws Exception {
3535
return Base64.decode(base64.toCharArray());
3636
}
3737

38-
public static String encode(byte[] bytes) throws Exception {
38+
public String encode(byte[] bytes) throws Exception {
3939
return new String(Base64.encode(bytes));
4040
}
4141

42-
public static String encodeFile(String filePath) throws Exception {
42+
public String encodeFile(String filePath) throws Exception {
4343
byte[] bytes = fileToByte(filePath);
4444
return encode(bytes);
4545
}
4646

47-
public static void decodeToFile(String filePath, String base64) throws Exception {
47+
public void decodeToFile(String filePath, String base64) throws Exception {
4848
byte[] bytes = decode(base64);
4949
byteArrayToFile(bytes, filePath);
5050
}
5151

52-
public static byte[] fileToByte(String filePath) throws Exception {
52+
public byte[] fileToByte(String filePath) throws Exception {
5353
byte[] data = new byte[0];
5454
File file = new File(filePath);
5555
if (file.exists()) {
@@ -71,7 +71,7 @@ public static byte[] fileToByte(String filePath) throws Exception {
7171
return data;
7272
}
7373

74-
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
74+
public void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
7575
InputStream in = new ByteArrayInputStream(bytes);
7676
File destFile = new File(filePath);
7777
if (!destFile.getParentFile().exists()) {

eventmesh-admin-server/src/main/java/org/apache/eventmesh/admin/server/web/utils/EncryptUtil.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
import java.io.FileReader;
2323
import java.io.IOException;
2424

25+
import lombok.experimental.UtilityClass;
26+
27+
@UtilityClass
2528
public class EncryptUtil {
26-
public EncryptUtil() {
27-
}
2829

29-
private static byte[] hexStringToBytes(String hexString) {
30+
private byte[] hexStringToBytes(String hexString) {
3031
if (hexString != null && !hexString.equals("")) {
3132
hexString = hexString.toUpperCase();
3233
int length = hexString.length() / 2;
@@ -44,7 +45,7 @@ private static byte[] hexStringToBytes(String hexString) {
4445
}
4546
}
4647

47-
public static String byteToHexString(byte[] b) {
48+
public String byteToHexString(byte[] b) {
4849
String a = "";
4950

5051
for (int i = 0; i < b.length; ++i) {
@@ -59,11 +60,11 @@ public static String byteToHexString(byte[] b) {
5960
return a;
6061
}
6162

62-
private static byte charToByte(char c) {
63+
private byte charToByte(char c) {
6364
return (byte) "0123456789ABCDEF".indexOf(c);
6465
}
6566

66-
private static String readFileContent(String filePath) {
67+
private String readFileContent(String filePath) {
6768
File file = new File(filePath);
6869
BufferedReader reader = null;
6970
StringBuffer key = new StringBuffer();
@@ -100,7 +101,7 @@ private static String readFileContent(String filePath) {
100101
return key.toString();
101102
}
102103

103-
public static String decrypt(String sysPubKeyFile, String appPrivKeyFile, String encStr) throws Exception {
104+
public String decrypt(String sysPubKeyFile, String appPrivKeyFile, String encStr) throws Exception {
104105
String pubKeyBase64 = readFileContent(sysPubKeyFile);
105106
String privKeyBase64 = readFileContent(appPrivKeyFile);
106107
byte[] encBin = hexStringToBytes(encStr);
@@ -109,7 +110,7 @@ public static String decrypt(String sysPubKeyFile, String appPrivKeyFile, String
109110
return new String(privDecBin);
110111
}
111112

112-
public static String decrypt(ParamType pubKeyType, String sysPubKey, ParamType privKeyType, String appPrivKey, ParamType passwdType,
113+
public String decrypt(ParamType pubKeyType, String sysPubKey, ParamType privKeyType, String appPrivKey, ParamType passwdType,
113114
String passwd) throws Exception {
114115
String pubKeyBase64 = pubKeyType == ParamType.FILE ? readFileContent(sysPubKey) : sysPubKey;
115116
String privKeyBase64 = privKeyType == ParamType.FILE ? readFileContent(appPrivKey) : appPrivKey;
@@ -120,15 +121,15 @@ public static String decrypt(ParamType pubKeyType, String sysPubKey, ParamType p
120121
return new String(privDecBin);
121122
}
122123

123-
public static String encrypt(String appPubKeyFile, String sysPrivKeyFile, String passwd) throws Exception {
124+
public String encrypt(String appPubKeyFile, String sysPrivKeyFile, String passwd) throws Exception {
124125
String pubKeyBase64 = readFileContent(appPubKeyFile);
125126
String privKeyBase64 = readFileContent(sysPrivKeyFile);
126127
byte[] pubEncBin = RSAUtils.encryptByPublicKeyBlock(passwd.getBytes(), pubKeyBase64);
127128
byte[] privEncBin = RSAUtils.encryptByPrivateKeyBlock(pubEncBin, privKeyBase64);
128129
return byteToHexString(privEncBin);
129130
}
130131

131-
public static String encrypt(ParamType pubKeyType, String appPubKey, ParamType privKeyType, String sysPrivKey, String passwd) throws Exception {
132+
public String encrypt(ParamType pubKeyType, String appPubKey, ParamType privKeyType, String sysPrivKey, String passwd) throws Exception {
132133
String pubKeyBase64 = pubKeyType == ParamType.FILE ? readFileContent(appPubKey) : appPubKey;
133134
String privKeyBase64 = privKeyType == ParamType.FILE ? readFileContent(sysPrivKey) : sysPrivKey;
134135
byte[] pubEncBin = RSAUtils.encryptByPublicKeyBlock(passwd.getBytes(), pubKeyBase64);

eventmesh-admin-server/src/main/java/org/apache/eventmesh/admin/server/web/utils/JdbcUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
import com.alibaba.druid.pool.DruidDataSource;
2121

22+
import lombok.experimental.UtilityClass;
23+
24+
@UtilityClass
2225
public class JdbcUtils {
2326

24-
public static DruidDataSource createDruidDataSource(String url, String userName, String passWord) {
27+
public DruidDataSource createDruidDataSource(String url, String userName, String passWord) {
2528
DruidDataSource dataSource = new DruidDataSource();
2629
dataSource.setUrl(url);
2730
dataSource.setUsername(userName);

eventmesh-admin-server/src/main/java/org/apache/eventmesh/admin/server/web/utils/RSAUtils.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@
3434

3535
import javax.crypto.Cipher;
3636

37+
import lombok.experimental.UtilityClass;
38+
39+
@UtilityClass
3740
public class RSAUtils {
38-
public static final String KEY_ALGORITHM = "RSA";
39-
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
40-
private static final String PUBLIC_KEY = "RSAPublicKey";
41-
private static final String PRIVATE_KEY = "RSAPrivateKey";
42-
private static final int MAX_ENCRYPT_BLOCK = 117;
43-
private static final int MAX_DECRYPT_BLOCK = 128;
44-
45-
public RSAUtils() {
46-
}
41+
public final String KEY_ALGORITHM = "RSA";
42+
public final String SIGNATURE_ALGORITHM = "MD5withRSA";
43+
private final String PUBLIC_KEY = "RSAPublicKey";
44+
private final String PRIVATE_KEY = "RSAPrivateKey";
45+
private final int MAX_ENCRYPT_BLOCK = 117;
46+
private final int MAX_DECRYPT_BLOCK = 128;
47+
4748

48-
public static Map<String, Object> genKeyPair() throws Exception {
49+
public Map<String, Object> genKeyPair() throws Exception {
4950
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
5051
keyPairGen.initialize(1024);
5152
KeyPair keyPair = keyPairGen.generateKeyPair();
@@ -57,7 +58,7 @@ public static Map<String, Object> genKeyPair() throws Exception {
5758
return keyMap;
5859
}
5960

60-
public static String sign(byte[] data, String privateKey) throws Exception {
61+
public String sign(byte[] data, String privateKey) throws Exception {
6162
byte[] keyBytes = Base64Utils.decode(privateKey);
6263
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
6364
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -68,7 +69,7 @@ public static String sign(byte[] data, String privateKey) throws Exception {
6869
return Base64Utils.encode(signature.sign());
6970
}
7071

71-
public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
72+
public boolean verify(byte[] data, String publicKey, String sign) throws Exception {
7273
byte[] keyBytes = Base64Utils.decode(publicKey);
7374
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
7475
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -79,7 +80,7 @@ public static boolean verify(byte[] data, String publicKey, String sign) throws
7980
return signature.verify(Base64Utils.decode(sign));
8081
}
8182

82-
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
83+
public byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
8384
byte[] keyBytes = Base64Utils.decode(privateKey);
8485
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
8586
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -107,7 +108,7 @@ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey
107108
return decryptedData;
108109
}
109110

110-
public static byte[] decryptByPrivateKeyBlock(byte[] encryptedData, String privateKey) throws Exception {
111+
public byte[] decryptByPrivateKeyBlock(byte[] encryptedData, String privateKey) throws Exception {
111112
byte[] keyBytes = Base64Utils.decode(privateKey);
112113
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
113114
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -120,7 +121,7 @@ public static byte[] decryptByPrivateKeyBlock(byte[] encryptedData, String priva
120121
return cache;
121122
}
122123

123-
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
124+
public byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
124125
byte[] keyBytes = Base64Utils.decode(publicKey);
125126
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
126127
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -148,7 +149,7 @@ public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
148149
return decryptedData;
149150
}
150151

151-
public static byte[] decryptByPublicKeyBlock(byte[] encryptedData, String publicKey) throws Exception {
152+
public byte[] decryptByPublicKeyBlock(byte[] encryptedData, String publicKey) throws Exception {
152153
byte[] keyBytes = Base64Utils.decode(publicKey);
153154
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
154155
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -161,7 +162,7 @@ public static byte[] decryptByPublicKeyBlock(byte[] encryptedData, String public
161162
return cache;
162163
}
163164

164-
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
165+
public byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
165166
byte[] keyBytes = Base64Utils.decode(publicKey);
166167
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
167168
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -189,7 +190,7 @@ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Ex
189190
return encryptedData;
190191
}
191192

192-
public static byte[] encryptByPublicKeyBlock(byte[] data, String publicKey) throws Exception {
193+
public byte[] encryptByPublicKeyBlock(byte[] data, String publicKey) throws Exception {
193194
byte[] keyBytes = Base64Utils.decode(publicKey);
194195
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
195196
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -202,7 +203,7 @@ public static byte[] encryptByPublicKeyBlock(byte[] data, String publicKey) thro
202203
return cache;
203204
}
204205

205-
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
206+
public byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
206207
byte[] keyBytes = Base64Utils.decode(privateKey);
207208
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
208209
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -230,7 +231,7 @@ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws
230231
return encryptedData;
231232
}
232233

233-
public static byte[] encryptByPrivateKeyBlock(byte[] data, String privateKey) throws Exception {
234+
public byte[] encryptByPrivateKeyBlock(byte[] data, String privateKey) throws Exception {
234235
byte[] keyBytes = Base64Utils.decode(privateKey);
235236
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
236237
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
@@ -243,12 +244,12 @@ public static byte[] encryptByPrivateKeyBlock(byte[] data, String privateKey) th
243244
return cache;
244245
}
245246

246-
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
247+
public String getPrivateKey(Map<String, Object> keyMap) throws Exception {
247248
Key key = (Key) keyMap.get("RSAPrivateKey");
248249
return Base64Utils.encode(key.getEncoded());
249250
}
250251

251-
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
252+
public String getPublicKey(Map<String, Object> keyMap) throws Exception {
252253
Key key = (Key) keyMap.get("RSAPublicKey");
253254
return Base64Utils.encode(key.getEncoded());
254255
}

0 commit comments

Comments
 (0)