View Javadoc

1   package org.apache.maven.model.profile;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  /**
29   * Describes the environmental context used to determine the activation status of profiles.
30   * 
31   * @author Benjamin Bentmann
32   */
33  public class DefaultProfileActivationContext
34      implements ProfileActivationContext
35  {
36  
37      private List<String> activeProfileIds = Collections.emptyList();
38  
39      private List<String> inactiveProfileIds = Collections.emptyList();
40  
41      private Map<String, String> systemProperties = Collections.emptyMap();
42  
43      private Map<String, String> userProperties = Collections.emptyMap();
44  
45      private File projectDirectory;
46  
47      public List<String> getActiveProfileIds()
48      {
49          return activeProfileIds;
50      }
51  
52      /**
53       * Sets the identifiers of those profiles that should be activated by explicit demand.
54       * 
55       * @param activeProfileIds The identifiers of those profiles to activate, may be {@code null}.
56       * @return This context, never {@code null}.
57       */
58      public DefaultProfileActivationContext setActiveProfileIds( List<String> activeProfileIds )
59      {
60          if ( activeProfileIds != null )
61          {
62              this.activeProfileIds = Collections.unmodifiableList( activeProfileIds );
63          }
64          else
65          {
66              this.activeProfileIds = Collections.emptyList();
67          }
68  
69          return this;
70      }
71  
72      public List<String> getInactiveProfileIds()
73      {
74          return inactiveProfileIds;
75      }
76  
77      /**
78       * Sets the identifiers of those profiles that should be deactivated by explicit demand.
79       * 
80       * @param inactiveProfileIds The identifiers of those profiles to deactivate, may be {@code null}.
81       * @return This context, never {@code null}.
82       */
83      public DefaultProfileActivationContext setInactiveProfileIds( List<String> inactiveProfileIds )
84      {
85          if ( inactiveProfileIds != null )
86          {
87              this.inactiveProfileIds = Collections.unmodifiableList( inactiveProfileIds );
88          }
89          else
90          {
91              this.inactiveProfileIds = Collections.emptyList();
92          }
93  
94          return this;
95      }
96  
97      public Map<String, String> getSystemProperties()
98      {
99          return systemProperties;
100     }
101 
102     /**
103      * Sets the system properties to use for interpolation and profile activation. The system properties are collected
104      * from the runtime environment like {@link System#getProperties()} and environment variables.
105      * 
106      * @param systemProperties The system properties, may be {@code null}.
107      * @return This context, never {@code null}.
108      */
109     @SuppressWarnings( "unchecked" )
110     public DefaultProfileActivationContext setSystemProperties( Properties systemProperties )
111     {
112         if ( systemProperties != null )
113         {
114             this.systemProperties = Collections.unmodifiableMap( (Map) systemProperties );
115         }
116         else
117         {
118             this.systemProperties = Collections.emptyMap();
119         }
120 
121         return this;
122     }
123 
124     /**
125      * Sets the system properties to use for interpolation and profile activation. The system properties are collected
126      * from the runtime environment like {@link System#getProperties()} and environment variables.
127      * 
128      * @param systemProperties The system properties, may be {@code null}.
129      * @return This context, never {@code null}.
130      */
131     public DefaultProfileActivationContext setSystemProperties( Map<String, String> systemProperties )
132     {
133         if ( systemProperties != null )
134         {
135             this.systemProperties = Collections.unmodifiableMap( systemProperties );
136         }
137         else
138         {
139             this.systemProperties = Collections.emptyMap();
140         }
141 
142         return this;
143     }
144 
145     public Map<String, String> getUserProperties()
146     {
147         return userProperties;
148     }
149 
150     /**
151      * Sets the user properties to use for interpolation and profile activation. The user properties have been
152      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
153      * line.
154      * 
155      * @param userProperties The user properties, may be {@code null}.
156      * @return This context, never {@code null}.
157      */
158     @SuppressWarnings( "unchecked" )
159     public DefaultProfileActivationContext setUserProperties( Properties userProperties )
160     {
161         if ( userProperties != null )
162         {
163             this.userProperties = Collections.unmodifiableMap( (Map) userProperties );
164         }
165         else
166         {
167             this.userProperties = Collections.emptyMap();
168         }
169 
170         return this;
171     }
172 
173     /**
174      * Sets the user properties to use for interpolation and profile activation. The user properties have been
175      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
176      * line.
177      * 
178      * @param userProperties The user properties, may be {@code null}.
179      * @return This context, never {@code null}.
180      */
181     public DefaultProfileActivationContext setUserProperties( Map<String, String> userProperties )
182     {
183         if ( userProperties != null )
184         {
185             this.userProperties = Collections.unmodifiableMap( userProperties );
186         }
187         else
188         {
189             this.userProperties = Collections.emptyMap();
190         }
191 
192         return this;
193     }
194 
195     public File getProjectDirectory()
196     {
197         return projectDirectory;
198     }
199 
200     /**
201      * Sets the base directory of the current project.
202      * 
203      * @param projectDirectory The base directory of the current project, may be {@code null} if profile activation
204      *            happens in the context of metadata retrieval rather than project building.
205      * @return This context, never {@code null}.
206      */
207     public DefaultProfileActivationContext setProjectDirectory( File projectDirectory )
208     {
209         this.projectDirectory = projectDirectory;
210 
211         return this;
212     }
213 
214 }