View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  import org.apache.maven.surefire.shared.utils.StringUtils;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public class PropertiesWrapper
32      implements KeyValueSource
33  {
34      private final Map<String, String> properties;
35  
36      public PropertiesWrapper( Map<String, String> properties )
37      {
38          if ( properties == null )
39          {
40              throw new IllegalStateException( "Properties cannot be null" );
41          }
42          this.properties = properties;
43      }
44  
45      public Map<String, String> getProperties()
46      {
47          return properties;
48      }
49  
50      public void setAsSystemProperties()
51      {
52          for ( Map.Entry<String, String> entry : properties.entrySet() )
53          {
54              System.setProperty( entry.getKey(), entry.getValue() );
55          }
56      }
57  
58      public String getProperty( String key )
59      {
60          return properties.get( key );
61      }
62  
63      public boolean getBooleanProperty( String propertyName )
64      {
65          return Boolean.valueOf( properties.get( propertyName ) );
66      }
67  
68      public int getIntProperty( String propertyName )
69      {
70          return Integer.parseInt( properties.get( propertyName ) );
71      }
72  
73      public Long getLongProperty( String propertyName )
74      {
75          String number = getProperty( propertyName );
76          return number == null ? null : Long.parseLong( number );
77      }
78  
79      public File getFileProperty( String key )
80      {
81          final String property = getProperty( key );
82          if ( property == null )
83          {
84              return null;
85          }
86          TypeEncodedValue typeEncodedValue = new TypeEncodedValue( File.class.getName(), property );
87          return (File) typeEncodedValue.getDecodedValue();
88      }
89  
90      public List<String> getStringList( String propertyPrefix )
91      {
92          List<String> result = new ArrayList<>();
93          for ( int i = 0; ; i++ )
94          {
95              String value = getProperty( propertyPrefix + i );
96  
97              if ( value == null )
98              {
99                  return result;
100             }
101 
102             result.add( value );
103         }
104     }
105 
106     /**
107      * Retrieves as single object that is persisted with type encoding
108      *
109      * @param key The key for the propery
110      * @return The object, of a supported type
111      */
112     public TypeEncodedValue getTypeEncodedValue( String key )
113     {
114         String typeEncoded = getProperty( key );
115         if ( typeEncoded != null )
116         {
117             int typeSep = typeEncoded.indexOf( "|" );
118             String type = typeEncoded.substring( 0, typeSep );
119             String value = typeEncoded.substring( typeSep + 1 );
120             return new TypeEncodedValue( type, value );
121         }
122         else
123         {
124             return null;
125         }
126     }
127 
128     Classpath getClasspath( String prefix )
129     {
130         List<String> elements = getStringList( prefix );
131         return new Classpath( elements );
132     }
133 
134     public void setClasspath( String prefix, Classpath classpath )
135     {
136         List classpathElements = classpath.getClassPath();
137         for ( int i = 0, size = classpathElements.size(); i < size; ++i )
138         {
139             String element = (String) classpathElements.get( i );
140             setProperty( prefix + i, element );
141         }
142     }
143 
144 
145     public void setProperty( String key, String value )
146     {
147         if ( value != null )
148         {
149             properties.put( key, value );
150         }
151     }
152 
153     public void addList( List items, String propertyPrefix )
154     {
155         if ( items != null && !items.isEmpty() )
156         {
157             int i = 0;
158             for ( Object item : items )
159             {
160                 if ( item == null )
161                 {
162                     throw new NullPointerException( propertyPrefix + i + " has null value" );
163                 }
164 
165                 String[] stringArray = StringUtils.split( item.toString(), "," );
166 
167                 for ( String aStringArray : stringArray )
168                 {
169                     properties.put( propertyPrefix + i, aStringArray );
170                     i++;
171                 }
172             }
173         }
174     }
175 
176     @Override
177     public void copyTo( Map<Object, Object> target )
178     {
179         target.putAll( properties );
180     }
181 }