KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > store > impl > JispFilesystemStore


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.store.impl;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import com.coyotegulch.jisp.BTreeIndex;
23 import com.coyotegulch.jisp.IndexedObjectDatabase;
24 import com.coyotegulch.jisp.KeyNotFound;
25
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.parameters.ParameterException;
28 import org.apache.avalon.framework.parameters.Parameterizable;
29 import org.apache.avalon.framework.parameters.Parameters;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.excalibur.store.Store;
32
33 /**
34  * This store is based on the Jisp library
35  * (http://www.coyotegulch.com/jisp/index.html). This store uses B-Tree indexes
36  * to access variable-length serialized data stored in files.
37  *
38  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
39  * @version CVS $Id: JispFilesystemStore.java,v 1.4 2004/02/28 11:47:31 cziegeler Exp $
40  */

41 public class JispFilesystemStore extends AbstractJispFilesystemStore
42     implements Store,
43                ThreadSafe,
44                Parameterizable,
45                Disposable {
46
47     /**
48      * Configure the Component.<br>
49      * A few options can be used
50      * <UL>
51      * <LI> directory - The directory to store the two files belowe
52      * </LI>
53      * <LI> data-file = the name of the data file (Default: store.dat)
54      * </LI>
55      * <LI> index-file = the name of the index file (Default: store.idx)
56      * </LI>
57      * <LI> order = The page size of the B-Tree</LI>
58      * </UL>
59      *
60      * @param params the configuration paramters
61      * @exception ParameterException
62      */

63      public void parameterize(Parameters params) throws ParameterException
64      {
65         // get the directory to use
66
try
67         {
68             final String JavaDoc dir = params.getParameter("directory");
69             this.setDirectory(new File JavaDoc(dir));
70         }
71         catch (IOException JavaDoc e)
72         {
73             throw new ParameterException("Unable to set directory", e);
74         }
75
76         final String JavaDoc databaseName = params.getParameter("data-file", "store.dat");
77         final String JavaDoc indexName = params.getParameter("index-file", "store.idx");
78         final int order = params.getParameterAsInteger("order", 301);
79         if (getLogger().isDebugEnabled())
80         {
81             getLogger().debug("Database file name = " + databaseName);
82             getLogger().debug("Index file name = " + indexName);
83             getLogger().debug("Order=" + order);
84         }
85
86         final File JavaDoc databaseFile = new File JavaDoc(m_directoryFile, databaseName);
87         final File JavaDoc indexFile = new File JavaDoc(m_directoryFile, indexName);
88
89         if (getLogger().isDebugEnabled())
90         {
91             getLogger().debug("Initializing JispFilesystemStore");
92         }
93
94         try
95         {
96             final boolean isOld = databaseFile.exists();
97             if (getLogger().isDebugEnabled())
98             {
99                 getLogger().debug("initialize(): Datafile exists: " + isOld);
100             }
101
102             if (!isOld) {
103                 m_Index = new BTreeIndex(indexFile.toString(),
104                                          order, super.getNullKey(), false);
105             } else {
106                 m_Index = new BTreeIndex(indexFile.toString());
107             }
108             m_Database = new IndexedObjectDatabase(databaseFile.toString(), !isOld);
109             m_Database.attachIndex(m_Index);
110         }
111         catch (KeyNotFound ignore)
112         {
113         }
114         catch (Exception JavaDoc e)
115         {
116             getLogger().error("initialize(..) Exception", e);
117         }
118     }
119
120     public void dispose()
121     {
122         try
123         {
124             getLogger().debug("Disposing");
125
126             if (m_Index != null)
127             {
128                 m_Index.close();
129             }
130
131             if (m_Database != null)
132             {
133                 m_Database.close();
134             }
135         }
136         catch (Exception JavaDoc e)
137         {
138             getLogger().error("dispose(..) Exception", e);
139         }
140     }
141 }
142
Popular Tags