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.Inject;
25 import javax.inject.Named;
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.spi.locator.Service;
31 import org.eclipse.aether.spi.locator.ServiceLocator;
32 import org.eclipse.aether.spi.log.Logger;
33 import org.eclipse.aether.spi.log.LoggerFactory;
34 import org.eclipse.aether.spi.log.NullLoggerFactory;
35 import org.eclipse.aether.transfer.RepositoryOfflineException;
36 import org.eclipse.aether.util.ConfigUtils;
37
38
39
40
41 @Named
42 public class DefaultOfflineController
43 implements OfflineController, Service
44 {
45
46 static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
47
48 static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
49
50 private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" );
51
52 private Logger logger = NullLoggerFactory.LOGGER;
53
54 public DefaultOfflineController()
55 {
56
57 }
58
59 @Inject
60 DefaultOfflineController( LoggerFactory loggerFactory )
61 {
62 setLoggerFactory( loggerFactory );
63 }
64
65 public void initService( ServiceLocator locator )
66 {
67 setLoggerFactory( locator.getService( LoggerFactory.class ) );
68 }
69
70 public DefaultOfflineController setLoggerFactory( LoggerFactory loggerFactory )
71 {
72 this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
73 return this;
74 }
75
76 public void checkOffline( RepositorySystemSession session, RemoteRepository repository )
77 throws RepositoryOfflineException
78 {
79 if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) )
80 {
81 return;
82 }
83
84 throw new RepositoryOfflineException( repository );
85 }
86
87 private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository )
88 {
89 String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS );
90 if ( protocols != null )
91 {
92 String protocol = repository.getProtocol();
93 if ( protocol.length() > 0 )
94 {
95 for ( String p : protocols )
96 {
97 if ( p.equalsIgnoreCase( protocol ) )
98 {
99 return true;
100 }
101 }
102 }
103 }
104 return false;
105 }
106
107 private boolean isOfflineHost( RepositorySystemSession session, RemoteRepository repository )
108 {
109 String[] hosts = getConfig( session, CONFIG_PROP_OFFLINE_HOSTS );
110 if ( hosts != null )
111 {
112 String host = repository.getHost();
113 if ( host.length() > 0 )
114 {
115 for ( String h : hosts )
116 {
117 if ( h.equalsIgnoreCase( host ) )
118 {
119 return true;
120 }
121 }
122 }
123 }
124 return false;
125 }
126
127 private String[] getConfig( RepositorySystemSession session, String key )
128 {
129 String value = ConfigUtils.getString( session, "", key ).trim();
130 if ( value.length() <= 0 )
131 {
132 return null;
133 }
134 return SEP.split( value );
135 }
136
137 }