001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.ojb.odmg.components;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.activity.Initializable;
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023:
024: import org.apache.ojb.odmg.OJB;
025: import org.odmg.Database;
026: import org.odmg.Implementation;
027: import org.odmg.ODMGException;
028:
029: import java.util.Iterator;
030: import java.util.Set;
031: import java.util.HashMap;
032:
033: /**
034: * OJB backed implementation of the ODMG component. Creates a ODMG Implementation
035: * Object and stores it for the future use.
036: *
037: * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
038: * @version CVS $Id: ODMGImpl.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class ODMGImpl extends AbstractLogEnabled implements ODMG,
041: ThreadSafe, Initializable, Disposable {
042:
043: private static final String DEFAULT_CONNECTION = "default";
044: private static final int DEFAULT_MODE = Database.OPEN_READ_WRITE;
045:
046: private Implementation odmg;
047: private HashMap databases = new HashMap();
048:
049: /* (non-Javadoc)
050: * @see org.apache.avalon.framework.activity.Initializable#initialize()
051: */
052: public void initialize() throws Exception {
053: // Get the Implementation
054: this .odmg = OJB.getInstance();
055: }
056:
057: /* (non-Javadoc)
058: * @see org.apache.avalon.framework.activity.Disposable#dispose()
059: */
060: public void dispose() {
061: final Set keys = this .databases.keySet();
062: for (Iterator i = keys.iterator(); i.hasNext();) {
063: final Database db = (Database) i.next();
064: try {
065: db.close();
066: } catch (ODMGException e) {
067: getLogger().error("OJB-ODMG: Cannot close Database", e);
068: }
069: i.remove();
070: }
071: }
072:
073: /* (non-Javadoc)
074: * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance()
075: */
076: public Implementation getInstance() throws ODMGException {
077: return getInstance(DEFAULT_CONNECTION, DEFAULT_MODE);
078: }
079:
080: /* (non-Javadoc)
081: * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance(java.lang.String)
082: */
083: public Implementation getInstance(String connection)
084: throws ODMGException {
085: return getInstance(connection, DEFAULT_MODE);
086: }
087:
088: /* (non-Javadoc)
089: * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance(int)
090: */
091: public Implementation getInstance(int mode) throws ODMGException {
092: return getInstance(DEFAULT_CONNECTION, mode);
093: }
094:
095: /* (non-Javadoc)
096: * @see org.apache.cocoon.ojb.odmg.components.ODMG#getInstance(java.lang.String, int)
097: */
098: public Implementation getInstance(String connection, int mode)
099: throws ODMGException {
100: synchronized (this .databases) {
101: Database db = (Database) this .databases.get(connection
102: + ":" + mode);
103: if (null == db) {
104: db = this .odmg.newDatabase();
105: db.open(connection, mode);
106: this .databases.put(connection + ":" + mode, db);
107: }
108: }
109: return this.odmg;
110: }
111: }
|