001 package org.apache.maven.profiles; 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 org.apache.maven.model.Activation; 023 import org.apache.maven.model.Profile; 024 import org.apache.maven.model.building.ModelProblem; 025 import org.apache.maven.model.building.ModelProblemCollector; 026 import org.apache.maven.model.profile.DefaultProfileActivationContext; 027 import org.apache.maven.model.profile.ProfileSelector; 028 import org.apache.maven.profiles.activation.ProfileActivationException; 029 import org.codehaus.plexus.MutablePlexusContainer; 030 import org.codehaus.plexus.PlexusContainer; 031 import org.codehaus.plexus.component.annotations.Requirement; 032 import org.codehaus.plexus.component.repository.exception.ComponentLookupException; 033 import org.codehaus.plexus.logging.Logger; 034 035 import java.util.ArrayList; 036 import java.util.Iterator; 037 import java.util.LinkedHashMap; 038 import java.util.List; 039 import java.util.Map; 040 import java.util.Properties; 041 import org.apache.maven.model.building.ModelProblemCollectorRequest; 042 043 @Deprecated 044 public class DefaultProfileManager 045 implements ProfileManager 046 { 047 048 @Requirement 049 private Logger logger; 050 051 @Requirement 052 private ProfileSelector profileSelector; 053 054 private List activatedIds = new ArrayList(); 055 056 private List deactivatedIds = new ArrayList(); 057 058 private List defaultIds = new ArrayList(); 059 060 private Map profilesById = new LinkedHashMap(); 061 062 private Properties requestProperties; 063 064 /** 065 * @deprecated without passing in the system properties, the SystemPropertiesProfileActivator will not work 066 * correctly in embedded envirnments. 067 */ 068 public DefaultProfileManager( PlexusContainer container ) 069 { 070 this( container, null ); 071 } 072 073 /** 074 * the properties passed to the profile manager are the props that 075 * are passed to maven, possibly containing profile activator properties 076 * 077 */ 078 public DefaultProfileManager( PlexusContainer container, Properties props ) 079 { 080 try 081 { 082 this.profileSelector = container.lookup( ProfileSelector.class ); 083 this.logger = ( (MutablePlexusContainer) container ).getLogger(); 084 } 085 catch ( ComponentLookupException e ) 086 { 087 throw new IllegalStateException( e ); 088 } 089 this.requestProperties = props; 090 } 091 092 public Properties getRequestProperties() 093 { 094 return requestProperties; 095 } 096 097 public Map getProfilesById() 098 { 099 return profilesById; 100 } 101 102 /* (non-Javadoc) 103 * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile) 104 */ 105 public void addProfile( Profile profile ) 106 { 107 String profileId = profile.getId(); 108 109 Profile existing = (Profile) profilesById.get( profileId ); 110 if ( existing != null ) 111 { 112 logger.warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource() 113 + ") with new instance from source: " + profile.getSource() ); 114 } 115 116 profilesById.put( profile.getId(), profile ); 117 118 Activation activation = profile.getActivation(); 119 120 if ( activation != null && activation.isActiveByDefault() ) 121 { 122 activateAsDefault( profileId ); 123 } 124 } 125 126 /* (non-Javadoc) 127 * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String) 128 */ 129 public void explicitlyActivate( String profileId ) 130 { 131 if ( !activatedIds.contains( profileId ) ) 132 { 133 logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." ); 134 135 activatedIds.add( profileId ); 136 } 137 } 138 139 /* (non-Javadoc) 140 * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List) 141 */ 142 public void explicitlyActivate( List profileIds ) 143 { 144 for ( Object profileId1 : profileIds ) 145 { 146 String profileId = (String) profileId1; 147 148 explicitlyActivate( profileId ); 149 } 150 } 151 152 /* (non-Javadoc) 153 * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String) 154 */ 155 public void explicitlyDeactivate( String profileId ) 156 { 157 if ( !deactivatedIds.contains( profileId ) ) 158 { 159 logger.debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." ); 160 161 deactivatedIds.add( profileId ); 162 } 163 } 164 165 /* (non-Javadoc) 166 * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List) 167 */ 168 public void explicitlyDeactivate( List profileIds ) 169 { 170 for ( Object profileId1 : profileIds ) 171 { 172 String profileId = (String) profileId1; 173 174 explicitlyDeactivate( profileId ); 175 } 176 } 177 178 /* (non-Javadoc) 179 * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles() 180 */ 181 public List getActiveProfiles() 182 throws ProfileActivationException 183 { 184 DefaultProfileActivationContext context = new DefaultProfileActivationContext(); 185 context.setActiveProfileIds( activatedIds ); 186 context.setInactiveProfileIds( deactivatedIds ); 187 context.setSystemProperties( System.getProperties() ); 188 context.setUserProperties( requestProperties ); 189 190 final List<ProfileActivationException> errors = new ArrayList<ProfileActivationException>(); 191 192 List<Profile> profiles = 193 profileSelector.getActiveProfiles( profilesById.values(), context, new ModelProblemCollector() 194 { 195 196 public void add( ModelProblemCollectorRequest req ) 197 { 198 if ( !ModelProblem.Severity.WARNING.equals( req.getSeverity() ) ) 199 { 200 errors.add( new ProfileActivationException( req.getMessage(), req.getException() ) ); 201 } 202 } 203 } ); 204 205 if ( !errors.isEmpty() ) 206 { 207 throw errors.get( 0 ); 208 } 209 210 return profiles; 211 } 212 213 /* (non-Javadoc) 214 * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List) 215 */ 216 public void addProfiles( List profiles ) 217 { 218 for ( Object profile1 : profiles ) 219 { 220 Profile profile = (Profile) profile1; 221 222 addProfile( profile ); 223 } 224 } 225 226 public void activateAsDefault( String profileId ) 227 { 228 if ( !defaultIds.contains( profileId ) ) 229 { 230 defaultIds.add( profileId ); 231 } 232 } 233 234 public List getExplicitlyActivatedIds() 235 { 236 return activatedIds; 237 } 238 239 public List getExplicitlyDeactivatedIds() 240 { 241 return deactivatedIds; 242 } 243 244 public List getIdsActivatedByDefault() 245 { 246 return defaultIds; 247 } 248 249 }