1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.regex.Pattern;
23
24 import javax.inject.Named;
25 import javax.inject.Singleton;
26
27 import org.eclipse.aether.RepositorySystemSession;
28 import org.eclipse.aether.impl.OfflineController;
29 import org.eclipse.aether.repository.RemoteRepository;
30 import org.eclipse.aether.transfer.RepositoryOfflineException;
31 import org.eclipse.aether.util.ConfigUtils;
32
33 import static java.util.Objects.requireNonNull;
34
35
36
37
38 @Singleton
39 @Named
40 public class DefaultOfflineController
41 implements OfflineController
42 {
43
44 static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
45
46 static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
47
48 private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" );
49
50 public DefaultOfflineController()
51 {
52
53 }
54
55 public void checkOffline( RepositorySystemSession session, RemoteRepository repository )
56 throws RepositoryOfflineException
57 {
58 requireNonNull( session, "session cannot be null" );
59 requireNonNull( repository, "repository cannot be null" );
60 if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) )
61 {
62 return;
63 }
64
65 throw new RepositoryOfflineException( repository );
66 }
67
68 private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository )
69 {
70 String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS );
71 if ( protocols != null )
72 {
73 String protocol = repository.getProtocol();
74 if ( protocol.length() > 0 )
75 {
76 for ( String p : protocols )
77 {
78 if ( p.equalsIgnoreCase( protocol ) )
79 {
80 return true;
81 }
82 }
83 }
84 }
85 return false;
86 }
87
88 private boolean isOfflineHost( RepositorySystemSession session, RemoteRepository repository )
89 {
90 String[] hosts = getConfig( session, CONFIG_PROP_OFFLINE_HOSTS );
91 if ( hosts != null )
92 {
93 String host = repository.getHost();
94 if ( host.length() > 0 )
95 {
96 for ( String h : hosts )
97 {
98 if ( h.equalsIgnoreCase( host ) )
99 {
100 return true;
101 }
102 }
103 }
104 }
105 return false;
106 }
107
108 private String[] getConfig( RepositorySystemSession session, String key )
109 {
110 String value = ConfigUtils.getString( session, "", key ).trim();
111 if ( value.isEmpty() )
112 {
113 return null;
114 }
115 return SEP.split( value );
116 }
117
118 }