001    package org.apache.maven.execution;
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 java.io.File;
023    import java.util.Arrays;
024    import java.util.Date;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Properties;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import org.apache.maven.artifact.repository.ArtifactRepository;
031    import org.apache.maven.artifact.repository.RepositoryCache;
032    import org.apache.maven.monitor.event.EventDispatcher;
033    import org.apache.maven.plugin.descriptor.PluginDescriptor;
034    import org.apache.maven.project.MavenProject;
035    import org.apache.maven.project.ProjectBuildingRequest;
036    import org.apache.maven.settings.Settings;
037    import org.codehaus.plexus.PlexusContainer;
038    import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
039    import org.sonatype.aether.RepositorySystemSession;
040    
041    /**
042     * @author Jason van Zyl
043     */
044    public class MavenSession
045        implements Cloneable
046    {
047        private PlexusContainer container;
048    
049        private MavenExecutionRequest request;
050    
051        private MavenExecutionResult result;
052    
053        private RepositorySystemSession repositorySession;
054    
055        private final Settings settings;
056    
057        private Properties executionProperties;
058    
059        private MavenProject currentProject;
060    
061        /**
062         * These projects have already been topologically sorted in the {@link org.apache.maven.Maven} component before
063         * being passed into the session.
064         */
065        private List<MavenProject> projects;
066    
067        private MavenProject topLevelProject;
068    
069        private ProjectDependencyGraph projectDependencyGraph;
070    
071        private boolean parallel;
072    
073        private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey =
074            new ConcurrentHashMap<String, Map<String, Map<String, Object>>>();
075    
076        @Deprecated
077        public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
078                             MavenProject project )
079        {
080            this( container, request, result, Arrays.asList( new MavenProject[]{project} ) );
081        }
082    
083        @Deprecated
084        public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
085                             EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
086                             String executionRootDir, Properties executionProperties, Date startTime )
087        {
088            this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir,
089                  executionProperties, null, startTime );
090        }
091    
092        @Deprecated
093        public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
094                             EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
095                             String executionRootDir, Properties executionProperties, Properties userProperties,
096                             Date startTime )
097        {
098            this.container = container;
099            this.settings = settings;
100            this.executionProperties = executionProperties;
101            this.request = new DefaultMavenExecutionRequest();
102            this.request.setUserProperties( userProperties );
103            this.request.setLocalRepository( localRepository );
104            this.request.setGoals( goals );
105            this.request.setBaseDirectory( ( executionRootDir != null ) ? new File( executionRootDir ) : null );
106            this.request.setStartTime( startTime );
107        }
108    
109        @Deprecated
110        public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
111                             List<MavenProject> projects )
112        {
113            this.container = container;
114            this.request = request;
115            this.result = result;
116            this.settings = new SettingsAdapter( request );
117            setProjects( projects );
118        }
119    
120        public MavenSession( PlexusContainer container, RepositorySystemSession repositorySession, MavenExecutionRequest request,
121                             MavenExecutionResult result )
122        {
123            this.container = container;
124            this.request = request;
125            this.result = result;
126            this.settings = new SettingsAdapter( request );
127            this.repositorySession = repositorySession;
128        }
129    
130        public void setProjects( List<MavenProject> projects )
131        {
132            if ( !projects.isEmpty() )
133            {
134                this.currentProject = projects.get( 0 );
135                this.topLevelProject = currentProject;
136                for ( MavenProject project : projects )
137                {
138                    if ( project.isExecutionRoot() )
139                    {
140                        topLevelProject = project;
141                        break;
142                    }
143                }
144            }
145            else
146            {
147                this.currentProject = null;
148                this.topLevelProject = null;
149            }
150            this.projects = projects;
151        }
152    
153        @Deprecated
154        public PlexusContainer getContainer()
155        {
156            return container;
157        }
158    
159        @Deprecated
160        public Object lookup( String role )
161            throws ComponentLookupException
162        {
163            return container.lookup( role );
164        }
165    
166        @Deprecated
167        public Object lookup( String role, String roleHint )
168            throws ComponentLookupException
169        {
170            return container.lookup( role, roleHint );
171        }
172    
173        @Deprecated
174        public List<Object> lookupList( String role )
175            throws ComponentLookupException
176        {
177            return container.lookupList( role );
178        }
179    
180        @Deprecated
181        public Map<String, Object> lookupMap( String role )
182            throws ComponentLookupException
183        {
184            return container.lookupMap( role );
185        }
186    
187        @Deprecated
188        public RepositoryCache getRepositoryCache()
189        {
190            return null;
191        }
192    
193        public ArtifactRepository getLocalRepository()
194        {
195            return request.getLocalRepository();
196        }
197    
198        public List<String> getGoals()
199        {
200            return request.getGoals();
201        }
202    
203        /**
204         * Gets the user properties to use for interpolation and profile activation. The user properties have been
205         * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
206         * line.
207         *
208         * @return The user properties, never {@code null}.
209         */
210        public Properties getUserProperties()
211        {
212            return request.getUserProperties();
213        }
214    
215        /**
216         * Gets the system properties to use for interpolation and profile activation. The system properties are collected
217         * from the runtime environment like {@link System#getProperties()} and environment variables.
218         *
219         * @return The system properties, never {@code null}.
220         */
221        public Properties getSystemProperties()
222        {
223            return request.getSystemProperties();
224        }
225    
226        /**
227         * @deprecated Use either {@link #getUserProperties()} or {@link #getSystemProperties()}.
228         */
229        @Deprecated
230        public Properties getExecutionProperties()
231        {
232            if ( executionProperties == null )
233            {
234                executionProperties = new Properties();
235                executionProperties.putAll( request.getSystemProperties() );
236                executionProperties.putAll( request.getUserProperties() );
237            }
238    
239            return executionProperties;
240        }
241    
242        public Settings getSettings()
243        {
244            return settings;
245        }
246    
247        public List<MavenProject> getProjects()
248        {
249            return projects;
250        }
251    
252        @Deprecated
253        public List<MavenProject> getSortedProjects()
254        {
255            return getProjects();
256        }
257    
258        public String getExecutionRootDirectory()
259        {
260            return request.getBaseDirectory();
261        }
262    
263        public boolean isUsingPOMsFromFilesystem()
264        {
265            return request.isProjectPresent();
266        }
267    
268        public MavenExecutionRequest getRequest()
269        {
270            return request;
271        }
272    
273        public void setCurrentProject( MavenProject currentProject )
274        {
275            this.currentProject = currentProject;
276        }
277    
278        public MavenProject getCurrentProject()
279        {
280            return currentProject;
281        }
282    
283        public ProjectBuildingRequest getProjectBuildingRequest()
284        {
285            return request.getProjectBuildingRequest().setRepositorySession( getRepositorySession() );
286        }
287    
288        public List<String> getPluginGroups()
289        {
290            return request.getPluginGroups();
291        }
292    
293        public boolean isOffline()
294        {
295            return request.isOffline();
296        }
297    
298        public MavenProject getTopLevelProject()
299        {
300            return topLevelProject;
301        }
302    
303        public MavenExecutionResult getResult()
304        {
305            return result;
306        }
307    
308        // Backward compat
309    
310        public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
311        {
312            String projectKey = project.getId();
313    
314            Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
315    
316            if ( pluginContextsByKey == null )
317            {
318                pluginContextsByKey = new ConcurrentHashMap<String, Map<String, Object>>();
319    
320                pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
321            }
322    
323            String pluginKey = plugin.getPluginLookupKey();
324    
325            Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
326    
327            if ( pluginContext == null )
328            {
329                pluginContext = new ConcurrentHashMap<String, Object>();
330    
331                pluginContextsByKey.put( pluginKey, pluginContext );
332            }
333    
334            return pluginContext;
335        }
336    
337        public ProjectDependencyGraph getProjectDependencyGraph()
338        {
339            return projectDependencyGraph;
340        }
341    
342        public void setProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph )
343        {
344            this.projectDependencyGraph = projectDependencyGraph;
345        }
346    
347        public String getReactorFailureBehavior()
348        {
349            return request.getReactorFailureBehavior();
350        }
351    
352        @Override
353        public MavenSession clone()
354        {
355            try
356            {
357                return (MavenSession) super.clone();
358            }
359            catch ( CloneNotSupportedException e )
360            {
361                throw new RuntimeException( "Bug", e );
362            }
363        }
364    
365        private String getId( MavenProject project )
366        {
367            return project.getGroupId() + ':' + project.getArtifactId() + ':' + project.getVersion();
368        }
369    
370        @Deprecated
371        public EventDispatcher getEventDispatcher()
372        {
373            return null;
374        }
375    
376        public Date getStartTime()
377        {
378            return request.getStartTime();
379        }
380    
381        public boolean isParallel()
382        {
383            return parallel;
384        }
385    
386        public void setParallel( boolean parallel )
387        {
388            this.parallel = parallel;
389        }
390    
391        public RepositorySystemSession getRepositorySession()
392        {
393            return repositorySession;
394        }
395    
396    }