View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.myeclipse;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileFilter;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Map;
30  
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.eclipse.Messages;
33  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
34  import org.apache.maven.plugin.ide.IdeUtils;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.codehaus.plexus.util.IOUtil;
37  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
38  import org.codehaus.plexus.util.xml.XMLWriter;
39  
40  /**
41   * MyEclipse .springBeans configuration file writer
42   * 
43   * @author Olivier Jacob
44   */
45  public class MyEclipseSpringBeansWriter
46      extends AbstractEclipseWriter
47  {
48      private static final String MYECLIPSE_SPRING_CONFIGURATION_FILENAME = ".springBeans";
49  
50      private static final String MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION = "beansProjectDescription";
51  
52      private static final String MYECLIPSE_SPRING_CONFIG_EXTENSIONS = "configExtensions";
53  
54      private static final String MYECLIPSE_SPRING_CONFIG_EXTENSION = "configExtension";
55  
56      private static final String MYECLIPSE_SPRING_CONFIGS = "configs";
57  
58      private static final String MYECLIPSE_SPRING_CONFIG = "config";
59  
60      private static final String MYECLIPSE_SPRING_CONFIGSETS = "configSets";
61  
62      private static final String MYECLIPSE_SPRING_VERSION = "springVersion";
63  
64      /**
65       * Spring configuration filenames (injected by the plugin)
66       */
67      private Map springConfig;
68  
69      /**
70       * Allow injection of Spring configuration filenames through constructor
71       * 
72       * @param springConfig a map holding Spring configuration properties
73       */
74      public MyEclipseSpringBeansWriter( Map springConfig )
75      {
76          this.springConfig = springConfig;
77      }
78  
79      /**
80       * Write MyEclipse .springBeans configuration file
81       * 
82       * @throws MojoExecutionException
83       */
84      public void write()
85          throws MojoExecutionException
86      {
87          FileWriter springFileWriter;
88          try
89          {
90              springFileWriter = new FileWriter( new File( config.getEclipseProjectDirectory(), 
91                                                           MYECLIPSE_SPRING_CONFIGURATION_FILENAME ) );
92          }
93          catch ( IOException ex )
94          {
95              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
96          }
97  
98          XMLWriter writer = new PrettyPrintXMLWriter( springFileWriter, "UTF-8", null );
99  
100         writer.startElement( MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION );
101         // Configuration extension
102         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSIONS );
103         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSION );
104         writer.writeText( "xml" );
105         writer.endElement();
106         writer.endElement();
107 
108         // Configuration files
109         writer.startElement( MYECLIPSE_SPRING_CONFIGS );
110 
111         // maven's cwd stays at the top of hierarchical projects so we
112         // do this with full path so it works as we descend through various modules (projects)
113         File basedir = config.getEclipseProjectDirectory();
114 
115         for ( Object o : getConfigurationFilesList( new File( basedir, (String) springConfig.get( "basedir" ) ),
116                                                     (String) springConfig.get( "file-pattern" ) ) )
117         {
118             String onConfigFileName = (String) o;
119             File onConfigFile = new File( onConfigFileName );
120             String relativeFileName = IdeUtils.toRelativeAndFixSeparator( basedir, onConfigFile, false );
121 
122             writer.startElement( MYECLIPSE_SPRING_CONFIG );
123             writer.writeText( relativeFileName );
124             writer.endElement();
125         }
126         writer.endElement();
127 
128         // Configuration sets
129         writer.startElement( MYECLIPSE_SPRING_CONFIGSETS );
130         writer.endElement();
131 
132         // Spring version
133         writer.startElement( MYECLIPSE_SPRING_VERSION );
134         writer.writeText( (String) springConfig.get( "version" ) );
135         writer.endElement();
136 
137         writer.endElement();
138 
139         IOUtil.close( springFileWriter );
140     }
141 
142     /**
143      * Retrieve the list of Spring configuration files recursively from the <code>basedir</code> directory, considering
144      * only filenames matching the <code>pattern</code> given
145      * 
146      * @param basedir the path to the base directory to search in
147      * @param pattern file include pattern
148      * @return the list of filenames matching the given pattern
149      */
150     private Collection getConfigurationFilesList( File basedir, String pattern )
151     {
152         ArrayList configFiles = new ArrayList();
153 
154         try
155         {
156             if ( basedir.exists() )
157             {
158                 log.debug( "Scanning " + basedir + " for spring definition files" );
159                 File[] subdirs = basedir.listFiles( new FileFilter()
160                 {
161                     public boolean accept( File pathname )
162                     {
163                         return pathname.isDirectory();
164                     }
165                 } );
166 
167                 if ( subdirs != null )
168                 {
169                     for ( File subdir : subdirs )
170                     {
171                         configFiles.addAll( getConfigurationFilesList( subdir, pattern ) );
172                     }
173                 }
174 
175                 configFiles.addAll( FileUtils.getFileNames( basedir, pattern, null, true ) );
176             }
177             else
178             {
179                 // This isn't fatal because sometimes we run this in a nested set of
180                 // projects where some of the projects may not have spring configuration
181                 log.warn( Messages.getString( "MyEclipseSpringBeansWriter.baseDirDoesNotExist",
182                                               new Object[] { basedir } ) );
183             }
184         }
185         catch ( IOException ioe )
186         {
187             log.error( "Error while retrieving Spring configuration files. Returning list in current state" );
188         }
189         // Sort them to have something constant
190         Collections.sort( configFiles );
191         return configFiles;
192     }
193 }