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.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  import org.apache.maven.surefire.util.internal.StringUtils;
29  
30  /**
31   * Makes java.util.Properties behave like it's 2013
32   *
33   * @author Kristian Rosenvold
34   */
35  public class PropertiesWrapper
36      implements KeyValueSource
37  {
38      private final Properties properties;
39  
40      public PropertiesWrapper( Properties properties )
41      {
42          if ( properties == null )
43          {
44              throw new IllegalStateException( "Properties cannot be null" );
45          }
46          this.properties = properties;
47      }
48  
49      public Properties getProperties()
50      {
51          return properties;
52      }
53  
54      public void setAsSystemProperties()
55      {
56          for ( Object o : properties.keySet() )
57          {
58              String key = (String) o;
59  
60              System.setProperty( key, properties.getProperty( key ) );
61          }
62      }
63  
64      public String getProperty( String key )
65      {
66          return properties.getProperty( key );
67      }
68  
69      public boolean getBooleanProperty( String propertyName )
70      {
71          return Boolean.valueOf( properties.getProperty( propertyName ) );
72      }
73  
74      public Boolean getBooleanObjectProperty( String propertyName )
75      {
76          return Boolean.valueOf( properties.getProperty( propertyName ) );
77      }
78  
79      public int getIntProperty( String propertyName )
80      {
81          return Integer.parseInt( properties.getProperty( propertyName ) );
82      }
83  
84      public File getFileProperty( String key )
85      {
86          final String property = getProperty( key );
87          if ( property == null )
88          {
89              return null;
90          }
91          TypeEncodedValue typeEncodedValue = new TypeEncodedValue( File.class.getName(), property );
92          return (File) typeEncodedValue.getDecodedValue();
93      }
94  
95      public List<String> getStringList( String propertyPrefix )
96      {
97          String value;
98          List<String> result = new ArrayList<String>();
99  
100         int i = 0;
101         while ( true )
102         {
103             value = getProperty( propertyPrefix + ( i++ ) );
104 
105             if ( value == null )
106             {
107                 return result;
108             }
109 
110             result.add( value );
111         }
112     }
113 
114     /**
115      * Retrieves as single object that is persisted with type encoding
116      *
117      * @param key The key for the propery
118      * @return The object, of a supported type
119      */
120     public TypeEncodedValue getTypeEncodedValue( String key )
121     {
122         String typeEncoded = getProperty( key );
123         if ( typeEncoded == null )
124         {
125             return null;
126         }
127         int typeSep = typeEncoded.indexOf( "|" );
128         String type = typeEncoded.substring( 0, typeSep );
129         String value = typeEncoded.substring( typeSep + 1 );
130         return new TypeEncodedValue( type, value );
131     }
132 
133 
134     Classpath getClasspath( String prefix )
135     {
136         List<String> elements = getStringList( prefix );
137         return new Classpath( elements );
138     }
139 
140     public void setClasspath( String prefix, Classpath classpath )
141     {
142         List classpathElements = classpath.getClassPath();
143         for ( int i = 0; i < classpathElements.size(); ++i )
144         {
145             String element = (String) classpathElements.get( i );
146             setProperty( prefix + i, element );
147         }
148     }
149 
150 
151     public void setProperty( String key, String value )
152     {
153         if ( value != null )
154         {
155             properties.setProperty( key, value );
156         }
157     }
158 
159     public void addList( List items, String propertyPrefix )
160     {
161         if ( items == null || items.size() == 0 )
162         {
163             return;
164         }
165         int i = 0;
166         for ( Object item : items )
167         {
168             if ( item == null )
169             {
170                 throw new NullPointerException( propertyPrefix + i + " has null value" );
171             }
172 
173             String[] stringArray = StringUtils.split( item.toString(), "," );
174 
175             for ( String aStringArray : stringArray )
176             {
177                 properties.setProperty( propertyPrefix + i, aStringArray );
178                 i++;
179             }
180 
181         }
182     }
183 
184     public void copyTo( Map target )
185     {
186         Iterator iter = properties.keySet().iterator();
187         Object key;
188         while ( iter.hasNext() )
189         {
190             key = iter.next();
191             //noinspection unchecked
192             target.put( key, properties.get( key ) );
193         }
194     }
195 }