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.check;
019:
020: import java.io.IOException;
021: import java.net.URL;
022: import java.text.ParseException;
023: import java.util.Arrays;
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: import org.apache.ivy.core.module.descriptor.Artifact;
029: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
030: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
031: import org.apache.ivy.core.resolve.ResolveData;
032: import org.apache.ivy.core.resolve.ResolveEngine;
033: import org.apache.ivy.core.resolve.ResolveOptions;
034: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
035: import org.apache.ivy.plugins.parser.ModuleDescriptorParserRegistry;
036: import org.apache.ivy.plugins.resolver.DependencyResolver;
037: import org.apache.ivy.util.Message;
038:
039: public class CheckEngine {
040: private CheckEngineSettings settings;
041:
042: private ResolveEngine resolveEngine;
043:
044: public CheckEngine(CheckEngineSettings settings,
045: ResolveEngine resolveEngine) {
046: this .settings = settings;
047: this .resolveEngine = resolveEngine;
048: }
049:
050: /**
051: * Checks the given ivy file using current settings to see if all dependencies are available,
052: * with good confs. If a resolver name is given, it also checks that the declared publications
053: * are available in the corresponding resolver. Note that the check is not performed
054: * recursively, i.e. if a dependency has itself dependencies badly described or not available,
055: * this check will not discover it.
056: */
057: public boolean check(URL ivyFile, String resolvername) {
058: try {
059: boolean result = true;
060: // parse ivy file
061: ModuleDescriptor md = ModuleDescriptorParserRegistry
062: .getInstance().parseDescriptor(settings, ivyFile,
063: settings.doValidate());
064:
065: // check publications if possible
066: if (resolvername != null) {
067: DependencyResolver resolver = settings
068: .getResolver(resolvername);
069: String[] confs = md.getConfigurationsNames();
070: Set artifacts = new HashSet();
071: for (int i = 0; i < confs.length; i++) {
072: artifacts.addAll(Arrays.asList(md
073: .getArtifacts(confs[i])));
074: }
075: for (Iterator iter = artifacts.iterator(); iter
076: .hasNext();) {
077: Artifact art = (Artifact) iter.next();
078: if (!resolver.exists(art)) {
079: Message.info("declared publication not found: "
080: + art);
081: result = false;
082: }
083: }
084: }
085:
086: // check dependencies
087: DependencyDescriptor[] dds = md.getDependencies();
088: ResolveData data = new ResolveData(resolveEngine,
089: new ResolveOptions());
090: for (int i = 0; i < dds.length; i++) {
091: // check master confs
092: String[] masterConfs = dds[i].getModuleConfigurations();
093: for (int j = 0; j < masterConfs.length; j++) {
094: if (!"*".equals(masterConfs[j].trim())
095: && md.getConfiguration(masterConfs[j]) == null) {
096: Message
097: .info("dependency required in non existing conf for "
098: + ivyFile
099: + " \n\tin "
100: + dds[i]
101: .getDependencyRevisionId()
102: + ": " + masterConfs[j]);
103: result = false;
104: }
105: }
106: // resolve
107: DependencyResolver resolver = settings
108: .getResolver(dds[i].getDependencyRevisionId());
109: ResolvedModuleRevision rmr = resolver.getDependency(
110: dds[i], data);
111: if (rmr == null) {
112: Message.info("dependency not found in " + ivyFile
113: + ":\n\t" + dds[i]);
114: result = false;
115: } else {
116: String[] depConfs = dds[i]
117: .getDependencyConfigurations(md
118: .getConfigurationsNames());
119: for (int j = 0; j < depConfs.length; j++) {
120: if (!Arrays.asList(
121: rmr.getDescriptor()
122: .getConfigurationsNames())
123: .contains(depConfs[j])) {
124: Message
125: .info("dependency configuration is missing for "
126: + ivyFile
127: + "\n\tin "
128: + dds[i]
129: .getDependencyRevisionId()
130: + ": " + depConfs[j]);
131: result = false;
132: }
133: Artifact[] arts = rmr.getDescriptor()
134: .getArtifacts(depConfs[j]);
135: for (int k = 0; k < arts.length; k++) {
136: if (!resolver.exists(arts[k])) {
137: Message
138: .info("dependency artifact is missing for "
139: + ivyFile
140: + "\n\t in "
141: + dds[i]
142: .getDependencyRevisionId()
143: + ": " + arts[k]);
144: result = false;
145: }
146: }
147: }
148: }
149: }
150: return result;
151: } catch (ParseException e) {
152: Message.info("parse problem on " + ivyFile + ": " + e);
153: return false;
154: } catch (IOException e) {
155: Message.info("io problem on " + ivyFile + ": " + e);
156: return false;
157: } catch (Exception e) {
158: Message.info("problem on " + ivyFile + ": " + e);
159: return false;
160: }
161: }
162:
163: }
|