KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > txfile > FileSequenceStore


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/txfile/FileSequenceStore.java,v 1.4 2004/07/28 09:33:58 ib Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/07/28 09:33:58 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.txfile;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.transaction.xa.XAException JavaDoc;
31 import javax.transaction.xa.XAResource JavaDoc;
32 import javax.transaction.xa.Xid JavaDoc;
33
34 import org.apache.commons.transaction.file.FileSequence;
35 import org.apache.commons.transaction.file.ResourceManagerException;
36 import org.apache.slide.common.ServiceAccessException;
37 import org.apache.slide.common.ServiceConnectionFailedException;
38 import org.apache.slide.common.ServiceDisconnectionFailedException;
39 import org.apache.slide.common.ServiceParameterErrorException;
40 import org.apache.slide.common.ServiceParameterMissingException;
41 import org.apache.slide.common.ServiceResetFailedException;
42 import org.apache.slide.common.AbstractServiceBase;
43 import org.apache.slide.store.SequenceStore;
44
45 import org.apache.slide.util.logger.Logger;
46 import org.apache.slide.util.logger.TxLogger;
47
48 /**
49  * Sequence store using the file system.
50  *
51  */

52 public class FileSequenceStore extends AbstractServiceBase implements SequenceStore {
53
54     protected static final String JavaDoc LOG_CHANNEL = FileSequenceStore.class.getName();
55
56     protected static final String JavaDoc STORE_DIR_PARAMETER = "rootpath";
57
58     protected FileSequence fileSequence;
59     protected String JavaDoc storeDir = null;
60     protected Map JavaDoc sequenceHash = new HashMap JavaDoc();
61     protected int sequenceIncrement = 100;
62     protected long sequenceStartValue = 1;
63
64     public void setParameters(Hashtable JavaDoc parameters)
65         throws ServiceParameterErrorException, ServiceParameterMissingException {
66
67         storeDir = (String JavaDoc) parameters.get(STORE_DIR_PARAMETER);
68         if (storeDir == null) {
69             throw new ServiceParameterMissingException(this, STORE_DIR_PARAMETER);
70         }
71
72         try {
73             fileSequence = new FileSequence(storeDir, new TxLogger(getLogger(), LOG_CHANNEL));
74             getLogger().log("File Sequence Store configured to " + storeDir, LOG_CHANNEL, Logger.INFO);
75         } catch (ResourceManagerException e) {
76             getLogger().log("Can not initialize File Sequence Store", e, LOG_CHANNEL, Logger.CRITICAL);
77             throw new ServiceParameterErrorException(this, STORE_DIR_PARAMETER);
78         }
79     }
80
81     public String JavaDoc toString() {
82         return "FileSequenceStore at " + storeDir;
83     }
84
85     /**
86      * @see org.apache.slide.common.AbstractServiceBase#connect()
87      */

88     public void connect() throws ServiceConnectionFailedException {
89     }
90
91     /**
92      * @see org.apache.slide.common.AbstractServiceBase#disconnect()
93      */

94     public void disconnect() throws ServiceDisconnectionFailedException {
95     }
96
97     /**
98      * @see org.apache.slide.common.AbstractServiceBase#reset()
99      */

100     public void reset() throws ServiceResetFailedException {
101     }
102
103     /**
104      * @see org.apache.slide.common.AbstractServiceBase#isConnected()
105      */

106     public boolean isConnected() throws ServiceAccessException {
107         return true;
108     }
109
110     /**
111      * @see org.apache.slide.store.SequenceStore#isSequenceSupported()
112      */

113     public boolean isSequenceSupported() {
114         return true;
115     }
116
117     /**
118      * @see org.apache.slide.store.SequenceStore#sequenceExists(java.lang.String)
119      */

120     public boolean sequenceExists(String JavaDoc sequenceName) throws ServiceAccessException {
121         SeqContext seq = (SeqContext) sequenceHash.get(sequenceName);
122         if (seq != null) {
123             return true;
124         } else {
125             return fileSequence.exists(sequenceName);
126         }
127     }
128
129     /**
130      * @see org.apache.slide.store.SequenceStore#createSequence(java.lang.String)
131      */

132     public synchronized boolean createSequence(String JavaDoc sequenceName) throws ServiceAccessException {
133         try {
134             if (fileSequence.create(sequenceName, sequenceStartValue)) {
135                 sequenceHash.put(sequenceName, new SeqContext(sequenceName, sequenceStartValue, sequenceStartValue));
136                 return true;
137             } else {
138                 return false;
139             }
140         } catch (ResourceManagerException e) {
141             throw new ServiceAccessException(this, e);
142         }
143     }
144
145     /**
146      * @see org.apache.slide.store.SequenceStore#nextSequenceValue(java.lang.String)
147      */

148     public synchronized long nextSequenceValue(String JavaDoc sequenceName) throws ServiceAccessException {
149         // get chunks of values to save disk access
150
SeqContext seq = (SeqContext) sequenceHash.get(sequenceName);
151         if (seq != null && seq.pos < seq.end - 1) {
152             seq.pos++;
153             return seq.pos;
154         } else {
155             try {
156                 if (seq == null) {
157                     seq = new SeqContext(sequenceName);
158                     sequenceHash.put(sequenceName, seq);
159                 }
160                 long pos = fileSequence.nextSequenceValueBottom(sequenceName, sequenceIncrement);
161                 seq.pos = pos;
162                 seq.end = seq.pos + sequenceIncrement;
163                 return seq.pos;
164             } catch (ResourceManagerException e) {
165                 throw new ServiceAccessException(this, e);
166             }
167         }
168     }
169
170     /**
171      * @see javax.transaction.xa.XAResource#commit(javax.transaction.xa.Xid, boolean)
172      */

173     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
174         // XXX we are not transactional, this is our trick
175
}
176
177     /**
178      * @see javax.transaction.xa.XAResource#end(javax.transaction.xa.Xid, int)
179      */

