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.util.internal.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 File getFileProperty( String key )
74      {
75          final String property = getProperty( key );
76          if ( property == null )
77          {
78              return null;
79          }
80          TypeEncodedValue typeEncodedValue = new TypeEncodedValue( File.class.getName(), property );
81          return (File) typeEncodedValue.getDecodedValue();
82      }
83  
84      public List<String> getStringList( String propertyPrefix )
85      {
86          List<String> result = new ArrayList<String>();
87          for ( int i = 0; ; i++ )
88          {
89              String value = getProperty( propertyPrefix + i );
90  
91              if ( value == null )
92              {
93                  return result;
94              }
95  
96              result.add( value );
97          }
98      }
99  
100     /**
101      * Retrieves as single object that is persisted with type encoding
102      *
103      * @param key The key for the propery
104      * @return The object, of a supported type
105      */
106     public TypeEncodedValue getTypeEncodedValue( String key )
107     {
108         String typeEncoded = getProperty( key );
109         if ( typeEncoded != null )
110         {
111             int typeSep = typeEncoded.indexOf( "|" );
112             String type = typeEncoded.substring( 0, typeSep );
113             String value = typeEncoded.substring( typeSep + 1 );
114             return new TypeEncodedValue( type, value );
115         }
116         else
117         {
118             return null;
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, size = classpathElements.size(); i < 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.put( key, value );
144         }
145     }
146 
147     public void addList( List items, String propertyPrefix )
148     {
149         if ( items != null && !items.isEmpty() )
150         {
151             int i = 0;
152             for ( Object item : items )
153             {
154                 if ( item == null )
155                 {
156                     throw new NullPointerException( propertyPrefix + i + " has null value" );
157                 }
158 
159                 String[] stringArray = StringUtils.split( item.toString(), "," );
160 
161                 for ( String aStringArray : stringArray )
162                 {
163                     properties.put( propertyPrefix + i, aStringArray );
164                     i++;
165                 }
166             }
167         }
168     }
169 
170     public void copyTo( Map<Object, Object> target )
171     {
172         target.putAll( properties );
173     }
174 }