Skip to content

Commit abc435a

Browse files
committed
Merge Log4j2PluginsCacheFileTransformer
1 parent 8eaba7f commit abc435a

File tree

2 files changed

+69
-201
lines changed

2 files changed

+69
-201
lines changed

src/main/java/Log4j2PluginCacheFileTransformer.kt

-173
This file was deleted.

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.kt

+69-28
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowStats
44
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
55
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
66
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext.Companion.getEntryTimestamp
7-
import java.io.File
87
import java.net.URL
8+
import java.nio.file.Path
99
import java.util.Collections
1010
import java.util.Enumeration
11+
import kotlin.io.path.createTempFile
12+
import kotlin.io.path.deleteIfExists
13+
import kotlin.io.path.outputStream
1114
import org.apache.commons.io.output.CloseShieldOutputStream
1215
import org.apache.logging.log4j.core.config.plugins.processor.PluginCache
16+
import org.apache.logging.log4j.core.config.plugins.processor.PluginEntry
1317
import org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor
1418
import org.apache.tools.zip.ZipEntry
1519
import org.apache.tools.zip.ZipOutputStream
@@ -25,64 +29,101 @@ import org.gradle.api.file.FileTreeElement
2529
*/
2630
@CacheableTransformer
2731
public open class Log4j2PluginsCacheFileTransformer : Transformer {
28-
private val temporaryFiles = mutableListOf<File>()
29-
private val relocators = mutableListOf<Relocator>()
32+
/**
33+
* Log4j config files to share across the transformation stages.
34+
*/
35+
private val tempFiles = mutableListOf<Path>()
36+
37+
/**
38+
* [Relocator] instances to share across the transformation stages.
39+
*/
40+
private val tempRelocators = mutableListOf<Relocator>()
3041
private var stats: ShadowStats? = null
3142

3243
override fun canTransformResource(element: FileTreeElement): Boolean {
3344
return PluginProcessor.PLUGIN_CACHE_FILE == element.name
3445
}
3546

3647
override fun transform(context: TransformerContext) {
37-
val temporaryFile = File.createTempFile("Log4j2Plugins", ".dat")
38-
temporaryFile.deleteOnExit()
39-
temporaryFiles.add(temporaryFile)
48+
val temporaryFile = createTempFile("Log4j2Plugins", ".dat")
49+
tempFiles.add(temporaryFile)
4050
val fos = temporaryFile.outputStream()
4151
context.inputStream.use {
4252
it.copyTo(fos)
4353
}
4454

45-
relocators.addAll(context.relocators)
55+
tempRelocators.addAll(context.relocators)
4656

4757
if (stats == null) {
4858
stats = context.stats
4959
}
5060
}
5161

62+
/**
63+
* @return true if any dat file collected
64+
*/
5265
override fun hasTransformedResource(): Boolean {
53-
// This functionality matches the original plugin, however, I'm not clear what
54-
// the exact logic is. From what I can tell temporaryFiles should be never be empty
55-
// if anything has been performed.
56-
return temporaryFiles.isNotEmpty() || relocators.isNotEmpty()
66+
return tempFiles.isNotEmpty()
5767
}
5868

5969
override fun modifyOutputStream(os: ZipOutputStream, preserveFileTimestamps: Boolean) {
60-
val pluginCache = PluginCache()
61-
pluginCache.loadCacheFiles(urlEnumeration)
62-
relocatePlugins(pluginCache)
63-
val entry = ZipEntry(PluginProcessor.PLUGIN_CACHE_FILE)
64-
entry.time = getEntryTimestamp(preserveFileTimestamps, entry.time)
65-
os.putNextEntry(entry)
66-
pluginCache.writeCache(CloseShieldOutputStream.wrap(os))
67-
temporaryFiles.clear()
70+
try {
71+
val aggregator = PluginCache()
72+
aggregator.loadCacheFiles(urlEnumeration)
73+
relocatePlugin(tempRelocators, aggregator.allCategories)
74+
val entry = ZipEntry(PluginProcessor.PLUGIN_CACHE_FILE)
75+
entry.time = getEntryTimestamp(preserveFileTimestamps, entry.time)
76+
os.putNextEntry(entry)
77+
aggregator.writeCache(CloseShieldOutputStream.wrap(os))
78+
} finally {
79+
deleteTempFiles()
80+
}
6881
}
6982

70-
private fun relocatePlugins(pluginCache: PluginCache) {
71-
pluginCache.allCategories.values.forEach { currentMap ->
72-
currentMap.values.forEach { currentPluginEntry ->
73-
val className = currentPluginEntry.className
74-
val relocateClassContext = RelocateClassContext(className, requireNotNull(stats))
75-
relocators.firstOrNull { it.canRelocateClass(className) }?.let { relocator ->
76-
// Then we perform that relocation and update the plugin entry to reflect the new value.
77-
currentPluginEntry.className = relocator.relocateClass(relocateClassContext)
83+
/**
84+
* Applies the given `relocators` to the `aggregator`.
85+
*
86+
* @param relocators relocators.
87+
* @param aggregatorCategories all categories of the aggregator
88+
*/
89+
private fun relocatePlugin(
90+
relocators: List<Relocator>,
91+
aggregatorCategories: Map<String, Map<String, PluginEntry>>,
92+
) {
93+
for (categoryEntry in aggregatorCategories.entries) {
94+
for (pluginMapEntry in categoryEntry.value.entries) {
95+
val pluginEntry = pluginMapEntry.value
96+
val originalClassName = pluginEntry.className
97+
val relocateClassContext = RelocateClassContext(originalClassName, requireNotNull(stats))
98+
99+
findFirstMatchingRelocator(originalClassName, relocators)?.let {
100+
pluginEntry.className = it.relocateClass(relocateClassContext)
78101
}
79102
}
80103
}
81104
}
82105

106+
private fun findFirstMatchingRelocator(originalClassName: String, relocators: List<Relocator>): Relocator? {
107+
for (relocator in relocators) {
108+
if (relocator.canRelocateClass(originalClassName)) {
109+
return relocator
110+
}
111+
}
112+
return null
113+
}
114+
115+
private fun deleteTempFiles() {
116+
val pathIterator = tempFiles.listIterator()
117+
while (pathIterator.hasNext()) {
118+
val path = pathIterator.next()
119+
path.deleteIfExists()
120+
pathIterator.remove()
121+
}
122+
}
123+
83124
private val urlEnumeration: Enumeration<URL>
84125
get() {
85-
val urls = temporaryFiles.map { it.toURI().toURL() }
126+
val urls = tempFiles.map { it.toUri().toURL() }
86127
return Collections.enumeration(urls)
87128
}
88129
}

0 commit comments

Comments
 (0)