Skip to content

Commit 2e1f5fa

Browse files
brido4125jhpark816
authored andcommitted
Fix: getVersion method concurrency error.
1 parent 57829aa commit 2e1f5fa

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

src/main/java/net/spy/memcached/ArcusClient.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
*/
185185
public class ArcusClient extends FrontCacheMemcachedClient implements ArcusClientIF {
186186

187-
private static String VERSION;
187+
private static String VERSION = "INIT";
188188
private static final Logger arcusLogger = LoggerFactory.getLogger(ArcusClient.class);
189189
private static final String ARCUS_CLOUD_ADDR = "127.0.0.1:2181";
190190
private static final String DEFAULT_ARCUS_CLIENT_NAME = "ArcusClient";
@@ -4624,30 +4624,37 @@ public void complete() {
46244624
* @return version string
46254625
*/
46264626
public static String getVersion() {
4627-
if (VERSION == null) {
4628-
VERSION = "NONE";
4629-
4630-
Enumeration<URL> resEnum;
4631-
try {
4632-
resEnum = Thread.currentThread()
4633-
.getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
4634-
while (resEnum.hasMoreElements()) {
4635-
URL url = resEnum.nextElement();
4636-
InputStream is = url.openStream();
4637-
if (is != null) {
4638-
Manifest manifest = new Manifest(is);
4639-
java.util.jar.Attributes mainAttribs = manifest.getMainAttributes();
4640-
String version = mainAttribs.getValue("Arcusclient-Version");
4641-
if (version != null) {
4642-
VERSION = version;
4627+
if (!VERSION.equals("INIT")) {
4628+
return VERSION;
4629+
}
4630+
synchronized (VERSION) {
4631+
if (VERSION.equals("INIT")) {
4632+
Enumeration<URL> resEnum;
4633+
try {
4634+
resEnum = Thread.currentThread()
4635+
.getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
4636+
while (resEnum.hasMoreElements()) {
4637+
URL url = resEnum.nextElement();
4638+
InputStream is = url.openStream();
4639+
if (is != null) {
4640+
Manifest manifest = new Manifest(is);
4641+
java.util.jar.Attributes mainAttribs = manifest.getMainAttributes();
4642+
String version = mainAttribs.getValue("Arcusclient-Version");
4643+
if (version != null) {
4644+
VERSION = version;
4645+
break;
4646+
}
46434647
}
46444648
}
4649+
} catch (Exception e) {
4650+
// Failed to get version.
4651+
} finally {
4652+
if (VERSION.equals("INIT")) {
4653+
VERSION = "NONE";
4654+
}
46454655
}
4646-
} catch (Exception e) {
4647-
// Failed to get version.
46484656
}
46494657
}
4650-
46514658
return VERSION;
46524659
}
46534660
}

0 commit comments

Comments
 (0)