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
26 import org.eclipse.aether.RepositorySystemSession;
27 import org.eclipse.aether.impl.OfflineController;
28 import org.eclipse.aether.repository.RemoteRepository;
29 import org.eclipse.aether.transfer.RepositoryOfflineException;
30 import org.eclipse.aether.util.ConfigUtils;
31
32
33
34
35 @Named
36 public class DefaultOfflineController
37 implements OfflineController
38 {
39
40 static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
41
42 static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
43
44 private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" );
45
46 public DefaultOfflineController()
47 {
48
49 }
50
51 public void checkOffline( RepositorySystemSession session, RemoteRepository repository )
52 throws RepositoryOfflineException
53 {
54 if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) )
55 {
56 return;
57 }
58
59 throw new RepositoryOfflineException( repository );
60 }
61
62 private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository )
63 {
64 String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS );
65 if ( protocols != null )
66 {
67 String protocol = repository.getProtocol();
68 if ( protocol.length() > 0 )
69 {
70 for ( String p : protocols )
71 {
72 if ( p.equalsIgnoreCase( protocol ) )
73 {
74 return true;
75 }
76 }
77 }
78 }
79 return false;
80 }
81
82 private boolean isOfflineHost( RepositorySystemSession session, RemoteRepository repository )
83 {
84 String[] hosts = getConfig( session, CONFIG_PROP_OFFLINE_HOSTS );
85 if ( hosts != null )
86 {
87 String host = repository.getHost();
88 if ( host.length() > 0 )
89 {
90 for ( String h : hosts )
91 {
92 if ( h.equalsIgnoreCase( host ) )
93 {
94 return true;
95 }
96 }
97 }
98 }
99 return false;
100 }
101
102 private String[] getConfig( RepositorySystemSession session, String key )
103 {
104 String value = ConfigUtils.getString( session, "", key ).trim();
105 if ( value.length() <= 0 )
106 {
107 return null;
108 }
109 return SEP.split( value );
110 }
111
112 }