[jboss-svn-commits] JBL Code SVN: r18995 - in labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta: src/org/jboss/soa/esb/util and 3 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Mar 14 16:10:48 EDT 2008
Author: tfennelly
Date: 2008-03-14 16:10:48 -0400 (Fri, 14 Mar 2008)
New Revision: 18995
Added:
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/util/FileUtilUnitTest.java
Modified:
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/util/FileUtil.java
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListenerUnitTest.java
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/testutils/FileUtil.java
Log:
http://jira.jboss.com/jira/browse/JBESB-1590
Modified: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java 2008-03-14 19:28:49 UTC (rev 18994)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java 2008-03-14 20:10:48 UTC (rev 18995)
@@ -250,18 +250,18 @@
}
}
- private File setFileWorking(File file) {
+ protected File setFileWorking(File file) {
File workingFile = getWorkFileName(file, _workingSuffix);
try {
- if (!renameFile(file, workingFile)) {
- return null;
+ if (renameFile(file, workingFile)) {
+ return workingFile;
}
} catch (GatewayException e) {
- _logger.error("Problems renaming file " + file + " to " + workingFile);
+ _logger.error("Unable to rename file '" + file.getAbsolutePath() + "' to it's working name '" + workingFile + "'. May be a contention issue with another listener. You should avoid having multiple listeners polling on the same file subset. Ignoring this file for now!");
}
- return workingFile;
+ return null;
}
protected File getWorkFileName(File fileIn, String suffix) {
Modified: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/util/FileUtil.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/util/FileUtil.java 2008-03-14 19:28:49 UTC (rev 18994)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/src/org/jboss/soa/esb/util/FileUtil.java 2008-03-14 20:10:48 UTC (rev 18995)
@@ -22,16 +22,20 @@
package org.jboss.soa.esb.util;
+import org.apache.log4j.Logger;
+import org.jboss.internal.soa.esb.assertion.AssertArgument;
+
import java.io.*;
+import java.util.UUID;
-import org.apache.log4j.Logger;
-
/**
* Common file utility functions.
* @author kevin
*/
public class FileUtil
{
+ public static final String classInstanceUUID = UUID.randomUUID().toString();
+
/**
* The logger for this class.
*/
@@ -45,37 +49,110 @@
*/
public static boolean renameTo(final File from, final File to)
{
+ AssertArgument.isNotNull(from, "from");
+ AssertArgument.isNotNull(to, "to");
+
+ if(!from.exists())
+ {
+ LOGGER.debug("Unable to rename file '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'. '" + from.getAbsolutePath() + "' doesn't exist.");
+ return false;
+ }
+
+ if(to.exists()) {
+ LOGGER.debug("Unable to rename file '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'. '" + to.getAbsolutePath() + "' already exist.");
+ return false;
+ }
+
if (!from.renameTo(to))
{
- final File tmpFile ;
- try
+ // The rename may have failed because it's really a file move that's being requested i.e.
+ // move the file to a different partition and the local VM doesn't supporting this
+ // in the rename. So we try moving the file...
+ return moveFile(from, to);
+ }
+
+ return true;
+ }
+
+ /**
+ * Moves a "from" file to a "to" file through copying of the file contents.
+ * <p/>
+ * This is not a file rename.
+ *
+ * @param from The source file.
+ * @param to The target file.
+ * @return True if the move was successful, otherwise false.
+ */
+ public static boolean moveFile(File from, File to)
+ {
+ AssertArgument.isNotNull(from, "from");
+ AssertArgument.isNotNull(to, "to");
+
+ if(!from.exists())
+ {
+ LOGGER.debug("Unable to move file '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'. '" + from.getAbsolutePath() + "' doesn't exist.");
+ return false;
+ }
+
+ if(to.exists()) {
+ LOGGER.debug("Unable to move file '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'. '" + to.getAbsolutePath() + "' already exist.");
+ return false;
+ }
+
+ // Before doing the copy, we perform a local rename of the from file. This is to make
+ // sure that the file is not picked up by another processor while the copy is taking place.
+ // We prefix and postfix the UUID on the rename file, because we don't know the filter pattern being
+ // used by the processors on picking up these files. This still doesn't guarantee that the rename file
+ // is not picked up too, but it is "stronger"!
+
+ File fromFileDir = from.getParentFile();
+ File fromLocalRename = new File(fromFileDir, classInstanceUUID + "." + from.getName() + "." + classInstanceUUID);
+
+ if(!from.renameTo(fromLocalRename))
+ {
+ LOGGER.debug("Unable to perform local rename of file '" + from.getAbsolutePath() + "' to '" + fromLocalRename.getAbsolutePath() + "'. Unable to move file.");
+ return true;
+ }
+
+ if(!fromLocalRename.exists())
+ {
+ LOGGER.debug("Failed to perform local rename of file '" + from.getAbsolutePath() + "' to '" + fromLocalRename.getAbsolutePath() + "'. Unable to move file.");
+ return true;
+ }
+
+ final File tmpFile ;
+ try
+ {
+ tmpFile = File.createTempFile("copy", ".tmp", to.getParentFile()) ;
+ }
+ catch (final IOException ioe)
+ {
+ LOGGER.debug("Could not create temporary file for writing", ioe) ;
+ return true;
+ }
+
+ try
+ {
+ copyFile(fromLocalRename, tmpFile) ;
+ if (!tmpFile.renameTo(to))
{
- tmpFile = File.createTempFile("copy", ".tmp", to.getParentFile()) ;
- }
- catch (final IOException ioe)
- {
- LOGGER.debug("Could not create temporary file for writing", ioe) ;
+ LOGGER.debug("Could not rename temporary file " + tmpFile.getAbsolutePath()) ;
return false ;
}
-
- try
- {
- copyFile(from, tmpFile) ;
- if (!tmpFile.renameTo(to))
- {
- LOGGER.debug("Could not rename temporary file " + tmpFile.getAbsolutePath()) ;
- return false ;
- }
- from.delete();
+ if(!fromLocalRename.delete()) {
+ LOGGER.debug("Failed to delete local rename file '" + fromLocalRename.getAbsolutePath() + "'.");
+ // This is not desireable, but shouldn't be fatal because the from file no longer exists
+ // and the to file has been successfully created.
}
- finally
- {
- tmpFile.delete() ;
- }
}
+ finally
+ {
+ tmpFile.delete() ;
+ }
+
return true;
}
-
+
/**
* Attempt to copy the file.
* @param from The original file
Modified: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListenerUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListenerUnitTest.java 2008-03-14 19:28:49 UTC (rev 18994)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListenerUnitTest.java 2008-03-14 20:10:48 UTC (rev 18995)
@@ -24,10 +24,13 @@
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
import org.apache.log4j.Logger;
import org.jboss.internal.soa.esb.services.registry.MockRegistry;
import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.services.registry.RegistryException;
import org.jboss.soa.esb.common.tests.BaseTest;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.listeners.ListenerTagNames;
@@ -39,39 +42,45 @@
public class FileGatewayListenerUnitTest extends BaseTest
{
private Logger log = Logger.getLogger( FileGatewayListenerUnitTest.class );
-
- public FileGatewayListenerUnitTest ()
+
+ private File tmpDir = new File(System.getProperty("user.dir")) ;
+ private File dir1 = new File(tmpDir, FileGatewayListenerUnitTest.class.getSimpleName());
+ private File dir2 = new File(tmpDir, FileGatewayListenerUnitTest.class.getSimpleName());
+ private File file1 = new File(dir1, "file1");
+ private final File file2 = new File(dir2, "file2");
+
+ public FileGatewayListenerUnitTest ()
{
- }
+ }
public void setUp()
{
MockRegistry.install();
- }
-
- public void tearDown()
+ cleanupTemps();
+ dir1.mkdirs();
+ dir2.mkdirs();
+ }
+
+ public void tearDown()
{
+ cleanupTemps();
MockRegistry.uninstall();
}
- public void testGateway () throws Exception
+ private void cleanupTemps() {
+ file1.delete();
+ file2.delete();
+ dir1.delete();
+ dir2.delete();
+ }
+
+ public void testGateway () throws Exception
{
- ConfigTree tree = new ConfigTree("test");
- final File tmpDir = new File(System.getProperty("user.dir")) ;
- final String tmpDirForm = tmpDir.toURL().toExternalForm() ;
+ ConfigTree tree = createTestListenerConfig();
+ FileGatewayListener gateway = new FileGatewayListener(tree);
- tree.setAttribute("inputDir", tmpDirForm);
- tree.setAttribute("target-service-category", "Example");
- tree.setAttribute("target-service-name", "Test");
- tree.setAttribute("gatewayClass", "org.jboss.soa.esb.listeners.gateway.FileGatewayListener");
- tree.setAttribute("inputSuffix", "dummy");
- tree.setAttribute("workSuffix", "work");
- tree.setAttribute("postDelete", "true");
- tree.setAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG, "abcd");
+ boolean exception = false;
- FileGatewayListener gateway = new FileGatewayListener(tree);
- boolean exception = false;
-
try
{
gateway.seeIfOkToWorkOnDir(new File("foobarDir"));
@@ -199,5 +208,72 @@
anTestFile.delete();
}
}
-
+
+ public void test_rename_ok() throws GatewayException, IOException, RegistryException, ConfigurationException {
+ ConfigTree tree = createTestListenerConfig();
+
+ FileGatewayListener gateway = new FileGatewayListener(tree) {
+ protected File getWorkFileName(File fileIn, String suffix) {
+ return file2;
+ }
+ };
+
+ file1.createNewFile();
+ assertTrue(file1.exists());
+ assertTrue(!file2.exists());
+
+ File workingFile = gateway.setFileWorking(file1);
+ assertNotNull(workingFile);
+ assertTrue(!file1.exists());
+ assertTrue(file2.exists());
+ assertTrue(file2.equals(workingFile));
+ }
+
+ public void test_rename_no_from_file() throws GatewayException, IOException, RegistryException, ConfigurationException {
+ ConfigTree tree = createTestListenerConfig();
+
+ FileGatewayListener gateway = new FileGatewayListener(tree) {
+ protected File getWorkFileName(File fileIn, String suffix) {
+ return file2;
+ }
+ };
+
+ assertTrue(!file1.exists());
+ assertTrue(!file2.exists());
+
+ assertNull(gateway.setFileWorking(file1));
+ }
+
+ public void test_rename_to_file_exists() throws GatewayException, IOException, RegistryException, ConfigurationException {
+ ConfigTree tree = createTestListenerConfig();
+
+ FileGatewayListener gateway = new FileGatewayListener(tree) {
+ protected File getWorkFileName(File fileIn, String suffix) {
+ return file2;
+ }
+ };
+
+ file2.createNewFile();
+ assertTrue(!file1.exists());
+ assertTrue(file2.exists());
+
+ assertNull(gateway.setFileWorking(file1));
+ }
+
+ private ConfigTree createTestListenerConfig() throws MalformedURLException, ConfigurationException, RegistryException, GatewayException {
+ ConfigTree tree = new ConfigTree("test");
+ final File tmpDir = new File(System.getProperty("user.dir")) ;
+ final String tmpDirForm = tmpDir.toURL().toExternalForm() ;
+
+ tree.setAttribute("inputDir", tmpDirForm);
+ tree.setAttribute("target-service-category", "Example");
+ tree.setAttribute("target-service-name", "Test");
+ tree.setAttribute("gatewayClass", "org.jboss.soa.esb.listeners.gateway.FileGatewayListener");
+ tree.setAttribute("inputSuffix", "dummy");
+ tree.setAttribute("workSuffix", "work");
+ tree.setAttribute("postDelete", "true");
+ tree.setAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG, "abcd");
+
+ return tree;
+ }
}
Modified: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/testutils/FileUtil.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/testutils/FileUtil.java 2008-03-14 19:28:49 UTC (rev 18994)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/testutils/FileUtil.java 2008-03-14 20:10:48 UTC (rev 18995)
@@ -67,6 +67,7 @@
reader.close();
return sb.toString();
}
+
/**
* Write a String into a file.
* @param file - File to which we write the String
Added: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/util/FileUtilUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/util/FileUtilUnitTest.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/util/FileUtilUnitTest.java 2008-03-14 20:10:48 UTC (rev 18995)
@@ -0,0 +1,82 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.soa.esb.util;
+
+import junit.framework.TestCase;
+
+import java.io.*;
+
+/**
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class FileUtilUnitTest extends TestCase {
+
+ private File tmpDir = new File(System.getProperty("user.dir")) ;
+ private File dir1 = new File(tmpDir, FileUtilUnitTest.class.getSimpleName());
+ private File dir2 = new File(tmpDir, FileUtilUnitTest.class.getSimpleName());
+ private File file1 = new File(dir1, "file1");
+ private final File file2 = new File(dir2, "file2");
+
+ protected void setUp() throws Exception {
+ cleanupTemps();
+ dir1.mkdirs();
+ dir2.mkdirs();
+ }
+
+ protected void tearDown() throws Exception {
+ cleanupTemps();
+ }
+
+ public void test_moveTo_good() throws IOException {
+ writeToFile(file1, "Hi there!");
+
+ assertTrue(file1.exists());
+ assertTrue(!file2.exists());
+
+ assertTrue(FileUtil.moveFile(file1, file2));
+
+ assertTrue(!file1.exists());
+ assertTrue(file2.exists());
+
+ assertEquals("Hi there!", FileUtil.readTextFile(file2));
+ }
+
+ public void test_moveTo_from_doesnt_exists() throws IOException {
+ assertTrue(!FileUtil.moveFile(file1, file2));
+ }
+
+ public void test_moveTo_to_exists() throws IOException {
+ file2.createNewFile();
+ assertTrue(!FileUtil.moveFile(file1, file2));
+ }
+
+ private void cleanupTemps() {
+ file1.delete();
+ file2.delete();
+ dir1.delete();
+ dir2.delete();
+ }
+
+ public static void writeToFile(File file, String str) throws IOException {
+ Writer writer = new FileWriter(file);
+ writer.write(str);
+ writer.close();
+ }
+}
Property changes on: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/rosetta/tests/src/org/jboss/soa/esb/util/FileUtilUnitTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list