Moved FailSafeTransactions to the fileaccess bundle.

cusax-fix
Benoit Pradelle 18 years ago
parent cf383ac102
commit acd777f360

@ -12,6 +12,7 @@
import org.osgi.framework.*;
import org.w3c.dom.*;
import net.java.sip.communicator.service.configuration.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.contactlist.event.*;
@ -290,7 +291,8 @@ void start(BundleContext bc,
// create the failsafe transaction and restore the file if needed
try {
contactlistTrans = new FailSafeTransaction(this.contactlistFile);
contactlistTrans = faService.createFailSafeTransaction(
this.contactlistFile);
contactlistTrans.restoreFile();
} catch (NullPointerException e) {
logger.error("the contactlist file is null", e);

@ -5,18 +5,20 @@
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.util;
package net.java.sip.communicator.impl.fileaccess;
import java.io.*;
import net.java.sip.communicator.service.fileaccess.*;
/**
* A failsafe transaction utility class. By failsafe we mean here that the file
* A failsafe transaction class. By failsafe we mean here that the file
* concerned always stays in a coherent state. This class use the transactional
* model.
*
* @author Benoit Pradelle
*/
public class FailSafeTransaction
public class FailSafeTransactionImpl
implements FailSafeTransaction
{
/**
@ -46,7 +48,7 @@ public class FailSafeTransaction
*
* @throws NullPointerException if the file is null
*/
public FailSafeTransaction(File file)
protected FailSafeTransactionImpl(File file)
throws NullPointerException
{
if (file == null) {

@ -330,5 +330,21 @@ else if (!homedirFile.canWrite())
return file;
}
/**
* Creates a failsafe transaction which can be used to safely store
* informations into a file.
*
* @param file The file concerned by the transaction, null if file is null.
*
* @return A new failsafe transaction related to the given file.
*/
public FailSafeTransaction createFailSafeTransaction(File file) {
if (file == null) {
return null;
}
return new FailSafeTransactionImpl(file);
}
}

@ -106,4 +106,14 @@ public interface FileAccessService {
* directory.
*/
File getPrivatePersistentDirectory(String[] dirNames) throws Exception;
/**
* Creates a failsafe transaction which can be used to safely store
* informations into a file.
*
* @param file The file concerned by the transaction, null if file is null.
*
* @return A new failsafe transaction related to the given file.
*/
FailSafeTransaction createFailSafeTransaction(File file);
}

@ -39,6 +39,7 @@ public void start(BundleContext bundleContext)
properties.put("service.pid", getName());
addTestSuite(TestFileAccessService.class);
addTestSuite(TestFailSafeTransaction.class);
bundleContext.registerService(getClass().getName(), this, properties);
logger.debug("Successfully registered " + getClass().getName());

@ -5,10 +5,14 @@
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.slick.slickless.util;
package net.java.sip.communicator.slick.fileaccess;
import java.io.*;
import net.java.sip.communicator.util.*;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import net.java.sip.communicator.service.fileaccess.*;
import junit.framework.*;
/**
@ -19,6 +23,11 @@
public class TestFailSafeTransaction
extends TestCase
{
/**
* The Service that we will be testing.
*/
private FileAccessService fileAccessService = null;
/**
* Test data to write in the original file
*/
@ -41,6 +50,19 @@ public class TestFailSafeTransaction
*/
private static String tempName = "wzsxedcrfv" + System.currentTimeMillis();
/**
* Standart constructor.
*
* @param name
*/
public TestFailSafeTransaction(String name)
{
super(name);
BundleContext context = FileAccessServiceLick.bc;
ServiceReference ref = context
.getServiceReference(FileAccessService.class.getName());
this.fileAccessService = (FileAccessService) context.getService(ref);
}
/**
* Tests the commit operation
*/
@ -53,7 +75,8 @@ public void testCommit() {
out.write(origData.getBytes());
// write a modification during a transaction
FailSafeTransaction trans = new FailSafeTransaction(temp);
FailSafeTransaction trans = this.fileAccessService
.createFailSafeTransaction(temp);
trans.beginTransaction();
out.write(addedData.getBytes());
@ -97,7 +120,8 @@ public void testRollback() {
out.write(origData.getBytes());
// write a modification during a transaction
FailSafeTransaction trans = new FailSafeTransaction(temp);
FailSafeTransaction trans = this.fileAccessService
.createFailSafeTransaction(temp);
trans.beginTransaction();
out.write(wrongData.getBytes());
@ -141,7 +165,8 @@ public void testCommitOnReOpen() {
out.write(origData.getBytes());
// write a modification during a transaction
FailSafeTransaction trans = new FailSafeTransaction(temp);
FailSafeTransaction trans = this.fileAccessService
.createFailSafeTransaction(temp);
trans.beginTransaction();
out.write(addedData.getBytes());
@ -190,8 +215,10 @@ public void testRollbackOnFailure() {
out.write(origData.getBytes());
// write a modification during a transaction
FailSafeTransaction trans = new FailSafeTransaction(temp);
FailSafeTransaction trans2 = new FailSafeTransaction(temp);
FailSafeTransaction trans = this.fileAccessService
.createFailSafeTransaction(temp);
FailSafeTransaction trans2 = this.fileAccessService
.createFailSafeTransaction(temp);
trans.beginTransaction();
out.write(wrongData.getBytes());

@ -43,7 +43,6 @@ public void start(BundleContext bundleContext) throws Exception
addTestSuite(TestXMLUtils.class);
addTestSuite(TestBase64.class);
addTestSuite(TestFailSafeTransaction.class);
bundleContext.registerService(getClass().getName(), this, properties);
logger.debug("Successfully registered " + getClass().getName());

Loading…
Cancel
Save