Home / Class/ BootArchiveSupport Class — spring-boot Architecture

BootArchiveSupport Class — spring-boot Architecture

Architecture documentation for the BootArchiveSupport class in BootArchiveSupport.java from the spring-boot codebase.

Entity Profile

Source Code

build-plugin/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java lines 52–223

class BootArchiveSupport {

	private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };

	private static final String UNSPECIFIED_VERSION = "unspecified";

	private static final Set<String> DEFAULT_LAUNCHER_CLASSES;

	static {
		Set<String> defaultLauncherClasses = new HashSet<>();
		defaultLauncherClasses.add("org.springframework.boot.loader.launch.JarLauncher");
		defaultLauncherClasses.add("org.springframework.boot.loader.launch.PropertiesLauncher");
		defaultLauncherClasses.add("org.springframework.boot.loader.launch.WarLauncher");
		DEFAULT_LAUNCHER_CLASSES = Collections.unmodifiableSet(defaultLauncherClasses);
	}

	private final PatternSet requiresUnpack = new PatternSet();

	private final PatternSet exclusions = new PatternSet();

	private final String loaderMainClass;

	private final Spec<FileCopyDetails> librarySpec;

	private final Function<FileCopyDetails, ZipCompression> compressionResolver;

	BootArchiveSupport(String loaderMainClass, Spec<FileCopyDetails> librarySpec,
			Function<FileCopyDetails, ZipCompression> compressionResolver) {
		this.loaderMainClass = loaderMainClass;
		this.librarySpec = librarySpec;
		this.compressionResolver = compressionResolver;
		this.requiresUnpack.include(Specs.satisfyNone());
	}

	void configureManifest(Manifest manifest, String mainClass, String classes, String lib,
			@Nullable String classPathIndex, @Nullable String layersIndex, String jdkVersion,
			String implementationTitle, @Nullable Object implementationVersion) {
		Attributes attributes = manifest.getAttributes();
		attributes.putIfAbsent("Main-Class", this.loaderMainClass);
		attributes.putIfAbsent("Start-Class", mainClass);
		attributes.computeIfAbsent("Spring-Boot-Version", (name) -> determineSpringBootVersion());
		attributes.putIfAbsent("Spring-Boot-Classes", classes);
		attributes.putIfAbsent("Spring-Boot-Lib", lib);
		if (classPathIndex != null) {
			attributes.putIfAbsent("Spring-Boot-Classpath-Index", classPathIndex);
		}
		if (layersIndex != null) {
			attributes.putIfAbsent("Spring-Boot-Layers-Index", layersIndex);
		}
		attributes.putIfAbsent("Build-Jdk-Spec", jdkVersion);
		attributes.putIfAbsent("Implementation-Title", implementationTitle);
		if (implementationVersion != null) {
			String versionString = implementationVersion.toString();
			if (!UNSPECIFIED_VERSION.equals(versionString)) {
				attributes.putIfAbsent("Implementation-Version", versionString);
			}
		}
	}

	private String determineSpringBootVersion() {
		String version = getClass().getPackage().getImplementationVersion();
		return (version != null) ? version : "unknown";
	}

	CopyAction createCopyAction(Jar jar, ResolvedDependencies resolvedDependencies) {
		return createCopyAction(jar, resolvedDependencies, null, null);
	}

	CopyAction createCopyAction(Jar jar, ResolvedDependencies resolvedDependencies,
			@Nullable LayerResolver layerResolver, @Nullable String jarmodeToolsLocation) {
		File output = jar.getArchiveFile().get().getAsFile();
		Manifest manifest = jar.getManifest();
		boolean preserveFileTimestamps = jar.isPreserveFileTimestamps();
		Integer dirPermissions = getUnixNumericDirPermissions(jar);
		Integer filePermissions = getUnixNumericFilePermissions(jar);
		boolean includeDefaultLoader = isUsingDefaultLoader(jar);
		Spec<FileTreeElement> requiresUnpack = this.requiresUnpack.getAsSpec();
		Spec<FileTreeElement> exclusions = this.exclusions.getAsExcludeSpec();
		Spec<FileCopyDetails> librarySpec = this.librarySpec;
		Function<FileCopyDetails, ZipCompression> compressionResolver = this.compressionResolver;
		String encoding = jar.getMetadataCharset();
		CopyAction action = new BootZipCopyAction(output, manifest, preserveFileTimestamps, dirPermissions,
				filePermissions, includeDefaultLoader, jarmodeToolsLocation, requiresUnpack, exclusions, librarySpec,
				compressionResolver, encoding, resolvedDependencies, layerResolver);
		return action;
	}

	private @Nullable Integer getUnixNumericDirPermissions(CopySpec copySpec) {
		return (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0)
				? asUnixNumeric(copySpec.getDirPermissions()) : getDirMode(copySpec);
	}

	private @Nullable Integer getUnixNumericFilePermissions(CopySpec copySpec) {
		return (GradleVersion.current().compareTo(GradleVersion.version("8.3")) >= 0)
				? asUnixNumeric(copySpec.getFilePermissions()) : getFileMode(copySpec);
	}

	private @Nullable Integer asUnixNumeric(Property<ConfigurableFilePermissions> permissions) {
		return permissions.isPresent() ? permissions.get().toUnixNumeric() : null;
	}

	private @Nullable Integer getDirMode(CopySpec copySpec) {
		try {
			return (Integer) copySpec.getClass().getMethod("getDirMode").invoke(copySpec);
		}
		catch (Exception ex) {
			throw new RuntimeException("Failed to get dir mode from CopySpec", ex);
		}
	}

	private @Nullable Integer getFileMode(CopySpec copySpec) {
		try {
			return (Integer) copySpec.getClass().getMethod("getFileMode").invoke(copySpec);
		}
		catch (Exception ex) {
			throw new RuntimeException("Failed to get file mode from CopySpec", ex);
		}
	}

	private boolean isUsingDefaultLoader(Jar jar) {
		return DEFAULT_LAUNCHER_CLASSES.contains(jar.getManifest().getAttributes().get("Main-Class"));
	}

	void requiresUnpack(String... patterns) {
		this.requiresUnpack.include(patterns);
	}

	void requiresUnpack(Spec<FileTreeElement> spec) {
		this.requiresUnpack.include(spec);
	}

	void excludeNonZipLibraryFiles(FileCopyDetails details) {
		if (this.librarySpec.isSatisfiedBy(details)) {
			excludeNonZipFiles(details);
		}
	}

	void excludeNonZipFiles(FileCopyDetails details) {
		if (!isZip(details.getFile())) {
			details.exclude();
		}
	}

	private boolean isZip(File file) {
		try {
			try (FileInputStream fileInputStream = new FileInputStream(file)) {
				return isZip(fileInputStream);
			}
		}
		catch (IOException ex) {
			return false;
		}
	}

	private boolean isZip(InputStream inputStream) throws IOException {
		for (byte headerByte : ZIP_FILE_HEADER) {
			if (inputStream.read() != headerByte) {
				return false;
			}
		}
		return true;
	}

	void moveModuleInfoToRoot(CopySpec spec) {
		spec.filesMatching("module-info.class", this::moveToRoot);
	}

	void moveToRoot(FileCopyDetails details) {
		details.setRelativePath(details.getRelativeSourcePath());
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free