KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > ojb > odmg > components > ODMGImpl


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 /**
33  * OJB backed implementation of the ODMG component. Creates a ODMG Implementation
34  * Object and stores it for the future use.
35  *
36  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
37  * @version CVS $Id: ODMGImpl.java 156248 2005-03-05 14:43:01Z cziegeler $
38  */

39 public class ODMGImpl extends AbstractLogEnabled
40                       implements ODMG, ThreadSafe, Initializable, Disposable {
41
42     private static final String JavaDoc DEFAULT_CONNECTION ="default";
43     private static final int DEFAULT_MODE = Database.OPEN_READ_WRITE;
44
45     private Implementation odmg;
46     private HashMap JavaDoc databases = new HashMap JavaDoc();
47
48     /* (non-Javadoc)
49      * @see org.apache.avalon.framework.activity.Initializable#initialize()
50      */

51     public void initialize() throws Exception JavaDoc {
52         // Get the Implementation
53
this.odmg = OJB.getInstance();
54     }
55
56     /* (non-Javadoc)
57      * @see org.apache.avalon.framework.activity.Disposable#dispose()
58      */

59     public void dispose() {
60         final Set JavaDoc keys = this.databases.keySet();
61         for (Iterator JavaDoc 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     /* (non-Javadoc)
73      * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance()
74      */

75     public Implementation getInstance() throws ODMGException {
76         return getInstance(DEFAULT_CONNECTION, DEFAULT_MODE);
77     }
78
79     /* (non-Javadoc)
80      * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance(java.lang.String)
81      */

82     public Implementation getInstance(String JavaDoc connection) throws ODMGException {
83         return getInstance(connection, DEFAULT_MODE);
84     }
85
86     /* (non-Javadoc)
87      * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance(int)
88      */

89     public Implementation getInstance(int mode) throws ODMGException {
90         return getInstance(DEFAULT_CONNECTION, mode);
91     }
92
93     /* (non-Javadoc)
94      * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance(java.lang.String, int)
95      */

96     public Implementation getInstance(String JavaDoc 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