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