1 /*
2 * $Source: /usr/cvsroot/melati/melati-example-odmg/src/main/java/org/melati/poem/odmg/Database.java,v $
3 * $Revision: 1.13 $
4 *
5 * Copyright (C) 2000 Chris Kimpton
6 *
7 * Part of Melati (http://melati.org), a framework for the rapid
8 * development of clean, maintainable web applications.
9 *
10 * Melati is free software; Permission is granted to copy, distribute
11 * and/or modify this software under the terms either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at your option)
15 * any later version,
16 *
17 * or
18 *
19 * b) any version of the Melati Software License, as published
20 * at http://melati.org
21 *
22 * You should have received a copy of the GNU General Public License and
23 * the Melati Software License along with this program;
24 * if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 * GNU General Public License and visit http://melati.org to obtain the
27 * Melati Software License.
28 *
29 * Feel free to contact the Developers of Melati (http://melati.org),
30 * if you would like to work out a different arrangement than the options
31 * outlined here. It is our intention to allow Melati to be used by as
32 * wide an audience as possible.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * Contact details for copyright holder:
40 *
41 * Chris Kimpton (kimtoc@techie.com)
42 *
43 */
44 package org.melati.poem.odmg;
45
46 import java.util.Map;
47 import java.util.HashMap;
48
49 import org.melati.LogicalDatabase;
50 import org.melati.util.DatabaseInitException;
51
52 /** POEM implementation of the ODMG Database API. **/
53 final class Database implements org.odmg.Database {
54
55 /** Hide this from general use. **/
56 private Database() {}
57
58 /** Provide a package factory method
59 so we could return a different type if needed. **/
60 static Database getNewDatabase() {
61 return new Database();
62 }
63
64 private org.melati.poem.Database _poemDB = null;
65 private String _logicalDB = null;
66
67 org.melati.poem.Database getPoemDatabase()
68 throws org.odmg.ODMGRuntimeException {
69 if (_poemDB == null) throw new org.odmg.DatabaseClosedException(
70 "org.melati.poem.odmg.Database::getPoemDatabase - POEM DB not set");
71 return _poemDB;
72 }
73
74 /** Opens a connection to the db
75 * @param openParameters the Poem logical db name
76 */
77 public void open(String openParameters, int openType)
78 throws org.odmg.ODMGException {
79 _logicalDB = openParameters;
80 try {
81 _poemDB = LogicalDatabase.getDatabase(_logicalDB);
82 _cachedTables = new HashMap();
83 } catch (DatabaseInitException err) {
84 err.printStackTrace();
85 throw new org.odmg.ODMGException(err.getMessage());
86 }
87 }
88
89 public void close() {
90 _poemDB.disconnect();
91 _poemDB = null;
92 _cachedTables = null;
93 ODMGFactory.resetDb();
94 }
95
96 private Map _cachedTables = null;
97
98 /**
99 * Retrieves a collection wrapper for the selected table.
100 *
101 * @param objectIdentifier the name of the table for which a collection
102 * is required
103 **/
104 public Object lookup(String objectIdentifier)
105 throws org.odmg.ObjectNameNotFoundException {
106 if (_poemDB == null) throw new org.odmg.DatabaseClosedException();
107 Object theObject = _cachedTables.get(objectIdentifier);
108 if (theObject == null) {
109 theObject = new PoemTableAsDCollection(_poemDB.
110 getTable(objectIdentifier));
111 _cachedTables.put(objectIdentifier,theObject);
112 }
113 return theObject;
114 }
115
116 /** Not supported with the Poem / ODMG wrapper **/
117 public void makePersistent(Object objectToBePersistent) {
118 throw new org.odmg.NotImplementedException();
119 }
120 /** Not supported with the Poem / ODMG wrapper **/
121 public void deletePersistent(Object persistedObject) {
122 throw new org.odmg.NotImplementedException();
123 }
124 /** Not supported with the Poem / ODMG wrapper **/
125 public void bind(Object objectToBePersistent, String objectIdentifier) {
126 throw new org.odmg.NotImplementedException();
127 }
128 /** Not supported with the Poem / ODMG wrapper **/
129 public void unbind(String objectIdentifier) {
130 throw new org.odmg.NotImplementedException();
131 }
132
133 }
134