1 /* 2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to 3 * the spring framework, http://el4j.sf.net 4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24, 5 * 1000 Lausanne, Switzerland, http://www.elca.ch 6 * 7 * EL4J is published under the GNU Lesser General Public License (LGPL) 8 * Version 2.1. See http://www.gnu.org/licenses/ 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * For alternative licensing, please contact info@elca.ch 16 */ 17 package ch.elca.el4j.maven.plugins.springide; 18 19 import java.io.File; 20 import java.io.FileNotFoundException; 21 import java.util.ArrayList; 22 import java.util.Arrays; 23 import java.util.List; 24 import java.util.Scanner; 25 26 import org.apache.commons.io.FileUtils; 27 import org.apache.commons.io.filefilter.FileFilterUtils; 28 29 /** 30 * 31 * This class is a helper class that will look for a file that defines the Module Application Context. 32 * First it will check if it finds a .xml file, as this would be the preferred way to define the Module 33 * Application Context in a web project, afterwards it will check through all .java files in the source 34 * alphabetically and return the first one that contains // $$ BEANS INCLUDE $$ 35 * 36 * @svnLink $Revision: 3884 $;$Date: 2009-08-04 15:48:31 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/plugins/maven-spring-ide-plugin/src/main/java/ch/elca/el4j/maven/plugins/springide/SourceResolver.java $ 37 * 38 * @author Daniel Thomas (DTH) 39 */ 40 41 public class SourceResolver { 42 43 /** 44 * Search for a file in the sourcefiles that contains the module application context. 45 * 46 * @param baseDir 47 * are the paths to the sourcefiles 48 * @return File of first found document that contains $$ BEANS EXCLUDE 49 * @throws FileNotFoundException 50 */ 51 public static File getSourceFile(File baseDir) { 52 File returnFile = null; 53 List<File> javaFiles = null; 54 55 javaFiles = getAllRelevantFiles(baseDir); 56 57 /* 58 * check if there is any .java file that contains // $$ BEANS INCLUDE $$, and return it 59 */ 60 61 javaFiles = sortJavaFilesAlpabetically(javaFiles); 62 63 for (File javaFile : javaFiles) { 64 Scanner scan = null; 65 try { 66 scan = new Scanner(javaFile); 67 68 while (scan.hasNextLine()) { 69 if (scan.nextLine().contains("$$ BEANS INCLUDE $$")) { 70 return javaFile; 71 } 72 } 73 74 } catch (Exception e) { 75 /* nothing to be done */ 76 } finally { 77 if (scan != null) { 78 scan.close(); 79 } 80 } 81 } 82 83 return returnFile; 84 } 85 86 /** 87 * Simple selection sort on the names of the files in the javaFiles List. 88 */ 89 private static List<File> sortJavaFilesAlpabetically(List<File> javaFiles) { 90 File[] javaFilesAsArray = (File[]) javaFiles.toArray(new File[1]); 91 Arrays.sort(javaFilesAsArray); 92 return Arrays.asList(javaFilesAsArray); 93 94 } 95 96 /** 97 * Populates the Lists javaFiles and xmlFiles with the corresponding Files it found in the sourceDirectory. Checks 98 * recursevely through folders. 99 * 100 * @param sourceDirectory 101 * folder to start with 102 */ 103 104 private static List<File> getAllRelevantFiles(File sourceDirectory) { 105 String[] extensions = {"java"}; 106 return (List<File>) FileUtils.listFiles(sourceDirectory, extensions, true); 107 108 } 109 110 }