package com.dbxml.db.server.labrador;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: ContentResolver.java,v 1.4 2006/02/02 19:04:15 bradford Exp $
*/
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.Container;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.Database;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.labrador.Discovery;
import com.dbxml.labrador.ID;
import com.dbxml.labrador.Instance;
import com.dbxml.labrador.Resolver;
import com.dbxml.labrador.broker.Broker;
import com.dbxml.labrador.objects.ObjectInstance;
/**
* ContentResolver
*/
public final class ContentResolver implements Resolver {
public static void initialize() {
Broker.getInstance().addResolver(new ContentResolver());
}
private ContentResolver() {
}
public boolean isIDValid(ID id) {
try {
return getContent(id) != null;
}
catch ( DBException e ) {
return false;
}
}
public Instance getInstance(ID id) {
try {
return getContent(id);
}
catch ( DBException e ) {
return null;
}
}
public Discovery getDiscovery(ID id) {
try {
return getContent(id).getDiscovery();
}
catch ( DBException e ) {
return null;
}
}
private ObjectInstance getContent(ID id) throws DBException {
String name = id.toString();
Database db = Database.getInstance();
int idx = name.lastIndexOf('/');
String colName = name.substring(0, idx);
Collection col = db.getCollection(colName);
String docName = name.substring(idx + 1);
Transaction tx = new Transaction();
try {
Container c = col.getContainer(tx, docName);
if ( c != null ) {
ContentProxy dp = new ContentProxy(col, docName);
return new ObjectInstance(dp);
}
else
throw new DBException(FaultCodes.COL_CANNOT_RETRIEVE, "Container '"+docName+"' not found");
}
catch ( DBException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
}