KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.builders;
12
13 import java.util.Vector JavaDoc;
14
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.pde.core.plugin.IPlugin;
17 import org.eclipse.pde.core.plugin.IPluginImport;
18 import org.eclipse.pde.core.plugin.IPluginModel;
19 import org.eclipse.pde.core.plugin.IPluginModelBase;
20 import org.eclipse.pde.core.plugin.PluginRegistry;
21 import org.eclipse.pde.internal.core.PDECoreMessages;
22
23 public class DependencyLoopFinder {
24
25     public static DependencyLoop [] findLoops(IPlugin root) {
26         return findLoops(root, null);
27     }
28
29     public static DependencyLoop [] findLoops(IPlugin root, IPlugin [] candidates) {
30         return findLoops(root, candidates, false);
31     }
32     
33     public static DependencyLoop [] findLoops(IPlugin root, IPlugin [] candidates, boolean onlyCandidates) {
34         Vector JavaDoc loops = new Vector JavaDoc();
35         
36         Vector JavaDoc path = new Vector JavaDoc();
37         findLoops(loops, path, root, candidates, onlyCandidates, new Vector JavaDoc());
38         return (DependencyLoop[])loops.toArray(new DependencyLoop[loops.size()]);
39     }
40     
41     private static void findLoops(
42         Vector JavaDoc loops,
43         Vector JavaDoc path,
44         IPlugin subroot,
45         IPlugin[] candidates,
46         boolean onlyCandidates,
47         Vector JavaDoc exploredPlugins) {
48         if (path.size() > 0) {
49             // test the path so far
50
// is the subroot the same as root - if yes, that's it
51

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