01: /*
02: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: All rights reserved.
04: [See end of file]
05: */
06:
07: package com.hp.hpl.jena.graph.impl;
08:
09: import java.io.File;
10:
11: import com.hp.hpl.jena.graph.TransactionHandler;
12: import com.hp.hpl.jena.shared.JenaException;
13:
14: /**
15: A TransactionHandler for FileGraphs. When a FileGraph begin()s a
16: transaction, its current contents are checkpointed into a sibling file
17: (whose name starts "checkPoint-"). On an abort(), the current contents
18: are discarded, and the checkpointed contents restored; on a commit(),
19: the current contents are written back to the backing file, and the
20: checkpoint file is deleted. Nested transactions are Not Allowed.
21:
22: @author kers
23: */
24: public class FileGraphTransactionHandler extends TransactionHandlerBase
25: implements TransactionHandler {
26: protected boolean inTransaction;
27: protected FileGraph fileGraph;
28: protected File checkPointFile;
29:
30: public FileGraphTransactionHandler(FileGraph fileGraph) {
31: this .fileGraph = fileGraph;
32: }
33:
34: public boolean transactionsSupported() {
35: return true;
36: }
37:
38: public void begin() {
39: if (inTransaction)
40: throw new JenaException("nested transactions not supported");
41: else {
42: checkPointFile = new File(checkPointName(fileGraph.name));
43: checkPointFile.deleteOnExit();
44: fileGraph.saveContents(checkPointFile);
45: inTransaction = true;
46: }
47: }
48:
49: protected String checkPointName(File name) {
50: String path = name.getPath();
51: int pos = path.lastIndexOf(File.separatorChar);
52: String start = path.substring(0, pos + 1);
53: String finish = path.substring(pos + 1);
54: return start + "checkPoint-" + finish;
55: }
56:
57: public void abort() {
58: fileGraph.getBulkUpdateHandler().removeAll();
59: fileGraph.readModelFrom(fileGraph.model, true, checkPointFile);
60: checkPointFile.delete();
61: inTransaction = false;
62: }
63:
64: public void commit() {
65: fileGraph.saveContents(fileGraph.name);
66: checkPointFile.delete();
67: inTransaction = false;
68: }
69:
70: }
71:
72: /*
73: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
74: * All rights reserved.
75: *
76: * Redistribution and use in source and binary forms, with or without
77: * modification, are permitted provided that the following conditions
78: * are met:
79: * 1. Redistributions of source code must retain the above copyright
80: * notice, this list of conditions and the following disclaimer.
81: * 2. Redistributions in binary form must reproduce the above copyright
82: * notice, this list of conditions and the following disclaimer in the
83: * documentation and/or other materials provided with the distribution.
84: * 3. The name of the author may not be used to endorse or promote products
85: * derived from this software without specific prior written permission.
86: *
87: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
88: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
90: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
91: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
92: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
93: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
94: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
95: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
96: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97: */
|