1 16 package org.apache.cocoon.ojb.odmg.components; 17 18 import org.apache.avalon.framework.activity.Disposable; 19 import org.apache.avalon.framework.activity.Initializable; 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.thread.ThreadSafe; 22 23 import org.apache.ojb.odmg.OJB; 24 import org.odmg.Database; 25 import org.odmg.Implementation; 26 import org.odmg.ODMGException; 27 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.HashMap ; 31 32 39 public class ODMGImpl extends AbstractLogEnabled 40 implements ODMG, ThreadSafe, Initializable, Disposable { 41 42 private static final String DEFAULT_CONNECTION ="default"; 43 private static final int DEFAULT_MODE = Database.OPEN_READ_WRITE; 44 45 private Implementation odmg; 46 private HashMap databases = new HashMap (); 47 48 51 public void initialize() throws Exception { 52 this.odmg = OJB.getInstance(); 54 } 55 56 59 public void dispose() { 60 final Set keys = this.databases.keySet(); 61 for (Iterator i = keys.iterator(); i.hasNext();) { 62 final Database db = (Database) i.next(); 63 try { 64 db.close(); 65 } catch (ODMGException e) { 66 getLogger().error("OJB-ODMG: Cannot close Database", e); 67 } 68 i.remove(); 69 } 70 } 71 72 75 public Implementation getInstance() throws ODMGException { 76 return getInstance(DEFAULT_CONNECTION, DEFAULT_MODE); 77 } 78 79 82 public Implementation getInstance(String connection) throws ODMGException { 83 return getInstance(connection, DEFAULT_MODE); 84 } 85 86 89 public Implementation getInstance(int mode) throws ODMGException { 90 return getInstance(DEFAULT_CONNECTION, mode); 91 } 92 93 96 public Implementation getInstance(String connection, int mode) throws ODMGException { 97 synchronized (this.databases) { 98 Database db = (Database) this.databases.get(connection + ":" + mode); 99 if (null == db) { 100 db = this.odmg.newDatabase(); 101 db.open(connection, mode); 102 this.databases.put(connection + ":" + mode, db); 103 } 104 } 105 return this.odmg; 106 } 107 } 108 | Popular Tags |