View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api.feature;
20  
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import org.apache.maven.api.Constants;
25  import org.apache.maven.api.Session;
26  import org.apache.maven.api.annotations.Nullable;
27  
28  /**
29   * Centralized class for Maven Core feature information.
30   * Features configured are supposed to be final in a given maven session.
31   *
32   * @since 4.0.0
33   */
34  public final class Features {
35  
36      private Features() {}
37  
38      /**
39       * Check if the consumer POM feature is active.
40       */
41      public static boolean consumerPom(@Nullable Properties userProperties) {
42          return doGet(userProperties, Constants.MAVEN_CONSUMER_POM, true);
43      }
44  
45      /**
46       * Check if the consumer POM feature is active.
47       */
48      public static boolean consumerPom(@Nullable Map<String, String> userProperties) {
49          return doGet(userProperties, Constants.MAVEN_CONSUMER_POM, true);
50      }
51  
52      /**
53       * Check if the consumer POM feature is active.
54       */
55      public static boolean consumerPom(@Nullable Session session) {
56          return consumerPom(session != null ? session.getUserProperties() : null);
57      }
58  
59      private static boolean doGet(Properties userProperties, String key, boolean def) {
60          return doGet(userProperties != null ? userProperties.get(key) : null, def);
61      }
62  
63      private static boolean doGet(Map<String, ?> userProperties, String key, boolean def) {
64          return doGet(userProperties != null ? userProperties.get(key) : null, def);
65      }
66  
67      private static boolean doGet(Object val, boolean def) {
68          if (val instanceof Boolean) {
69              return (Boolean) val;
70          } else if (val != null) {
71              return Boolean.parseBoolean(val.toString());
72          } else {
73              return def;
74          }
75      }
76  }