001    package org.apache.maven.scm.manager.plexus;
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    
022    import org.apache.maven.scm.log.ScmLogger;
023    import org.apache.maven.scm.manager.AbstractScmManager;
024    import org.apache.maven.scm.provider.ScmProvider;
025    import org.codehaus.plexus.logging.LogEnabled;
026    import org.codehaus.plexus.logging.Logger;
027    import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
028    
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    /**
033     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
034     * @author <a href="mailto:brett@apache.org">Brett Porter</a>
035     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
036     * @author Olivier Lamy
037     *
038     * @plexus.component role="org.apache.maven.scm.manager.ScmManager"
039     */
040    public class DefaultScmManager
041        extends AbstractScmManager
042        implements Initializable, LogEnabled
043    {
044        /**
045         * @plexus.requirement role="org.apache.maven.scm.provider.ScmProvider"
046         */
047        private Map<String,ScmProvider> scmProviders;
048    
049        private Logger logger;
050    
051        // ----------------------------------------------------------------------
052        // LogEnabled implementation
053        // ----------------------------------------------------------------------
054    
055        public void enableLogging( Logger logger )
056        {
057            this.logger = logger;
058        }
059    
060        protected Logger getLogger()
061        {
062            return logger;
063        }
064    
065        protected void setupLogger( Object component )
066        {
067            setupLogger( component, logger );
068        }
069    
070        protected void setupLogger( Object component, String subCategory )
071        {
072            if ( subCategory == null )
073            {
074                throw new IllegalStateException( "Logging category must be defined." );
075            }
076    
077            Logger logger = this.logger.getChildLogger( subCategory );
078    
079            setupLogger( component, logger );
080        }
081    
082        protected void setupLogger( Object component, Logger logger )
083        {
084            if ( component instanceof LogEnabled )
085            {
086                ( (LogEnabled) component ).enableLogging( logger );
087            }
088        }
089    
090        // ----------------------------------------------------------------------
091        // Component Lifecycle
092        // ----------------------------------------------------------------------
093    
094        /** {@inheritDoc} */
095        public void initialize()
096        {
097            if ( scmProviders == null )
098            {
099                scmProviders = new HashMap<String,ScmProvider>( 0 );
100            }
101    
102            if ( getLogger().isWarnEnabled() && scmProviders.size() == 0 )
103            {
104                getLogger().warn( "No SCM providers configured." );
105            }
106    
107            setScmProviders( scmProviders );
108        }
109    
110        /** {@inheritDoc} */
111        protected ScmLogger getScmLogger()
112        {
113            return new PlexusLogger( getLogger() );
114        }
115    }