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.tools.analyser;
019:
020: import java.io.BufferedReader;
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.util.Date;
025: import java.util.HashMap;
026: import java.util.Map;
027:
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.util.Message;
032:
033: public class JarJarDependencyAnalyser implements DependencyAnalyser {
034: private File _jarjarjarLocation;
035:
036: public JarJarDependencyAnalyser(File jarjarjarLocation) {
037: _jarjarjarLocation = jarjarjarLocation;
038: }
039:
040: public ModuleDescriptor[] analyze(JarModule[] modules) {
041:
042: StringBuffer jarjarCmd = new StringBuffer("java -jar \"")
043: .append(_jarjarjarLocation.getAbsolutePath()).append(
044: "\" --find --level=jar ");
045: Map jarModulesMap = new HashMap();
046: Map mds = new HashMap();
047:
048: for (int i = 0; i < modules.length; i++) {
049: jarModulesMap.put(modules[i].getJar().getAbsolutePath(),
050: modules[i]);
051: DefaultModuleDescriptor md = DefaultModuleDescriptor
052: .newBasicInstance(modules[i].getMrid(), new Date(
053: modules[i].getJar().lastModified()));
054: mds.put(modules[i].getMrid(), md);
055: jarjarCmd.append("\"").append(
056: modules[i].getJar().getAbsolutePath()).append("\"");
057: if (i + 1 < modules.length) {
058: jarjarCmd.append(File.pathSeparator);
059: }
060: }
061:
062: Message.verbose("jarjar command: " + jarjarCmd);
063:
064: try {
065: Process p = Runtime.getRuntime().exec(jarjarCmd.toString());
066: BufferedReader r = new BufferedReader(
067: new InputStreamReader(p.getInputStream()));
068: String line;
069: while ((line = r.readLine()) != null) {
070: String[] deps = line.split(" -> ");
071: JarModule module = (JarModule) jarModulesMap
072: .get(deps[0]);
073: JarModule dependency = (JarModule) jarModulesMap
074: .get(deps[1]);
075:
076: if (module.getMrid().getModuleId().equals(
077: dependency.getMrid().getModuleId())) {
078: continue;
079: }
080: Message.verbose(module.getMrid() + " depends on "
081: + dependency.getMrid());
082:
083: DefaultModuleDescriptor md = (DefaultModuleDescriptor) mds
084: .get(module.getMrid());
085:
086: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
087: md, dependency.getMrid(), false, false, true);
088: dd.addDependencyConfiguration(
089: ModuleDescriptor.DEFAULT_CONFIGURATION,
090: ModuleDescriptor.DEFAULT_CONFIGURATION);
091: md.addDependency(dd);
092: }
093: } catch (IOException e) {
094: // TODO Auto-generated catch block
095: e.printStackTrace();
096: }
097: return (ModuleDescriptor[]) mds.values().toArray(
098: new ModuleDescriptor[mds.values().size()]);
099: }
100:
101: public static void main(String[] args) {
102: JarJarDependencyAnalyser a = new JarJarDependencyAnalyser(
103: new File("D:/temp/test2/jarjar-0.7.jar"));
104: a
105: .analyze(new JarModuleFinder(
106: "D:/temp/test2/ivyrep/[organisation]/[module]/[revision]/[artifact].[ext]")
107: .findJarModules());
108: }
109: }
|