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  
23  import java.io.File;
24  import java.io.FileFilter;
25  import java.io.FileWriter;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.Map;
31  
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugin.eclipse.Messages;
34  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
35  import org.apache.maven.plugin.ide.IdeUtils;
36  import org.codehaus.plexus.util.FileUtils;
37  import org.codehaus.plexus.util.IOUtil;
38  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
39  import org.codehaus.plexus.util.xml.XMLWriter;
40  
41  /**
42   * MyEclipse .springBeans configuration file writer
43   * 
44   * @author Olivier Jacob
45   */
46  public class MyEclipseSpringBeansWriter
47      extends AbstractEclipseWriter
48  {
49      private static final String MYECLIPSE_SPRING_CONFIGURATION_FILENAME = ".springBeans";
50  
51      private static final String MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION = "beansProjectDescription";
52  
53      private static final String MYECLIPSE_SPRING_CONFIG_EXTENSIONS = "configExtensions";
54  
55      private static final String MYECLIPSE_SPRING_CONFIG_EXTENSION = "configExtension";
56  
57      private static final String MYECLIPSE_SPRING_CONFIGS = "configs";
58  
59      private static final String MYECLIPSE_SPRING_CONFIG = "config";
60  
61      private static final String MYECLIPSE_SPRING_CONFIGSETS = "configSets";
62  
63      private static final String MYECLIPSE_SPRING_VERSION = "springVersion";
64  
65      /**
66       * Spring configuration filenames (injected by the plugin)
67       */
68      private Map springConfig;
69  
70      /**
71       * Allow injection of Spring configuration filenames through constructor
72       * 
73       * @param springConfig a map holding Spring configuration properties
74       */
75      public MyEclipseSpringBeansWriter( Map springConfig )
76      {
77          this.springConfig = springConfig;
78      }
79  
80      /**
81       * Write MyEclipse .springBeans configuration file
82       * 
83       * @throws MojoExecutionException
84       */
85      public void write()
86          throws MojoExecutionException
87      {
88          FileWriter springFileWriter;
89          try
90          {
91              springFileWriter =
92                  new FileWriter( new File( config.getEclipseProjectDirectory(), MYECLIPSE_SPRING_CONFIGURATION_FILENAME ) );
93          }
94          catch ( IOException ex )
95          {
96              throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
97          }
98  
99          XMLWriter writer = new PrettyPrintXMLWriter( springFileWriter, "UTF-8", null );
100 
101         writer.startElement( MYECLIPSE_SPRING_BEANS_PROJECT_DESCRIPTION );
102         // Configuration extension
103         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSIONS );
104         writer.startElement( MYECLIPSE_SPRING_CONFIG_EXTENSION );
105         writer.writeText( "xml" );
106         writer.endElement();
107         writer.endElement();
108 
109         // Configuration files
110         writer.startElement( MYECLIPSE_SPRING_CONFIGS );
111 
112         // maven's cwd stays at the top of hierarchical projects so we
113         // do this with full path so it works as we descend through various modules (projects)
114         File basedir = config.getEclipseProjectDirectory();
115 
116         for (Object o : getConfigurationFilesList(new File(basedir, (String) springConfig.get("basedir")),
117                 (String) springConfig.get("file-pattern"))) {
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                         configFiles.addAll(getConfigurationFilesList(subdir, pattern));
171                     }
172                 }
173 
174                 configFiles.addAll( FileUtils.getFileNames( basedir, pattern, null, true ) );
175             }
176             else
177             {
178                 // This isn't fatal because sometimes we run this in a nested set of
179                 // projects where some of the projects may not have spring configuration
180                 log.warn( Messages.getString( "MyEclipseSpringBeansWriter.baseDirDoesNotExist",
181                                               new Object[] { basedir } ) );
182             }
183         }
184         catch ( IOException ioe )
185         {
186             log.error( "Error while retrieving Spring configuration files. Returning list in current state" );
187         }
188         // Sort them to have something constant
189         Collections.sort( configFiles );
190         return configFiles;
191     }
192 }