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