mirror of https://github.com/apache/maven.git
MNG-5578: Make the workspace reader pluggable by creating a session scope where the MavenSession created can be injected in implementations that have the @SessionScoped annotation
This commit is contained in:
parent
276c7636d3
commit
1d84cbeffa
|
@ -143,6 +143,9 @@ public class DefaultMaven
|
||||||
@Requirement
|
@Requirement
|
||||||
private EventSpyDispatcher eventSpyDispatcher;
|
private EventSpyDispatcher eventSpyDispatcher;
|
||||||
|
|
||||||
|
@Requirement
|
||||||
|
private SessionScope sessionScope;
|
||||||
|
|
||||||
public MavenExecutionResult execute( MavenExecutionRequest request )
|
public MavenExecutionResult execute( MavenExecutionRequest request )
|
||||||
{
|
{
|
||||||
MavenExecutionResult result;
|
MavenExecutionResult result;
|
||||||
|
@ -267,6 +270,27 @@ public class DefaultMaven
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
session.setProjectMap( getProjectMap( session.getProjects() ) );
|
||||||
|
}
|
||||||
|
catch ( DuplicateProjectException e )
|
||||||
|
{
|
||||||
|
return addExceptionToResult( result, e );
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkspaceReader reactorWorkspace;
|
||||||
|
sessionScope.enter();
|
||||||
|
sessionScope.seed( MavenSession.class, session );
|
||||||
|
try
|
||||||
|
{
|
||||||
|
reactorWorkspace = container.lookup( WorkspaceReader.class );
|
||||||
|
}
|
||||||
|
catch ( ComponentLookupException e )
|
||||||
|
{
|
||||||
|
return addExceptionToResult( result, e );
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Desired order of precedence for local artifact repositories
|
// Desired order of precedence for local artifact repositories
|
||||||
//
|
//
|
||||||
|
@ -274,17 +298,7 @@ public class DefaultMaven
|
||||||
// Workspace
|
// Workspace
|
||||||
// User Local Repository
|
// User Local Repository
|
||||||
//
|
//
|
||||||
ReactorReader reactorRepository = null;
|
repoSession.setWorkspaceReader( ChainedWorkspaceReader.newInstance( reactorWorkspace,
|
||||||
try
|
|
||||||
{
|
|
||||||
reactorRepository = new ReactorReader( session, getProjectMap( session.getProjects() ) );
|
|
||||||
}
|
|
||||||
catch ( DuplicateProjectException e )
|
|
||||||
{
|
|
||||||
return addExceptionToResult( result, e );
|
|
||||||
}
|
|
||||||
|
|
||||||
repoSession.setWorkspaceReader( ChainedWorkspaceReader.newInstance( reactorRepository,
|
|
||||||
repoSession.getWorkspaceReader() ) );
|
repoSession.getWorkspaceReader() ) );
|
||||||
|
|
||||||
repoSession.setReadOnly();
|
repoSession.setReadOnly();
|
||||||
|
@ -358,6 +372,8 @@ public class DefaultMaven
|
||||||
return addExceptionToResult( result, e );
|
return addExceptionToResult( result, e );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sessionScope.exit();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,10 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Named;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.apache.maven.artifact.ArtifactUtils;
|
import org.apache.maven.artifact.ArtifactUtils;
|
||||||
import org.apache.maven.execution.MavenSession;
|
import org.apache.maven.execution.MavenSession;
|
||||||
import org.apache.maven.project.MavenProject;
|
import org.apache.maven.project.MavenProject;
|
||||||
|
@ -42,6 +46,8 @@ import org.eclipse.aether.util.artifact.ArtifactIdUtils;
|
||||||
*
|
*
|
||||||
* @author Jason van Zyl
|
* @author Jason van Zyl
|
||||||
*/
|
*/
|
||||||
|
@Named
|
||||||
|
@SessionScoped
|
||||||
class ReactorReader
|
class ReactorReader
|
||||||
implements WorkspaceReader
|
implements WorkspaceReader
|
||||||
{
|
{
|
||||||
|
@ -53,12 +59,13 @@ class ReactorReader
|
||||||
|
|
||||||
private WorkspaceRepository repository;
|
private WorkspaceRepository repository;
|
||||||
|
|
||||||
public ReactorReader( MavenSession session, Map<String, MavenProject> reactorProjects )
|
@Inject
|
||||||
|
public ReactorReader( MavenSession session )
|
||||||
{
|
{
|
||||||
projectsByGAV = reactorProjects;
|
projectsByGAV = session.getProjectMap();
|
||||||
|
|
||||||
projectsByGA = new HashMap<String, List<MavenProject>>( reactorProjects.size() * 2 );
|
projectsByGA = new HashMap<String, List<MavenProject>>( projectsByGAV.size() * 2 );
|
||||||
for ( MavenProject project : reactorProjects.values() )
|
for ( MavenProject project : projectsByGAV.values() )
|
||||||
{
|
{
|
||||||
String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
|
String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package org.apache.maven;
|
||||||
|
|
||||||
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
import org.apache.maven.execution.MavenSession;
|
||||||
|
|
||||||
|
import com.google.inject.AbstractModule;
|
||||||
|
|
||||||
|
@Named
|
||||||
|
public class SessionModule extends AbstractModule
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected void configure()
|
||||||
|
{
|
||||||
|
SessionScope scope = new SessionScope();
|
||||||
|
bindScope( SessionScoped.class, scope );
|
||||||
|
bind( SessionScope.class).toInstance( scope );
|
||||||
|
bind( MavenSession.class ).toProvider( SessionScope.<MavenSession> seededKeyProvider() ).in( scope );
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,136 @@
|
||||||
|
package org.apache.maven;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "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
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.inject.Key;
|
||||||
|
import com.google.inject.OutOfScopeException;
|
||||||
|
import com.google.inject.Provider;
|
||||||
|
import com.google.inject.Scope;
|
||||||
|
import com.google.inject.util.Providers;
|
||||||
|
|
||||||
|
public class SessionScope
|
||||||
|
implements Scope
|
||||||
|
{
|
||||||
|
private static final Provider<Object> SEEDED_KEY_PROVIDER = new Provider<Object>()
|
||||||
|
{
|
||||||
|
public Object get()
|
||||||
|
{
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final class ScopeState
|
||||||
|
{
|
||||||
|
public final Map<Key<?>, Provider<?>> seeded = Maps.newHashMap();
|
||||||
|
|
||||||
|
public final Map<Key<?>, Object> provided = Maps.newHashMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final ThreadLocal<LinkedList<ScopeState>> values = new ThreadLocal<LinkedList<ScopeState>>();
|
||||||
|
|
||||||
|
public void enter()
|
||||||
|
{
|
||||||
|
LinkedList<ScopeState> stack = values.get();
|
||||||
|
if ( stack == null )
|
||||||
|
{
|
||||||
|
stack = new LinkedList<ScopeState>();
|
||||||
|
values.set( stack );
|
||||||
|
}
|
||||||
|
stack.addFirst( new ScopeState() );
|
||||||
|
}
|
||||||
|
|
||||||
|
private ScopeState getScopeState()
|
||||||
|
{
|
||||||
|
LinkedList<ScopeState> stack = values.get();
|
||||||
|
if ( stack == null || stack.isEmpty() )
|
||||||
|
{
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
return stack.getFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exit()
|
||||||
|
{
|
||||||
|
final LinkedList<ScopeState> stack = values.get();
|
||||||
|
if ( stack == null || stack.isEmpty() )
|
||||||
|
{
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
stack.removeFirst();
|
||||||
|
if ( stack.isEmpty() )
|
||||||
|
{
|
||||||
|
values.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void seed( Class<T> clazz, Provider<T> value )
|
||||||
|
{
|
||||||
|
getScopeState().seeded.put( Key.get( clazz ), value );
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void seed( Class<T> clazz, final T value )
|
||||||
|
{
|
||||||
|
getScopeState().seeded.put( Key.get( clazz ), Providers.of( value ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Provider<T> scope( final Key<T> key, final Provider<T> unscoped )
|
||||||
|
{
|
||||||
|
return new Provider<T>()
|
||||||
|
{
|
||||||
|
@SuppressWarnings( "unchecked" )
|
||||||
|
public T get()
|
||||||
|
{
|
||||||
|
LinkedList<ScopeState> stack = values.get();
|
||||||
|
if ( stack == null || stack.isEmpty() )
|
||||||
|
{
|
||||||
|
throw new OutOfScopeException( "Cannot access " + key + " outside of a scoping block" );
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopeState state = stack.getFirst();
|
||||||
|
|
||||||
|
Provider<?> seeded = state.seeded.get( key );
|
||||||
|
|
||||||
|
if ( seeded != null )
|
||||||
|
{
|
||||||
|
return (T) seeded.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
T provided = (T) state.provided.get( key );
|
||||||
|
if ( provided == null && unscoped != null )
|
||||||
|
{
|
||||||
|
provided = unscoped.get();
|
||||||
|
state.provided.put( key, provided );
|
||||||
|
}
|
||||||
|
|
||||||
|
return provided;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings( { "unchecked" } )
|
||||||
|
public static <T> Provider<T> seededKeyProvider()
|
||||||
|
{
|
||||||
|
return (Provider<T>) SEEDED_KEY_PROVIDER;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package org.apache.maven;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "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
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import com.google.inject.ScopeAnnotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that annotated component should be instantiated before session starts and discarded after session execution completes.
|
||||||
|
*
|
||||||
|
* @author Jason van Zyl
|
||||||
|
* @since 3.2.0
|
||||||
|
*/
|
||||||
|
@Target( { TYPE } )
|
||||||
|
@Retention( RUNTIME )
|
||||||
|
@ScopeAnnotation
|
||||||
|
public @interface SessionScoped
|
||||||
|
{
|
||||||
|
}
|
|
@ -393,4 +393,15 @@ public class MavenSession
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, MavenProject> projectMap;
|
||||||
|
|
||||||
|
public void setProjectMap( Map<String, MavenProject> projectMap )
|
||||||
|
{
|
||||||
|
this.projectMap = projectMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, MavenProject> getProjectMap()
|
||||||
|
{
|
||||||
|
return projectMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue