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