View Javadoc
1   package org.codehaus.plexus.metadata;
2   
3   /*
4    * The MIT License
5    * 
6    * Copyright (c) 2004-2006, The Codehaus
7    * 
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
9    * associated documentation files (the "Software"), to deal in the Software without restriction,
10   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
11   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in all copies or
15   * substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22   */
23  
24  import java.io.BufferedWriter;
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.OutputStreamWriter;
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.codehaus.plexus.component.repository.ComponentDependency;
35  import org.codehaus.plexus.component.repository.ComponentDescriptor;
36  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
37  import org.codehaus.plexus.logging.AbstractLogEnabled;
38  import org.codehaus.plexus.metadata.gleaner.AnnotationComponentGleaner;
39  import org.codehaus.plexus.metadata.merge.Merger;
40  import org.codehaus.plexus.util.FileUtils;
41  import org.codehaus.plexus.util.IOUtil;
42  
43  /**
44   * @author Jason van Zyl
45   */
46  @Component(role = MetadataGenerator.class)
47  public class DefaultMetadataGenerator
48      extends AbstractLogEnabled
49      implements MetadataGenerator
50  {
51      @Requirement
52      private Merger merger;
53  
54      private ComponentDescriptor<?>[] roleDefaults;
55  
56      // should be a component
57      private ComponentDescriptorExtractor[] extractors;
58  
59      // should be a component
60      private ComponentDescriptorWriter writer = new DefaultComponentDescriptorWriter();
61  
62      public void generateDescriptor( MetadataGenerationRequest request )
63          throws Exception
64      {
65          assert request.outputFile != null;
66  
67          if ( extractors == null || extractors.length == 0 )
68          {
69              extractors = new ComponentDescriptorExtractor[] { new SourceComponentDescriptorExtractor(), new ClassComponentDescriptorExtractor( new AnnotationComponentGleaner() ) };
70          }
71  
72          List<ComponentDescriptor<?>> descriptors = new ArrayList<ComponentDescriptor<?>>();
73  
74          for (ComponentDescriptorExtractor extractor : extractors) {
75              try {
76                  List<ComponentDescriptor<?>> list = extractor.extract(request, roleDefaults);
77                  if (list != null && !list.isEmpty()) {
78                      descriptors.addAll(list);
79                  }
80              } catch (Exception e) {
81                  throw new Exception("Failed to extract descriptors", e);
82              }
83          }
84  
85          List<File> componentDescriptors = new ArrayList<File>();        
86          
87          //
88          // If we found descriptors, write out the discovered descriptors
89          //
90          if ( descriptors.size() > 0 )
91          {
92              getLogger().info( "Discovered " + descriptors.size() + " component descriptors(s)" );
93  
94              ComponentSetDescriptor set = new ComponentSetDescriptor();
95              set.setComponents( descriptors );
96              set.setDependencies( Collections.<ComponentDependency> emptyList() );
97              
98              if ( request.componentDescriptorDirectory == null )
99              {
100                 writeDescriptor( set, request.outputFile );
101             }
102             else
103             {
104                 if ( request.intermediaryFile == null )
105                 {
106                     request.intermediaryFile = File.createTempFile( "plexus-metadata", "xml" );
107                     request.intermediaryFile.deleteOnExit();
108                 }
109                 writeDescriptor( set, request.intermediaryFile );
110                 componentDescriptors.add( request.intermediaryFile );
111             }
112         }
113         
114 
115         //
116         // Deal with merging
117         //
118         if ( request.componentDescriptorDirectory != null && request.componentDescriptorDirectory.isDirectory() )
119         {
120             File[] files = request.componentDescriptorDirectory.listFiles();
121 
122             for ( File file : files )
123             {
124                 if ( file.getName().endsWith( ".xml" ) && !file.getName().equals( "plexus.xml" ) )
125                 {
126                     componentDescriptors.add( file );
127                 }
128             }
129         }
130 
131         if ( componentDescriptors.size() > 0 )
132         {
133             merger.mergeDescriptors( request.outputFile, componentDescriptors );
134         }
135     }
136 
137     private void writeDescriptor( ComponentSetDescriptor desc, File outputFile )
138         throws Exception
139     {
140         assert desc != null;
141         assert outputFile != null;
142 
143         FileUtils.forceMkdir( outputFile.getParentFile() );
144 
145         BufferedWriter output = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( outputFile ), "UTF-8" ) );
146 
147         try
148         {
149             writer.writeDescriptorSet( output, desc, false );
150             output.flush();
151         }
152         finally
153         {
154             IOUtil.close( output );
155         }
156 
157         getLogger().debug( "Wrote: " + outputFile );
158     }
159 }