@@ -5,7 +5,6 @@ package extract
55
66import (
77 "archive/zip"
8- "bufio"
98 "context"
109 "fmt"
1110 "io"
@@ -14,6 +13,7 @@ import (
1413 "strings"
1514
1615 "github.com/aws/amazon-cloudwatch-agent/internal/detector"
16+ "github.com/aws/amazon-cloudwatch-agent/internal/detector/util"
1717 "github.com/aws/amazon-cloudwatch-agent/internal/util/collections"
1818)
1919
@@ -23,6 +23,8 @@ const (
2323
2424 // metaManifestFile is the path to the Manifest in the Java archive.
2525 metaManifestFile = "META-INF/MANIFEST.MF"
26+ // metaManifestSeparator is the separator of the key/value pairs in the file.
27+ metaManifestSeparator = ':'
2628 // metaManifestApplicationName is a non-standard, but explicit field that should be used if present.
2729 metaManifestApplicationName = "Application-Name"
2830 // metaManifestImplementationTitle is a standard field that is conventionally used to name the application.
@@ -188,7 +190,7 @@ func (e *archiveManifestNameExtractor) Extract(ctx context.Context, process dete
188190 return "" , detector .ErrIncompatibleExtractor
189191 }
190192 fallback := strings .TrimSuffix (filepath .Base (arg ), filepath .Ext (arg ))
191- path , err := absPath (ctx , process , arg )
193+ path , err := util . AbsPath (ctx , process , arg )
192194 e .logger .Debug ("Trying to extract name from Java Archive" , "pid" , process .PID (), "path" , path )
193195 if err != nil {
194196 return fallback , nil
@@ -225,40 +227,22 @@ func (e *archiveManifestNameExtractor) readManifest(jarPath string) (string, err
225227 return "" , err
226228 }
227229 defer rc .Close ()
228- return e . parseManifest (rc ), nil
230+ return parseManifest (rc , e . fieldPriority , e . fieldLookup ), nil
229231}
230232
231- func (e * archiveManifestNameExtractor ) parseManifest (r io.Reader ) string {
232- manifest := make (map [string ]string , len (e .fieldPriority ))
233- scanner := bufio .NewScanner (r )
234- for scanner .Scan () {
235- line := scanner .Text ()
236- if parts := strings .SplitN (line , ":" , 2 ); len (parts ) == 2 {
237- field := strings .TrimSpace (parts [0 ])
238- if e .fieldLookup .Contains (field ) {
239- manifest [field ] = strings .TrimSpace (parts [1 ])
240- // exit early if the highest priority field is found
241- if field == e .fieldPriority [0 ] && manifest [field ] != "" {
242- return manifest [field ]
243- }
244- }
233+ func parseManifest (r io.Reader , fieldPriority []string , fieldLookup collections.Set [string ]) string {
234+ manifest := make (map [string ]string , len (fieldPriority ))
235+ _ = util .ReadProperties (r , metaManifestSeparator , func (key , value string ) bool {
236+ if ! fieldLookup .Contains (key ) || value == "" {
237+ return true
245238 }
246- }
247- for _ , field := range e .fieldPriority {
239+ manifest [key ] = value
240+ return key != fieldPriority [0 ]
241+ })
242+ for _ , field := range fieldPriority {
248243 if name := manifest [field ]; name != "" {
249244 return name
250245 }
251246 }
252247 return ""
253248}
254-
255- func absPath (ctx context.Context , process detector.Process , path string ) (string , error ) {
256- if filepath .IsAbs (path ) {
257- return path , nil
258- }
259- cwd , err := process .CwdWithContext (ctx )
260- if err != nil {
261- return "" , err
262- }
263- return filepath .Join (cwd , path ), nil
264- }
0 commit comments