KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > tools > ComponentMetaInfoCollector


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.*;
33
34 /**
35  * ANT task to collect all the meta information for the components.
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">The Avalon Team</a>
38  * @version CVS $Revision: 1.27 $ $Date: 2004/02/28 15:16:27 $
39  */

40 public final class ComponentMetaInfoCollector extends AbstractQdoxTask
41 {
42     /**
43      * The services to write the meta info for.
44      */

45     private final Map m_services = new HashMap();
46
47     /**
48      * The destination directory for metadata files.
49      */

50     private File JavaDoc m_destDir;
51
52     /**
53      * The service list destination.
54      */

55     private File JavaDoc m_serviceFile;
56
57     private static final String JavaDoc TAG_COMPONENT = "avalon.component";
58
59     /**
60      * Set the destination directory for the meta information.
61      *
62      * @param destDir The destination directory
63      */

64     public void setDestDir( final File JavaDoc destDir )
65     {
66         m_destDir = destDir;
67     }
68
69     /**
70      * Execute generator task.
71      *
72      * @throws BuildException if there was a problem collecting the info
73      */

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 JavaDoc e )
94         {
95             throw new BuildException( e.toString(), e );
96         }
97         finally
98         {
99             Component.m_repository.clear();
100         }
101     }
102
103     /**
104      * Write the component meta information to the associated files.
105      *
106      * @throws IOException if there is a problem.
107      */

108     private void writeComponents() throws IOException JavaDoc, 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     /**
123      * Write the service list to the "/service.list" file.
124      *
125      * @param it The iterator for the services
126      * @throws IOException if there is a problem writing the file
127      */

128     public void writeServiceList( final Iterator it ) throws IOException JavaDoc
129     {
130         final PrintWriter JavaDoc writer = new PrintWriter JavaDoc( new FileWriter JavaDoc( 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     /**
148      * Validate that the parameters are valid.
149      */

150     private void validate()
151     {
152         if ( null == m_destDir )
153         {
154             final String JavaDoc message =
155                     "DestDir (" + m_destDir + ") not specified";
156             throw new BuildException( message );
157         }
158
159         if ( !m_destDir.isDirectory() )
160         {
161             final String JavaDoc 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 JavaDoc message =
169                     "DestDir (" + m_destDir + ") could not be created.";
170             throw new BuildException( message );
171         }
172
173         m_serviceFile = new File JavaDoc( m_destDir, "services.list" );
174     }
175
176     /**
177      * Output the metadata files.
178      */

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 JavaDoc servName = (String JavaDoc) 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 JavaDoc depName = (String JavaDoc) dit.next();
203                     Service service = getService( depName );
204                     comp.addDependency( service );
205                 }
206             }
207         }
208     }
209
210     /**
211      * Get the unique Service object for the specified type.
212      *
213      * @param type The service type name
214      * @return the Service object
215      */

216     protected Service getService( final String JavaDoc 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     /**
230      * Collect all the services and write out the implementations.
231      */

232     private void writeServices()
233     {
234         final File JavaDoc baseDir = new File JavaDoc( 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 JavaDoc e )
248             {
249                 log( "Could not save information for service " + service.getType(), Project.MSG_WARN );
250             }
251         }
252     }
253 }
254
Popular Tags