1 87 package org.codehaus.loom.components.application; 88 89 import java.util.ArrayList ; 90 91 import org.codehaus.loom.components.util.info.DependencyDescriptor; 92 import org.codehaus.loom.components.util.metadata.DependencyDirective; 93 import org.codehaus.loom.components.util.profile.ComponentProfile; 94 95 98 class DependencyGraph 99 { 100 private DependencyGraph() 102 { 103 } 104 105 114 public static String [] walkGraph( final boolean forward, 115 final ComponentProfile[] blocks ) 116 { 117 final ArrayList result = new ArrayList (); 118 119 final ArrayList done = new ArrayList (); 122 123 for( int i = 0; i < blocks.length; i++ ) 124 { 125 visitBlock( blocks[ i ], blocks, forward, done, result ); 126 } 127 128 return (String [])result.toArray( new String [ 0 ] ); 129 } 130 131 private static void visitBlock( final ComponentProfile block, 132 final ComponentProfile[] blocks, 133 final boolean forward, 134 final ArrayList done, 135 final ArrayList order ) 136 { 137 final String name = block.getTemplate().getName(); 139 if( done.contains( name ) ) 140 { 141 return; 142 } 143 done.add( name ); 144 145 if( forward ) 146 { 147 visitDependencies( block, blocks, done, order ); 148 } 149 else 150 { 151 visitReverseDependencies( block, blocks, done, order ); 152 } 153 154 order.add( name ); 155 } 156 157 162 private static void visitDependencies( final ComponentProfile block, 163 final ComponentProfile[] blocks, 164 final ArrayList done, 165 final ArrayList order ) 166 { 167 final DependencyDescriptor[] descriptors = block.getInfo() 168 .getDependencies(); 169 for( int i = 0; i < descriptors.length; i++ ) 170 { 171 final String key = descriptors[ i ].getKey(); 172 final DependencyDirective[] dependencySet = block.getTemplate() 173 .getDependencies( key ); 174 for( int j = 0; j < dependencySet.length; j++ ) 175 { 176 final DependencyDirective dependency = dependencySet[ j ]; 177 final ComponentProfile other = getBlock( 178 dependency.getProviderName(), blocks ); 179 visitBlock( other, blocks, true, done, order ); 180 } 181 } 182 } 183 184 190 private static void visitReverseDependencies( final ComponentProfile block, 191 final ComponentProfile[] blocks, 192 final ArrayList done, 193 final ArrayList order ) 194 { 195 final String name = block.getTemplate().getName(); 196 197 for( int i = 0; i < blocks.length; i++ ) 198 { 199 final ComponentProfile other = blocks[ i ]; 200 final DependencyDirective[] roles = other.getTemplate() 201 .getDependencies(); 202 203 for( int j = 0; j < roles.length; j++ ) 204 { 205 final String depends = roles[ j ].getProviderName(); 206 if( depends.equals( name ) ) 207 { 208 visitBlock( other, blocks, false, done, order ); 209 } 210 } 211 } 212 } 213 214 221 private static ComponentProfile getBlock( final String name, 222 final ComponentProfile[] blocks ) 223 { 224 for( int i = 0; i < blocks.length; i++ ) 225 { 226 if( blocks[ i ].getTemplate().getName().equals( name ) ) 227 { 228 return blocks[ i ]; 229 } 230 } 231 232 throw new IllegalStateException (); 234 } 235 } 236 | Popular Tags |