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.services;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.util.Optional;
24  
25  import org.apache.maven.api.Session;
26  import org.apache.maven.api.annotations.Experimental;
27  import org.apache.maven.api.annotations.Immutable;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.apache.maven.api.annotations.NotThreadSafe;
30  import org.apache.maven.api.annotations.Nullable;
31  
32  import static org.apache.maven.api.services.BaseRequest.nonNull;
33  
34  /**
35   * Collects settings that control the building of effective settings.
36   */
37  @Experimental
38  @Immutable
39  public interface SettingsBuilderRequest {
40  
41      @Nonnull
42      Session getSession();
43  
44      /**
45       * Gets the global settings source.
46       *
47       * @return the global settings source or {@code null} if none
48       */
49      @Nonnull
50      Optional<Source> getGlobalSettingsSource();
51  
52      /**
53       * Gets the project settings source.
54       *
55       * @return the project settings source or {@code null} if none
56       */
57      @Nonnull
58      Optional<Source> getProjectSettingsSource();
59  
60      /**
61       * Gets the user settings source.
62       *
63       * @return the user settings source or {@code null} if none
64       */
65      @Nonnull
66      Optional<Source> getUserSettingsSource();
67  
68      @Nonnull
69      static SettingsBuilderRequest build(
70              @Nonnull Session session, @Nonnull Source globalSettingsSource, @Nonnull Source userSettingsSource) {
71          return build(session, globalSettingsSource, null, userSettingsSource);
72      }
73  
74      @Nonnull
75      static SettingsBuilderRequest build(
76              @Nonnull Session session, @Nonnull Path globalSettingsPath, @Nonnull Path userSettingsPath) {
77          return build(session, Source.fromPath(globalSettingsPath), null, Source.fromPath(userSettingsPath));
78      }
79  
80      @Nonnull
81      static SettingsBuilderRequest build(
82              @Nonnull Session session,
83              @Nullable Source globalSettingsSource,
84              @Nullable Source projectSettingsSource,
85              @Nullable Source userSettingsSource) {
86          return builder()
87                  .session(nonNull(session, "session cannot be null"))
88                  .globalSettingsSource(globalSettingsSource)
89                  .projectSettingsSource(projectSettingsSource)
90                  .userSettingsSource(userSettingsSource)
91                  .build();
92      }
93  
94      @Nonnull
95      static SettingsBuilderRequest build(
96              @Nonnull Session session,
97              @Nullable Path globalSettingsPath,
98              @Nullable Path projectSettingsPath,
99              @Nullable Path userSettingsPath) {
100         return builder()
101                 .session(nonNull(session, "session cannot be null"))
102                 .globalSettingsSource(
103                         globalSettingsPath != null && Files.exists(globalSettingsPath)
104                                 ? Source.fromPath(globalSettingsPath)
105                                 : null)
106                 .projectSettingsSource(
107                         projectSettingsPath != null && Files.exists(projectSettingsPath)
108                                 ? Source.fromPath(projectSettingsPath)
109                                 : null)
110                 .userSettingsSource(
111                         userSettingsPath != null && Files.exists(userSettingsPath)
112                                 ? Source.fromPath(userSettingsPath)
113                                 : null)
114                 .build();
115     }
116 
117     @Nonnull
118     static SettingsBuilderRequestBuilder builder() {
119         return new SettingsBuilderRequestBuilder();
120     }
121 
122     @NotThreadSafe
123     class SettingsBuilderRequestBuilder {
124         Session session;
125         Source globalSettingsSource;
126         Source projectSettingsSource;
127         Source userSettingsSource;
128 
129         public SettingsBuilderRequestBuilder session(Session session) {
130             this.session = session;
131             return this;
132         }
133 
134         public SettingsBuilderRequestBuilder globalSettingsSource(Source globalSettingsSource) {
135             this.globalSettingsSource = globalSettingsSource;
136             return this;
137         }
138 
139         public SettingsBuilderRequestBuilder projectSettingsSource(Source projectSettingsSource) {
140             this.projectSettingsSource = projectSettingsSource;
141             return this;
142         }
143 
144         public SettingsBuilderRequestBuilder userSettingsSource(Source userSettingsSource) {
145             this.userSettingsSource = userSettingsSource;
146             return this;
147         }
148 
149         public SettingsBuilderRequest build() {
150             return new DefaultSettingsBuilderRequest(
151                     session, globalSettingsSource, projectSettingsSource, userSettingsSource);
152         }
153 
154         private static class DefaultSettingsBuilderRequest extends BaseRequest implements SettingsBuilderRequest {
155             private final Source globalSettingsSource;
156             private final Source projectSettingsSource;
157             private final Source userSettingsSource;
158 
159             @SuppressWarnings("checkstyle:ParameterNumber")
160             DefaultSettingsBuilderRequest(
161                     @Nonnull Session session,
162                     @Nullable Source globalSettingsSource,
163                     @Nullable Source projectSettingsSource,
164                     @Nullable Source userSettingsSource) {
165                 super(session);
166                 this.globalSettingsSource = globalSettingsSource;
167                 this.projectSettingsSource = projectSettingsSource;
168                 this.userSettingsSource = userSettingsSource;
169             }
170 
171             @Nonnull
172             @Override
173             public Optional<Source> getGlobalSettingsSource() {
174                 return Optional.ofNullable(globalSettingsSource);
175             }
176 
177             @Nonnull
178             @Override
179             public Optional<Source> getProjectSettingsSource() {
180                 return Optional.ofNullable(projectSettingsSource);
181             }
182 
183             @Nonnull
184             @Override
185             public Optional<Source> getUserSettingsSource() {
186                 return Optional.ofNullable(userSettingsSource);
187             }
188         }
189     }
190 }