001    package org.apache.maven.model.profile;
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.Collections;
024    import java.util.Enumeration;
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Properties;
029    
030    /**
031     * Describes the environmental context used to determine the activation status of profiles.
032     *
033     * @author Benjamin Bentmann
034     */
035    public class DefaultProfileActivationContext
036        implements ProfileActivationContext
037    {
038    
039        private List<String> activeProfileIds = Collections.emptyList();
040    
041        private List<String> inactiveProfileIds = Collections.emptyList();
042    
043        private Map<String, String> systemProperties = Collections.emptyMap();
044    
045        private Map<String, String> userProperties = Collections.emptyMap();
046    
047        private Map<String, String> projectProperties = Collections.emptyMap();
048    
049        private File projectDirectory;
050    
051        public List<String> getActiveProfileIds()
052        {
053            return activeProfileIds;
054        }
055    
056        /**
057         * Sets the identifiers of those profiles that should be activated by explicit demand.
058         *
059         * @param activeProfileIds The identifiers of those profiles to activate, may be {@code null}.
060         * @return This context, never {@code null}.
061         */
062        public DefaultProfileActivationContext setActiveProfileIds( List<String> activeProfileIds )
063        {
064            if ( activeProfileIds != null )
065            {
066                this.activeProfileIds = Collections.unmodifiableList( activeProfileIds );
067            }
068            else
069            {
070                this.activeProfileIds = Collections.emptyList();
071            }
072    
073            return this;
074        }
075    
076        public List<String> getInactiveProfileIds()
077        {
078            return inactiveProfileIds;
079        }
080    
081        /**
082         * Sets the identifiers of those profiles that should be deactivated by explicit demand.
083         *
084         * @param inactiveProfileIds The identifiers of those profiles to deactivate, may be {@code null}.
085         * @return This context, never {@code null}.
086         */
087        public DefaultProfileActivationContext setInactiveProfileIds( List<String> inactiveProfileIds )
088        {
089            if ( inactiveProfileIds != null )
090            {
091                this.inactiveProfileIds = Collections.unmodifiableList( inactiveProfileIds );
092            }
093            else
094            {
095                this.inactiveProfileIds = Collections.emptyList();
096            }
097    
098            return this;
099        }
100    
101        public Map<String, String> getSystemProperties()
102        {
103            return systemProperties;
104        }
105    
106        /**
107         * Sets the system properties to use for interpolation and profile activation. The system properties are collected
108         * from the runtime environment like {@link System#getProperties()} and environment variables.
109         *
110         * @param systemProperties The system properties, may be {@code null}.
111         * @return This context, never {@code null}.
112         */
113        @SuppressWarnings( "unchecked" )
114        public DefaultProfileActivationContext setSystemProperties( Properties systemProperties )
115        {
116            if ( systemProperties != null )
117            {
118                this.systemProperties = Collections.unmodifiableMap( (Map) systemProperties );
119            }
120            else
121            {
122                this.systemProperties = Collections.emptyMap();
123            }
124    
125            return this;
126        }
127    
128        /**
129         * Sets the system properties to use for interpolation and profile activation. The system properties are collected
130         * from the runtime environment like {@link System#getProperties()} and environment variables.
131         *
132         * @param systemProperties The system properties, may be {@code null}.
133         * @return This context, never {@code null}.
134         */
135        public DefaultProfileActivationContext setSystemProperties( Map<String, String> systemProperties )
136        {
137            if ( systemProperties != null )
138            {
139                this.systemProperties = Collections.unmodifiableMap( systemProperties );
140            }
141            else
142            {
143                this.systemProperties = Collections.emptyMap();
144            }
145    
146            return this;
147        }
148    
149        public Map<String, String> getUserProperties()
150        {
151            return userProperties;
152        }
153    
154        /**
155         * Sets the user properties to use for interpolation and profile activation. The user properties have been
156         * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
157         * line.
158         *
159         * @param userProperties The user properties, may be {@code null}.
160         * @return This context, never {@code null}.
161         */
162        @SuppressWarnings( "unchecked" )
163        public DefaultProfileActivationContext setUserProperties( Properties userProperties )
164        {
165            if ( userProperties != null )
166            {
167                this.userProperties = Collections.unmodifiableMap( (Map) userProperties );
168            }
169            else
170            {
171                this.userProperties = Collections.emptyMap();
172            }
173    
174            return this;
175        }
176    
177        /**
178         * Sets the user properties to use for interpolation and profile activation. The user properties have been
179         * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
180         * line.
181         *
182         * @param userProperties The user properties, may be {@code null}.
183         * @return This context, never {@code null}.
184         */
185        public DefaultProfileActivationContext setUserProperties( Map<String, String> userProperties )
186        {
187            if ( userProperties != null )
188            {
189                this.userProperties = Collections.unmodifiableMap( userProperties );
190            }
191            else
192            {
193                this.userProperties = Collections.emptyMap();
194            }
195    
196            return this;
197        }
198    
199        public File getProjectDirectory()
200        {
201            return projectDirectory;
202        }
203    
204        /**
205         * Sets the base directory of the current project.
206         *
207         * @param projectDirectory The base directory of the current project, may be {@code null} if profile activation
208         *                         happens in the context of metadata retrieval rather than project building.
209         * @return This context, never {@code null}.
210         */
211        public DefaultProfileActivationContext setProjectDirectory( File projectDirectory )
212        {
213            this.projectDirectory = projectDirectory;
214    
215            return this;
216        }
217    
218        public Map<String, String> getProjectProperties()
219        {
220            return projectProperties;
221        }
222    
223        public DefaultProfileActivationContext setProjectProperties( Properties projectProperties )
224        {
225            if ( projectProperties != null )
226            {
227    
228                this.projectProperties = Collections.unmodifiableMap( toMap( projectProperties ) );
229            }
230            else
231            {
232                this.projectProperties = Collections.emptyMap();
233            }
234    
235            return this;
236        }
237    
238        private Map<String, String> toMap( Properties properties )
239        {
240            if ( properties == null )
241            {
242                return Collections.emptyMap();
243            }
244            Map<String, String> map = new HashMap<String, String>();
245            Enumeration keys = properties.keys();
246            while ( keys.hasMoreElements() )
247            {
248                String key = (String) keys.nextElement();
249                map.put( key, properties.getProperty( key ) );
250            }
251            return map;
252        }
253    }