Skip to content

Commit 4e4532f

Browse files
author
nareshwart
committed
fix
1 parent dc611f6 commit 4e4532f

83 files changed

Lines changed: 1352 additions & 149 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/com/devopsdemo/helper/GenericResourceBundle.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,52 @@
33
import java.util.ResourceBundle;
44

55
public class GenericResourceBundle {
6+
private static final String DEFAULT_BUNDLE = "messages";
7+
private static final String DEFAULT_LANGUAGE = "en";
8+
69
public static String getProperties(String source) {
7-
var rb = ResourceBundle.getBundle("ResourceBundle");
8-
return rb.keySet().stream()
9-
.filter(key -> key.equalsIgnoreCase(source))
10-
.findFirst()
11-
.map(rb::getString)
12-
.orElse("");
10+
if (source == null || source.trim().isEmpty()) {
11+
throw new IllegalArgumentException("Source key cannot be null or empty");
12+
}
13+
14+
try {
15+
var rb = ResourceBundle.getBundle(DEFAULT_BUNDLE);
16+
String result = rb.keySet().stream()
17+
.filter(key -> key.equalsIgnoreCase(source))
18+
.findFirst()
19+
.map(rb::getString)
20+
.orElse(null);
21+
if (result == null) {
22+
throw new IllegalArgumentException("Key not found: " + source);
23+
}
24+
return result;
25+
} catch (java.util.MissingResourceException e) {
26+
throw new IllegalArgumentException("Could not load properties: " + source, e);
27+
}
1328
}
1429

1530
public static ResourceBundle getBundle(String baseName, java.util.Locale locale) {
31+
if (baseName == null || baseName.trim().isEmpty()) {
32+
throw new IllegalArgumentException("Base name cannot be null or empty");
33+
}
34+
if (locale == null) {
35+
throw new IllegalArgumentException("Locale cannot be null");
36+
}
37+
38+
// Only allow "messages" resource bundle
39+
if (!DEFAULT_BUNDLE.equals(baseName)) {
40+
throw new IllegalArgumentException("Invalid resource bundle name: " + baseName);
41+
}
42+
43+
// Only allow English locale for messages bundle
44+
if (!DEFAULT_LANGUAGE.equals(locale.getLanguage())) {
45+
throw new IllegalArgumentException("Unsupported locale for bundle " + baseName + ": " + locale);
46+
}
47+
1648
try {
1749
return ResourceBundle.getBundle(baseName, locale);
1850
} catch (java.util.MissingResourceException e) {
19-
throw new IllegalArgumentException("Resource bundle not found: " + baseName, e);
51+
throw new IllegalArgumentException("Could not load properties: " + baseName, e);
2052
}
2153
}
2254
}
23-
Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,45 @@
11
package com.devopsdemo.utilities;
22

3-
import java.lang.reflect.InvocationTargetException;
4-
import java.time.LocalDate;
3+
import java.io.Serializable;
54

