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  
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          // enables default constructor
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 }