77package com.diffplug.atplug
88
99import java.io.ByteArrayOutputStream
10+ import java.io.EOFException
1011import java.lang.reflect.Constructor
1112import java.net.URL
1213import java.nio.charset.StandardCharsets
@@ -51,13 +52,23 @@ interface PlugRegistry {
5152 val values = Eager ::class .java.classLoader.getResources(PATH_MANIFEST )
5253 while (values.hasMoreElements()) {
5354 val manifestUrl = values.nextElement()
54- parseManifest(manifestUrl)
55+ try {
56+ parseManifest(manifestUrl, true )
57+ } catch (e: EOFException ) {
58+ // do the parsing again but this time disable caching
59+ // https://stackoverflow.com/questions/36517604/closing-a-jarurlconnection
60+ parseManifest(manifestUrl, false )
61+ }
5562 }
5663 }
5764 }
5865
59- private fun parseManifest (manifestUrl : URL ) {
60- manifestUrl.openStream().use { stream ->
66+ private fun parseManifest (manifestUrl : URL , allowCaching : Boolean ) {
67+ val connection = manifestUrl.openConnection()
68+ if (! allowCaching) {
69+ connection.useCaches = false
70+ }
71+ connection.getInputStream().use { stream ->
6172 // parse the manifest
6273 val manifest = Manifest (stream)
6374 val services = manifest.mainAttributes.getValue(DS_WITHIN_MANIFEST )
@@ -69,7 +80,7 @@ interface PlugRegistry {
6980 try {
7081 if (servicePath.isNotEmpty()) {
7182 val asString = manifestUrl.toExternalForm()
72- val component = parseComponent(asString, servicePath)
83+ val component = parseComponent(asString, servicePath, useCaches )
7384 synchronized(this ) {
7485 data.putDescriptor(component.provides, component)
7586 owners[component.provides]?.doRegister(component)
@@ -93,12 +104,20 @@ interface PlugRegistry {
93104 }
94105 }
95106
96- private fun parseComponent (manifestUrl : String , servicePath : String ): PlugDescriptor {
107+ private fun parseComponent (
108+ manifestUrl : String ,
109+ servicePath : String ,
110+ allowCaching : Boolean
111+ ): PlugDescriptor {
97112 val serviceUrl =
98113 URL (manifestUrl.substring(0 , manifestUrl.length - PATH_MANIFEST .length) + servicePath)
99114
115+ val connection = serviceUrl.openConnection()
116+ if (! allowCaching) {
117+ connection.useCaches = false
118+ }
100119 val out = ByteArrayOutputStream ()
101- serviceUrl.openStream ().use { it.copyTo(out ) }
120+ connection.getInputStream ().use { it.copyTo(out ) }
102121 val serviceFileContent = String (out .toByteArray(), StandardCharsets .UTF_8 )
103122 return PlugDescriptor .fromJson(serviceFileContent)
104123 }
0 commit comments