001package org.apache.maven.lifecycle.mapping;
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
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026public class DefaultLifecycleMapping
027    implements LifecycleMapping
028{
029
030    private List<Lifecycle> lifecycles;
031
032    private Map<String, Lifecycle> lifecycleMap;
033
034    /** @deprecated use lifecycles instead */
035    private Map<String, LifecyclePhase> phases;
036
037    /**
038     * Populates the lifecycle map from the injected list of lifecycle mappings (if not already done).
039     */
040    private void initLifecycleMap()
041    {
042        if ( lifecycleMap == null )
043        {
044            lifecycleMap = new HashMap<>();
045
046            if ( lifecycles != null )
047            {
048                for ( Lifecycle lifecycle : lifecycles )
049                {
050                    lifecycleMap.put( lifecycle.getId(), lifecycle );
051                }
052            }
053            else
054            {
055                /*
056                 * NOTE: This is to provide a migration path for implementors of the legacy API which did not know about
057                 * getLifecycles().
058                 */
059
060                String[] lifecycleIds = { "default", "clean", "site" };
061
062                for ( String lifecycleId : lifecycleIds )
063                {
064                    Map<String, LifecyclePhase> phases = getLifecyclePhases( lifecycleId );
065                    if ( phases != null )
066                    {
067                        Lifecycle lifecycle = new Lifecycle();
068
069                        lifecycle.setId( lifecycleId );
070                        lifecycle.setLifecyclePhases( phases );
071
072                        lifecycleMap.put( lifecycleId, lifecycle );
073                    }
074                }
075            }
076        }
077    }
078
079    public Map<String, Lifecycle> getLifecycles()
080    {
081        initLifecycleMap();
082
083        return lifecycleMap;
084    }
085
086    public List<String> getOptionalMojos( String lifecycle )
087    {
088        return null;
089    }
090
091    private Map<String, LifecyclePhase> getLifecyclePhases( String lifecycle )
092    {
093        initLifecycleMap();
094
095        Lifecycle lifecycleMapping = lifecycleMap.get( lifecycle );
096
097        if ( lifecycleMapping != null )
098        {
099            return lifecycleMapping.getLifecyclePhases();
100        }
101        else if ( "default".equals( lifecycle ) )
102        {
103            return phases;
104        }
105        else
106        {
107            return null;
108        }
109    }
110    
111    @Deprecated
112    public Map<String, String> getPhases( String lifecycle )
113    {
114        return LifecyclePhase.toLegacyMap( getLifecyclePhases( lifecycle ) );
115    }
116
117}