KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > builders > DependencyLoopFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.builders;
12
13 /**
14  * @version 1.0
15  * @author
16  */

17
18 import java.util.*;
19
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.pde.core.plugin.*;
22 import org.eclipse.pde.internal.*;
23 import org.eclipse.pde.internal.core.*;
24
25 public class DependencyLoopFinder {
26
27     public static DependencyLoop [] findLoops(IPlugin root) {
28         return findLoops(root, null);
29     }
30
31     public static DependencyLoop [] findLoops(IPlugin root, IPlugin [] candidates) {
32         return findLoops(root, candidates, false);
33     }
34     
35     public static DependencyLoop [] findLoops(IPlugin root, IPlugin [] candidates, boolean onlyCandidates) {
36         Vector loops = new Vector();
37         
38         Vector path = new Vector();
39         findLoops(loops, path, root, candidates, onlyCandidates, new Vector());
40         return (DependencyLoop[])loops.toArray(new DependencyLoop[loops.size()]);
41     }
42     
43     private static void findLoops(
44         Vector loops,
45         Vector path,
46         IPlugin subroot,
47         IPlugin[] candidates,
48         boolean onlyCandidates,
49         Vector exploredPlugins) {
50         if (path.size() > 0) {
51             // test the path so far
52
// is the subroot the same as root - if yes, that's it
53

54             IPlugin root = (IPlugin) path.elementAt(0);
55             if (isEquivalent(root, subroot)) {
56                 // our loop!!
57
DependencyLoop loop = new DependencyLoop();
58                 loop.setMembers((IPlugin[]) path.toArray(new IPlugin[path.size()]));
59                 int no = loops.size() + 1;
60                 loop.setName(NLS.bind(PDEMessages.Builders_DependencyLoopFinder_loopName, ("" + no))); //$NON-NLS-1$
61
loops.add(loop);
62                 return;
63             }
64             // is the subroot the same as any other node?
65
// if yes, abort - local loop that is not ours
66
for (int i = 1; i < path.size(); i++) {
67                 IPlugin node = (IPlugin) path.elementAt(i);
68                 if (isEquivalent(subroot, node)) {
69                     // local loop
70
return;
71                 }
72             }
73         }
74         Vector newPath = path.size() > 0 ? ((Vector) path.clone()) : path;
75         newPath.add(subroot);
76
77         if (!onlyCandidates) {
78             IPluginImport[] iimports = subroot.getImports();
79             for (int i = 0; i < iimports.length; i++) {
80                 IPluginImport iimport = iimports[i];
81                 String JavaDoc id = iimport.getId();
82                 //Be paranoid
83
if (id == null)
84                     continue;
85                 if (!exploredPlugins.contains(id)) {
86                     // is plugin in list of non loop yielding plugins
87
//Commenting linear lookup - was very slow
88
//when called from here. We will use
89
//model manager instead because it
90
//has a hash table lookup that is much faster.
91
//IPlugin child = PDECore.getDefault().findPlugin(id);
92
IPlugin child = findPlugin(id);
93                     if (child != null) {
94                         // number of loops before traversing plugin
95
int oldLoopSize = loops.size();
96
97                         findLoops(loops, newPath, child, null, false, exploredPlugins);
98
99                         // number of loops after traversing plugin
100
int newLoopsSize = loops.size();
101
102                         if (oldLoopSize == newLoopsSize) {// no change in number of loops
103
// no loops from going to this node, skip next time
104
exploredPlugins.add(id);
105                         }
106                     }
107                 }
108
109             }
110
111         }
112         if (candidates != null) {
113             for (int i = 0; i < candidates.length; i++) {
114                 IPlugin candidate = candidates[i];
115
116                 // number of loops before traversing plugin
117
int oldLoopSize = loops.size();
118
119                 findLoops(loops, newPath, candidate, null, false, exploredPlugins);
120
121                 // number of loops after traversing plugin
122
int newLoopsSize = loops.size();
123
124                 if (oldLoopSize == newLoopsSize) { // no change in number of loops
125
// no loops from going to this node, skip next time
126
exploredPlugins.add(candidate.getId());
127                 }
128             }
129         }
130
131     }
132
133     private static IPlugin findPlugin(String JavaDoc id) {
134         IPluginModelBase childModel = PDECore.getDefault().getModelManager().findPlugin(id, null, 0);
135         if (childModel==null || !(childModel instanceof IPluginModel)) return null;
136         return (IPlugin)childModel.getPluginBase();
137     }
138     
139     private static boolean isEquivalent(IPlugin left, IPlugin right) {
140         return left.getId().equals(right.getId());
141     }
142 }
143
Popular Tags