180     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
181         // XXX we are not transactional, this is our trick
182
}
183
184     /**
185      * @see javax.transaction.xa.XAResource#forget(javax.transaction.xa.Xid)
186      */

187     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
188         // XXX we are not transactional, this is our trick
189
}
190
191     /**
192      * @see javax.transaction.xa.XAResource#getTransactionTimeout()
193      */

194     public int getTransactionTimeout() throws XAException JavaDoc {
195         // XXX we are not transactional, this is our trick
196
return 0;
197     }
198
199     /**
200      * @see javax.transaction.xa.XAResource#isSameRM(javax.transaction.xa.XAResource)
201      */

202     public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
203         return (xaResource == this);
204     }
205
206     /**
207      * @see javax.transaction.xa.XAResource#prepare(javax.transaction.xa.Xid)
208      */

209     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
210         // tell TM we do not need to commit
211
return XA_RDONLY;
212     }
213
214     /**
215      * @see javax.transaction.xa.XAResource#recover(int)
216      */

217     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
218         // XXX we are not transactional, this is our trick
219
return null;
220     }
221
222     /**
223      * @see javax.transaction.xa.XAResource#rollback(javax.transaction.xa.Xid)
224      */

225     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
226         // XXX we are not transactional, this is our trick
227
}
228
229     /**
230      * @see javax.transaction.xa.XAResource#setTransactionTimeout(int)
231      */

232     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
233         // XXX we are not transactional, this is our trick
234
return true;
235     }
236
237     /**
238      * @see javax.transaction.xa.XAResource#start(javax.transaction.xa.Xid, int)
239      */

240     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
241         // XXX we are not transactional, this is our trick
242

243     }
244
245     protected static class SeqContext {
246         public String JavaDoc name;
247         public long pos;
248         public long end;
249
250         public SeqContext(String JavaDoc name) {
251             this(name, -1, -1);
252         }
253         public SeqContext(String JavaDoc name, long pos, long end) {
254             this.name = name;
255             this.pos = pos;
256             this.end = end;
257         }
258     }
259 }
260
Popular Tags