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