001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.regex.Pattern;
023
024import javax.inject.Inject;
025import javax.inject.Named;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.impl.OfflineController;
029import org.eclipse.aether.repository.RemoteRepository;
030import org.eclipse.aether.spi.locator.Service;
031import org.eclipse.aether.spi.locator.ServiceLocator;
032import org.eclipse.aether.spi.log.Logger;
033import org.eclipse.aether.spi.log.LoggerFactory;
034import org.eclipse.aether.spi.log.NullLoggerFactory;
035import org.eclipse.aether.transfer.RepositoryOfflineException;
036import org.eclipse.aether.util.ConfigUtils;
037
038/**
039 * 
040 */
041@Named
042public class DefaultOfflineController
043    implements OfflineController, Service
044{
045
046    static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
047
048    static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
049
050    private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" );
051
052    private Logger logger = NullLoggerFactory.LOGGER;
053
054    public DefaultOfflineController()
055    {
056        // enables default constructor
057    }
058
059    @Inject
060    DefaultOfflineController( LoggerFactory loggerFactory )
061    {
062        setLoggerFactory( loggerFactory );
063    }
064
065    public void initService( ServiceLocator locator )
066    {
067        setLoggerFactory( locator.getService( LoggerFactory.class ) );
068    }
069
070    public DefaultOfflineController setLoggerFactory( LoggerFactory loggerFactory )
071    {
072        this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
073        return this;
074    }
075
076    public void checkOffline( RepositorySystemSession session, RemoteRepository repository )
077        throws RepositoryOfflineException
078    {
079        if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) )
080        {
081            return;
082        }
083
084        throw new RepositoryOfflineException( repository );
085    }
086
087    private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository )
088    {
089        String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS );
090        if ( protocols != null )
091        {
092            String protocol = repository.getProtocol();
093            if ( protocol.length() > 0 )
094            {
095                for ( String p : protocols )
096                {
097                    if ( p.equalsIgnoreCase( protocol ) )
098                    {
099                        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}