Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

FODBMainFile.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.database.fastobjectdb.db.store;
00030 
00031 import java.io.*;
00032 
00033 import org.openmobileis.common.util.collection.Array;
00034 
00045 public class FODBMainFile {
00046   private final static int ACTION_CREATE = 0;
00047   private final static int ACTION_OPEN = 1;
00048 
00049   private Array collectionTable = null;
00050   private String mainFilePath;
00051 
00052   private FODBMainFile(String filePath, int action) throws IOException, ClassNotFoundException {
00053     if (action == ACTION_CREATE) {
00054       File f1 = new File(filePath);
00055       f1.delete();
00056     }
00057 
00058     mainFilePath = filePath;
00059 
00060   // Let's open our file (or create it if it doesn't exist)
00061     if (action == ACTION_CREATE) {
00062 
00063       // create and write collectionTable
00064       collectionTable = new Array();
00065       rewriteCollectionTable(collectionTable);
00066     } else if (action == ACTION_OPEN) {
00067       collectionTable = readCollectionTable();
00068     }
00069   }
00070 
00071   public static FODBMainFile createDbFile(String filePath) throws IOException, ClassNotFoundException {
00072     return new FODBMainFile(filePath, ACTION_CREATE);
00073   }
00074 
00075   public static FODBMainFile openDbFile(String filePath) throws IOException, ClassNotFoundException {
00076     File dbFile = new File(filePath);
00077     if (!dbFile.exists()) {
00078       return null;
00079     }
00080     return new FODBMainFile(filePath, ACTION_OPEN);
00081   }
00082 
00083   public String addCollection(String collectionName) throws IOException {
00084     boolean modified;
00085     modified = collectionTable.add(collectionName);
00086     rewriteCollectionTable(collectionTable);
00087     return collectionName;
00088   }
00089 
00090   private void rewriteCollectionTable(Array obj) throws IOException {                   
00091                 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(mainFilePath)));
00092                 try     {
00093                         out.writeObject(obj);
00094                 } finally       {
00095                         out.flush();
00096                         out.close();
00097                 }
00098   }
00099 
00100   private Array readCollectionTable() throws IOException, ClassNotFoundException {
00101                 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(mainFilePath)));
00102                 try     {
00103                         return (Array)in.readObject();
00104                 } finally       {
00105                         in.close();
00106                 }
00107   }
00108 
00109   public String[] getCollectionsList() {
00110                 String[] collectionsNameDuplic = new String[collectionTable.size()];
00111                 collectionsNameDuplic = (String[])collectionTable.toArray(collectionsNameDuplic);
00112                 return collectionsNameDuplic;
00113   }
00114 }

Generated on Thu Oct 6 10:06:32 2005 for OpenMobileIS by  doxygen 1.4.3