This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract away FileIdGenerator implementations
- Loading branch information
Showing
5 changed files
with
113 additions
and
29 deletions.
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
82 changes: 82 additions & 0 deletions
82
...a-core/src/main/java/io/github/qubitpi/athena/file/identifier/FileIdGeneratorFactory.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,82 @@ | ||
/* | ||
* Copyright 2024 Jiaqi Liu | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.qubitpi.athena.file.identifier; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.github.qubitpi.athena.application.AbstractBinderFactory; | ||
import io.github.qubitpi.athena.config.SystemConfig; | ||
import io.github.qubitpi.athena.config.SystemConfigFactory; | ||
import jakarta.validation.constraints.NotNull; | ||
import net.jcip.annotations.Immutable; | ||
import net.jcip.annotations.ThreadSafe; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** | ||
* {@link FileIdGeneratorFactory} is an abstraction layer that hides the details of {@link FileIdGenerator} instance | ||
* creation. | ||
* <p> | ||
* Please use {@link #getInstance()} always to get instance of {@link FileIdGenerator} whenever needed. | ||
*/ | ||
@Immutable | ||
@ThreadSafe | ||
public class FileIdGeneratorFactory { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AbstractBinderFactory.class); | ||
|
||
private static final SystemConfig SYSTEM_CONFIG = SystemConfigFactory.getInstance(); | ||
|
||
private static final String FILE_ID_HASHING_ALGORITHM_KEY = "file_id_hashing_algorithm"; | ||
private static final String FILE_ID_HASHING_ALGORITHM_DEFAULT = "MD5"; | ||
|
||
private static final String FILE_ID_HASHING_ALGORITHM = SYSTEM_CONFIG.getStringProperty( | ||
SYSTEM_CONFIG.getPackageVariableName(FILE_ID_HASHING_ALGORITHM_KEY) | ||
).orElse(FILE_ID_HASHING_ALGORITHM_DEFAULT); | ||
|
||
private static FileIdGenerator instance = null; | ||
|
||
/** | ||
* Returns a cached and fully initialized {@link FileIdGenerator} object with the "MD5" as the default hashing | ||
* algorithm. | ||
* <p> | ||
* One can override the hashing algorithm by setting a {@link SystemConfig config property} whose key is | ||
* {@code file_id_hashing_algorithm}. | ||
* | ||
* @return the same instance | ||
* | ||
* @throws IllegalStateException if the particular cryptographic algorithm is requested but is not available in the | ||
* environment. | ||
*/ | ||
@NotNull | ||
public static FileIdGenerator getInstance() { | ||
if (instance == null) { | ||
try { | ||
instance = FileNameAndUploadedTimeBasedIdGenerator.algorithm(FILE_ID_HASHING_ALGORITHM); | ||
} catch (final NoSuchAlgorithmException exception) { | ||
final String message = String.format( | ||
"'%s' is not a valid message digest algorithm name", | ||
FILE_ID_HASHING_ALGORITHM | ||
); | ||
LOG.error(message, exception); | ||
throw new IllegalStateException(message, exception); | ||
} | ||
} | ||
|
||
return instance; | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...rc/test/groovy/io/github/qubitpi/athena/file/identifier/FileIdGeneratorFactorySpec.groovy
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,26 @@ | ||
/* | ||
* Copyright Jiaqi Liu | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.qubitpi.athena.file.identifier | ||
|
||
import spock.lang.Specification | ||
|
||
class FileIdGeneratorFactorySpec extends Specification { | ||
|
||
def "Factory produces singleton"() { | ||
expect: | ||
FileIdGeneratorFactory.getInstance() is FileIdGeneratorFactory.getInstance() | ||
} | ||
} |