FastObjectDBManager.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  */
00025 package org.openmobileis.database.fastobjectdb;
00026 
00027 import java.util.Hashtable;
00028 
00029 import org.openmobileis.common.context.ApplicationContextManager;
00030 import org.openmobileis.common.util.PropertiesManager;
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 import org.openmobileis.common.util.log.LogManager;
00033 import org.openmobileis.database.fastobjectdb.db.FODBCollection;
00034 import org.openmobileis.database.fastobjectdb.db.crypto.FODBCypher;
00035 
00045 public class FastObjectDBManager implements FastObjectDBManagerService {
00046         protected static Hashtable cypherList;
00047 
00048         protected FastObjectDB db;
00049         protected boolean fodbStarted = false;
00050         protected static FastObjectDBManager manager;
00051 
00052         protected FastObjectDBManager() {
00053 
00054         }
00055 
00056         public final static void registerManager(FastObjectDBManager newManager) {
00057                 if (manager == null) {
00058                         manager = newManager;
00059                         ApplicationContextManager.getManager().addManager(manager);
00060                 } else {
00061                         LogManager.traceInfo(0, "SessionContextManager registerManager already done no use");
00062                 }
00063         }
00064 
00065         public final static FastObjectDBManager getManager() {
00066                 if (manager == null) {
00067                         manager = new FastObjectDBManager();
00068                         ApplicationContextManager.getManager().addManager(manager);
00069                 }
00070                 return manager;
00071         }
00072 
00079         public synchronized FastObjectDB getCurrentFODB() throws DatabaseException {
00080                 if (db == null) {
00081                         String dbPath = PropertiesManager.getManager().getProperty("fastobjectdb.database.path");
00082                         String dbname = PropertiesManager.getManager().getProperty("fastobjectdb.database.name");
00083                         if ((dbPath == null) || (dbname == null)) {
00084                                 throw new DatabaseException("FODB ERROR : FODB path or name property is not set. Can't create db");
00085                         }
00086                         synchronized (FastObjectDBManager.class) {
00087                                 if (db == null) {
00088                                         try {
00089                                                 db = FastObjectDB.open(dbPath, dbname);
00090                                                 ApplicationContextManager.getManager().addManager(db);
00091                                                 fodbStarted = true;
00092                                         } catch (Throwable ex) {
00093                                                 throw new DatabaseException(ex);
00094                                         }
00095                                 }
00096                         }
00097                 }
00098                 return db;
00099         }
00100 
00101         public synchronized void CloseCurrentFODB() throws DatabaseException {
00102                 db.getTransactionManager().begin();
00103                 FODBCollection[] colList = db.getDatabaseCollectionArray();
00104                 for (int i = 0; i < colList.length; i++) {
00105                         db.getTransactionManager().enterTransaction(colList[i].getName());
00106                 }
00107                 db = null;
00108                 fodbStarted = false;
00109         }
00110 
00111         public synchronized boolean isFodbStarted() {
00112                 return fodbStarted;
00113         }
00114 
00118         public synchronized void flushDB() throws DatabaseException {
00119                 if (db != null) {
00120                         db.getTransactionManager().begin();
00121                         FODBCollection[] colList = db.getDatabaseCollectionArray();
00122                         cypherList = new Hashtable();
00123                         for (int i = 0; i < colList.length; i++) {
00124                                 db.getTransactionManager().enterTransaction(colList[i].getName());
00125                                 FODBCypher cypher = colList[i].getCollectionCypher();
00126                                 if (cypher != null) {
00127                                         cypherList.put(colList[i].getName(), cypher);
00128                                 }
00129                         }
00130                         fodbStarted = false;
00131                         db = null;
00132                 }
00133                 FastObjectDBManager.getManager().getCurrentFODB();
00134         }
00135 
00139         public synchronized void resetDB() throws DatabaseException {
00140                 if (db != null) {
00141                         db.getTransactionManager().begin();
00142                         FODBCollection[] colList = db.getDatabaseCollectionArray();
00143                         for (int i = 0; i < colList.length; i++) {
00144                                 db.getTransactionManager().enterTransaction(colList[i].getName());
00145                                 db.removeCollecionFile(colList[i].getName());
00146                         }
00147                         db.getTransactionManager().commit();
00148                 }
00149 
00150         }
00151 
00152 }

Generated on Mon Jan 11 21:19:14 2010 for OpenMobileIS by  doxygen 1.5.4