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.core.install;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Arrays;
023: import java.util.Collection;
024: import java.util.Date;
025: import java.util.Iterator;
026:
027: import org.apache.ivy.core.module.descriptor.Configuration;
028: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
029: import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
030: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
031: import org.apache.ivy.core.module.id.ModuleId;
032: import org.apache.ivy.core.module.id.ModuleRevisionId;
033: import org.apache.ivy.core.publish.PublishEngine;
034: import org.apache.ivy.core.report.ArtifactDownloadReport;
035: import org.apache.ivy.core.report.ResolveReport;
036: import org.apache.ivy.core.resolve.DownloadOptions;
037: import org.apache.ivy.core.resolve.IvyNode;
038: import org.apache.ivy.core.resolve.ResolveEngine;
039: import org.apache.ivy.core.resolve.ResolveOptions;
040: import org.apache.ivy.core.search.SearchEngine;
041: import org.apache.ivy.plugins.conflict.NoConflictManager;
042: import org.apache.ivy.plugins.matcher.ExactPatternMatcher;
043: import org.apache.ivy.plugins.matcher.MatcherHelper;
044: import org.apache.ivy.plugins.matcher.PatternMatcher;
045: import org.apache.ivy.plugins.resolver.DependencyResolver;
046: import org.apache.ivy.util.Message;
047: import org.apache.ivy.util.filter.Filter;
048: import org.apache.ivy.util.filter.FilterHelper;
049:
050: public class InstallEngine {
051: private InstallEngineSettings settings;
052:
053: private ResolveEngine resolveEngine;
054:
055: private PublishEngine publishEngine;
056:
057: private SearchEngine searchEngine;
058:
059: public InstallEngine(InstallEngineSettings settings,
060: SearchEngine searchEngine, ResolveEngine resolveEngine,
061: PublishEngine publishEngine) {
062: this .settings = settings;
063: this .searchEngine = searchEngine;
064: this .resolveEngine = resolveEngine;
065: this .publishEngine = publishEngine;
066: }
067:
068: public ResolveReport install(ModuleRevisionId mrid, String from,
069: String to, boolean transitive, boolean validate,
070: boolean overwrite, Filter artifactFilter, String matcherName)
071: throws IOException {
072: if (artifactFilter == null) {
073: artifactFilter = FilterHelper.NO_FILTER;
074: }
075: DependencyResolver fromResolver = settings.getResolver(from);
076: DependencyResolver toResolver = settings.getResolver(to);
077: if (fromResolver == null) {
078: throw new IllegalArgumentException("unknown resolver "
079: + from + ". Available resolvers are: "
080: + settings.getResolverNames());
081: }
082: if (toResolver == null) {
083: throw new IllegalArgumentException("unknown resolver " + to
084: + ". Available resolvers are: "
085: + settings.getResolverNames());
086: }
087: PatternMatcher matcher = settings.getMatcher(matcherName);
088: if (matcher == null) {
089: throw new IllegalArgumentException("unknown matcher "
090: + matcherName + ". Available matchers are: "
091: + settings.getMatcherNames());
092: }
093:
094: // build module file declaring the dependency
095: Message.info(":: installing " + mrid + " ::");
096: DependencyResolver oldDicator = resolveEngine
097: .getDictatorResolver();
098: boolean log = settings.logNotConvertedExclusionRule();
099: try {
100: settings.setLogNotConvertedExclusionRule(true);
101: resolveEngine.setDictatorResolver(fromResolver);
102:
103: DefaultModuleDescriptor md = new DefaultModuleDescriptor(
104: ModuleRevisionId.newInstance("apache",
105: "ivy-install", "1.0"), settings
106: .getStatusManager().getDefaultStatus(),
107: new Date());
108: String resolveId = ResolveOptions.getDefaultResolveId(md);
109: md.addConfiguration(new Configuration("default"));
110: md.addConflictManager(new ModuleId(
111: ExactPatternMatcher.ANY_EXPRESSION,
112: ExactPatternMatcher.ANY_EXPRESSION),
113: ExactPatternMatcher.INSTANCE,
114: new NoConflictManager());
115:
116: if (MatcherHelper.isExact(matcher, mrid)) {
117: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
118: md, mrid, false, false, transitive);
119: dd.addDependencyConfiguration("default", "*");
120: md.addDependency(dd);
121: } else {
122: Collection mrids = searchEngine.findModuleRevisionIds(
123: fromResolver, mrid, matcher);
124:
125: for (Iterator iter = mrids.iterator(); iter.hasNext();) {
126: ModuleRevisionId foundMrid = (ModuleRevisionId) iter
127: .next();
128: Message.info("\tfound " + foundMrid
129: + " to install: adding to the list");
130: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
131: md, foundMrid, false, false, transitive);
132: dd.addDependencyConfiguration("default", "*");
133: md.addDependency(dd);
134: }
135: }
136:
137: // resolve using appropriate resolver
138: ResolveReport report = new ResolveReport(md, resolveId);
139:
140: Message.info(":: resolving dependencies ::");
141: ResolveOptions options = new ResolveOptions().setResolveId(
142: resolveId).setConfs(new String[] { "default" })
143: .setValidate(validate);
144: IvyNode[] dependencies = resolveEngine.getDependencies(md,
145: options, report);
146: report.setDependencies(Arrays.asList(dependencies),
147: artifactFilter);
148:
149: Message.info(":: downloading artifacts to cache ::");
150: resolveEngine.downloadArtifacts(report, artifactFilter,
151: new DownloadOptions());
152:
153: // now that everything is in cache, we can publish all these modules
154: Message.info(":: installing in " + to + " ::");
155: for (int i = 0; i < dependencies.length; i++) {
156: ModuleDescriptor depmd = dependencies[i]
157: .getDescriptor();
158: if (depmd != null) {
159: ModuleRevisionId depMrid = depmd
160: .getModuleRevisionId();
161: Message.verbose("installing " + depMrid);
162: boolean successfullyPublished = false;
163: try {
164: toResolver.beginPublishTransaction(depMrid,
165: overwrite);
166:
167: // publish artifacts
168: ArtifactDownloadReport[] artifacts = report
169: .getArtifactsReports(depMrid);
170: for (int j = 0; j < artifacts.length; j++) {
171: if (artifacts[j].getLocalFile() != null) {
172: toResolver.publish(artifacts[j]
173: .getArtifact(), artifacts[j]
174: .getLocalFile(), overwrite);
175: }
176: }
177:
178: // publish metadata
179: File localIvyFile = dependencies[i]
180: .getModuleRevision().getReport()
181: .getLocalFile();
182: toResolver.publish(depmd.getMetadataArtifact(),
183: localIvyFile, overwrite);
184:
185: // end module publish
186: toResolver.commitPublishTransaction();
187: successfullyPublished = true;
188: } finally {
189: if (!successfullyPublished) {
190: toResolver.abortPublishTransaction();
191: }
192: }
193: }
194: }
195:
196: Message.info(":: install resolution report ::");
197:
198: // output report
199: resolveEngine.outputReport(report, settings
200: .getResolutionCacheManager(), options);
201:
202: return report;
203: } finally {
204: resolveEngine.setDictatorResolver(oldDicator);
205: settings.setLogNotConvertedExclusionRule(log);
206: }
207: }
208:
209: }
|