o inject the logger

git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@760303 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-03-31 03:34:13 +00:00
parent 453781bc67
commit d9ac548e59
1 changed files with 64 additions and 78 deletions

View File

@ -1,20 +1,16 @@
/* /*
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* or more contributor license agreements. See the NOTICE file * agreements. See the NOTICE file distributed with this work for additional information regarding
* distributed with this work for additional information * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
* regarding copyright ownership. The ASF licenses this file * "License"); you may not use this file except in compliance with the License. You may obtain a
* to you under the Apache License, Version 2.0 (the * copy of the License at
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing, software distributed under the License
* software distributed under the License is distributed on an * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * or implied. See the License for the specific language governing permissions and limitations under
* KIND, either express or implied. See the License for the * the License.
* specific language governing permissions and limitations
* under the License.
*/ */
package org.apache.maven.toolchain; package org.apache.maven.toolchain;
@ -24,54 +20,47 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.model.PersistedToolchains; import org.apache.maven.toolchain.model.PersistedToolchains;
import org.apache.maven.toolchain.model.ToolchainModel; import org.apache.maven.toolchain.model.ToolchainModel;
import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader; import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.context.Context; import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
/** /**
* *
* @author mkleint * @author mkleint
*/ */
@Component(role=ToolchainManager.class) @Component(role = ToolchainManager.class)
public class DefaultToolchainManager extends AbstractLogEnabled public class DefaultToolchainManager
implements ToolchainManager, implements ToolchainManager, ToolchainManagerPrivate
ToolchainManagerPrivate
{ {
@Requirement @Requirement
private PlexusContainer container; private Logger logger;
public DefaultToolchainManager( ) @Requirement
{ private PlexusContainer container;
}
public ToolchainPrivate[] getToolchainsForType( String type ) public ToolchainPrivate[] getToolchainsForType( String type )
throws MisconfiguredToolchainException throws MisconfiguredToolchainException
{ {
try try
{ {
PersistedToolchains pers = readToolchainSettings (); PersistedToolchains pers = readToolchainSettings();
Map<String, ToolchainFactory> factories = container.lookupMap( ToolchainFactory.class ); Map<String, ToolchainFactory> factories = container.lookupMap( ToolchainFactory.class );
List toRet = new ArrayList( ); List toRet = new ArrayList();
if ( pers != null ) if ( pers != null )
{ {
List lst = pers.getToolchains(); List lst = pers.getToolchains();
@ -88,7 +77,7 @@ public class DefaultToolchainManager extends AbstractLogEnabled
} }
else else
{ {
getLogger().error("Missing toolchain factory for type:" + toolchainModel.getType() + ". Possibly caused by misconfigured project."); logger.error( "Missing toolchain factory for type:" + toolchainModel.getType() + ". Possibly caused by misconfigured project." );
} }
} }
} }
@ -101,54 +90,55 @@ public class DefaultToolchainManager extends AbstractLogEnabled
toRet.add( tool ); toRet.add( tool );
} }
} }
ToolchainPrivate[] tc = new ToolchainPrivate[ toRet.size() ]; ToolchainPrivate[] tc = new ToolchainPrivate[toRet.size()];
return (ToolchainPrivate[]) toRet.toArray(tc); return (ToolchainPrivate[]) toRet.toArray( tc );
} }
catch ( ComponentLookupException ex ) catch ( ComponentLookupException ex )
{ {
getLogger().fatalError("Error in component lookup", ex); logger.fatalError( "Error in component lookup", ex );
} }
return new ToolchainPrivate[0]; return new ToolchainPrivate[0];
} }
public Toolchain getToolchainFromBuildContext( String type, public Toolchain getToolchainFromBuildContext( String type, MavenSession session )
MavenSession session )
{ {
Map context = retrieveContext(session); Map context = retrieveContext( session );
if ( "javac".equals( type )) if ( "javac".equals( type ) )
{ {
//HACK to make compiler plugin happy //HACK to make compiler plugin happy
type = "jdk"; type = "jdk";
} }
Object obj = context.get( getStorageKey( type ) ); Object obj = context.get( getStorageKey( type ) );
ToolchainModel model = (ToolchainModel)obj; ToolchainModel model = (ToolchainModel) obj;
if ( model != null ) if ( model != null )
{ {
try try
{ {
ToolchainFactory fact = container.lookup(ToolchainFactory.class, type); ToolchainFactory fact = container.lookup( ToolchainFactory.class, type );
return fact.createToolchain( model ); return fact.createToolchain( model );
} }
catch ( ComponentLookupException ex ) catch ( ComponentLookupException ex )
{ {
getLogger().fatalError("Error in component lookup", ex); logger.fatalError( "Error in component lookup", ex );
} }
catch ( MisconfiguredToolchainException ex ) catch ( MisconfiguredToolchainException ex )
{ {
getLogger().error("Misconfigured toolchain.", ex); logger.error( "Misconfigured toolchain.", ex );
} }
} }
return null; return null;
} }
private MavenProject getCurrentProject(MavenSession session) { private MavenProject getCurrentProject( MavenSession session )
{
//use reflection since MavenSession.getCurrentProject() is not part of 2.0.8 //use reflection since MavenSession.getCurrentProject() is not part of 2.0.8
try try
{ {
Method meth = session.getClass().getMethod("getCurrentProject", new Class[0]); Method meth = session.getClass().getMethod( "getCurrentProject", new Class[0] );
return (MavenProject) meth.invoke (session ); return (MavenProject) meth.invoke( session );
} catch (Exception ex) }
catch ( Exception ex )
{ {
//just ignore, we're running in pre- 2.0.9 //just ignore, we're running in pre- 2.0.9
} }
@ -157,14 +147,14 @@ public class DefaultToolchainManager extends AbstractLogEnabled
private Map retrieveContext( MavenSession session ) private Map retrieveContext( MavenSession session )
{ {
if (session == null) if ( session == null )
{ {
return new HashMap(); return new HashMap();
} }
PluginDescriptor desc = new PluginDescriptor(); PluginDescriptor desc = new PluginDescriptor();
desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() ); desc.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId ("toolchains") ); desc.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( "toolchains" ) );
MavenProject current = getCurrentProject(session); MavenProject current = getCurrentProject( session );
if ( current != null ) if ( current != null )
{ {
return session.getPluginContext( desc, current ); return session.getPluginContext( desc, current );
@ -173,12 +163,10 @@ public class DefaultToolchainManager extends AbstractLogEnabled
return new HashMap(); return new HashMap();
} }
public void storeToolchainToBuildContext( ToolchainPrivate toolchain, MavenSession session )
public void storeToolchainToBuildContext( ToolchainPrivate toolchain,
MavenSession session )
{ {
Map context = retrieveContext( session ); Map context = retrieveContext( session );
context.put( getStorageKey( toolchain.getType() ), toolchain.getModel () ); context.put( getStorageKey( toolchain.getType() ), toolchain.getModel() );
} }
public static final String getStorageKey( String type ) public static final String getStorageKey( String type )
@ -186,13 +174,11 @@ public class DefaultToolchainManager extends AbstractLogEnabled
return "toolchain-" + type; //NOI18N return "toolchain-" + type; //NOI18N
} }
private PersistedToolchains readToolchainSettings()
private PersistedToolchains readToolchainSettings( )
throws MisconfiguredToolchainException throws MisconfiguredToolchainException
{ {
//TODO how to point to the local path? //TODO how to point to the local path?
File tch = new File( System.getProperty( "user.home" ), File tch = new File( System.getProperty( "user.home" ), ".m2/toolchains.xml" );
".m2/toolchains.xml" );
if ( tch.exists() ) if ( tch.exists() )
{ {
MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader(); MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader();
@ -204,21 +190,21 @@ public class DefaultToolchainManager extends AbstractLogEnabled
} }
catch ( Exception ex ) catch ( Exception ex )
{ {
throw new MisconfiguredToolchainException( "Cannot read toolchains file at " + tch.getAbsolutePath( ), throw new MisconfiguredToolchainException( "Cannot read toolchains file at " + tch.getAbsolutePath(), ex );
ex );
} }
finally finally
{ {
if (in != null) if ( in != null )
{ {
try try
{ {
in.close(); in.close();
} }
catch (IOException ex) catch ( IOException ex )
{ } {
}
} }
// IOUtil.close( in ); // IOUtil.close( in );
} }
} }
else else