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 org.apache.maven.surefire.util.NestedRuntimeException;
23  import org.apache.maven.surefire.util.ReflectionUtils;
24  import org.apache.maven.surefire.util.internal.StringUtils;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Properties;
32  
33  /**
34   * @author Kristian Rosenvold
35   */
36  public class PropertiesWrapper
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 ( Iterator i = properties.keySet().iterator(); i.hasNext(); )
57          {
58              String key = (String) i.next();
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          final Boolean aBoolean = Boolean.valueOf( properties.getProperty( propertyName ) );
72          return aBoolean.booleanValue();
73      }
74  
75      public Boolean getBooleanObjectProperty( String propertyName )
76      {
77          return Boolean.valueOf( properties.getProperty( propertyName ) );
78      }
79  
80      public Integer getIntegerObjectProperty( String propertyName )
81      {
82          String property = properties.getProperty( propertyName );
83          return property != null ? Integer.valueOf( property ) : null;
84      }
85  
86      public File getFileProperty( String key )
87      {
88          final String property = getProperty( key );
89          if ( property == null )
90          {
91              return null;
92          }
93          return (File) getParamValue( property, File.class.getName() );
94      }
95  
96      public List getListOfTypedObjects( String propertyPrefix )
97      {
98          String type;
99          String value;
100         List result = new ArrayList();
101         for ( int i = 0; ( type = getProperty( propertyPrefix + i + BooterConstants.TYPES_SUFIX ) ) != null; i++ )
102         {
103             value = getProperty( propertyPrefix + i + BooterConstants.PARAMS_SUFIX );
104             result.add( getParamValue( value, type ) );
105         }
106         return result;
107     }
108 
109     public List getStringList( String propertyPrefix )
110     {
111         String value;
112         List result = new ArrayList();
113         // Whoa, C !!
114         for ( int i = 0; ( value = getProperty( propertyPrefix + i ) ) != null; i++ )
115         {
116             result.add( value );
117         }
118         return result;
119     }
120 
121     /**
122      * Retrieves as single object that is persisted with type encoding
123      *
124      * @param key The key for the propery
125      * @return The object, of a supported type
126      */
127     public Object getTypeDecoded( String key )
128     {
129         String typeEncoded = getProperty( key );
130         if ( typeEncoded == null )
131         {
132             return null;
133         }
134         int typeSep = typeEncoded.indexOf( "|" );
135         String type = typeEncoded.substring( 0, typeSep );
136         String value = typeEncoded.substring( typeSep + 1 );
137         return getParamValue( value, type );
138     }
139 
140     private Object getParamValue( String param, String typeName )
141     {
142         if ( typeName.trim().length() == 0 )
143         {
144             return null;
145         }
146         else if ( typeName.equals( String.class.getName() ) )
147         {
148             return param;
149         }
150         else if ( typeName.equals( Class.class.getName() ) )
151         {
152             return ReflectionUtils.loadClass( Thread.currentThread().getContextClassLoader(), param );
153         }
154         else if ( typeName.equals( File.class.getName() ) )
155         {
156             return new File( param );
157         }
158         else if ( typeName.equals( File[].class.getName() ) )
159         {
160             List stringList = processStringList( param );
161             File[] fileList = new File[stringList.size()];
162             for ( int j = 0; j < stringList.size(); j++ )
163             {
164                 fileList[j] = new File( (String) stringList.get( j ) );
165             }
166             return fileList;
167         }
168         else if ( typeName.equals( ArrayList.class.getName() ) )
169         {
170             return processStringList( param );
171         }
172         else if ( typeName.equals( Boolean.class.getName() ) )
173         {
174             return Boolean.valueOf( param );
175         }
176         else if ( typeName.equals( Integer.class.getName() ) )
177         {
178             return Integer.valueOf( param );
179         }
180         else if ( typeName.equals( Properties.class.getName() ) )
181         {
182             final Properties result = new Properties();
183             try
184             {
185                 ByteArrayInputStream bais = new ByteArrayInputStream( param.getBytes( "8859_1" ) );
186                 result.load( bais );
187             }
188             catch ( Exception e )
189             {
190                 throw new NestedRuntimeException( "bug in property conversion", e );
191             }
192             return result;
193         }
194         else
195         {
196             // TODO: could attempt to construct with a String constructor if needed
197             throw new IllegalArgumentException( "Unknown parameter type: " + typeName );
198         }
199     }
200 
201     private static List processStringList( String stringList )
202     {
203         String sl = stringList;
204 
205         if ( sl.startsWith( "[" ) && sl.endsWith( "]" ) )
206         {
207             sl = sl.substring( 1, sl.length() - 1 );
208         }
209 
210         List list = new ArrayList();
211 
212         String[] stringArray = StringUtils.split( sl, "," );
213 
214         for ( int i = 0; i < stringArray.length; i++ )
215         {
216             list.add( stringArray[i].trim() );
217         }
218         return list;
219     }
220 
221     public void setProperty( String key, File file )
222     {
223         if ( file != null )
224         {
225             setProperty( key, file.toString() );
226         }
227     }
228 
229     public void setProperty( String key, Boolean aBoolean )
230     {
231         if ( aBoolean != null )
232         {
233             setProperty( key, aBoolean.toString() );
234         }
235     }
236 
237     Classpath getClasspath( String prefix  )
238     {
239         List elements = getStringList( prefix );
240         return new Classpath( elements );
241     }
242 
243     public void setClasspath( String prefix, Classpath classpath )
244     {
245         List classpathElements = classpath.getClassPath();
246         for ( int i = 0; i < classpathElements.size(); ++i )
247         {
248             String element = (String) classpathElements.get( i );
249             setProperty( prefix + i, element );
250         }
251     }
252 
253     public void setProperty( String key, Integer integer )
254     {
255         if ( integer != null )
256         {
257             setProperty( key, integer.toString() );
258         }
259     }
260 
261 
262     public void setProperty( String key, String value )
263     {
264         if ( value != null )
265         {
266             properties.setProperty( key, value );
267         }
268     }
269 
270     public void addList( List items, String propertyPrefix )
271     {
272         if ( items == null || items.size() == 0 )
273         {
274             return;
275         }
276         int i = 0;
277         for (Iterator iterator = items.iterator(); iterator.hasNext();)
278         {
279             Object item = iterator.next();
280             if ( item == null )
281             {
282                 throw new NullPointerException( propertyPrefix + i + " has null value" );
283             }
284 
285             String[] stringArray = StringUtils.split(item.toString(), ",");
286 
287             for ( int j = 0; j < stringArray.length; j++ )
288             {
289                 properties.setProperty( propertyPrefix + i, stringArray[j] );
290                 i++;
291             }
292 
293         }
294     }
295 
296 }