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.profiles.activation.ProfileActivationException;
025: import org.apache.maven.profiles.activation.ProfileActivator;
026: import org.codehaus.plexus.PlexusContainer;
027: import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
028: import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.LinkedHashMap;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.Map.Entry;
036:
037: public class DefaultProfileManager implements ProfileManager {
038: private PlexusContainer container;
039:
040: private List activatedIds = new ArrayList();
041:
042: private List deactivatedIds = new ArrayList();
043:
044: private List defaultIds = new ArrayList();
045:
046: private Map profilesById = new LinkedHashMap();
047:
048: /**
049: * the properties passed to the profile manager are the props that
050: * are passed to maven, possibly containing profile activator properties
051: *
052: */
053: public DefaultProfileManager(PlexusContainer container) {
054: this .container = container;
055: }
056:
057: public Map getProfilesById() {
058: return profilesById;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.apache.maven.profiles.ProfileManager#addProfile(org.apache.maven.model.Profile)
063: */
064: public void addProfile(Profile profile) {
065: String profileId = profile.getId();
066:
067: Profile existing = (Profile) profilesById.get(profileId);
068: if (existing != null) {
069: container.getLogger().warn(
070: "Overriding profile: \'" + profileId
071: + "\' (source: " + existing.getSource()
072: + ") with new instance from source: "
073: + profile.getSource());
074: }
075:
076: profilesById.put(profile.getId(), profile);
077:
078: Activation activation = profile.getActivation();
079:
080: if (activation != null && activation.isActiveByDefault()) {
081: activateAsDefault(profileId);
082: }
083: }
084:
085: /* (non-Javadoc)
086: * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.lang.String)
087: */
088: public void explicitlyActivate(String profileId) {
089: if (!activatedIds.contains(profileId)) {
090: container.getLogger().debug(
091: "Profile with id: \'" + profileId
092: + "\' has been explicitly activated.");
093:
094: activatedIds.add(profileId);
095: }
096: }
097:
098: /* (non-Javadoc)
099: * @see org.apache.maven.profiles.ProfileManager#explicitlyActivate(java.util.List)
100: */
101: public void explicitlyActivate(List profileIds) {
102: for (Iterator it = profileIds.iterator(); it.hasNext();) {
103: String profileId = (String) it.next();
104:
105: explicitlyActivate(profileId);
106: }
107: }
108:
109: /* (non-Javadoc)
110: * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.lang.String)
111: */
112: public void explicitlyDeactivate(String profileId) {
113: if (!deactivatedIds.contains(profileId)) {
114: container.getLogger().debug(
115: "Profile with id: \'" + profileId
116: + "\' has been explicitly deactivated.");
117:
118: deactivatedIds.add(profileId);
119: }
120: }
121:
122: /* (non-Javadoc)
123: * @see org.apache.maven.profiles.ProfileManager#explicitlyDeactivate(java.util.List)
124: */
125: public void explicitlyDeactivate(List profileIds) {
126: for (Iterator it = profileIds.iterator(); it.hasNext();) {
127: String profileId = (String) it.next();
128:
129: explicitlyDeactivate(profileId);
130: }
131: }
132:
133: /* (non-Javadoc)
134: * @see org.apache.maven.profiles.ProfileManager#getActiveProfiles()
135: */
136: public List getActiveProfiles() throws ProfileActivationException {
137: List activeFromPom = new ArrayList();
138: List activeExternal = new ArrayList();
139:
140: for (Iterator it = profilesById.entrySet().iterator(); it
141: .hasNext();) {
142: Map.Entry entry = (Entry) it.next();
143:
144: String profileId = (String) entry.getKey();
145: Profile profile = (Profile) entry.getValue();
146:
147: boolean shouldAdd = false;
148: if (activatedIds.contains(profileId)) {
149: shouldAdd = true;
150: } else if (!deactivatedIds.contains(profileId)
151: && isActive(profile)) {
152: shouldAdd = true;
153: }
154:
155: if (shouldAdd) {
156: if ("pom".equals(profile.getSource())) {
157: activeFromPom.add(profile);
158: } else {
159: activeExternal.add(profile);
160: }
161: }
162: }
163:
164: if (activeFromPom.isEmpty()) {
165: for (Iterator it = defaultIds.iterator(); it.hasNext();) {
166: String profileId = (String) it.next();
167:
168: Profile profile = (Profile) profilesById.get(profileId);
169:
170: activeFromPom.add(profile);
171: }
172: }
173:
174: List allActive = new ArrayList(activeFromPom.size()
175: + activeExternal.size());
176:
177: allActive.addAll(activeExternal);
178: allActive.addAll(activeFromPom);
179:
180: return allActive;
181: }
182:
183: private boolean isActive(Profile profile)
184: throws ProfileActivationException {
185: List activators = null;
186:
187: try {
188: activators = container.lookupList(ProfileActivator.ROLE);
189:
190: for (Iterator activatorIterator = activators.iterator(); activatorIterator
191: .hasNext();) {
192: ProfileActivator activator = (ProfileActivator) activatorIterator
193: .next();
194:
195: if (activator.canDetermineActivation(profile)) {
196: boolean result = activator.isActive(profile);
197:
198: if (result) {
199: container.getLogger().debug(
200: "Profile: " + profile.getId()
201: + " is active. (source: "
202: + profile.getSource() + ")");
203: }
204:
205: return result;
206: }
207: }
208:
209: return false;
210: } catch (ComponentLookupException e) {
211: throw new ProfileActivationException(
212: "Cannot retrieve list of profile activators.", e);
213: } finally {
214: container.getContext().put("SystemProperties", null);
215: if (activators != null) {
216: try {
217: container.releaseAll(activators);
218: } catch (ComponentLifecycleException e) {
219: container
220: .getLogger()
221: .debug(
222: "Error releasing profile activators - ignoring.",
223: e);
224: }
225: }
226: }
227: }
228:
229: /* (non-Javadoc)
230: * @see org.apache.maven.profiles.ProfileManager#addProfiles(java.util.List)
231: */
232: public void addProfiles(List profiles) {
233: for (Iterator it = profiles.iterator(); it.hasNext();) {
234: Profile profile = (Profile) it.next();
235:
236: addProfile(profile);
237: }
238: }
239:
240: public void activateAsDefault(String profileId) {
241: if (!defaultIds.contains(profileId)) {
242: defaultIds.add(profileId);
243: }
244: }
245:
246: public List getExplicitlyActivatedIds() {
247: return activatedIds;
248: }
249:
250: public List getExplicitlyDeactivatedIds() {
251: return deactivatedIds;
252: }
253:
254: public List getIdsActivatedByDefault() {
255: return defaultIds;
256: }
257: }
|