-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Netty versions to the self-diagnostics (#2716)
- Loading branch information
1 parent
21deab5
commit 07d40e3
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
...ng/src/main/java/com/microsoft/applicationinsights/agent/internal/init/NettyVersions.java
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,58 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.microsoft.applicationinsights.agent.internal.init; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
class NettyVersions { | ||
|
||
// Example: **19** **Group:** `io.netty` **Name:** `netty-codec-dns` **Version:** `4.1.79.Final` | ||
private static final Pattern DEPENDENCY_COORDINATE_PATTERN = | ||
Pattern.compile(".*`(.*)`.*`(.*)`.*`(.*)`"); | ||
|
||
private NettyVersions() {} | ||
|
||
static String extract() { | ||
String moreLicenseResource = "/META-INF/licenses/more-licenses.md"; | ||
InputStream moreLicenseAsStream = NettyVersions.class.getResourceAsStream(moreLicenseResource); | ||
if (moreLicenseAsStream == null) { | ||
return moreLicenseResource + " notFound"; | ||
} | ||
return extractNettyVersions(moreLicenseAsStream); | ||
} | ||
|
||
private static String extractNettyVersions(InputStream moreLicenseAsStream) { | ||
try (InputStreamReader inputStreamReader = | ||
new InputStreamReader(moreLicenseAsStream, StandardCharsets.UTF_8); | ||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { | ||
return bufferedReader | ||
.lines() | ||
.filter(line -> line.contains("netty") && line.contains("Group")) | ||
.map(line -> extractDependency(line.trim())) | ||
.collect(Collectors.joining(", ")); | ||
} catch (IOException e) { | ||
return e.getMessage(); | ||
} | ||
} | ||
|
||
private static String extractDependency(String line) { | ||
Matcher matcher = DEPENDENCY_COORDINATE_PATTERN.matcher(line); | ||
boolean matched = matcher.find(); | ||
if (!matched) { | ||
return "(" + line + ")"; | ||
} | ||
try { | ||
return "(" + matcher.group(1) + ", " + matcher.group(2) + ", " + matcher.group(3) + ")"; | ||
} catch (IllegalStateException | IndexOutOfBoundsException e) { | ||
return "(" + e.getMessage() + " for " + line + ")"; | ||
} | ||
} | ||
} |