KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > impl > FileGraphTransactionHandler


1 /*
2     (c) Copyright 2005 Hewlett-Packard Development Company, LP
3     All rights reserved.
4     [See end of file]
5 */

6
7 package com.hp.hpl.jena.graph.impl;
8
9 import java.io.File JavaDoc;
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
25     extends TransactionHandlerBase implements TransactionHandler
26     {
27     protected boolean inTransaction;
28     protected FileGraph fileGraph;
29     protected File JavaDoc checkPointFile;
30     
31     public FileGraphTransactionHandler( FileGraph fileGraph )
32         { this.fileGraph = fileGraph; }
33     
34     public boolean transactionsSupported()
35         { return true; }
36     
37     public void begin()
38         { if (inTransaction)
39             throw new JenaException( "nested transactions not supported" );
40         else
41             { checkPointFile = new File JavaDoc( checkPointName( fileGraph.name ) );
42             checkPointFile.deleteOnExit();
43             fileGraph.saveContents( checkPointFile );
44             inTransaction = true; } }
45     
46     protected String JavaDoc checkPointName( File JavaDoc name )
47         {
48         String JavaDoc path = name.getPath();
49         int pos = path.lastIndexOf( File.separatorChar );
50         String JavaDoc start = path.substring( 0, pos + 1 );
51         String JavaDoc finish = path.substring( pos + 1 );
52         return start + "checkPoint-" + finish;
53         }
54     
55     public void abort()
56         { fileGraph.getBulkUpdateHandler().removeAll();
57         fileGraph.readModelFrom( fileGraph.model, true, checkPointFile );
58         checkPointFile.delete();
59         inTransaction = false; }
60     
61     public void commit()
62         { fileGraph.saveContents( fileGraph.name );
63         checkPointFile.delete();
64         inTransaction = false; }
65     
66     }
67
68 /*
69  * (c) Copyright 2005 Hewlett-Packard Development Company, LP
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  * notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  * notice, this list of conditions and the following disclaimer in the
79  * documentation and/or other materials provided with the distribution.
80  * 3. The name of the author may not be used to endorse or promote products
81  * derived from this software without specific prior written permission.
82  *
83  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
84  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
85  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
86  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
87  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
88  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
89  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
90  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
91  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
92  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93 */
Popular Tags