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.Session;
25 import org.apache.maven.api.annotations.Nullable;
26
27 /**
28 * Centralized class for Maven Core feature information.
29 * Features configured are supposed to be final in a given maven session.
30 *
31 * @since 4.0.0
32 */
33 public final class Features {
34
35 /**
36 * Name of the Maven user property to enable or disable the build/consumer POM feature.
37 *
38 * TODO: v4: remove experimental bit
39 */
40 public static final String BUILDCONSUMER = "maven.experimental.buildconsumer";
41
42 private Features() {}
43
44 /**
45 * Check if the build/consumer POM feature is active.
46 */
47 public static boolean buildConsumer(@Nullable Properties userProperties) {
48 return doGet(userProperties, BUILDCONSUMER, true);
49 }
50
51 /**
52 * Check if the build/consumer POM feature is active.
53 */
54 public static boolean buildConsumer(@Nullable Map<String, String> userProperties) {
55 return doGet(userProperties, BUILDCONSUMER, true);
56 }
57
58 /**
59 * Check if the build/consumer POM feature is active.
60 */
61 public static boolean buildConsumer(@Nullable Session session) {
62 return buildConsumer(session != null ? session.getUserProperties() : null);
63 }
64
65 private static boolean doGet(Properties userProperties, String key, boolean def) {
66 return doGet(userProperties != null ? userProperties.get(key) : null, def);
67 }
68
69 private static boolean doGet(Map<String, ?> userProperties, String key, boolean def) {
70 return doGet(userProperties != null ? userProperties.get(key) : null, def);
71 }
72
73 private static boolean doGet(Object val, boolean def) {
74 if (val instanceof Boolean) {
75 return (Boolean) val;
76 } else if (val != null) {
77 return Boolean.parseBoolean(val.toString());
78 } else {
79 return def;
80 }
81 }
82 }