1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.surefire.booter;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.surefire.shared.utils.StringUtils;
27  
28  
29  
30  
31  public class PropertiesWrapper implements KeyValueSource {
32      private final Map<String, String> properties;
33  
34      public PropertiesWrapper(Map<String, String> properties) {
35          if (properties == null) {
36              throw new IllegalStateException("Properties cannot be null");
37          }
38          this.properties = properties;
39      }
40  
41      public Map<String, String> getProperties() {
42          return properties;
43      }
44  
45      public void setAsSystemProperties() {
46          for (Map.Entry<String, String> entry : properties.entrySet()) {
47              System.setProperty(entry.getKey(), entry.getValue());
48          }
49      }
50  
51      public String getProperty(String key) {
52          return properties.get(key);
53      }
54  
55      public boolean getBooleanProperty(String propertyName) {
56          return Boolean.parseBoolean(properties.get(propertyName));
57      }
58  
59      public int getIntProperty(String propertyName) {
60          return Integer.parseInt(properties.get(propertyName));
61      }
62  
63      public Long getLongProperty(String propertyName) {
64          String number = getProperty(propertyName);
65          return number == null ? null : Long.parseLong(number);
66      }
67  
68      public File getFileProperty(String key) {
69          final String property = getProperty(key);
70          if (property == null) {
71              return null;
72          }
73          TypeEncodedValue typeEncodedValue = new TypeEncodedValue(File.class.getName(), property);
74          return (File) typeEncodedValue.getDecodedValue();
75      }
76  
77      public List<String> getStringList(String propertyPrefix) {
78          List<String> result = new ArrayList<>();
79          for (int i = 0; ; i++) {
80              String value = getProperty(propertyPrefix + i);
81  
82              if (value == null) {
83                  return result;
84              }
85  
86              result.add(value);
87          }
88      }
89  
90      
91  
92  
93  
94  
95  
96      public TypeEncodedValue getTypeEncodedValue(String key) {
97          String typeEncoded = getProperty(key);
98          if (typeEncoded != null) {
99              int typeSep = typeEncoded.indexOf("|");
100             String type = typeEncoded.substring(0, typeSep);
101             String value = typeEncoded.substring(typeSep + 1);
102             return new TypeEncodedValue(type, value);
103         } else {
104             return null;
105         }
106     }
107 
108     Classpath getClasspath(String prefix) {
109         List<String> elements = getStringList(prefix);
110         return new Classpath(elements);
111     }
112 
113     public void setClasspath(String prefix, Classpath classpath) {
114         List classpathElements = classpath.getClassPath();
115         for (int i = 0, size = classpathElements.size(); i < size; ++i) {
116             String element = (String) classpathElements.get(i);
117             setProperty(prefix + i, element);
118         }
119     }
120 
121     public void setProperty(String key, String value) {
122         if (value != null) {
123             properties.put(key, value);
124         }
125     }
126 
127     public void addList(List items, String propertyPrefix) {
128         if (items != null && !items.isEmpty()) {
129             int i = 0;
130             for (Object item : items) {
131                 if (item == null) {
132                     throw new NullPointerException(propertyPrefix + i + " has null value");
133                 }
134 
135                 String[] stringArray = StringUtils.split(item.toString(), ",");
136 
137                 for (String aStringArray : stringArray) {
138                     properties.put(propertyPrefix + i, aStringArray);
139                     i++;
140                 }
141             }
142         }
143     }
144 
145     @Override
146     public void copyTo(Map<Object, Object> target) {
147         target.putAll(properties);
148     }
149 }