1 package org.apache.maven.profiles; 2 3 import org.apache.maven.model.Activation; 4 import org.apache.maven.model.Profile; 5 import org.apache.maven.profiles.activation.ProfileActivationException; 6 import org.apache.maven.profiles.activation.ProfileActivator; 7 import org.apache.maven.settings.Settings; 8 import org.apache.maven.settings.SettingsUtils; 9 import org.codehaus.plexus.PlexusContainer; 10 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; 11 import org.codehaus.plexus.component.repository.exception.ComponentLookupException; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Map.Entry; 19 20 35 36 public class DefaultProfileManager 37 implements ProfileManager 38 { 39 private PlexusContainer container; 40 41 private List activatedIds = new ArrayList (); 42 43 private List deactivatedIds = new ArrayList (); 44 45 private List defaultIds = new ArrayList (); 46 47 private Map profilesById = new HashMap (); 48 49 public DefaultProfileManager( PlexusContainer container ) 50 { 51 this( container, null ); 52 } 53 54 public DefaultProfileManager( PlexusContainer container, Settings settings ) 55 { 56 this.container = container; 57 58 loadSettingsProfiles( settings ); 59 } 60 61 public Map getProfilesById() 62 { 63 return profilesById; 64 } 65 66 69 public void addProfile( Profile profile ) 70 { 71 String profileId = profile.getId(); 72 73 Profile existing = (Profile) profilesById.get( profileId ); 74 if ( existing != null ) 75 { 76 container.getLogger().warn( "Overriding profile: \'" + profileId + "\' (source: " + existing.getSource() + 77 ") with new instance from source: " + profile.getSource() ); 78 } 79 80 profilesById.put( profile.getId(), profile ); 81 82 Activation activation = profile.getActivation(); 83 84 if ( activation != null && activation.isActiveByDefault() ) 85 { 86 activateAsDefault( profileId ); 87 } 88 } 89 90 93 public void explicitlyActivate( String profileId ) 94 { 95 if ( !activatedIds.contains( profileId ) ) 96 { 97 container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly activated." ); 98 99 activatedIds.add( profileId ); 100 } 101 } 102 103 106 public void explicitlyActivate( List profileIds ) 107 { 108 for ( Iterator it = profileIds.iterator(); it.hasNext(); ) 109 { 110 String profileId = (String ) it.next(); 111 112 explicitlyActivate( profileId ); 113 } 114 } 115 116 119 public void explicitlyDeactivate( String profileId ) 120 { 121 if ( !deactivatedIds.contains( profileId ) ) 122 { 123 container.getLogger().debug( "Profile with id: \'" + profileId + "\' has been explicitly deactivated." ); 124 125 deactivatedIds.add( profileId ); 126 } 127 } 128 129 132 public void explicitlyDeactivate( List profileIds ) 133 { 134 for ( Iterator it = profileIds.iterator(); it.hasNext(); ) 135 { 136 String profileId = (String ) it.next(); 137 138 explicitlyDeactivate( profileId ); 139 } 140 } 141 142 145 public List getActiveProfiles() 146 throws ProfileActivationException 147 { 148 List activeFromPom = new ArrayList (); 149 List activeExternal = new ArrayList (); 150 151 for ( Iterator it = profilesById.entrySet().iterator(); it.hasNext(); ) 152 { 153 Map.Entry entry = (Entry) it.next(); 154 155 String profileId = (String ) entry.getKey(); 156 Profile profile = (Profile) entry.getValue(); 157 158 boolean shouldAdd = false; 159 if ( activatedIds.contains( profileId ) ) 160 { 161 shouldAdd = true; 162 } 163 else if ( !deactivatedIds.contains( profileId ) && isActive( profile ) ) 164 { 165 shouldAdd = true; 166 } 167 168 if ( shouldAdd ) 169 { 170 if ( "pom".equals( profile.getSource() ) ) 171 { 172 activeFromPom.add( profile ); 173 } 174 else 175 { 176 activeExternal.add( profile ); 177 } 178 } 179 } 180 181 if ( activeFromPom.isEmpty() ) 182 { 183 for ( Iterator it = defaultIds.iterator(); it.hasNext(); ) 184 { 185 String profileId = (String ) it.next(); 186 187 Profile profile = (Profile) profilesById.get( profileId ); 188 189 activeFromPom.add( profile ); 190 } 191 } 192 193 List allActive = new ArrayList ( activeFromPom.size() + activeExternal.size() ); 194 195 allActive.addAll( activeExternal ); 196 allActive.addAll( activeFromPom ); 197 198 return allActive; 199 } 200 201 private boolean isActive( Profile profile ) 202 throws ProfileActivationException 203 { 204 List activators = null; 205 try 206 { 207 activators = container.lookupList( ProfileActivator.ROLE ); 208 209 for ( Iterator activatorIterator = activators.iterator(); activatorIterator.hasNext(); ) 210 { 211 ProfileActivator activator = (ProfileActivator) activatorIterator.next(); 212 213 if ( activator.canDetermineActivation( profile ) ) 214 { 215 return activator.isActive( profile ); 216 } 217 } 218 219 return false; 220 } 221 catch ( ComponentLookupException e ) 222 { 223 throw new ProfileActivationException( "Cannot retrieve list of profile activators.", e ); 224 } 225 finally 226 { 227 try 228 { 229 container.releaseAll( activators ); 230 } 231 catch ( ComponentLifecycleException e ) 232 { 233 container.getLogger().debug( "Error releasing profile activators - ignoring.", e ); 234 } 235 } 236 } 237 238 241 public void addProfiles( List profiles ) 242 { 243 for ( Iterator it = profiles.iterator(); it.hasNext(); ) 244 { 245 Profile profile = (Profile) it.next(); 246 247 addProfile( profile ); 248 } 249 } 250 251 public void activateAsDefault( String profileId ) 252 { 253 if ( !defaultIds.contains( profileId ) ) 254 { 255 defaultIds.add( profileId ); 256 } 257 } 258 259 public List getExplicitlyActivatedIds() 260 { 261 return activatedIds; 262 } 263 264 public List getExplicitlyDeactivatedIds() 265 { 266 return deactivatedIds; 267 } 268 269 public List getIdsActivatedByDefault() 270 { 271 return defaultIds; 272 } 273 274 public void loadSettingsProfiles( Settings settings ) 275 { 276 if ( settings == null ) 277 { 278 return; 279 } 280 281 List settingsProfiles = settings.getProfiles(); 282 283 if ( settingsProfiles != null && !settingsProfiles.isEmpty() ) 284 { 285 List settingsActiveProfileIds = settings.getActiveProfiles(); 286 287 explicitlyActivate( settingsActiveProfileIds ); 288 289 for ( Iterator it = settings.getProfiles().iterator(); it.hasNext(); ) 290 { 291 org.apache.maven.settings.Profile rawProfile = (org.apache.maven.settings.Profile) it.next(); 292 293 Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile ); 294 295 addProfile( profile ); 296 } 297 } 298 } 299 } 300 | Popular Tags |