33
44package com .oracle .weblogic .imagetool .inspect ;
55
6- import java .util .ArrayList ;
6+ import java .util .AbstractMap ;
77import java .util .Arrays ;
88import java .util .Iterator ;
99import java .util .List ;
1010import java .util .Map ;
1111import java .util .Properties ;
1212import java .util .TreeMap ;
13-
14- import com .oracle .weblogic .imagetool .util .Utils ;
13+ import java .util .stream .Collectors ;
1514
1615/**
1716 * Convert image properties to JSON.
1817 * This class should be replaced if/when a full JSON parser is added to the project.
1918 */
2019public class InspectOutput {
21- private final String patchesKey = "oraclePatches" ;
20+ private static final String patchesKey = "oraclePatches" ;
2221 Map <String ,String > attributes ;
23- List <PatchJson > patches ;
22+ List <InventoryPatch > patches ;
23+ OperatingSystemProperties os ;
2424
2525 /**
2626 * Convert image properties to JSON output.
2727 * @param imageProperties Properties from the image.
2828 */
2929 public InspectOutput (Properties imageProperties ) {
30- Map <String ,String > sortedSet = new TreeMap <>();
31- for (Map .Entry <Object ,Object > x : imageProperties .entrySet ()) {
32- sortedSet .put (x .getKey ().toString (), x .getValue ().toString ());
33- }
34- if (sortedSet .containsKey (patchesKey )) {
35- patches = new ArrayList <>();
36- String patchesValue = sortedSet .get (patchesKey );
37- if (!Utils .isEmptyString (patchesValue )) {
38- String [] tokens = patchesValue .split (";" );
39- for (int i = 0 ; i < tokens .length ; i ++) {
40- PatchJson patch = new PatchJson ();
41- patch .bug = tokens [i ];
42- if (i ++ < tokens .length ) {
43- patch .uid = tokens [i ];
44- }
45- if (i ++ < tokens .length ) {
46- patch .description = tokens [i ].replace ("\" " , "" );
47- }
48- patches .add (patch );
49- }
50- }
51- sortedSet .remove (patchesKey );
30+ // convert Properties to TreeMap (to sort attributes alphabetically)
31+ Map <String ,String > sorted = imageProperties .entrySet ().stream ()
32+ .map (InspectOutput ::convertToStringEntry )
33+ .filter (e -> !e .getKey ().equals (patchesKey )) // do not store patches entry as a normal attribute
34+ .filter (e -> !e .getKey ().startsWith ("__OS__" )) // do not store OS entries as a normal attribute
35+ .collect (Collectors .toMap (
36+ Map .Entry ::getKey , Map .Entry ::getValue ,
37+ (v1 , v2 ) -> v1 , // discard duplicates, but there shouldn't be any dupes
38+ TreeMap ::new )); // use a sorted map
39+
40+ if (imageProperties .containsKey (patchesKey )) {
41+ patches = InventoryPatch .parseInventoryPatches (imageProperties .get (patchesKey ).toString ());
5242 }
53- attributes = sortedSet ;
43+
44+ attributes = sorted ;
45+ os = OperatingSystemProperties .getOperatingSystemProperties (imageProperties );
46+ }
47+
48+ private static Map .Entry <String ,String > convertToStringEntry (Map .Entry <Object ,Object > entry ) {
49+ return new AbstractMap .SimpleEntry <>(entry .getKey ().toString (), entry .getValue ().toString ());
5450 }
5551
5652 @ Override
5753 public String toString () {
5854 StringBuilder result = new StringBuilder ().append ("{\n " );
5955 if (patches != null ) {
6056 result .append (pad (1 )).append ('\"' ).append (patchesKey ).append ('\"' ).append (" : [\n " );
61- Iterator <PatchJson > patchesIter = patches .iterator ();
57+ Iterator <InventoryPatch > patchesIter = patches .iterator ();
6258 while (patchesIter .hasNext ()) {
63- PatchJson patch = patchesIter .next ();
59+ InventoryPatch patch = patchesIter .next ();
6460 result .append (pad (2 )).append ('{' ).append ('\n' );
65- result .append (jsonKeyValuePair (3 , "patch" , patch .bug )).append (",\n " );
66- result .append (jsonKeyValuePair (3 , "description" , patch .description )).append ('\n' );
61+ result .append (jsonKeyValuePair (3 , "patch" , patch .bug () )).append (",\n " );
62+ result .append (jsonKeyValuePair (3 , "description" , patch .description () )).append ('\n' );
6763 result .append (pad (2 )).append ('}' );
6864 if (patchesIter .hasNext ()) {
6965 result .append (',' );
@@ -72,6 +68,14 @@ public String toString() {
7268 }
7369 result .append (pad (1 )).append ("],\n " );
7470 }
71+ if (os != null ) {
72+ result .append (pad (1 )).append ('\"' ).append ("os" ).append ('\"' ).append (" : {\n " );
73+ result .append (jsonKeyValuePair (2 , "id" , os .id ())).append (",\n " );
74+ result .append (jsonKeyValuePair (2 , "name" , os .name ())).append (",\n " );
75+ result .append (jsonKeyValuePair (2 , "version" , os .version ())).append ("\n " );
76+ result .append (pad (1 )).append ("}," );
77+ result .append ('\n' );
78+ }
7579 Iterator <Map .Entry <String ,String >> attributeIter = attributes .entrySet ().iterator ();
7680 while (attributeIter .hasNext ()) {
7781 Map .Entry <String ,String > entry = attributeIter .next ();
@@ -94,10 +98,4 @@ private char[] pad(int size) {
9498 Arrays .fill (result , ' ' );
9599 return result ;
96100 }
97-
98- private static class PatchJson {
99- public String bug ;
100- public String uid ;
101- public String description ;
102- }
103101}
0 commit comments