FODBMainFile.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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     collectionTable.add(collectionName);
00085     rewriteCollectionTable(collectionTable);
00086     return collectionName;
00087   }
00088 
00089   private void rewriteCollectionTable(Array obj) throws IOException {                   
00090                 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(mainFilePath)));
00091                 try     {
00092                         out.writeObject(obj);
00093                 } finally       {
00094                         out.flush();
00095                         out.close();
00096                 }
00097   }
00098 
00099   private Array readCollectionTable() throws IOException, ClassNotFoundException {
00100                 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(mainFilePath)));
00101                 try     {
00102                         return (Array)in.readObject();
00103                 } finally       {
00104                         in.close();
00105                 }
00106   }
00107 
00108   public String[] getCollectionsList() {
00109                 String[] collectionsNameDuplic = new String[collectionTable.size()];
00110                 collectionsNameDuplic = (String[])collectionTable.toArray(collectionsNameDuplic);
00111                 return collectionsNameDuplic;
00112   }
00113 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1