1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jjar;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Iterator;
22 import java.util.ArrayList;
23 import java.lang.Thread;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class DependencyEngine
50 {
51 private HashMap projects = new HashMap();
52 private ArrayList buildList = null;
53
54
55
56
57
58
59 private long currentTimestamp = -1;
60
61
62
63
64 public DependencyEngine()
65 {
66 }
67
68
69
70
71
72 public void reset()
73 {
74 projects = new HashMap();
75 }
76
77
78
79
80
81
82
83
84 public List getDependencies( String pkg )
85 {
86 return getDependencies(pkg, true);
87 }
88
89
90
91
92
93
94
95
96
97 public List getDependencies( String pkg, boolean excludeTarget )
98 {
99 buildList = new ArrayList();
100
101 try
102 {
103
104
105
106
107
108
109 if (System.currentTimeMillis() == currentTimestamp)
110 {
111 Thread.sleep(1);
112 }
113
114
115
116
117
118 currentTimestamp = System.currentTimeMillis();
119
120
121
122
123 doIt( pkg );
124 }
125 catch( Exception e )
126 {
127 System.out.println("DE.getDependencies() : " + pkg + " : " + e);
128 }
129
130
131
132
133
134
135
136
137 if( excludeTarget && buildList.size() > 0)
138 {
139 buildList.remove( buildList.size() - 1 );
140 }
141
142 return buildList;
143 }
144
145
146
147
148
149
150
151 public List getDependencies(List packages)
152 {
153 return getDependencies(packages, true);
154 }
155
156
157
158
159
160
161
162
163
164
165 public List getDependencies( List packages, boolean excludeTarget )
166 {
167 HashMap h = new HashMap();
168 ArrayList l = new ArrayList();
169
170
171
172
173
174
175
176 for( Iterator i = packages.iterator(); i.hasNext(); )
177 {
178 String pkg = (String) i.next();
179
180 List deps = getDependencies( pkg, excludeTarget );
181
182 for (Iterator ii = deps.iterator(); ii.hasNext(); )
183 {
184 String dep = (String) ii.next();
185
186 if ( h.get( dep ) == null)
187 {
188 h.put(dep, dep);
189 l.add(dep);
190 }
191 }
192 }
193
194 return l;
195 }
196
197
198
199
200
201
202
203 public List generateNamelist()
204 throws Exception
205 {
206
207
208
209
210 buildList = new ArrayList();
211
212 Iterator i = projects.keySet().iterator();
213
214 while(i.hasNext())
215 {
216 String s = (String) i.next();
217
218
219
220
221
222 doIt( s );
223 }
224
225 return buildList;
226 }
227
228
229
230
231
232
233 public List generateCookielist()
234 throws Exception
235 {
236
237
238
239
240 List list = generateNamelist();
241 ArrayList cookies = new ArrayList();
242
243 Iterator i = list.iterator();
244
245 while(i.hasNext())
246 {
247 String s = (String) i.next();
248 Node n = (Node) projects.get( s );
249
250 cookies.add( n.getCookie() );
251 }
252
253 return cookies;
254 }
255
256
257
258
259 void doIt( String current )
260 throws Exception
261 {
262 Node project = (Node) projects.get(current);
263
264 if (project == null)
265 {
266
267
268
269
270
271 buildList.add( current );
272 return;
273 }
274
275
276
277
278
279 if ( project.getTimestamp() != currentTimestamp)
280 {
281 project.setStatus( Node.ZILCH );
282 }
283
284 project.setTimestamp( currentTimestamp );
285
286
287
288
289
290 int status = project.getStatus();
291
292 if ( status == Node.WORKING )
293 {
294 throw new Exception("Detected loop while trying to build " + current);
295 }
296 else if ( status == Node.ZILCH )
297 {
298
299
300
301 project.setStatus( Node.WORKING );
302
303
304
305
306 Iterator deps = project.getDeps();
307
308
309
310
311
312 while( deps.hasNext() )
313 {
314 String dep = (String) deps.next();
315 Node depnode = (Node) projects.get( dep );
316
317 if (depnode == null)
318 {
319
320
321
322
323
324
325
326 buildList.add( dep );
327 continue;
328 }
329
330
331
332
333
334 if ( depnode.getTimestamp() != currentTimestamp)
335 {
336 depnode.setStatus( Node.ZILCH );
337 }
338
339 depnode.setTimestamp( currentTimestamp );
340
341
342
343
344
345 int depstatus = depnode.getStatus();
346
347 if ( depstatus == Node.WORKING )
348 {
349
350
351
352 throw new Exception("LOOP : checking dep " + dep + " for current = " + current );
353 }
354 else if ( depstatus == Node.ZILCH )
355 {
356
357
358
359
360
361
362 doIt( dep );
363 }
364 else if( depstatus == Node.DONE )
365 {
366
367 }
368 }
369
370
371
372
373
374
375
376
377
378 buildList.add( current );
379 project.setStatus( Node.DONE );
380
381 return;
382 }
383
384
385
386
387
388 return;
389 }
390
391 public void addProject(String project, List dependencies)
392 throws Exception
393 {
394 addProject(project, dependencies, project);
395 }
396
397
398
399
400
401
402
403
404
405 public void addProject( String project, List dependencies, Object cookie )
406 throws Exception
407 {
408
409
410
411 Node n = (Node) projects.get( project );
412
413 if (n != null)
414 {
415
416 throw new Exception("already have it...");
417 }
418
419
420
421
422
423
424 n = new Node( project, cookie );
425
426 Iterator i = dependencies.iterator();
427
428 while( i.hasNext() )
429 {
430 String dep = (String) i.next();
431
432 if ( dep.equals( project ) )
433 {
434
435 }
436 else
437 {
438
439 n.addDep( dep );
440 }
441 }
442
443
444
445
446
447 projects.put( project, n );
448
449 return;
450 }
451 }
452
453 class Node
454 {
455 public static int ZILCH = 0;
456 public static int WORKING = 1;
457 public static int DONE = 2;
458
459 private int status = ZILCH;
460 private ArrayList deps = new ArrayList();
461 private String name = "";
462 private Object cookie = null;
463 private long timestamp = 0;
464
465 public Node( String name, Object cookie)
466 {
467 this.name = name;
468 this.cookie = cookie;
469 }
470
471 public Object getCookie()
472 {
473 return cookie;
474 }
475
476 public void addDep( String dep )
477 {
478 deps.add( dep );
479 }
480
481 public Iterator getDeps()
482 {
483 return deps.iterator();
484 }
485
486 public void setStatus( int i )
487 {
488 status = i;
489 }
490
491 public int getStatus()
492 {
493 return status;
494 }
495
496 public long getTimestamp()
497 {
498 return timestamp;
499 }
500
501 public void setTimestamp( long t)
502 {
503 timestamp = t;
504 }
505 }
506
507