View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.plugin.buildmetadata.maven;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.Properties;
20  
21  import org.apache.commons.beanutils.PropertyUtils;
22  import org.apache.maven.project.MavenProject;
23  
24  /**
25   * Fetches properties from a Maven project. This includes properties of the
26   * Maven project instance as well as properties from its properties section.
27   * <p>
28   * It is allowed to change the project passed to the helper instance after
29   * creation of the instance as long as the caller makes sure that it is not
30   * changed within a call of this instance. No synchronization is cared for by
31   * this instance.
32   * </p>
33   *
34   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
35   * @version $Revision:591 $
36   */
37  public final class MavenPropertyHelper
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    /**
44     * All properties referring to the Maven project instance start with this
45     * prefix.
46     * <p>
47     * The value of this constant is {@value}.
48     * </p>
49     */
50    private static final String PROJECT_PROPERTY_NAME_PREFIX = "project.";
51  
52    /**
53     * The length of the {@link #PROJECT_PROPERTY_NAME_PREFIX project property
54     * prefix}.
55     * <p>
56     * The value of this constant is {@value}.
57     * </p>
58     */
59    private static final int PROJECT_PROPERTY_NAME_PREFIX_LENGTH =
60        PROJECT_PROPERTY_NAME_PREFIX.length();
61  
62    // --- members --------------------------------------------------------------
63  
64    /**
65     * The project to fetch properties from.
66     */
67    private final MavenProject project;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    /**
74     * Default constructor.
75     *
76     * @param project the project to fetch properties from.
77     */
78    public MavenPropertyHelper(final MavenProject project)
79    {
80      if (project == null)
81      {
82        throw new NullPointerException(
83            "The property helper requires the project reference to be not 'null'.");
84      }
85      this.project = project;
86    }
87  
88    // ****************************** Inner Classes *****************************
89  
90    // ********************************* Methods ********************************
91  
92    // --- init -----------------------------------------------------------------
93  
94    // --- get&set --------------------------------------------------------------
95  
96    // --- business -------------------------------------------------------------
97  
98    /**
99     * Returns the property value specified by the given name.
100    *
101    * @param name the name of the property to return.
102    * @return the property value for the given {@code name}.
103    */
104   public String getProperty(final String name)
105   {
106     if (name == null)
107     {
108       throw new NullPointerException(
109           "Name of requested property must not be 'null'");
110     }
111 
112     String value = null;
113     if (isProjectProperty(name))
114     {
115       value = getProjectProperty(name);
116     }
117     if (value == null)
118     {
119       value = getPropertiesProperty(name);
120     }
121     return value;
122   }
123 
124   private boolean isProjectProperty(final String name)
125   {
126     return name != null && name.length() >= PROJECT_PROPERTY_NAME_PREFIX_LENGTH
127            && name.startsWith(PROJECT_PROPERTY_NAME_PREFIX);
128   }
129 
130   /**
131    * Returns a property from the project instance.
132    */
133   private String getProjectProperty(final String name)
134   {
135     final String projectName =
136         name.substring(PROJECT_PROPERTY_NAME_PREFIX_LENGTH);
137     if (PropertyUtils.isReadable(project, projectName))
138     {
139       try
140       {
141         return getProjectProperty(project, projectName);
142       }
143       catch (final Exception e)
144       {
145         throw new IllegalStateException("Cannot access project property '"
146                                         + name + "'.");
147       }
148     }
149 
150     return null;
151   }
152 
153   private static String getProjectProperty(final MavenProject project,
154       final String projectName) throws IllegalAccessException,
155     InvocationTargetException, NoSuchMethodException
156   {
157     if (project != null)
158     {
159       final Object value = PropertyUtils.getProperty(project, projectName);
160       if (value != null)
161       {
162         return String.valueOf(value);
163       }
164       else
165       {
166         return getProjectProperty(project.getParent(), projectName);
167       }
168     }
169     return null;
170   }
171 
172   /**
173    * Returns a property from the <code>properties</code> section.
174    */
175   private String getPropertiesProperty(final String name)
176   {
177     return getPropertiesProperty(project, name);
178   }
179 
180   private static String getPropertiesProperty(final MavenProject project,
181       final String name)
182   {
183     String value = null;
184     final Properties properties = project.getProperties();
185     if (properties != null)
186     {
187       value = properties.getProperty(name);
188     }
189 
190     if (value == null)
191     {
192       value = getPropertiesPropertyFromParent(project, name);
193     }
194 
195     return value;
196   }
197 
198   private static String getPropertiesPropertyFromParent(
199       final MavenProject project, final String name)
200   {
201     final String value;
202     final MavenProject parentProject = project.getParent();
203     if (parentProject != null)
204     {
205       value = getPropertiesProperty(parentProject, name);
206     }
207     else
208     {
209       value = null;
210     }
211     return value;
212   }
213 
214   // --- object basics --------------------------------------------------------
215 
216 }