View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Kristian Rosenvold
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       * Retrieves as single object that is persisted with type encoding
92       *
93       * @param key The key for the propery
94       * @return The object, of a supported type
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 }