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.ByteArrayInputStream;
23  import java.io.File;
24  import java.util.Properties;
25  import org.apache.maven.surefire.util.ReflectionUtils;
26  
27  /**
28   * @author Kristian Rosenvold
29   */
30  public class TypeEncodedValue
31  {
32      String type;
33  
34      String value;
35  
36      public TypeEncodedValue( String type, String value )
37      {
38          this.type = type;
39          this.value = value;
40      }
41  
42      public boolean isTypeClass()
43      {
44          return Class.class.getName().equals( type );
45      }
46  
47      public Object getDecodedValue()
48      {
49          return getDecodedValue( Thread.currentThread().getContextClassLoader() );
50      }
51  
52      public Object getDecodedValue( ClassLoader classLoader )
53      {
54          if ( type.trim().length() == 0 )
55          {
56              return null;
57          }
58          else if ( type.equals( String.class.getName() ) )
59          {
60              return value;
61          }
62          else if ( isTypeClass() )
63          {
64              return ReflectionUtils.loadClass( classLoader, value );
65          }
66          else if ( type.equals( File.class.getName() ) )
67          {
68              return new File( value );
69          }
70          else if ( type.equals( Boolean.class.getName() ) )
71          {
72              return Boolean.valueOf( value );
73          }
74          else if ( type.equals( Integer.class.getName() ) )
75          {
76              return Integer.valueOf( value );
77          }
78          else if ( type.equals( Properties.class.getName() ) )
79          {
80              final Properties result = new Properties();
81              try
82              {
83                  ByteArrayInputStream bais = new ByteArrayInputStream( value.getBytes( "8859_1" ) );
84                  result.load( bais );
85              }
86              catch ( Exception e )
87              {
88                  throw new RuntimeException( "bug in property conversion", e );
89              }
90              return result;
91          }
92          else
93          {
94              throw new IllegalArgumentException( "Unknown parameter type: " + type );
95          }
96      }
97  
98      @SuppressWarnings( "SimplifiableIfStatement" )
99      public boolean equals( Object o )
100     {
101         if ( this == o )
102         {
103             return true;
104         }
105         if ( o == null || getClass() != o.getClass() )
106         {
107             return false;
108         }
109 
110         TypeEncodedValue that = (TypeEncodedValue) o;
111 
112         if ( type != null ? !type.equals( that.type ) : that.type != null )
113         {
114             return false;
115         }
116         return !( value != null ? !value.equals( that.value ) : that.value != null );
117 
118     }
119 
120     public int hashCode()
121     {
122         int result = type != null ? type.hashCode() : 0;
123         result = 31 * result + ( value != null ? value.hashCode() : 0 );
124         return result;
125     }
126 }