Coverage Report - org.codehaus.plexus.metadata.DefaultMetadataGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMetadataGenerator
0 %
0/45
0 %
0/40
9
 
 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  0
 @Component(role = MetadataGenerator.class)
 47  0
 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  0
     private ComponentDescriptorWriter writer = new DefaultComponentDescriptorWriter();
 61  
 
 62  
     public void generateDescriptor( MetadataGenerationRequest request )
 63  
         throws Exception
 64  
     {
 65  0
         assert request.outputFile != null;
 66  
 
 67  0
         if ( extractors == null || extractors.length == 0 )
 68  
         {
 69  0
             extractors = new ComponentDescriptorExtractor[] { new SourceComponentDescriptorExtractor(), new ClassComponentDescriptorExtractor( new AnnotationComponentGleaner() ) };
 70  
         }
 71  
 
 72  0
         List<ComponentDescriptor<?>> descriptors = new ArrayList<ComponentDescriptor<?>>();
 73  
 
 74  0
         for (ComponentDescriptorExtractor extractor : extractors) {
 75  
             try {
 76  0
                 List<ComponentDescriptor<?>> list = extractor.extract(request, roleDefaults);
 77  0
                 if (list != null && !list.isEmpty()) {
 78  0
                     descriptors.addAll(list);
 79  
                 }
 80  0
             } catch (Exception e) {
 81  0
                 throw new Exception("Failed to extract descriptors", e);
 82  0
             }
 83  
         }
 84  
 
 85  0
         List<File> componentDescriptors = new ArrayList<File>();        
 86  
         
 87  
         //
 88  
         // If we found descriptors, write out the discovered descriptors
 89  
         //
 90  0
         if ( descriptors.size() > 0 )
 91  
         {
 92  0
             getLogger().info( "Discovered " + descriptors.size() + " component descriptors(s)" );
 93  
 
 94  0
             ComponentSetDescriptor set = new ComponentSetDescriptor();
 95  0
             set.setComponents( descriptors );
 96  0
             set.setDependencies( Collections.<ComponentDependency> emptyList() );
 97  
             
 98  0
             if ( request.componentDescriptorDirectory == null )
 99  
             {
 100  0
                 writeDescriptor( set, request.outputFile );
 101  
             }
 102  
             else
 103  
             {
 104  0
                 if ( request.intermediaryFile == null )
 105  
                 {
 106  0
                     request.intermediaryFile = File.createTempFile( "plexus-metadata", "xml" );
 107  0
                     request.intermediaryFile.deleteOnExit();
 108  
                 }
 109  0
                 writeDescriptor( set, request.intermediaryFile );
 110  0
                 componentDescriptors.add( request.intermediaryFile );
 111  
             }
 112  
         }
 113  
         
 114  
 
 115  
         //
 116  
         // Deal with merging
 117  
         //
 118  0
         if ( request.componentDescriptorDirectory != null && request.componentDescriptorDirectory.isDirectory() )
 119  
         {
 120  0
             File[] files = request.componentDescriptorDirectory.listFiles();
 121  
 
 122  0
             for ( File file : files )
 123  
             {
 124  0
                 if ( file.getName().endsWith( ".xml" ) && !file.getName().equals( "plexus.xml" ) )
 125  
                 {
 126  0
                     componentDescriptors.add( file );
 127  
                 }
 128  
             }
 129  
         }
 130  
 
 131  0
         if ( componentDescriptors.size() > 0 )
 132  
         {
 133  0
             merger.mergeDescriptors( request.outputFile, componentDescriptors );
 134  
         }
 135  0
     }
 136  
 
 137  
     private void writeDescriptor( ComponentSetDescriptor desc, File outputFile )
 138  
         throws Exception
 139  
     {
 140  0
         assert desc != null;
 141  0
         assert outputFile != null;
 142  
 
 143  0
         FileUtils.forceMkdir( outputFile.getParentFile() );
 144  
 
 145  0
         BufferedWriter output = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( outputFile ), "UTF-8" ) );
 146  
 
 147  
         try
 148  
         {
 149  0
             writer.writeDescriptorSet( output, desc, false );
 150  0
             output.flush();
 151  
         }
 152  
         finally
 153  
         {
 154  0
             IOUtil.close( output );
 155  0
         }
 156  
 
 157  0
         getLogger().debug( "Wrote: " + outputFile );
 158  0
     }
 159  
 }