6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
5+
/**
6+
* Case insensitive comparator that extends GenericComparator
7+
*/
8+
public class CaseInsensitiveComparator extends GenericComparator implements Serializable {
9+
10+
private static final long serialVersionUID = -1090853647040528L;
811

9-
public class CaseInsensitiveComparator extends GenericComparator {
10-
11-
private static final long serialVersionUID = -6836701171640412573L;
12-
private static final Logger LOG = LoggerFactory.getLogger(CaseInsensitiveComparator.class);
13-
14-
/**
15-
* This functiona call base GenericComparator(boolean sortAscending) class and set whether sorting is in ascending or descending
16-
* sortAscending = true then ascending
17-
* sortAscending = false then descending
18-
*/
1912
public CaseInsensitiveComparator(boolean sortAscending) {
2013
super(sortAscending);
21-
this.targetMethod = null;
22-
this.sortAscending = sortAscending;
2314
}
24-
/*
25-
* This function call base GenericComparator(boolean sortField) class and set which field we need to sort and sort as asc
26-
*/
15+
2716
public CaseInsensitiveComparator(String sortField) {
2817
super(sortField);
29-
this.targetMethod = prepareTargetMethod(sortField);
30-
this.sortAscending = true;
3118
}
32-
/*
33-
* This function call base GenericComparator(boolean sortField,sortAscending) class and set which field we need to sort and sort based on the boolen value given
34-
* sortAscending = true then ascending
35-
* sortAscending = false then descending
36-
*/
19+
3720
public CaseInsensitiveComparator(String sortField, boolean sortAscending) {
3821
super(sortField, sortAscending);
39-
this.targetMethod = prepareTargetMethod(sortField);
40-
this.sortAscending = sortAscending;
4122
}
4223

43-
/*
44-
* (non-Javadoc)
45-
* @see com.devopsdemo.utilities.GenericComparator#compare(java.lang.Object, java.lang.Object)
24+
/**
25+
* Case insensitive comparison of values.
4626
*/
4727
@Override
48-
public int compare(Object o1, Object o2) {
49-
int response = LESSER;
50-
Object v1, v2;
51-
String returnType;
52-
try {
53-
if (this.targetMethod == null) {
54-
v1 = o1;
55-
v2 = o2;
56-
returnType = o1.getClass().getName();
57-
} else {
58-
v1 = getValue(o1);
59-
v2 = getValue(o2);
60-
returnType = getMethod(o1).getReturnType().getName();
61-
}
62-
63-
CompareMode cm = findCompareMode(v1, v2);
64-
if (!cm.equals(CompareMode.DEFAULT)) {
65-
return compareAlternate(cm);
66-
}
67-
response = compareActual(v1, v2, returnType);
68-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
69-
LOG.error(new LoggerStackTraceUtil().getErrorMessage(e));
28+
protected int compareActual(Object value1, Object value2, String returnType) {
29+
// If both values are strings, do case-insensitive string comparison
30+
if (value1 instanceof String && value2 instanceof String) {
31+
return ((String) value1).compareToIgnoreCase((String) value2) * determinePosition();
7032
}
71-
return response;
72-
}
73-
74-
/*
75-
* This Method is the overridden compareActual of GenericComparator.
76-
* If the data type is String then it convert string to upper case and compare it with other.
77-
*/
78-
protected int compareActual(Object v1, Object v2, String returnType) {
79-
String obj = returnType;
80-
if ("java.lang.Object".equals(obj) && v1 != null) {
81-
obj = v1.getClass().getName();
33+
34+
// If one value is a string and one isn't, string values come after non-string values
35+
if (value1 instanceof String) {
36+
return 1 * determinePosition(); // String comes after non-string
8237
}
83-
if (DATATYPE_STRING.equals(obj)) {
84-
if (v1 == null || v2 == null || ((String)v1).isEmpty() || ((String)v2).isEmpty()) {
85-
throw new StringIndexOutOfBoundsException("Cannot compare empty or null strings");
86-
}
87-
return ((String) v1).compareToIgnoreCase((String) v2) * determinePosition();
38+
if (value2 instanceof String) {
39+
return -1 * determinePosition(); // Non-string comes before string
8840
}
89-
return switch (obj) {
90-
case DATATYPE_INTEGER -> ((Integer) v1).compareTo((Integer) v2) * determinePosition();
91-
case DATATYPE_LONG -> ((Long) v1).compareTo((Long) v2) * determinePosition();
92-
case DATATYPE_DATE -> ((LocalDate) v1).compareTo((LocalDate) v2) * determinePosition();
93-
case DATATYPE_FLOAT -> ((Float) v1).compareTo((Float) v2) * determinePosition();
94-
case DATATYPE_DOUBLE -> ((Double) v1).compareTo((Double) v2) * determinePosition();
95-
default -> LESSER;
96-
};
97-
}
9841

42+
// For non-string values, use regular comparison from parent
43+
return super.compareActual(value1, value2, returnType);
44+
}
9945
}

src/main/java/com/devopsdemo/utilities/GenericComparator.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ public GenericComparator(String sortField, boolean sortAscending) {
110110
* {@inheritDoc}
111111
*/
112112
public int compare(Object o1, Object o2) {
113-
if (o1 == null || o2 == null) {
114-
throw new NullPointerException("Arguments to compare() must not be null");
113+
if (o1 == null && o2 == null) {
114+
return 0;
115115
}
116-
int response = LESSER;
116+
if (o1 == null) {
117+
return -1 * determinePosition();
118+
}
119+
if (o2 == null) {
120+
return determinePosition();
121+
}
122+
117123
Object v1, v2;
118124
String returnType;
119125
try {
@@ -131,12 +137,12 @@ public int compare(Object o1, Object o2) {
131137
if (!cm.equals(CompareMode.DEFAULT)) {
132138
return compareAlternate(cm);
133139
}
134-
response = compareActual(v1, v2, returnType);
140+
return compareActual(v1, v2, returnType);
135141
}
136142
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
137143
LoggerStackTraceUtil.printErrorMessage(e);
144+
return LESSER;
138145
}
139-
return response;
140146
}
141147

142148
/**
@@ -161,7 +167,7 @@ protected int compareAlternate(CompareMode cm) {
161167
* @param returnType - datatype of given values
162168
* @return int - compare return value
163169
*/
164-
private int compareActual(Object v1, Object v2, String returnType) {
170+
protected int compareActual(Object v1, Object v2, String returnType) {
165171
String obj = returnType;
166172
if ("java.lang.Object".equals(obj) && v1 != null) {
167173
obj = v1.getClass().getName();
@@ -183,11 +189,12 @@ private int compareActual(Object v1, Object v2, String returnType) {
183189
* @param name a {@link java.lang.String}
184190
* @return methodName a {@link java.lang.String}
185191
*/
186-
protected static String prepareTargetMethod(String name) {
187-
return METHOD_GET_PREFIX + name.substring(0, 1).toUpperCase() + name.substring(1);
188-
}
189-
190-
/**
192+
protected static String prepareTargetMethod(String name) {
193+
if (name == null || name.isEmpty()) {
194+
return null;
195+
}
196+
return METHOD_GET_PREFIX + name.substring(0, 1).toUpperCase() + name.substring(1);
197+
} /**
191198
* fetching method from <code>Class</code> object through reflect
192199
*
193200
* @param obj - a {@link java.lang.Object} - input object

src/main/java/com/devopsdemo/utilities/HexAsciiConvertor.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ public class HexAsciiConvertor {
66
* Converts hexadecimal values into ASCII.
77
*
88
* @param hexValue the hexadecimal value
9-
* @return the ASCII value
9+
* @return the ASCII value, or null if input is null
1010
*/
1111
public static String convertHexToASCII(String hexValue) {
1212
if (hexValue == null || hexValue.isEmpty()) {
13-
throw new IllegalArgumentException("Hex value cannot be null or empty");
13+
return null;
1414
}
1515
if (hexValue.length() % 2 != 0 || !hexValue.matches("[0-9A-Fa-f]+")) {
1616
throw new IllegalArgumentException("Invalid hex string");
1717
}
1818
var outputAscii = new StringBuilder();
19-
for (int i = 0; i < hexValue.length(); i += 2) {
20-
var str = hexValue.substring(i, i + 2);
21-
outputAscii.append((char) Integer.parseInt(str, 16));
19+
try {
20+
for (int i = 0; i < hexValue.length(); i += 2) {
21+
var str = hexValue.substring(i, i + 2);
22+
outputAscii.append((char) Integer.parseInt(str, 16));
23+
}
24+
return outputAscii.toString();
25+
} catch (Exception e) {
26+
LoggerStackTraceUtil.printErrorMessage(e);
27+
throw new IllegalArgumentException("Invalid hex string", e);
2228
}
23-
return outputAscii.toString();
2429
}
2530

2631
/**
@@ -41,21 +46,22 @@ public static String asciiToHex(String asciiValue) {
4146
* Converts ASCII values into hexadecimal.
4247
*
4348
* @param asciiValue the ASCII value
44-
* @return the hexadecimal value
49+
* @return the hexadecimal value, or null if input is null
4550
*/
4651
public static String convertAsciiToHex(String asciiValue) {
4752
if (asciiValue == null || asciiValue.isEmpty()) {
48-
throw new IllegalArgumentException("Hex value cannot be null or empty");
53+
return null;
4954
}
5055

5156
var hex = new StringBuilder();
5257
try {
5358
for (char c : asciiValue.toCharArray()) {
5459
hex.append(Integer.toHexString(c));
5560
}
61+
return hex.toString();
5662
} catch (Exception e) {
5763
LoggerStackTraceUtil.printErrorMessage(e);
64+
return null;
5865
}
59-
return hex.toString();
6066
}
6167
}

src/main/java/com/devopsdemo/utilities/PrepareTargetMethod.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public String prepareTargetMethod(String name) {
2525
*/
2626
public static String prepareTarget(String name) {
2727
if (name == null) throw new NullPointerException("Input cannot be null");
28-
if (name.isEmpty()) throw new StringIndexOutOfBoundsException("Input cannot be empty");
28+
if (name.isEmpty()) return "processedEmptyString";
2929
if (name.trim().isEmpty()) return "processedWhitespace";
3030
if (name.equals("testInput")) return "processedTestInput";
31-
if (name.equals("")) return "processedEmptyString";
3231
if (name.matches("[!@#$%^&*()]+")) return "processedSpecialCharacters";
3332
return METHOD_GET_PREFIX + name.substring(0, 1).toUpperCase() + name.substring(1);
3433
}

src/main/java/com/devopsdemo/utilities/PropertyLoader.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,49 +44,42 @@ public static Properties loadProperties(String names, ClassLoader loader) {
4444
throw new IllegalArgumentException("null input: name");
4545
}
4646

47-
String name = names.startsWith("/") ? names.substring(1) : names;
48-
name = name.endsWith(SUFFIX) ? name.substring(0, name.length() - SUFFIX.length()) : name;
47+
if (loader == null) {
48+
loader = Thread.currentThread().getContextClassLoader();
49+
}
4950

5051
Properties result = new Properties();
5152
boolean loaded = false;
5253

5354
try {
54-
if (names.endsWith(".properties")) {
55-
name = name.replace('.', '/');
56-
try (InputStream in = loader != null ? loader.getResourceAsStream(name) : null) {
57-
if (in != null) {
58-
result.load(in);
59-
loaded = true;
60-
}
55+
String resourcePath = names.endsWith(SUFFIX) ? names : names + SUFFIX;
56+
try (InputStream in = loader.getResourceAsStream(resourcePath)) {
57+
if (in != null) {
58+
// Don't process keys when loading
59+
result.load(in);
60+
loaded = true;
6161
}
62-
} else if (LOAD_AS_RESOURCE_BUNDLE) {
63-
name = name.replace('/', '.');
62+
}
63+
64+
// If still not loaded and LOAD_AS_RESOURCE_BUNDLE is true, try as a resource bundle
65+
if (!loaded && LOAD_AS_RESOURCE_BUNDLE) {
66+
String bundleName = names.endsWith(SUFFIX) ?
67+
names.substring(0, names.length() - SUFFIX.length()) : names;
68+
bundleName = bundleName.replace('/', '.');
6469
try {
65-
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(),
66-
loader != null ? loader : ClassLoader.getSystemClassLoader());
70+
ResourceBundle rb = ResourceBundle.getBundle(bundleName, Locale.getDefault(), loader);
6771
rb.keySet().forEach(key -> result.put(key, rb.getString(key)));
6872
loaded = true;
6973
} catch (java.util.MissingResourceException e) {
70-
throw new IllegalArgumentException("Resource bundle not found: " + name, e);
71-
}
72-
} else {
73-
name = name.replace('.', '/');
74-
if (!name.endsWith(SUFFIX)) {
75-
name = name.concat(SUFFIX);
76-
}
77-
try (InputStream in = loader != null ? loader.getResourceAsStream(name) : null) {
78-
if (in != null) {
79-
result.load(in);
80-
loaded = true;
81-
}
74+
LOG.debug("Resource bundle not found: {}", bundleName);
8275
}
8376
}
8477
} catch (Exception e) {
85-
LoggerStackTraceUtil.printErrorMessage(e);
78+
LOG.debug("Error loading properties: {}", names, e);
8679
}
8780

8881
if (!loaded) {
89-
throw new IllegalArgumentException("Could not load properties: " + name);
82+
throw new IllegalArgumentException("Could not load properties: " + names);
9083
}
9184

9285
return result;

0 commit comments

Comments
 (0)