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.resolver.internal.ant.types;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.maven.resolver.internal.ant.AntRepoSys;
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.Project;
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.types.DataType;
29  import org.apache.tools.ant.types.Reference;
30  import org.eclipse.aether.repository.RepositoryPolicy;
31  
32  /**
33   */
34  public class RemoteRepository extends DataType implements RemoteRepositoryContainer {
35  
36      private String id;
37  
38      private String url;
39  
40      private String type;
41  
42      private Policy releasePolicy;
43  
44      private Policy snapshotPolicy;
45  
46      private boolean releases = true;
47  
48      private boolean snapshots = false;
49  
50      private String checksums;
51  
52      private String updates;
53  
54      private Authentication authentication;
55  
56      @Override
57      public void setProject(Project project) {
58          super.setProject(project);
59  
60          // NOTE: Just trigger side-effect of default initialization before this type potentially overrides central
61          AntRepoSys.getInstance(project);
62      }
63  
64      protected RemoteRepository getRef() {
65          return getCheckedRef(RemoteRepository.class);
66      }
67  
68      @Override
69      public void validate(Task task) {
70          if (isReference()) {
71              getRef().validate(task);
72          } else {
73              if (url == null || url.length() <= 0) {
74                  throw new BuildException("You must specify the 'url' for a remote repository");
75              }
76              if (id == null || id.length() <= 0) {
77                  throw new BuildException("You must specify the 'id' for a remote repository");
78              }
79          }
80      }
81  
82      @Override
83      public void setRefid(Reference ref) {
84          if (id != null || url != null || type != null || checksums != null || updates != null) {
85              throw tooManyAttributes();
86          }
87          if (releasePolicy != null || snapshotPolicy != null || authentication != null) {
88              throw noChildrenAllowed();
89          }
90          super.setRefid(ref);
91      }
92  
93      public String getId() {
94          if (isReference()) {
95              return getRef().getId();
96          }
97          return id;
98      }
99  
100     public void setId(String id) {
101         this.id = id;
102     }
103 
104     public String getUrl() {
105         if (isReference()) {
106             return getRef().getUrl();
107         }
108         return url;
109     }
110 
111     public void setUrl(String url) {
112         checkAttributesAllowed();
113         this.url = url;
114     }
115 
116     public String getType() {
117         if (isReference()) {
118             return getRef().getType();
119         }
120         return (type != null) ? type : "default";
121     }
122 
123     public void setType(String type) {
124         checkAttributesAllowed();
125         this.type = type;
126     }
127 
128     public Policy getReleasePolicy() {
129         if (isReference()) {
130             return getRef().getReleasePolicy();
131         }
132         return releasePolicy;
133     }
134 
135     public void addReleases(Policy policy) {
136         checkChildrenAllowed();
137         if (this.releasePolicy != null) {
138             throw new BuildException("You must not specify multiple <releases> elements");
139         }
140         this.releasePolicy = policy;
141     }
142 
143     public Policy getSnapshotPolicy() {
144         if (isReference()) {
145             return getRef().getSnapshotPolicy();
146         }
147         return snapshotPolicy;
148     }
149 
150     public void addSnapshots(Policy policy) {
151         checkChildrenAllowed();
152         if (this.snapshotPolicy != null) {
153             throw new BuildException("You must not specify multiple <snapshots> elements");
154         }
155         this.snapshotPolicy = policy;
156     }
157 
158     public boolean isReleases() {
159         if (isReference()) {
160             return getRef().isReleases();
161         }
162         return releases;
163     }
164 
165     public void setReleases(boolean releases) {
166         checkAttributesAllowed();
167         this.releases = releases;
168     }
169 
170     public boolean isSnapshots() {
171         if (isReference()) {
172             return getRef().isSnapshots();
173         }
174         return snapshots;
175     }
176 
177     public void setSnapshots(boolean snapshots) {
178         checkAttributesAllowed();
179         this.snapshots = snapshots;
180     }
181 
182     public String getUpdates() {
183         if (isReference()) {
184             return getRef().getUpdates();
185         }
186         return (updates != null) ? updates : RepositoryPolicy.UPDATE_POLICY_DAILY;
187     }
188 
189     public void setUpdates(String updates) {
190         checkAttributesAllowed();
191         checkUpdates(updates);
192         this.updates = updates;
193     }
194 
195     protected static void checkUpdates(String updates) {
196         if (!RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(updates)
197                 && !RepositoryPolicy.UPDATE_POLICY_DAILY.equals(updates)
198                 && !RepositoryPolicy.UPDATE_POLICY_NEVER.equals(updates)
199                 && !updates.startsWith(RepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
200             throw new BuildException("'" + updates + "' is not a permitted update policy");
201         }
202     }
203 
204     public String getChecksums() {
205         if (isReference()) {
206             return getRef().getChecksums();
207         }
208         return (checksums != null) ? checksums : RepositoryPolicy.CHECKSUM_POLICY_WARN;
209     }
210 
211     public void setChecksums(String checksums) {
212         checkAttributesAllowed();
213         checkChecksums(checksums);
214         this.checksums = checksums;
215     }
216 
217     protected static void checkChecksums(String checksums) {
218         if (!RepositoryPolicy.CHECKSUM_POLICY_FAIL.equals(checksums)
219                 && !RepositoryPolicy.CHECKSUM_POLICY_WARN.equals(checksums)
220                 && !RepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals(checksums)) {
221             throw new BuildException("'" + checksums + "' is not a permitted checksum policy");
222         }
223     }
224 
225     public Authentication getAuthentication() {
226         if (isReference()) {
227             return getRef().getAuthentication();
228         }
229         return authentication;
230     }
231 
232     public void addAuthentication(Authentication authentication) {
233         checkChildrenAllowed();
234         if (this.authentication != null) {
235             throw new BuildException("You must not specify multiple <authentication> elements");
236         }
237         this.authentication = authentication;
238     }
239 
240     public void setAuthRef(Reference ref) {
241         checkAttributesAllowed();
242         if (authentication == null) {
243             authentication = new Authentication();
244             authentication.setProject(getProject());
245         }
246         authentication.setRefid(ref);
247     }
248 
249     @Override
250     public List<RemoteRepository> getRepositories() {
251         return Collections.singletonList(this);
252     }
253 
254     /**
255      */
256     public static class Policy {
257 
258         private boolean enabled = true;
259 
260         private String checksumPolicy;
261 
262         private String updatePolicy;
263 
264         public boolean isEnabled() {
265             return enabled;
266         }
267 
268         public void setEnabled(boolean enabled) {
269             this.enabled = enabled;
270         }
271 
272         public String getChecksums() {
273             return checksumPolicy;
274         }
275 
276         public void setChecksums(String checksumPolicy) {
277             checkChecksums(checksumPolicy);
278             this.checksumPolicy = checksumPolicy;
279         }
280 
281         public String getUpdates() {
282             return updatePolicy;
283         }
284 
285         public void setUpdates(String updatePolicy) {
286             checkUpdates(updatePolicy);
287             this.updatePolicy = updatePolicy;
288         }
289     }
290 }