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.resolver;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.text.ParseException;
023:
024: import org.apache.ivy.core.module.descriptor.Artifact;
025: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
026: import org.apache.ivy.core.module.id.ModuleRevisionId;
027: import org.apache.ivy.core.report.DownloadReport;
028: import org.apache.ivy.core.resolve.DownloadOptions;
029: import org.apache.ivy.core.resolve.ResolveData;
030: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
031: import org.apache.ivy.plugins.resolver.util.ResolvedResource;
032: import org.apache.ivy.util.Message;
033:
034: /**
035: * DualResolver is used to resolve dependencies with one dependency revolver, called ivy resolver,
036: * and then download artifacts found in the resolved dependencies from a second dependency resolver,
037: * called artifact resolver. It is especially useful with resolvers using repository where there is
038: * a lot of artifact, but no ivy file, like the maven ibiblio repository. If no ivy file is found by
039: * the ivy resolver, the artifact resolver is used to check if there is artifact corresponding to
040: * the request (with default ivy file). For artifact download, however, only the artifact resolver
041: * is used. Exactly two resolvers should be added to this resolver for it to work properly. The
042: * first resolver added if the ivy resolver, the second is the artifact one.
043: */
044: public class DualResolver extends AbstractResolver {
045: private DependencyResolver ivyResolver;
046:
047: private DependencyResolver artifactResolver;
048:
049: private boolean allownomd = true;
050:
051: public void add(DependencyResolver resolver) {
052: if (ivyResolver == null) {
053: ivyResolver = resolver;
054: } else if (artifactResolver == null) {
055: artifactResolver = resolver;
056: } else {
057: throw new IllegalStateException(
058: "exactly two resolvers must be added: ivy(1) and artifact(2) one");
059: }
060: }
061:
062: public ResolvedModuleRevision getDependency(
063: DependencyDescriptor dd, ResolveData data)
064: throws ParseException {
065: if (ivyResolver == null || artifactResolver == null) {
066: throw new IllegalStateException(
067: "exactly two resolvers must be added: ivy(1) and artifact(2) one");
068: }
069: data = new ResolveData(data, doValidate(data));
070: final ResolvedModuleRevision mr = ivyResolver.getDependency(dd,
071: data);
072: if (mr == null) {
073: checkInterrupted();
074: if (isAllownomd()) {
075: Message.verbose("ivy resolver didn't find "
076: + dd.getDependencyRevisionId()
077: + ": trying with artifact resolver");
078: return artifactResolver.getDependency(dd, data);
079: } else {
080: return null;
081: }
082: } else {
083: return new ResolvedModuleRevision(mr.getResolver(), this ,
084: mr.getDescriptor(), mr.getReport());
085: }
086: }
087:
088: public ResolvedResource findIvyFileRef(DependencyDescriptor dd,
089: ResolveData data) {
090: return ivyResolver.findIvyFileRef(dd, data);
091: }
092:
093: public void reportFailure() {
094: ivyResolver.reportFailure();
095: artifactResolver.reportFailure();
096: }
097:
098: public void reportFailure(Artifact art) {
099: ivyResolver.reportFailure(art);
100: artifactResolver.reportFailure(art);
101: }
102:
103: public DownloadReport download(Artifact[] artifacts,
104: DownloadOptions options) {
105: return artifactResolver.download(artifacts, options);
106: }
107:
108: public DependencyResolver getArtifactResolver() {
109: return artifactResolver;
110: }
111:
112: public void setArtifactResolver(DependencyResolver artifactResolver) {
113: this .artifactResolver = artifactResolver;
114: }
115:
116: public DependencyResolver getIvyResolver() {
117: return ivyResolver;
118: }
119:
120: public void setIvyResolver(DependencyResolver ivyResolver) {
121: this .ivyResolver = ivyResolver;
122: }
123:
124: public void publish(Artifact artifact, File src, boolean overwrite)
125: throws IOException {
126: if ("ivy".equals(artifact.getType())) {
127: ivyResolver.publish(artifact, src, overwrite);
128: } else {
129: artifactResolver.publish(artifact, src, overwrite);
130: }
131: }
132:
133: public void abortPublishTransaction() throws IOException {
134: ivyResolver.abortPublishTransaction();
135: artifactResolver.abortPublishTransaction();
136: }
137:
138: public void beginPublishTransaction(ModuleRevisionId module,
139: boolean overwrite) throws IOException {
140: ivyResolver.beginPublishTransaction(module, overwrite);
141: artifactResolver.beginPublishTransaction(module, overwrite);
142: }
143:
144: public void commitPublishTransaction() throws IOException {
145: ivyResolver.commitPublishTransaction();
146: artifactResolver.commitPublishTransaction();
147: }
148:
149: public void dumpSettings() {
150: if (ivyResolver == null || artifactResolver == null) {
151: throw new IllegalStateException(
152: "exactly two resolvers must be added: ivy(1) and artifact(2) one");
153: }
154: Message.verbose("\t" + getName() + " [dual "
155: + ivyResolver.getName() + " "
156: + artifactResolver.getName() + "]");
157: }
158:
159: public boolean exists(Artifact artifact) {
160: return artifactResolver.exists(artifact);
161: }
162:
163: public boolean isAllownomd() {
164: return allownomd;
165: }
166:
167: public void setAllownomd(boolean allownomd) {
168: this.allownomd = allownomd;
169: }
170:
171: }
|