-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metadata reader to allow Spring Boot can read another plugin jar'…
…s classpath resources
- Loading branch information
Showing
4 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...c/main/kotlin/kr/summitsystems/springbukkit/core/SpringBukkitMetadataReaderFactoryBean.kt
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,36 @@ | ||
package kr.summitsystems.springbukkit.core | ||
|
||
import org.springframework.beans.factory.FactoryBean | ||
import org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory | ||
import org.springframework.context.ApplicationListener | ||
import org.springframework.context.ResourceLoaderAware | ||
import org.springframework.context.event.ContextRefreshedEvent | ||
import org.springframework.core.io.ResourceLoader | ||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory | ||
|
||
/** | ||
* https://github.com/spring-projects/spring-boot/pull/39321 | ||
*/ | ||
internal class SpringBukkitMetadataReaderFactoryBean : FactoryBean<ConcurrentReferenceCachingMetadataReaderFactory>, ApplicationListener<ContextRefreshedEvent>, ResourceLoaderAware { | ||
private var metadataReaderFactory: ConcurrentReferenceCachingMetadataReaderFactory? = null | ||
|
||
override fun setResourceLoader(resourceLoader: ResourceLoader) { | ||
metadataReaderFactory = ConcurrentReferenceCachingMetadataReaderFactory(resourceLoader) | ||
} | ||
|
||
override fun getObject(): ConcurrentReferenceCachingMetadataReaderFactory? { | ||
return metadataReaderFactory | ||
} | ||
|
||
override fun getObjectType(): Class<*> { | ||
return CachingMetadataReaderFactory::class.java | ||
} | ||
|
||
override fun isSingleton(): Boolean { | ||
return true | ||
} | ||
|
||
override fun onApplicationEvent(event: ContextRefreshedEvent) { | ||
metadataReaderFactory?.clearCache() | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...kit-core/src/main/kotlin/kr/summitsystems/springbukkit/core/SpringBukkitResourceLoader.kt
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,57 @@ | ||
package kr.summitsystems.springbukkit.core | ||
|
||
import org.bukkit.plugin.PluginManager | ||
import org.bukkit.plugin.java.JavaPlugin | ||
import org.springframework.core.io.DefaultResourceLoader | ||
import org.springframework.core.io.Resource | ||
|
||
class SpringBukkitResourceLoader( | ||
plugin: JavaPlugin, | ||
classLoader: ClassLoader, | ||
private val pluginManager: PluginManager | ||
): DefaultResourceLoader(classLoader) { | ||
private val dependResourceLoaders: List<InternalDefaultResourceLoader> = plugin.description.depend.map { pluginId -> | ||
val javaPlugin = pluginManager.getPlugin(pluginId) as? JavaPlugin | ||
?: throw IllegalStateException("The plugin with id $pluginId is not found.") | ||
val getClassLoaderMethodAccessor = JavaPlugin::class.java.getDeclaredMethod("getClassLoader") | ||
getClassLoaderMethodAccessor.isAccessible = true | ||
val pluginClassLoader = getClassLoaderMethodAccessor.invoke(javaPlugin) as ClassLoader | ||
InternalDefaultResourceLoader(pluginClassLoader) | ||
} | ||
|
||
override fun getResource(location: String): Resource { | ||
val superResource = super.getResource(location) | ||
if (superResource.exists()) { | ||
return superResource | ||
} else { | ||
dependResourceLoaders.forEach { dependResourceLoader -> | ||
val dependResource = dependResourceLoader.getResource(location) | ||
if (dependResource.exists()) { | ||
return dependResource | ||
} | ||
} | ||
} | ||
return superResource | ||
} | ||
|
||
override fun getResourceByPath(path: String): Resource { | ||
val superResource = super.getResourceByPath(path) | ||
if (superResource.exists()) { | ||
return superResource | ||
} else { | ||
dependResourceLoaders.forEach { dependResourceLoader -> | ||
val dependResource = dependResourceLoader.getResourceByPath(path) | ||
if (dependResource.exists()) { | ||
return dependResource | ||
} | ||
} | ||
} | ||
return superResource | ||
} | ||
|
||
private class InternalDefaultResourceLoader(classLoader: ClassLoader): DefaultResourceLoader(classLoader) { | ||
public override fun getResourceByPath(path: String): Resource { | ||
return super.getResourceByPath(path) | ||
} | ||
} | ||
} |
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