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