001package org.apache.archiva.redback.rest.services.utils;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.redback.system.check.EnvironmentCheck;
022import org.apache.commons.lang3.time.StopWatch;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.springframework.context.ApplicationContext;
026import org.springframework.stereotype.Service;
027
028import javax.inject.Inject;
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.List;
032
033/**
034 * @author Olivier Lamy
035 * @since 1.4
036 */
037@Service("environmentChecker#rest")
038public class EnvironmentChecker
039{
040
041    private Logger log = LoggerFactory.getLogger( getClass() );
042
043
044    @Inject
045    public EnvironmentChecker( ApplicationContext applicationContext )
046    {
047        Collection<EnvironmentCheck> checkers = applicationContext.getBeansOfType( EnvironmentCheck.class ).values();
048
049        StopWatch stopWatch = new StopWatch();
050        stopWatch.reset();
051        stopWatch.start();
052
053        if ( checkers != null )
054        {
055            List<String> violations = new ArrayList<String>();
056
057            for ( EnvironmentCheck check : checkers )
058            {
059                check.validateEnvironment( violations );
060            }
061
062            if ( !violations.isEmpty() )
063            {
064                StringBuilder msg = new StringBuilder();
065                msg.append( "EnvironmentCheck Failure.\n" );
066                msg.append( "======================================================================\n" );
067                msg.append( " ENVIRONMENT FAILURE !! \n" );
068                msg.append( "\n" );
069
070                for ( String v : violations )
071                {
072                    msg.append( v ).append( "\n" );
073                }
074
075                msg.append( "\n" );
076                msg.append( "======================================================================" );
077                log.error( msg.toString() );
078            }
079        }
080
081        stopWatch.stop();
082        log.info( "time to execute all EnvironmentCheck: {} ms", stopWatch.getTime() );
083    }
084}