001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.conflict;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
027: import org.apache.ivy.core.resolve.IvyNode;
028: import org.apache.ivy.plugins.latest.ArtifactInfo;
029: import org.apache.ivy.plugins.latest.LatestStrategy;
030: import org.apache.ivy.util.Message;
031:
032: public class LatestConflictManager extends AbstractConflictManager {
033: public static class NoConflictResolvedYetException extends
034: RuntimeException {
035: }
036:
037: protected static final class IvyNodeArtifactInfo implements
038: ArtifactInfo {
039: private final IvyNode node;
040:
041: private IvyNodeArtifactInfo(IvyNode dep) {
042: node = dep;
043: }
044:
045: public long getLastModified() {
046: long lastModified = node.getLastModified();
047: if (lastModified == 0) {
048: // if the last modified timestamp is unknown, we can't resolve
049: // the conflicts now, and trigger an exception which will be catched
050: // in the main resolveConflicts method
051: throw new NoConflictResolvedYetException();
052: } else {
053: return lastModified;
054: }
055: }
056:
057: public String getRevision() {
058: return node.getResolvedId().getRevision();
059: }
060:
061: public IvyNode getNode() {
062: return node;
063: }
064: }
065:
066: private LatestStrategy strategy;
067:
068: private String strategyName;
069:
070: public LatestConflictManager() {
071: }
072:
073: public LatestConflictManager(LatestStrategy strategy) {
074: this .strategy = strategy;
075: }
076:
077: public LatestConflictManager(String name, LatestStrategy strategy) {
078: setName(name);
079: this .strategy = strategy;
080: }
081:
082: public Collection resolveConflicts(IvyNode parent,
083: Collection conflicts) {
084: if (conflicts.size() < 2) {
085: return conflicts;
086: }
087: for (Iterator iter = conflicts.iterator(); iter.hasNext();) {
088: IvyNode node = (IvyNode) iter.next();
089: DependencyDescriptor dd = node
090: .getDependencyDescriptor(parent);
091: if (dd != null
092: && dd.isForce()
093: && parent.getResolvedId().equals(
094: dd.getParentRevisionId())) {
095: return Collections.singleton(node);
096: }
097: }
098: try {
099: IvyNodeArtifactInfo latest = (IvyNodeArtifactInfo) getStrategy()
100: .findLatest(toArtifactInfo(conflicts), null);
101: if (latest != null) {
102: return Collections.singleton(latest.getNode());
103: } else {
104: return conflicts;
105: }
106: } catch (NoConflictResolvedYetException ex) {
107: // we have not enough informations in the nodes to resolve conflict
108: // according to the resolveConflicts contract, we must return null
109: return null;
110: }
111: }
112:
113: protected ArtifactInfo[] toArtifactInfo(Collection conflicts) {
114: List artifacts = new ArrayList(conflicts.size());
115: for (Iterator iter = conflicts.iterator(); iter.hasNext();) {
116: IvyNode node = (IvyNode) iter.next();
117: artifacts.add(new IvyNodeArtifactInfo(node));
118: }
119: return (ArtifactInfo[]) artifacts
120: .toArray(new ArtifactInfo[artifacts.size()]);
121: }
122:
123: public LatestStrategy getStrategy() {
124: if (strategy == null) {
125: if (strategyName != null) {
126: strategy = getSettings()
127: .getLatestStrategy(strategyName);
128: if (strategy == null) {
129: Message.error("unknown latest strategy: "
130: + strategyName);
131: strategy = getSettings().getDefaultLatestStrategy();
132: }
133: } else {
134: strategy = getSettings().getDefaultLatestStrategy();
135: }
136: }
137: return strategy;
138: }
139:
140: /**
141: * To conform to configurator API
142: *
143: * @param latestStrategy
144: */
145: public void setLatest(String strategyName) {
146: this .strategyName = strategyName;
147: }
148:
149: public void setStrategy(LatestStrategy strategy) {
150: this .strategy = strategy;
151: }
152:
153: public String toString() {
154: return strategy != null ? String.valueOf(strategy)
155: : strategyName;
156: }
157: }
|