New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added parameterless ctor to the Serializers to allow load them from hazelcast standard config file. #453
Merged
Merged
Added parameterless ctor to the Serializers to allow load them from hazelcast standard config file. #453
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b731046
Added parameterless ctor to the Serializers to allow load them from h…
MonDeveloper 37de396
Changed the value of the static property TYPE_ID_BASE_PROP_NAME accor…
MonDeveloper 2e670ac
Added a chapter into the hazelcast asciidoc page to describe how to p…
MonDeveloper File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../github/bucket4j/grid/hazelcast/serialization/InvalidConfigurationParameterException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.github.bucket4j.grid.hazelcast.serialization; | ||
|
||
/** | ||
* Thrown to indicate that an expected Parameter (both from System Parameters or Environment Variables) | ||
* is invalid. | ||
* | ||
* @author MonDeveloper | ||
*/ | ||
public class InvalidConfigurationParameterException extends RuntimeException { | ||
|
||
/** | ||
* Constructs an <code>IllegalArgumentException</code> with no | ||
* detail message. | ||
*/ | ||
public InvalidConfigurationParameterException() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Constructs an <code>IllegalArgumentException</code> with the | ||
* specified detail message. | ||
* | ||
* @param s the detail message. | ||
*/ | ||
public InvalidConfigurationParameterException(String s) { | ||
super(s); | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified detail message and | ||
* cause. | ||
* | ||
* <p>Note that the detail message associated with <code>cause</code> is | ||
* <i>not</i> automatically incorporated in this exception's detail | ||
* message. | ||
* | ||
* @param message the detail message (which is saved for later retrieval | ||
* by the {@link Throwable#getMessage()} method). | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link Throwable#getCause()} method). (A {@code null} value | ||
* is permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
* @since 1.5 | ||
*/ | ||
public InvalidConfigurationParameterException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified cause and a detail | ||
* message of {@code (cause==null ? null : cause.toString())} (which | ||
* typically contains the class and detail message of {@code cause}). | ||
* This constructor is useful for exceptions that are little more than | ||
* wrappers for other throwables (for example, {@link | ||
* java.security.PrivilegedActionException}). | ||
* | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link Throwable#getCause()} method). (A {@code null} value is | ||
* permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
* @since 1.5 | ||
*/ | ||
public InvalidConfigurationParameterException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
.../github/bucket4j/grid/hazelcast/serialization/MissingConfigurationParameterException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.github.bucket4j.grid.hazelcast.serialization; | ||
|
||
/** | ||
* Thrown to indicate that an expected Parameter (both from System Parameters or Environment Variables) | ||
* is missing. | ||
* | ||
* @author MonDeveloper | ||
*/ | ||
public class MissingConfigurationParameterException extends RuntimeException { | ||
|
||
/** | ||
* Constructs an <code>IllegalArgumentException</code> with no | ||
* detail message. | ||
*/ | ||
public MissingConfigurationParameterException() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Constructs an <code>IllegalArgumentException</code> with the | ||
* specified detail message. | ||
* | ||
* @param s the detail message. | ||
*/ | ||
public MissingConfigurationParameterException(String s) { | ||
super(s); | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified detail message and | ||
* cause. | ||
* | ||
* <p>Note that the detail message associated with <code>cause</code> is | ||
* <i>not</i> automatically incorporated in this exception's detail | ||
* message. | ||
* | ||
* @param message the detail message (which is saved for later retrieval | ||
* by the {@link Throwable#getMessage()} method). | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link Throwable#getCause()} method). (A {@code null} value | ||
* is permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
* @since 1.5 | ||
*/ | ||
public MissingConfigurationParameterException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Constructs a new exception with the specified cause and a detail | ||
* message of {@code (cause==null ? null : cause.toString())} (which | ||
* typically contains the class and detail message of {@code cause}). | ||
* This constructor is useful for exceptions that are little more than | ||
* wrappers for other throwables (for example, {@link | ||
* java.security.PrivilegedActionException}). | ||
* | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link Throwable#getCause()} method). (A {@code null} value is | ||
* permitted, and indicates that the cause is nonexistent or | ||
* unknown.) | ||
* @since 1.5 | ||
*/ | ||
public MissingConfigurationParameterException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...src/main/java/io/github/bucket4j/grid/hazelcast/serialization/SerializationUtilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.bucket4j.grid.hazelcast.serialization; | ||
|
||
import com.hazelcast.internal.util.StringUtil; | ||
import com.hazelcast.nio.serialization.Serializer; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.*; | ||
import java.util.function.Supplier; | ||
|
||
public class SerializationUtilities { | ||
public static final String TYPE_ID_BASE_PROP_NAME = "Bucket4jSerializers_TypeId_Base"; | ||
private static final Map<Class<? extends Serializer>, Integer> serializerTypeIdOffsets = Map.ofEntries( | ||
new AbstractMap.SimpleEntry<Class<? extends Serializer>, Integer>(HazelcastEntryProcessorSerializer.class, 0), | ||
new AbstractMap.SimpleEntry<Class<? extends Serializer>, Integer>(SimpleBackupProcessorSerializer.class, 1), | ||
new AbstractMap.SimpleEntry<Class<? extends Serializer>, Integer>(HazelcastOffloadableEntryProcessorSerializer.class, 2) | ||
); | ||
|
||
public static int getSerializerTypeId(Class<? extends Serializer> serializerType) { | ||
var typeIdBase = getSerializersTypeIdBase(); | ||
|
||
if (typeIdBase.isEmpty()) { | ||
String msg = MessageFormat.format("Missing TypeIdBase number, impossible to load Bucket4j custom serializers. It must be provided in form of Environment Variable or System Property, both using the following key: [{0}]", TYPE_ID_BASE_PROP_NAME); | ||
throw new MissingConfigurationParameterException(msg); | ||
} | ||
|
||
return getSerializerTypeId(serializerType, typeIdBase.get()); | ||
} | ||
|
||
public static int getSerializerTypeId(Class<? extends Serializer> serializerType, int typeIdBase) { | ||
return typeIdBase + SerializationUtilities.getSerializerTypeIdOffset(serializerType); | ||
} | ||
|
||
private static Optional<Integer> getSerializersTypeIdBase() { | ||
return Optional.ofNullable( | ||
getSerializerTypeIdBaseFromSystemProperty() | ||
.orElseGet(() -> getSerializerTypeIdBaseFromEnvironmentVariable() | ||
.orElseGet(() -> null))); | ||
} | ||
|
||
private static int getSerializerTypeIdOffset(Class<? extends Serializer> serializerType) { | ||
if (serializerTypeIdOffsets.containsKey(serializerType)) { | ||
return serializerTypeIdOffsets.get(serializerType); | ||
} else { | ||
String msg = MessageFormat.format("The internal configuration does not include any offset for the serializerType [{0}]", serializerType); | ||
throw new IllegalStateException(msg); | ||
} | ||
} | ||
|
||
private static Optional<Integer> getSerializerTypeIdBaseFromEnvironmentVariable() { | ||
return getPropertyValueFromExternal(() -> System.getenv(SerializationUtilities.TYPE_ID_BASE_PROP_NAME), "Environment Variable"); | ||
vladimir-bukhtoyarov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private static Optional<Integer> getSerializerTypeIdBaseFromSystemProperty() { | ||
return getPropertyValueFromExternal(() -> System.getProperty(SerializationUtilities.TYPE_ID_BASE_PROP_NAME), "System Property"); | ||
} | ||
|
||
private static Optional<Integer> getPropertyValueFromExternal(Supplier<String> typeIdSupplier, String source) { | ||
Optional<Integer> retVal = Optional.empty(); | ||
|
||
String typeIdBaseStr = typeIdSupplier.get(); | ||
if (!StringUtil.isNullOrEmptyAfterTrim(typeIdBaseStr)) { | ||
retVal = parseInteger(typeIdBaseStr); | ||
if (retVal.isEmpty()) { | ||
String msg = MessageFormat.format("The {0} [{1}] has an invalid format. It must be a positive Integer.", source, TYPE_ID_BASE_PROP_NAME); | ||
throw new InvalidConfigurationParameterException(msg); | ||
} | ||
} | ||
|
||
return retVal; | ||
} | ||
|
||
private static Optional<Integer> parseInteger(String strNum) { | ||
Optional<Integer> retVal = Optional.empty(); | ||
if (null != strNum) { | ||
try { | ||
Integer d = Integer.parseInt(strNum.trim()); | ||
retVal = Optional.of(d); | ||
} catch (NumberFormatException nfe) { | ||
retVal = Optional.empty(); | ||
} | ||
} | ||
return retVal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to rename to "bucket4j.hazelcast.serializer.type_id_base"