package org.apache.ojb.odmg;
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.OJBRuntimeException;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.query.QueryByIdentity;
import org.apache.ojb.broker.util.logging.Logger;
import org.apache.ojb.broker.util.logging.LoggerFactory;
import org.odmg.ObjectNameNotFoundException;
import org.odmg.ObjectNameNotUniqueException;
import org.odmg.Transaction;
/**
* ODMG NamedRoots implementation.
* this implementation stores the (name, Identity) pairs in
* a database table.
* therefore the NamedRootsMap underlies the same transaction management
* as all other persistent objects
*
* @author Thomas Mahler
* @version $Id: NamedRootsMap.java,v 1.12 2004/04/04 23:53:38 brianm Exp $
*/
public class NamedRootsMap
{
private Logger log = LoggerFactory.getLogger(NamedRootsMap.class);
/**
* singleton instance
*/
private static NamedRootsMap _instance;
private OJBTxManager txManager;
/**
*
* constructor is private, use getInstance()
*
*/
private NamedRootsMap()
{
txManager = TxManagerFactory.instance();
}
/**
* lookup key in map. If found return a deserialized Identity object, else null
*/
public Identity get(String key)
{
NamedRootsEntry example = new NamedRootsEntry(key, null);
Identity oid;
try
{
PersistenceBroker broker = txManager.getCurrentTransaction().getBroker();
oid = new Identity(example, broker);
NamedRootsEntry entry = (NamedRootsEntry) broker.getObjectByIdentity(oid);
return (entry == null ? null : Identity.fromByteArray(entry.getOid()));
}
catch (Exception e)
{
if(log.isDebugEnabled()) log.debug("Could not get identity for key "+key, e);
return null;
}
}
/**
* factory method returns singleton instance
*/
public synchronized static NamedRootsMap getInstance()
{
if (_instance == null)
{
_instance = new NamedRootsMap();
}
return _instance;
}
/**
* put a serialized version of oid into the map
*/
public void put(String key, Identity oid) throws ObjectNameNotUniqueException
{
NamedRootsEntry entry = new NamedRootsEntry(key, oid.serialize());
try
{
TransactionImpl tx = txManager.getCurrentTransaction();
PersistenceBroker broker = tx.getBroker();
NamedRootsEntry stored =
(NamedRootsEntry) broker.getObjectByQuery(new QueryByIdentity(entry));
if (stored != null)
{
throw new ObjectNameNotUniqueException("object key already found, the key " + key + " is not unique");
}
else
{
// don't store us, we'll store you :-)
// i.e. no need to store entry here, just ask for beeing stored on next commit
tx.lock(entry, Transaction.WRITE);
}
}
catch (PersistenceBrokerException e)
{
log.error("Could not put identity to NamedRoots. Key was "+key+", identity was "+oid, e);
throw new OJBRuntimeException(e);
}
}
/**
* remove an entry from the NamedRoots map
*/
public void unbind(String key) throws ObjectNameNotFoundException
{
NamedRootsEntry example = new NamedRootsEntry(key, null);
try
{
TransactionImpl tx = txManager.getCurrentTransaction();
PersistenceBroker broker = tx.getBroker();
NamedRootsEntry stored =
(NamedRootsEntry) broker.getObjectByQuery(new QueryByIdentity(example));
if (stored == null)
{
throw new ObjectNameNotFoundException("Could not find a bound object for key " + key);
}
else
{
// mark stored entry for deletion
// deletion will occur on next commit
tx.markDelete(stored);
}
}
catch (PersistenceBrokerException e)
{
log.error("Could not unbind identity with key "+key+" from NamedRoots", e);
throw new OJBRuntimeException(e);
}
}
}