1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.repository
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.Collections
;
16 import java
.util
.List
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
21 * A repository on a remote server.
23 public final class RemoteRepository
24 implements ArtifactRepository
27 private static final Pattern URL_PATTERN
=
28 Pattern
.compile( "([^:/]+(:[^:/]{2,}+(?=://))?):(//([^@/]*@)?([^/:]+))?.*" );
30 private final String id
;
32 private final String type
;
34 private final String url
;
36 private final String host
;
38 private final String protocol
;
40 private final RepositoryPolicy releasePolicy
;
42 private final RepositoryPolicy snapshotPolicy
;
44 private final Proxy proxy
;
46 private final Authentication authentication
;
48 private final List
<RemoteRepository
> mirroredRepositories
;
50 private final boolean repositoryManager
;
52 RemoteRepository( Builder builder
)
54 if ( builder
.prototype
!= null )
56 id
= ( builder
.delta
& Builder
.ID
) != 0 ? builder
.id
: builder
.prototype
.id
;
57 type
= ( builder
.delta
& Builder
.TYPE
) != 0 ? builder
.type
: builder
.prototype
.type
;
58 url
= ( builder
.delta
& Builder
.URL
) != 0 ? builder
.url
: builder
.prototype
.url
;
60 ( builder
.delta
& Builder
.RELEASES
) != 0 ? builder
.releasePolicy
: builder
.prototype
.releasePolicy
;
62 ( builder
.delta
& Builder
.SNAPSHOTS
) != 0 ? builder
.snapshotPolicy
: builder
.prototype
.snapshotPolicy
;
63 proxy
= ( builder
.delta
& Builder
.PROXY
) != 0 ? builder
.proxy
: builder
.prototype
.proxy
;
65 ( builder
.delta
& Builder
.AUTH
) != 0 ? builder
.authentication
: builder
.prototype
.authentication
;
67 ( builder
.delta
& Builder
.REPOMAN
) != 0 ? builder
.repositoryManager
68 : builder
.prototype
.repositoryManager
;
69 mirroredRepositories
=
70 ( builder
.delta
& Builder
.MIRRORED
) != 0 ?
copy( builder
.mirroredRepositories
)
71 : builder
.prototype
.mirroredRepositories
;
78 releasePolicy
= builder
.releasePolicy
;
79 snapshotPolicy
= builder
.snapshotPolicy
;
80 proxy
= builder
.proxy
;
81 authentication
= builder
.authentication
;
82 repositoryManager
= builder
.repositoryManager
;
83 mirroredRepositories
= copy( builder
.mirroredRepositories
);
86 Matcher m
= URL_PATTERN
.matcher( url
);
89 protocol
= m
.group( 1 );
90 String host
= m
.group( 5 );
91 this.host
= ( host
!= null ) ? host
: "";
99 private static List
<RemoteRepository
> copy( List
<RemoteRepository
> repos
)
101 if ( repos
== null || repos
.isEmpty() )
103 return Collections
.emptyList();
105 return Collections
.unmodifiableList( Arrays
.asList( repos
.toArray( new RemoteRepository
[repos
.size()] ) ) );
108 public String
getId()
113 public String
getContentType()
119 * Gets the (base) URL of this repository.
121 * @return The (base) URL of this repository, never {@code null}.
123 public String
getUrl()
129 * Gets the protocol part from the repository's URL, for example {@code file} or {@code http}. As suggested by RFC
130 * 2396, section 3.1 "Scheme Component", the protocol name should be treated case-insensitively.
132 * @return The protocol or an empty string if none, never {@code null}.
134 public String
getProtocol()
140 * Gets the host part from the repository's URL.
142 * @return The host or an empty string if none, never {@code null}.
144 public String
getHost()
150 * Gets the policy to apply for snapshot/release artifacts.
152 * @param snapshot {@code true} to retrieve the snapshot policy, {@code false} to retrieve the release policy.
153 * @return The requested repository policy, never {@code null}.
155 public RepositoryPolicy
getPolicy( boolean snapshot
)
157 return snapshot ? snapshotPolicy
: releasePolicy
;
161 * Gets the proxy that has been selected for this repository.
163 * @return The selected proxy or {@code null} if none.
165 public Proxy
getProxy()
171 * Gets the authentication that has been selected for this repository.
173 * @return The selected authentication or {@code null} if none.
175 public Authentication
getAuthentication()
177 return authentication
;
181 * Gets the repositories that this repository serves as a mirror for.
183 * @return The (read-only) repositories being mirrored by this repository, never {@code null}.
185 public List
<RemoteRepository
> getMirroredRepositories()
187 return mirroredRepositories
;
191 * Indicates whether this repository refers to a repository manager or not.
193 * @return {@code true} if this repository is a repository manager, {@code false} otherwise.
195 public boolean isRepositoryManager()
197 return repositoryManager
;
201 public String
toString()
203 StringBuilder buffer
= new StringBuilder( 256 );
204 buffer
.append( getId() );
205 buffer
.append( " (" ).append( getUrl() );
206 buffer
.append( ", " ).append( getContentType() );
207 boolean r
= getPolicy( false ).isEnabled(), s
= getPolicy( true ).isEnabled();
210 buffer
.append( ", releases+snapshots" );
214 buffer
.append( ", releases" );
218 buffer
.append( ", snapshots" );
222 buffer
.append( ", disabled" );
224 if ( isRepositoryManager() )
226 buffer
.append( ", managed" );
228 buffer
.append( ")" );
229 return buffer
.toString();
233 public boolean equals( Object obj
)
239 if ( obj
== null || !getClass().equals( obj
.getClass() ) )
244 RemoteRepository that
= (RemoteRepository
) obj
;
246 return eq( url
, that
.url
) && eq( type
, that
.type
) && eq( id
, that
.id
)
247 && eq( releasePolicy
, that
.releasePolicy
) && eq( snapshotPolicy
, that
.snapshotPolicy
)
248 && eq( proxy
, that
.proxy
) && eq( authentication
, that
.authentication
)
249 && eq( mirroredRepositories
, that
.mirroredRepositories
) && repositoryManager
== that
.repositoryManager
;
252 private static <T
> boolean eq( T s1
, T s2
)
254 return s1
!= null ? s1
.equals( s2
) : s2
== null;
258 public int hashCode()
261 hash
= hash
* 31 + hash( url
);
262 hash
= hash
* 31 + hash( type
);
263 hash
= hash
* 31 + hash( id
);
264 hash
= hash
* 31 + hash( releasePolicy
);
265 hash
= hash
* 31 + hash( snapshotPolicy
);
266 hash
= hash
* 31 + hash( proxy
);
267 hash
= hash
* 31 + hash( authentication
);
268 hash
= hash
* 31 + hash( mirroredRepositories
);
269 hash
= hash
* 31 + ( repositoryManager ?
1 : 0 );
273 private static int hash( Object obj
)
275 return obj
!= null ? obj
.hashCode() : 0;
279 * A builder to create remote repositories.
281 public static final class Builder
284 private static final RepositoryPolicy DEFAULT_POLICY
= new RepositoryPolicy();
286 static final int ID
= 0x0001, TYPE
= 0x0002, URL
= 0x0004, RELEASES
= 0x0008, SNAPSHOTS
= 0x0010,
287 PROXY
= 0x0020, AUTH
= 0x0040, MIRRORED
= 0x0080, REPOMAN
= 0x0100;
291 RemoteRepository prototype
;
299 RepositoryPolicy releasePolicy
= DEFAULT_POLICY
;
301 RepositoryPolicy snapshotPolicy
= DEFAULT_POLICY
;
305 Authentication authentication
;
307 List
<RemoteRepository
> mirroredRepositories
;
309 boolean repositoryManager
;
312 * Creates a new repository builder.
314 * @param id The identifier of the repository, may be {@code null}.
315 * @param type The type of the repository, may be {@code null}.
316 * @param url The (base) URL of the repository, may be {@code null}.
318 public Builder( String id
, String type
, String url
)
320 this.id
= ( id
!= null ) ? id
: "";
321 this.type
= ( type
!= null ) ? type
: "";
322 this.url
= ( url
!= null ) ? url
: "";
326 * Creates a new repository builder which uses the specified remote repository as a prototype for the new one.
327 * All properties which have not been set on the builder will be copied from the prototype when building the
330 * @param prototype The remote repository to use as prototype, must not be {@code null}.
332 public Builder( RemoteRepository prototype
)
334 if ( prototype
== null )
336 throw new IllegalArgumentException( "repository prototype missing" );
338 this.prototype
= prototype
;
342 * Builds a new remote repository from the current values of this builder. The state of the builder itself
345 * @return The remote repository, never {@code null}.
347 public RemoteRepository
build()
349 if ( prototype
!= null && delta
== 0 )
353 return new RemoteRepository( this );
356 private <T
> void delta( int flag
, T builder
, T prototype
)
358 boolean equal
= ( builder
!= null ) ? builder
.equals( prototype
) : prototype
== null;
370 * Sets the identifier of the repository.
372 * @param id The identifier of the repository, may be {@code null}.
373 * @return This builder for chaining, never {@code null}.
375 public Builder
setId( String id
)
377 this.id
= ( id
!= null ) ? id
: "";
378 if ( prototype
!= null )
380 delta( ID
, this.id
, prototype
.getId() );
386 * Sets the type of the repository, e.g. "default".
388 * @param type The type of the repository, may be {@code null}.
389 * @return This builder for chaining, never {@code null}.
391 public Builder
setContentType( String type
)
393 this.type
= ( type
!= null ) ? type
: "";
394 if ( prototype
!= null )
396 delta( TYPE
, this.type
, prototype
.getContentType() );
402 * Sets the (base) URL of the repository.
404 * @param url The URL of the repository, may be {@code null}.
405 * @return This builder for chaining, never {@code null}.
407 public Builder
setUrl( String url
)
409 this.url
= ( url
!= null ) ? url
: "";
410 if ( prototype
!= null )
412 delta( URL
, this.url
, prototype
.getUrl() );
418 * Sets the policy to apply for snapshot and release artifacts.
420 * @param policy The repository policy to set, may be {@code null} to use a default policy.
421 * @return This builder for chaining, never {@code null}.
423 public Builder
setPolicy( RepositoryPolicy policy
)
425 this.releasePolicy
= this.snapshotPolicy
= ( policy
!= null ) ? policy
: DEFAULT_POLICY
;
426 if ( prototype
!= null )
428 delta( RELEASES
, this.releasePolicy
, prototype
.getPolicy( false ) );
429 delta( SNAPSHOTS
, this.snapshotPolicy
, prototype
.getPolicy( true ) );
435 * Sets the policy to apply for release artifacts.
437 * @param releasePolicy The repository policy to set, may be {@code null} to use a default policy.
438 * @return This builder for chaining, never {@code null}.
440 public Builder
setReleasePolicy( RepositoryPolicy releasePolicy
)
442 this.releasePolicy
= ( releasePolicy
!= null ) ? releasePolicy
: DEFAULT_POLICY
;
443 if ( prototype
!= null )
445 delta( RELEASES
, this.releasePolicy
, prototype
.getPolicy( false ) );
451 * Sets the policy to apply for snapshot artifacts.
453 * @param snapshotPolicy The repository policy to set, may be {@code null} to use a default policy.
454 * @return This builder for chaining, never {@code null}.
456 public Builder
setSnapshotPolicy( RepositoryPolicy snapshotPolicy
)
458 this.snapshotPolicy
= ( snapshotPolicy
!= null ) ? snapshotPolicy
: DEFAULT_POLICY
;
459 if ( prototype
!= null )
461 delta( SNAPSHOTS
, this.snapshotPolicy
, prototype
.getPolicy( true ) );
467 * Sets the proxy to use in order to access the repository.
469 * @param proxy The proxy to use, may be {@code null}.
470 * @return This builder for chaining, never {@code null}.
472 public Builder
setProxy( Proxy proxy
)
475 if ( prototype
!= null )
477 delta( PROXY
, this.proxy
, prototype
.getProxy() );
483 * Sets the authentication to use in order to access the repository.
485 * @param authentication The authentication to use, may be {@code null}.
486 * @return This builder for chaining, never {@code null}.
488 public Builder
setAuthentication( Authentication authentication
)
490 this.authentication
= authentication
;
491 if ( prototype
!= null )
493 delta( AUTH
, this.authentication
, prototype
.getAuthentication() );
499 * Sets the repositories being mirrored by the repository.
501 * @param mirroredRepositories The repositories being mirrored by the repository, may be {@code null}.
502 * @return This builder for chaining, never {@code null}.
504 public Builder
setMirroredRepositories( List
<RemoteRepository
> mirroredRepositories
)
506 if ( this.mirroredRepositories
== null )
508 this.mirroredRepositories
= new ArrayList
<RemoteRepository
>();
512 this.mirroredRepositories
.clear();
514 if ( mirroredRepositories
!= null )
516 this.mirroredRepositories
.addAll( mirroredRepositories
);
518 if ( prototype
!= null )
520 delta( MIRRORED
, this.mirroredRepositories
, prototype
.getMirroredRepositories() );
526 * Adds the specified repository to the list of repositories being mirrored by the repository. If this builder
527 * was {@link #RemoteRepository.Builder(RemoteRepository) constructed from a prototype}, the given repository
528 * will be added to the list of mirrored repositories from the prototype.
530 * @param mirroredRepository The repository being mirrored by the repository, may be {@code null}.
531 * @return This builder for chaining, never {@code null}.
533 public Builder
addMirroredRepository( RemoteRepository mirroredRepository
)
535 if ( mirroredRepository
!= null )
537 if ( this.mirroredRepositories
== null )
539 this.mirroredRepositories
= new ArrayList
<RemoteRepository
>();
540 if ( prototype
!= null )
542 mirroredRepositories
.addAll( prototype
.getMirroredRepositories() );
545 mirroredRepositories
.add( mirroredRepository
);
546 if ( prototype
!= null )
555 * Marks the repository as a repository manager or not.
557 * @param repositoryManager {@code true} if the repository points at a repository manager, {@code false} if the
558 * repository is just serving static contents.
559 * @return This builder for chaining, never {@code null}.
561 public Builder
setRepositoryManager( boolean repositoryManager
)
563 this.repositoryManager
= repositoryManager
;
564 if ( prototype
!= null )
566 delta( REPOMAN
, this.repositoryManager
, prototype
.isRepositoryManager() );