1 8 package org.apache.avalon.phoenix.components.application; 9 10 import java.util.ArrayList ; 11 import org.apache.avalon.phoenix.metadata.BlockMetaData; 12 import org.apache.avalon.phoenix.metadata.DependencyMetaData; 13 import org.apache.avalon.phoenix.metainfo.DependencyDescriptor; 14 15 20 class DependencyGraph 21 { 22 private DependencyGraph() 24 { 25 } 26 27 36 public static String [] walkGraph( final boolean forward, final BlockMetaData[] blocks ) 37 { 38 final ArrayList result = new ArrayList (); 39 40 final ArrayList done = new ArrayList (); 43 44 for( int i = 0; i < blocks.length; i++ ) 45 { 46 visitBlock( blocks[ i ], blocks, forward, done, result ); 47 } 48 49 return (String [])result.toArray( new String [ 0 ] ); 50 } 51 52 private static void visitBlock( final BlockMetaData block, 53 final BlockMetaData[] blocks, 54 final boolean forward, 55 final ArrayList done, 56 final ArrayList order ) 57 { 58 final String name = block.getName(); 60 if( done.contains( name ) ) 61 { 62 return; 63 } 64 done.add( name ); 65 66 if( forward ) 67 { 68 visitDependencies( block, blocks, done, order ); 69 } 70 else 71 { 72 visitReverseDependencies( block, blocks, done, order ); 73 } 74 75 order.add( name ); 76 } 77 78 83 private static void visitDependencies( final BlockMetaData block, 84 final BlockMetaData[] blocks, 85 final ArrayList done, 86 final ArrayList order ) 87 { 88 final DependencyDescriptor[] descriptors = block.getBlockInfo().getDependencies(); 89 for( int i = 0; i < descriptors.length; i++ ) 90 { 91 final DependencyMetaData dependency = block.getDependency( descriptors[ i ].getRole() ); 92 final BlockMetaData other = getBlock( dependency.getName(), blocks ); 93 visitBlock( other, blocks, true, done, order ); 94 } 95 } 96 97 103 private static void visitReverseDependencies( final BlockMetaData block, 104 final BlockMetaData[] blocks, 105 final ArrayList done, 106 final ArrayList order ) 107 { 108 final String name = block.getName(); 109 110 for( int i = 0; i < blocks.length; i++ ) 111 { 112 final BlockMetaData other = blocks[ i ]; 113 final DependencyMetaData[] roles = other.getDependencies(); 114 115 for( int j = 0; j < roles.length; j++ ) 116 { 117 final String depends = roles[ j ].getName(); 118 if( depends.equals( name ) ) 119 { 120 visitBlock( other, blocks, false, done, order ); 121 } 122 } 123 } 124 } 125 126 133 private static BlockMetaData getBlock( final String name, final BlockMetaData[] blocks ) 134 { 135 for( int i = 0; i < blocks.length; i++ ) 136 { 137 if( blocks[ i ].getName().equals( name ) ) 138 { 139 return blocks[ i ]; 140 } 141 } 142 143 throw new IllegalStateException (); 145 } 146 } 147
| Popular Tags
|