KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > j2seimport > DependencyValidator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.j2seimport;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Stack JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27
28 /**
29  *
30  * @author Radek Matous
31  */

32 public final class DependencyValidator {
33     private Stack JavaDoc solved = new Stack JavaDoc();
34     private StringBuffer JavaDoc errorMessage = null;
35     private static final Logger JavaDoc 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 JavaDoc getErrorMessage() {
52         return errorMessage != null ? errorMessage.toString() : ""; //NOI18N
53
}
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 JavaDoc/*<Project>*/ subProjects = projectDefinition.getDependencies();
66         if (subProjects != null && !subProjects.isEmpty()) {
67             for (Iterator JavaDoc 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."; // NOI18N
84
ProjectModel rootOfCycle = (ProjectModel) solved.get(solved.size() - where);
85         assert start == rootOfCycle;
86         StringBuffer JavaDoc cycle = new StringBuffer JavaDoc();
87         for (Iterator JavaDoc it = solved.iterator(); it.hasNext(); ) {
88             cycle.append(((ProjectModel)it.next()).getName() + " --> "); // NOI18N
89
}
90         cycle.append(rootOfCycle.getName() + " --> ..."); // NOI18N
91
errorMessage = cycle;
92         logger.warning("Cycle dependencies was detected. Detected cycle: " + cycle); // NOI18N
93
}
94     
95 }
96
Popular Tags