1 19 20 package org.netbeans.modules.projectimport.j2seimport; 21 22 import java.util.Iterator ; 23 import java.util.Set ; 24 import java.util.Stack ; 25 import java.util.logging.Logger ; 26 27 28 32 public final class DependencyValidator { 33 private Stack solved = new Stack (); 34 private StringBuffer errorMessage = null; 35 private static final Logger logger = 36 LoggerFactory.getDefault().createLogger(DependencyValidator.class); 37 38 39 private DependencyValidator(ProjectModel projectDefinition) { 40 checkDependencies(projectDefinition); 41 } 42 43 public static DependencyValidator checkProject(ProjectModel projectDefinition) { 44 return new DependencyValidator(projectDefinition); 45 } 46 47 public boolean isValid() { 48 return errorMessage == null; 49 } 50 51 public String getErrorMessage() { 52 return errorMessage != null ? errorMessage.toString() : ""; } 54 55 private void checkDependencies(ProjectModel projectDefinition) { 56 solved.push(projectDefinition); 57 recursiveDependencyCheck(projectDefinition); 58 ProjectModel retrievedProject = (ProjectModel)solved.pop(); 59 assert retrievedProject.equals(projectDefinition); 60 assert solved.isEmpty(); 61 } 62 63 64 private void recursiveDependencyCheck(ProjectModel projectDefinition) { 65 Set subProjects = projectDefinition.getDependencies(); 66 if (subProjects != null && !subProjects.isEmpty()) { 67 for (Iterator it = subProjects.iterator(); it.hasNext(); ) { 68 ProjectModel subDefinition = (ProjectModel) it.next(); 69 if (solved.contains(subDefinition)) { 70 recursionDetected(subDefinition); 71 return; 72 } 73 solved.push(subDefinition); 74 recursiveDependencyCheck(subDefinition); 75 ProjectModel retrievedProject = (ProjectModel)solved.pop(); 76 assert retrievedProject.equals(subDefinition); 77 } 78 } 79 } 80 81 private void recursionDetected(ProjectModel start) { 82 int where = solved.search(start); 83 assert where != -1 : "Cannot find start of the cycle."; ProjectModel rootOfCycle = (ProjectModel) solved.get(solved.size() - where); 85 assert start == rootOfCycle; 86 StringBuffer cycle = new StringBuffer (); 87 for (Iterator it = solved.iterator(); it.hasNext(); ) { 88 cycle.append(((ProjectModel)it.next()).getName() + " --> "); } 90 cycle.append(rootOfCycle.getName() + " --> ..."); errorMessage = cycle; 92 logger.warning("Cycle dependencies was detected. Detected cycle: " + cycle); } 94 95 } 96 | Popular Tags |