1 17 18 package org.apache.avalon.fortress.tools; 19 20 import com.thoughtworks.qdox.ant.AbstractQdoxTask; 21 import com.thoughtworks.qdox.model.DocletTag; 22 import com.thoughtworks.qdox.model.JavaClass; 23 import org.apache.avalon.fortress.util.dag.CyclicDependencyException; 24 import org.apache.avalon.fortress.util.dag.DirectedAcyclicGraphVerifier; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.IOException ; 31 import java.io.PrintWriter ; 32 import java.util.*; 33 34 40 public final class ComponentMetaInfoCollector extends AbstractQdoxTask 41 { 42 45 private final Map m_services = new HashMap(); 46 47 50 private File m_destDir; 51 52 55 private File m_serviceFile; 56 57 private static final String TAG_COMPONENT = "avalon.component"; 58 59 64 public void setDestDir( final File destDir ) 65 { 66 m_destDir = destDir; 67 } 68 69 74 public void execute() 75 throws BuildException 76 { 77 validate(); 78 79 log( "Writing Info descriptors as property files (.meta)." ); 80 81 super.execute(); 82 83 try 84 { 85 collectInfoMetaData(); 86 writeComponents(); 87 88 writeServiceList( m_services.values().iterator() ); 89 90 log( "Collecting service information." ); 91 writeServices(); 92 } 93 catch ( final Exception e ) 94 { 95 throw new BuildException( e.toString(), e ); 96 } 97 finally 98 { 99 Component.m_repository.clear(); 100 } 101 } 102 103 108 private void writeComponents() throws IOException , CyclicDependencyException 109 { 110 final List dagVerifyList = new ArrayList( Component.m_repository.size() ); 111 final Iterator it = Component.m_repository.iterator(); 112 while ( it.hasNext() ) 113 { 114 final Component comp = (Component) it.next(); 115 comp.serialize( m_destDir ); 116 dagVerifyList.add( comp.getVertex() ); 117 } 118 119 DirectedAcyclicGraphVerifier.verify( dagVerifyList ); 120 } 121 122 128 public void writeServiceList( final Iterator it ) throws IOException 129 { 130 final PrintWriter writer = new PrintWriter ( new FileWriter ( m_serviceFile ) ); 131 int numServices = 0; 132 133 while ( it.hasNext() ) 134 { 135 writer.println( ( (Service) it.next() ).getType() ); 136 numServices++; 137 } 138 139 writer.close(); 140 141 if ( numServices == 0 ) 142 { 143 m_serviceFile.delete(); 144 } 145 } 146 147 150 private void validate() 151 { 152 if ( null == m_destDir ) 153 { 154 final String message = 155 "DestDir (" + m_destDir + ") not specified"; 156 throw new BuildException( message ); 157 } 158 159 if ( !m_destDir.isDirectory() ) 160 { 161 final String message = 162 "DestDir (" + m_destDir + ") is not a directory."; 163 throw new BuildException( message ); 164 } 165 166 if ( !m_destDir.exists() && !m_destDir.mkdirs() ) 167 { 168 final String message = 169 "DestDir (" + m_destDir + ") could not be created."; 170 throw new BuildException( message ); 171 } 172 173 m_serviceFile = new File ( m_destDir, "services.list" ); 174 } 175 176 179 private void collectInfoMetaData() 180 { 181 final Iterator it = allClasses.iterator(); 182 while ( it.hasNext() ) 183 { 184 final JavaClass javaClass = (JavaClass) it.next(); 185 final DocletTag tag = javaClass.getTagByName( TAG_COMPONENT ); 186 187 if ( null != tag ) 188 { 189 final Component comp = new Component( javaClass ); 190 191 Iterator sit = comp.getServiceNames(); 192 while ( sit.hasNext() ) 193 { 194 String servName = (String ) sit.next(); 195 Service service = getService( servName ); 196 service.addComponent( comp ); 197 } 198 199 Iterator dit = comp.getDependencyNames(); 200 while ( dit.hasNext() ) 201 { 202 String depName = (String ) dit.next(); 203 Service service = getService( depName ); 204 comp.addDependency( service ); 205 } 206 } 207 } 208 } 209 210 216 protected Service getService( final String type ) 217 { 218 Service service = (Service) m_services.get( type ); 219 220 if ( null == service ) 221 { 222 service = new Service( type ); 223 m_services.put( service.getType(), service ); 224 } 225 226 return service; 227 } 228 229 232 private void writeServices() 233 { 234 final File baseDir = new File ( m_destDir, "META-INF/services/" ); 235 baseDir.mkdirs(); 236 237 final Iterator services = m_services.values().iterator(); 238 239 while ( services.hasNext() ) 240 { 241 final Service service = (Service) services.next(); 242 log( "Processing service " + service.getType(), Project.MSG_VERBOSE ); 243 try 244 { 245 service.serialize( m_destDir ); 246 } 247 catch ( Exception e ) 248 { 249 log( "Could not save information for service " + service.getType(), Project.MSG_WARN ); 250 } 251 } 252 } 253 } 254 | Popular Tags |