Skip to content

Commit 6137616

Browse files
authoredAug 10, 2021
Define constants instead of using String literals (#3402)
* Define a constant instead of using String literals
1 parent f20be3d commit 6137616

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed
 

‎jib-cli/src/main/java/com/google/cloud/tools/jib/cli/jar/SpringBootExplodedProcessor.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
public class SpringBootExplodedProcessor implements ArtifactProcessor {
4444

45+
private static final String BOOT_INF = "BOOT-INF";
46+
4547
private final Path jarPath;
4648
private final Path targetExplodedJarRoot;
4749
private final Integer jarJavaVersion;
@@ -69,7 +71,7 @@ public List<FileEntriesLayer> createLayers() throws IOException {
6971

7072
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
7173
ZipUtil.unzip(jarPath, targetExplodedJarRoot, true);
72-
ZipEntry layerIndex = jarFile.getEntry("BOOT-INF/layers.idx");
74+
ZipEntry layerIndex = jarFile.getEntry(BOOT_INF + "/layers.idx");
7375
if (layerIndex != null) {
7476
return createLayersForLayeredSpringBootJar(targetExplodedJarRoot);
7577
}
@@ -78,7 +80,7 @@ public List<FileEntriesLayer> createLayers() throws IOException {
7880

7981
// Non-snapshot layer
8082
Predicate<Path> isInBootInfLib =
81-
path -> path.startsWith(targetExplodedJarRoot.resolve("BOOT-INF").resolve("lib"));
83+
path -> path.startsWith(targetExplodedJarRoot.resolve(BOOT_INF).resolve("lib"));
8284
Predicate<Path> isSnapshot = path -> path.getFileName().toString().contains("SNAPSHOT");
8385
Predicate<Path> isInBootInfLibAndIsNotSnapshot = isInBootInfLib.and(isSnapshot.negate());
8486
Predicate<Path> nonSnapshotPredicate = isFile.and(isInBootInfLibAndIsNotSnapshot);
@@ -109,7 +111,7 @@ public List<FileEntriesLayer> createLayers() throws IOException {
109111
// Classes layer.
110112
Predicate<Path> isClass = path -> path.getFileName().toString().endsWith(".class");
111113
Predicate<Path> isInBootInfClasses =
112-
path -> path.startsWith(targetExplodedJarRoot.resolve("BOOT-INF").resolve("classes"));
114+
path -> path.startsWith(targetExplodedJarRoot.resolve(BOOT_INF).resolve("classes"));
113115
Predicate<Path> classesPredicate = isInBootInfClasses.and(isClass);
114116
FileEntriesLayer classesLayer =
115117
ArtifactLayers.getDirectoryContentsAsLayer(
@@ -158,7 +160,7 @@ public Integer getJavaVersion() {
158160
*/
159161
private static List<FileEntriesLayer> createLayersForLayeredSpringBootJar(
160162
Path localExplodedJarRoot) throws IOException {
161-
Path layerIndexPath = localExplodedJarRoot.resolve("BOOT-INF").resolve("layers.idx");
163+
Path layerIndexPath = localExplodedJarRoot.resolve(BOOT_INF).resolve("layers.idx");
162164
Pattern layerNamePattern = Pattern.compile("- \"(.*)\":");
163165
Pattern layerEntryPattern = Pattern.compile(" - \"(.*)\"");
164166
Map<String, List<String>> layersMap = new LinkedHashMap<>();

‎jib-core/src/main/java/com/google/cloud/tools/jib/api/ImageReference.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class ImageReference {
3838
private static final String DOCKER_HUB_REGISTRY = "registry-1.docker.io";
3939
private static final String DEFAULT_TAG = "latest";
4040
private static final String LIBRARY_REPOSITORY_PREFIX = "library/";
41+
private static final String SCRATCH = "scratch";
4142

4243
/**
4344
* Matches all sequences of alphanumeric characters possibly separated by any number of dashes in
@@ -94,7 +95,7 @@ public class ImageReference {
9495
* @throws InvalidImageReferenceException if {@code reference} is formatted incorrectly
9596
*/
9697
public static ImageReference parse(String reference) throws InvalidImageReferenceException {
97-
if (reference.equals("scratch")) {
98+
if (reference.equals(SCRATCH)) {
9899
return ImageReference.scratch();
99100
}
100101

@@ -204,7 +205,7 @@ public static ImageReference of(
204205
* to "scratch"
205206
*/
206207
public static ImageReference scratch() {
207-
return new ImageReference("", "scratch", null, null);
208+
return new ImageReference("", SCRATCH, null, null);
208209
}
209210

210211
/**
@@ -270,9 +271,7 @@ public static boolean isDefaultTag(@Nullable String tag) {
270271
private ImageReference(
271272
String registry, String repository, @Nullable String tag, @Nullable String digest) {
272273
Preconditions.checkArgument(
273-
"scratch".equals(repository)
274-
|| !Strings.isNullOrEmpty(tag)
275-
|| !Strings.isNullOrEmpty(digest),
274+
SCRATCH.equals(repository) || !Strings.isNullOrEmpty(tag) || !Strings.isNullOrEmpty(digest),
276275
"Either tag or digest needs to be set.");
277276
this.registry = RegistryAliasGroup.getHost(registry);
278277
this.repository = repository;
@@ -345,7 +344,7 @@ public boolean usesDefaultTag() {
345344
*/
346345
public boolean isScratch() {
347346
return "".equals(registry)
348-
&& "scratch".equals(repository)
347+
&& SCRATCH.equals(repository)
349348
&& Strings.isNullOrEmpty(tag)
350349
&& Strings.isNullOrEmpty(digest);
351350
}
@@ -395,7 +394,7 @@ public String toStringWithQualifier() {
395394
*/
396395
private String toString(boolean singleQualifier) {
397396
if (isScratch()) {
398-
return "scratch";
397+
return SCRATCH;
399398
}
400399

401400
StringBuilder referenceString = new StringBuilder();

‎jib-core/src/main/java/com/google/cloud/tools/jib/image/ImageTarball.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class ImageTarball {
4949
/** Time that entry is set in the tar. */
5050
private static final Instant TAR_ENTRY_MODIFICATION_TIME = Instant.EPOCH;
5151

52+
private static final String BLOB_PREFIX = "blobs/sha256/";
53+
5254
private final Image image;
5355
private final ImageReference imageReference;
5456
private final ImmutableSet<String> allTargetImageTags;
@@ -93,7 +95,7 @@ private void ociWriteTo(OutputStream out) throws IOException {
9395
long size = layer.getBlobDescriptor().getSize();
9496

9597
tarStreamBuilder.addBlobEntry(
96-
layer.getBlob(), size, "blobs/sha256/" + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
98+
layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
9799
manifest.addLayer(size, digest);
98100
}
99101

@@ -104,14 +106,14 @@ private void ociWriteTo(OutputStream out) throws IOException {
104106
manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
105107
tarStreamBuilder.addByteEntry(
106108
JsonTemplateMapper.toByteArray(containerConfiguration),
107-
"blobs/sha256/" + configDescriptor.getDigest().getHash(),
109+
BLOB_PREFIX + configDescriptor.getDigest().getHash(),
108110
TAR_ENTRY_MODIFICATION_TIME);
109111

110112
// Adds the manifest to the tarball
111113
BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
112114
tarStreamBuilder.addByteEntry(
113115
JsonTemplateMapper.toByteArray(manifest),
114-
"blobs/sha256/" + manifestDescriptor.getDigest().getHash(),
116+
BLOB_PREFIX + manifestDescriptor.getDigest().getHash(),
115117
TAR_ENTRY_MODIFICATION_TIME);
116118

117119
// Adds the oci-layout and index.json

‎jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/JavaContainerBuilderHelper.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
/** Helper for constructing {@link JavaContainerBuilder}-based {@link JibContainerBuilder}s. */
4343
public class JavaContainerBuilderHelper {
4444

45+
private static final String GLOB_PREFIX = "glob:";
46+
4547
/**
4648
* Returns a {@link FileEntriesLayer} for adding the extra directory to the container.
4749
*
@@ -67,21 +69,21 @@ public static FileEntriesLayer extraDirectoryLayerConfiguration(
6769
Map<PathMatcher, FilePermissions> permissionsPathMatchers = new LinkedHashMap<>();
6870
for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
6971
permissionsPathMatchers.put(
70-
FileSystems.getDefault().getPathMatcher("glob:" + entry.getKey()), entry.getValue());
72+
FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + entry.getKey()), entry.getValue());
7173
}
7274

7375
DirectoryWalker walker = new DirectoryWalker(sourceDirectory).filterRoot();
7476
// add exclusion filters
7577
excludes
7678
.stream()
77-
.map(pattern -> FileSystems.getDefault().getPathMatcher("glob:" + pattern))
79+
.map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern))
7880
.forEach(
7981
pathMatcher ->
8082
walker.filter(path -> !pathMatcher.matches(sourceDirectory.relativize(path))));
8183
// add an inclusion filter
8284
includes
8385
.stream()
84-
.map(pattern -> FileSystems.getDefault().getPathMatcher("glob:" + pattern))
86+
.map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern))
8587
.map(
8688
pathMatcher ->
8789
(Predicate<Path>) path -> pathMatcher.matches(sourceDirectory.relativize(path)))

0 commit comments

Comments
 (0)
Please sign in to comment.