/** * Convenience method to copy content from one Resource to another specifying whether token * filtering must be used, whether filter chains must be used, whether newer destination files may * be overwritten and whether the last modified time of <code>dest</code> file should be made * equal to the last modified time of <code>source</code>. * * @param source the Resource to copy from. Must not be <code>null</code>. * @param dest the Resource to copy to. Must not be <code>null</code>. * @param filters the collection of filters to apply to this copy. * @param filterChains filterChains to apply during the copy. * @param overwrite Whether or not the destination Resource should be overwritten if it already * exists. * @param preserveLastModified Whether or not the last modified time of the destination Resource * should be set to that of the source. * @param inputEncoding the encoding used to read the files. * @param outputEncoding the encoding used to write the files. * @param project the project instance. * @throws IOException if the copying fails. * @since Ant 1.7 */ public static void copyResource( Resource source, Resource dest, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, String inputEncoding, String outputEncoding, Project project) throws IOException { if (!overwrite) { long slm = source.getLastModified(); if (dest.isExists() && slm != 0 && dest.getLastModified() > slm) { return; } } final boolean filterSetsAvailable = (filters != null && filters.hasFilters()); final boolean filterChainsAvailable = (filterChains != null && filterChains.size() > 0); if (filterSetsAvailable) { BufferedReader in = null; BufferedWriter out = null; try { InputStreamReader isr = null; if (inputEncoding == null) { isr = new InputStreamReader(source.getInputStream()); } else { isr = new InputStreamReader(source.getInputStream(), inputEncoding); } in = new BufferedReader(isr); OutputStreamWriter osw = null; if (outputEncoding == null) { osw = new OutputStreamWriter(dest.getOutputStream()); } else { osw = new OutputStreamWriter(dest.getOutputStream(), outputEncoding); } out = new BufferedWriter(osw); if (filterChainsAvailable) { ChainReaderHelper crh = new ChainReaderHelper(); crh.setBufferSize(FileUtils.BUF_SIZE); crh.setPrimaryReader(in); crh.setFilterChains(filterChains); crh.setProject(project); Reader rdr = crh.getAssembledReader(); in = new BufferedReader(rdr); } LineTokenizer lineTokenizer = new LineTokenizer(); lineTokenizer.setIncludeDelims(true); String newline = null; String line = lineTokenizer.getToken(in); while (line != null) { if (line.length() == 0) { // this should not happen, because the lines are // returned with the end of line delimiter out.newLine(); } else { newline = filters.replaceTokens(line); out.write(newline); } line = lineTokenizer.getToken(in); } } finally { FileUtils.close(out); FileUtils.close(in); } } else if (filterChainsAvailable || (inputEncoding != null && !inputEncoding.equals(outputEncoding)) || (inputEncoding == null && outputEncoding != null)) { BufferedReader in = null; BufferedWriter out = null; try { InputStreamReader isr = null; if (inputEncoding == null) { isr = new InputStreamReader(source.getInputStream()); } else { isr = new InputStreamReader(source.getInputStream(), inputEncoding); } in = new BufferedReader(isr); OutputStreamWriter osw = null; if (outputEncoding == null) { osw = new OutputStreamWriter(dest.getOutputStream()); } else { osw = new OutputStreamWriter(dest.getOutputStream(), outputEncoding); } out = new BufferedWriter(osw); if (filterChainsAvailable) { ChainReaderHelper crh = new ChainReaderHelper(); crh.setBufferSize(FileUtils.BUF_SIZE); crh.setPrimaryReader(in); crh.setFilterChains(filterChains); crh.setProject(project); Reader rdr = crh.getAssembledReader(); in = new BufferedReader(rdr); } char[] buffer = new char[FileUtils.BUF_SIZE]; while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead == -1) { break; } out.write(buffer, 0, nRead); } } finally { FileUtils.close(out); FileUtils.close(in); } } else { InputStream in = null; OutputStream out = null; try { in = source.getInputStream(); out = dest.getOutputStream(); byte[] buffer = new byte[FileUtils.BUF_SIZE]; int count = 0; do { out.write(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); } finally { FileUtils.close(out); FileUtils.close(in); } } if (preserveLastModified && dest instanceof Touchable) { setLastModified((Touchable) dest, source.getLastModified()); } }
@Override protected boolean createTestProject() { if (!(Boolean) model.getProperty(ISeamFacetDataModelProperties.TEST_PROJECT_CREATING) || !shouldCopyLibrariesAndTemplates(model)) return false; File testProjectDir = new File( seamWebProject.getLocation().removeLastSegments(1).toFile(), testProjectName); //$NON-NLS-1$ testProjectDir.mkdir(); IVirtualComponent component = ComponentCore.createComponent(seamWebProject); IVirtualFolder webRootVirtFolder = component.getRootFolder().getFolder(new Path("/")); // $NON-NLS-1$ File testLibDir = new File(testProjectDir, "lib"); // $NON-NLS-1$ File embededEjbDir = new File(testProjectDir, "bootstrap"); // $NON-NLS-1$ File testSrcDir = new File(testProjectDir, "test-src"); // $NON-NLS-1$ String seamGenResFolder = seamRuntime.getResourceTemplatesDir(); File dataSourceFile = new File(seamGenResFolder, "datasource-ds.xml"); File seamPropertiesFile = new File(seamGenResFolder, "seam.properties"); // File jbossBeansFile = new File(seamGenResFolder ,"META-INF/jboss-beans.xml"); //$NON-NLS-1$ FilterSet filterSet = new FilterSet(); filterSet.addFilter("projectName", seamWebProject.getName()); // $NON-NLS-1$ filterSet.addFilter("earProjectName", earProjectName); // $NON-NLS-1$ filterSet.addFilter("ejbProjectName", ejbProjectName); // $NON-NLS-1$ filterSet.addFilter("testProjectName", testProjectName); // $NON-NLS-1$ filterSet.addFilter( "runtimeName", WtpUtils.getServerRuntimeName(seamWebProject)); // $NON-NLS-1$ filterSet.addFilter( "webRootFolder", webRootVirtFolder .getUnderlyingFolder() .getFullPath() .removeFirstSegments(1) .toString()); //$NON-NLS-1$ FilterSet jdbcFilterSet = SeamFacetFilterSetFactory.createJdbcFilterSet(model); // TODO: why are these filters not shared!? filterSet.addConfiguredFilterSet( SeamFacetFilterSetFactory.createHibernateDialectFilterSet(model)); AntCopyUtils.FileSet includeLibs = new AntCopyUtils.FileSet(getJBossTestLibFileset()) .dir(new File(seamRuntime.getHomeDir(), "lib")); // $NON-NLS-1$ AntCopyUtils.FileSet secondSetincludeLibs = new AntCopyUtils.FileSet(getJBossTestLibFileset()) .dir(new File(seamRuntime.getHomeDir(), "lib/test")); // $NON-NLS-1$ File[] firstlibs = includeLibs.getDir().listFiles(new AntCopyUtils.FileSetFileFilter(includeLibs)); File[] secondLibs = secondSetincludeLibs .getDir() .listFiles(new AntCopyUtils.FileSetFileFilter(secondSetincludeLibs)); Set<String> allLibs = new HashSet<String>(); // HACK: needed to be unique because some jboss-*.jars are duplicated if (firstlibs != null) for (File f : firstlibs) { allLibs.add(f.getName()); } if (secondLibs != null) for (File f : secondLibs) { allLibs.add(f.getName()); } StringBuffer testLibraries = new StringBuffer(); if (!SeamCorePlugin.getDefault().hasM2Facet(seamWebProject)) { for (String file : allLibs) { testLibraries.append( "\t<classpathentry kind=\"lib\" path=\"lib/" + file + "\"/>\n"); //$NON-NLS-1$ //$NON-NLS-2$ } } StringBuffer requiredProjects = new StringBuffer(); requiredProjects.append( "\t<classpathentry combineaccessrules=\"false\" kind=\"src\" path=\"/" + seamWebProject.getName() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ if (!SeamFacetAbstractInstallDelegate.isWarConfiguration(model)) { requiredProjects.append( "\n\t<classpathentry combineaccessrules=\"false\" kind=\"src\" path=\"/" + ejbProjectName + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ } filterSet.addFilter("testLibraries", testLibraries.toString()); // $NON-NLS-1$ filterSet.addFilter("requiredProjects", requiredProjects.toString()); // $NON-NLS-1$ File testTemplateDir = null; try { testTemplateDir = new File( SeamFacetInstallDataModelProvider.getTemplatesFolder(), "test-seam2"); // $NON-NLS-1$ } catch (IOException e) { SeamCorePlugin.getPluginLog().logError(e); return false; } AntCopyUtils.FileSet excludeCvsSvn = new AntCopyUtils.FileSet(SeamFacetAbstractInstallDelegate.CVS_SVN).dir(testTemplateDir); AntCopyUtils.copyFilesAndFolders( testTemplateDir, testProjectDir, new AntCopyUtils.FileSetFileFilter(excludeCvsSvn), new FilterSetCollection(filterSet), true); excludeCvsSvn.dir(new File(seamRuntime.getHomeDir(), "bootstrap")); // $NON-NLS-1$ AntCopyUtils.copyFilesAndFolders( new File(seamRuntime.getHomeDir(), "bootstrap"), // $NON-NLS-1$ embededEjbDir, new AntCopyUtils.FileSetFileFilter(excludeCvsSvn), new FilterSetCollection(), true); // AntCopyUtils.copyFileToFile( // persistenceFile, // new File(testProjectDir,"test-src/META-INF/persistence.xml"), //$NON-NLS-1$ // new FilterSetCollection(filterSet), true); FilterSetCollection f = new FilterSetCollection(); f.addFilterSet(filterSet); f.addFilterSet(jdbcFilterSet); AntCopyUtils.copyFileToFile( dataSourceFile, new File( testProjectDir, "test-src/META-INF/" + seamWebProject.getName() + "-test-ds.xml"), // $NON-NLS-1$ f, true); AntCopyUtils.copyFileToFolder( seamPropertiesFile, testSrcDir, //$NON-NLS-1$ new FilterSetCollection(filterSet), true); // Add "org.jboss.seam.core.init.debug=false" for Seam 2.1/2.2/2.3 // to seam.properties file to avoid https://jira.jboss.org/jira/browse/JBIDE-3623 if (getVersion() == SeamVersion.SEAM_2_1 || getVersion() == SeamVersion.SEAM_2_2 || getVersion() == SeamVersion.SEAM_2_3) { Properties seamProperties = new Properties(); File testSeamPropertiesFile = new File(testSrcDir, "seam.properties"); FileInputStream inStream = null; FileOutputStream out = null; try { inStream = new FileInputStream(testSeamPropertiesFile); seamProperties.load(inStream); seamProperties.setProperty( "org.jboss.seam.core.init.debug", "false"); // $NON-NLS-1$ //$NON-NLS-2$ out = new FileOutputStream(testSeamPropertiesFile); seamProperties.store(out, "debug is explicitly disabled in test to avoid JBIDE-3623"); } catch (FileNotFoundException e) { SeamCorePlugin.getPluginLog().logError(e); } catch (IOException e) { SeamCorePlugin.getPluginLog().logError(e); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { /** ignore */ } } if (out != null) { try { out.close(); } catch (IOException e) { /** ignore */ } } } } if (!SeamCorePlugin.getDefault().hasM2Facet(seamWebProject) && shouldCopyLibraries(model)) { AntCopyUtils.copyFiles( new File(seamRuntime.getHomeDir(), "lib"), // $NON-NLS-1$ testLibDir, new AntCopyUtils.FileSetFileFilter(includeLibs)); // seam2 has a lib/test AntCopyUtils.copyFiles( new File(seamRuntime.getHomeDir(), "lib/test"), // $NON-NLS-1$ testLibDir, new AntCopyUtils.FileSetFileFilter(includeLibs)); } SeamFacetAbstractInstallDelegate.createComponentsProperties( testSrcDir, "", true); // $NON-NLS-1$ return true; }