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 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          String value;
93          List<String> result = new ArrayList<String>();
94          // Whoa, C !!
95          for ( int i = 0; ( value = getProperty( propertyPrefix + i ) ) != null; i++ )
96          {
97              result.add( value );
98          }
99          return result;
100     }
101 
102     /**
103      * Retrieves as single object that is persisted with type encoding
104      *
105      * @param key The key for the propery
106      * @return The object, of a supported type
107      */
108     public TypeEncodedValue getTypeEncodedValue( String key )
109     {
110         String typeEncoded = getProperty( key );
111         if ( typeEncoded == null )
112         {
113             return null;
114         }
115         int typeSep = typeEncoded.indexOf( "|" );
116         String type = typeEncoded.substring( 0, typeSep );
117         String value = typeEncoded.substring( typeSep + 1 );
118         return new TypeEncodedValue( type, value );
119     }
120 
121 
122     Classpath getClasspath( String prefix )
123     {
124         List<String> elements = getStringList( prefix );
125         return new Classpath( elements );
126     }
127 
128     public void setClasspath( String prefix, Classpath classpath )
129     {
130         List classpathElements = classpath.getClassPath();
131         for ( int i = 0; i < classpathElements.size(); ++i )
132         {
133             String element = (String) classpathElements.get( i );
134             setProperty( prefix + i, element );
135         }
136     }
137 
138 
139     public void setProperty( String key, String value )
140     {
141         if ( value != null )
142         {
143             properties.setProperty( key, value );
144         }
145     }
146 
147     public void addList( List items, String propertyPrefix )
148     {
149         if ( items == null || items.size() == 0 )
150         {
151             return;
152         }
153         int i = 0;
154         for ( Object item : items )
155         {
156             if ( item == null )
157             {
158                 throw new NullPointerException( propertyPrefix + i + " has null value" );
159             }
160 
161             String[] stringArray = StringUtils.split( item.toString(), "," );
162 
163             for ( String aStringArray : stringArray )
164             {
165                 properties.setProperty( propertyPrefix + i, aStringArray );
166                 i++;
167             }
168 
169         }
170     }
171 
172     public void copyTo( Map target )
173     {
174         Iterator iter = properties.keySet().iterator();
175         Object key;
176         while ( iter.hasNext() )
177         {
178             key = iter.next();
179             //noinspection unchecked
180             target.put( key, properties.get( key ) );
181         }
182     }
183 }