View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // enables default constructor
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 }