View Javadoc

1   package org.sonatype.aether.repository;
2   
3   /*******************************************************************************
4    * Copyright (c) 2010-2011 Sonatype, Inc.
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    *   http://www.eclipse.org/legal/epl-v10.html
9    *******************************************************************************/
10  
11  import java.util.Collections;
12  import java.util.List;
13  import java.util.regex.Matcher;
14  import java.util.regex.Pattern;
15  
16  /**
17   * A repository on a remote server.
18   * 
19   * @author Benjamin Bentmann
20   */
21  public class RemoteRepository
22      implements ArtifactRepository
23  {
24  
25      private static final Pattern URL_PATTERN =
26          Pattern.compile( "([^:/]+(:[^:/]{2,}+(?=://))?):(//([^@/]*@)?([^/:]+))?.*" );
27  
28      private String id = "";
29  
30      private String type = "";
31  
32      private String url = "";
33  
34      private RepositoryPolicy releasePolicy;
35  
36      private RepositoryPolicy snapshotPolicy;
37  
38      private Proxy proxy;
39  
40      private Authentication authentication;
41  
42      private List<RemoteRepository> mirroredRepositories = Collections.emptyList();
43  
44      private boolean repositoryManager;
45  
46      /**
47       * Creates a new repository using the default release/snapshot policies.
48       */
49      public RemoteRepository()
50      {
51          setPolicy( true, null );
52          setPolicy( false, null );
53      }
54  
55      /**
56       * Creates a shallow copy of the specified repository.
57       * 
58       * @param repository The repository to copy, must not be {@code null}.
59       */
60      public RemoteRepository( RemoteRepository repository )
61      {
62          setId( repository.getId() );
63          setContentType( repository.getContentType() );
64          setUrl( repository.getUrl() );
65          setPolicy( true, repository.getPolicy( true ) );
66          setPolicy( false, repository.getPolicy( false ) );
67          setAuthentication( repository.getAuthentication() );
68          setProxy( repository.getProxy() );
69          setMirroredRepositories( repository.getMirroredRepositories() );
70          setRepositoryManager( repository.isRepositoryManager() );
71      }
72  
73      /**
74       * Creates a new repository with the specified properties and the default policies.
75       * 
76       * @param id The identifier of the repository, may be {@code null}.
77       * @param type The type of the repository, may be {@code null}.
78       * @param url The (base) URL of the repository, may be {@code null}.
79       */
80      public RemoteRepository( String id, String type, String url )
81      {
82          setId( id );
83          setContentType( type );
84          setUrl( url );
85          setPolicy( true, null );
86          setPolicy( false, null );
87      }
88  
89      public String getId()
90      {
91          return id;
92      }
93  
94      /**
95       * Sets the identifier of this repository.
96       * 
97       * @param id The identifier of this repository, may be {@code null}.
98       * @return This repository for chaining, never {@code null}.
99       */
100     public RemoteRepository setId( String id )
101     {
102         this.id = ( id != null ) ? id : "";
103 
104         return this;
105     }
106 
107     public String getContentType()
108     {
109         return type;
110     }
111 
112     /**
113      * Sets the type of this repository, e.g. "default".
114      * 
115      * @param type The type of this repository, may be {@code null}.
116      * @return This repository for chaining, never {@code null}.
117      */
118     public RemoteRepository setContentType( String type )
119     {
120         this.type = ( type != null ) ? type : "";
121 
122         return this;
123     }
124 
125     /**
126      * Gets the (base) URL of this repository.
127      * 
128      * @return The (base) URL of this repository, never {@code null}.
129      */
130     public String getUrl()
131     {
132         return url;
133     }
134 
135     /**
136      * Sets the (base) URL of this repository.
137      * 
138      * @param url The URL of this repository, may be {@code null}.
139      * @return This repository for chaining, never {@code null}.
140      */
141     public RemoteRepository setUrl( String url )
142     {
143         this.url = ( url != null ) ? url : "";
144 
145         return this;
146     }
147 
148     /**
149      * Gets the protocol part from the repository's URL, for example {@code file} or {@code http}. As suggested by RFC
150      * 2396, section 3.1 "Scheme Component", the protocol name should be treated case-insensitively.
151      * 
152      * @return The protocol or an empty string if none, never {@code null}.
153      */
154     public String getProtocol()
155     {
156         Matcher m = URL_PATTERN.matcher( this.url );
157 
158         if ( m.matches() )
159         {
160             return m.group( 1 );
161         }
162         return "";
163     }
164 
165     /**
166      * Gets the host part from the repository's URL.
167      * 
168      * @return The host or an empty string if none, never {@code null}.
169      */
170     public String getHost()
171     {
172         Matcher m = URL_PATTERN.matcher( this.url );
173 
174         if ( m.matches() )
175         {
176             String host = m.group( 5 );
177             if ( host != null )
178             {
179                 return host;
180             }
181         }
182         return "";
183     }
184 
185     /**
186      * Gets the policy to apply for snapshot/release artifacts.
187      * 
188      * @param snapshot {@code true} to retrieve the snapshot policy, {@code false} to retrieve the release policy.
189      * @return The requested repository policy, never {@code null}.
190      */
191     public RepositoryPolicy getPolicy( boolean snapshot )
192     {
193         return snapshot ? snapshotPolicy : releasePolicy;
194     }
195 
196     /**
197      * Sets the policy to apply for snapshot/release artifacts.
198      * 
199      * @param snapshot {@code true} to set the snapshot policy, {@code false} to set the release policy.
200      * @param policy The repository policy to set, may be {@code null} to use a default policy.
201      * @return This repository for chaining, never {@code null}.
202      */
203     public RemoteRepository setPolicy( boolean snapshot, RepositoryPolicy policy )
204     {
205         if ( policy == null )
206         {
207             policy = new RepositoryPolicy();
208         }
209 
210         if ( snapshot )
211         {
212             snapshotPolicy = policy;
213         }
214         else
215         {
216             releasePolicy = policy;
217         }
218 
219         return this;
220     }
221 
222     /**
223      * Gets the proxy that has been selected for this repository.
224      * 
225      * @return The selected proxy or {@code null} if none.
226      */
227     public Proxy getProxy()
228     {
229         return proxy;
230     }
231 
232     /**
233      * Sets the proxy to use in order to access this repository.
234      * 
235      * @param proxy The proxy to use, may be {@code null}.
236      * @return This repository for chaining, never {@code null}.
237      */
238     public RemoteRepository setProxy( Proxy proxy )
239     {
240         this.proxy = proxy;
241 
242         return this;
243     }
244 
245     /**
246      * Gets the authentication that has been selected for this repository.
247      * 
248      * @return The selected authentication or {@code null} if none.
249      */
250     public Authentication getAuthentication()
251     {
252         return authentication;
253     }
254 
255     /**
256      * Sets the authentication to use in order to access this repository.
257      * 
258      * @param authentication The authentication to use, may be {@code null}.
259      * @return This repository for chaining, never {@code null}.
260      */
261     public RemoteRepository setAuthentication( Authentication authentication )
262     {
263         this.authentication = authentication;
264 
265         return this;
266     }
267 
268     /**
269      * Gets the repositories that this repository serves as a mirror for.
270      * 
271      * @return The repositories being mirrored by this repository, never {@code null}.
272      */
273     public List<RemoteRepository> getMirroredRepositories()
274     {
275         return mirroredRepositories;
276     }
277 
278     /**
279      * Sets the repositories being mirrored by this repository.
280      * 
281      * @param mirroredRepositories The repositories being mirrored by this repository, may be {@code null}.
282      * @return This repository for chaining, never {@code null}.
283      */
284     public RemoteRepository setMirroredRepositories( List<RemoteRepository> mirroredRepositories )
285     {
286         if ( mirroredRepositories == null || mirroredRepositories.isEmpty() )
287         {
288             this.mirroredRepositories = Collections.emptyList();
289         }
290         else
291         {
292             this.mirroredRepositories = mirroredRepositories;
293         }
294         return this;
295     }
296 
297     /**
298      * Indicates whether this repository refers to a repository manager or not.
299      * 
300      * @return {@code true} if this repository is a repository manager, {@code false} otherwise.
301      */
302     public boolean isRepositoryManager()
303     {
304         return repositoryManager;
305     }
306 
307     /**
308      * Marks this repository as a repository manager or not.
309      * 
310      * @param repositoryManager {@code true} if this repository points at a repository manager, {@code false} if the
311      *            repository is just serving static contents.
312      * @return This repository for chaining, never {@code null}.
313      */
314     public RemoteRepository setRepositoryManager( boolean repositoryManager )
315     {
316         this.repositoryManager = repositoryManager;
317         return this;
318     }
319 
320     @Override
321     public String toString()
322     {
323         StringBuilder buffer = new StringBuilder( 256 );
324         buffer.append( getId() );
325         buffer.append( " (" ).append( getUrl() );
326         boolean r = getPolicy( false ).isEnabled(), s = getPolicy( true ).isEnabled();
327         if ( r && s )
328         {
329             buffer.append( ", releases+snapshots" );
330         }
331         else if ( r )
332         {
333             buffer.append( ", releases" );
334         }
335         else if ( s )
336         {
337             buffer.append( ", snapshots" );
338         }
339         else
340         {
341             buffer.append( ", disabled" );
342         }
343         if ( isRepositoryManager() )
344         {
345             buffer.append( ", managed" );
346         }
347         buffer.append( ")" );
348         return buffer.toString();
349     }
350 
351     @Override
352     public boolean equals( Object obj )
353     {
354         if ( this == obj )
355         {
356             return true;
357         }
358         if ( obj == null || !getClass().equals( obj.getClass() ) )
359         {
360             return false;
361         }
362 
363         RemoteRepository that = (RemoteRepository) obj;
364 
365         return eq( url, that.url ) && eq( type, that.type ) && eq( id, that.id )
366             && eq( releasePolicy, that.releasePolicy ) && eq( snapshotPolicy, that.snapshotPolicy )
367             && eq( proxy, that.proxy ) && eq( authentication, that.authentication )
368             && eq( mirroredRepositories, that.mirroredRepositories ) && repositoryManager == that.repositoryManager;
369     }
370 
371     private static <T> boolean eq( T s1, T s2 )
372     {
373         return s1 != null ? s1.equals( s2 ) : s2 == null;
374     }
375 
376     @Override
377     public int hashCode()
378     {
379         int hash = 17;
380         hash = hash * 31 + hash( url );
381         hash = hash * 31 + hash( type );
382         hash = hash * 31 + hash( id );
383         hash = hash * 31 + hash( releasePolicy );
384         hash = hash * 31 + hash( snapshotPolicy );
385         hash = hash * 31 + hash( proxy );
386         hash = hash * 31 + hash( authentication );
387         hash = hash * 31 + hash( mirroredRepositories );
388         hash = hash * 31 + ( repositoryManager ? 1 : 0 );
389         return hash;
390     }
391 
392     private static int hash( Object obj )
393     {
394         return obj != null ? obj.hashCode() : 0;
395     }
396 
397 }