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