KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > dao > file > berkeleydb > BerkeleyDBFileProcessor


1 /*
2  * $Id: BerkeleyDBFileProcessor.java,v 1.3 2005/06/07 12:32:34 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 08.08.2004
27  *
28  */

29 package org.jresearch.gossip.dao.file.berkeleydb;
30
31 import java.io.UnsupportedEncodingException JavaDoc;
32
33 import org.jresearch.gossip.dao.file.IFileProcessor;
34 import org.jresearch.gossip.exception.SystemException;
35
36 import com.sleepycat.je.Database;
37 import com.sleepycat.je.DatabaseEntry;
38 import com.sleepycat.je.DatabaseException;
39 import com.sleepycat.je.LockMode;
40 import com.sleepycat.je.OperationStatus;
41
42 /**
43  * @author Dmitry Belov
44  *
45  */

46 public class BerkeleyDBFileProcessor implements IFileProcessor {
47
48     private String JavaDoc dbname;
49
50     private FileDbEnv env;
51
52     /**
53      * @throws SystemException
54      *
55      */

56     public BerkeleyDBFileProcessor() throws SystemException {
57         env = FileDbEnv.getInstance();
58     }
59
60     private Database getFileDB() throws SystemException {
61         return env.getFileDb(dbname);
62     }
63
64     /**
65      * @return Returns the dbname.
66      */

67     public String JavaDoc getDbname() {
68         return dbname;
69     }
70
71     /*
72      * (non-Javadoc)
73      *
74      * @see org.jresearch.gossip.dao.file.IFileProcessor#setDbname(java.lang.String)
75      */

76     public void setStoreName(String JavaDoc dbname) {
77         this.dbname = dbname;
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.jresearch.gossip.dao.file.IFileProcessor#saveFileData(byte[],
84      * java.lang.String)
85      */

86     public void saveFileData(byte[] data, String JavaDoc key) throws SystemException {
87         Database db = null;
88         try {
89             db = getFileDB();
90             DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
91             DatabaseEntry theData = new DatabaseEntry(data);
92             db.put(null, theKey, theData);
93         } catch (UnsupportedEncodingException JavaDoc e) {
94             throw new SystemException(e);
95         } catch (DatabaseException e) {
96             throw new SystemException(e);
97         } finally {
98             if (db != null) {
99                 try {
100                     db.close();
101                 } catch (DatabaseException e) {
102                     throw new SystemException(e);
103                 }
104             }
105         }
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.jresearch.gossip.dao.file.IFileProcessor#getFileData(java.lang.String)
112      */

113     public byte[] getFileData(String JavaDoc key) throws SystemException {
114         Database db = null;
115         try {
116             db = getFileDB();
117             DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
118             DatabaseEntry theData = new DatabaseEntry();
119             if (db.get(null, theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
120                 return theData.getData();
121             }
122             return null;
123         } catch (UnsupportedEncodingException JavaDoc e) {
124             throw new SystemException(e);
125         } catch (DatabaseException e) {
126             throw new SystemException(e);
127         } finally {
128             if (db != null) {
129                 try {
130                     db.close();
131                 } catch (DatabaseException e) {
132                     throw new SystemException(e);
133                 }
134             }
135         }
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * @see org.jresearch.gossip.dao.file.IFileProcessor#removeFileData(java.lang.String)
142      */

143     public void removeFileData(String JavaDoc key) throws SystemException {
144         Database db = null;
145         try {
146             db = getFileDB();
147             DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
148             db.delete(null, theKey);
149         } catch (UnsupportedEncodingException JavaDoc e) {
150             throw new SystemException(e);
151         } catch (DatabaseException e) {
152             throw new SystemException(e);
153         } finally {
154             if (db != null) {
155                 try {
156                     db.close();
157                 } catch (DatabaseException e) {
158                     throw new SystemException(e);
159                 }
160             }
161         }
162
163     }
164
165 }
Popular Tags