CAS Unit Tests
Clover coverage report - CAS Unit Tests
Coverage timestamp: Mon Jul 4 2005 18:13:17 CDT
file stats: LOC: 560   Methods: 15
NCLOC: 460   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ObjectDataHandler.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Portions of this file Copyright 1999-2005 University of Chicago
 3    * Portions of this file Copyright 1999-2005 The University of Southern California.
 4    *
 5    * This file or a portion of this file is licensed under the
 6    * terms of the Globus Toolkit Public License, found at
 7    * http://www.globus.org/toolkit/download/license.html.
 8    * If you redistribute this file, with or without
 9    * modifications, you must include this notice in the file.
 10    */
 11    package org.globus.cas.impl.databaseAccess;
 12   
 13    import java.sql.Connection;
 14    import java.sql.Statement;
 15    import java.sql.SQLException;
 16    import java.sql.ResultSet;
 17   
 18    import java.util.Vector;
 19   
 20    import org.apache.commons.logging.Log;
 21    import org.apache.commons.logging.LogFactory;
 22   
 23    import org.globus.cas.impl.CasConstants;
 24   
 25    import org.globus.cas.impl.service.ObjectComparison;
 26   
 27    import org.globus.cas.types.ObjectData;
 28    import org.globus.cas.types.CasObjectData;
 29    import org.globus.cas.types.ArrayOfString;
 30   
 31    import org.globus.util.I18n;
 32   
 33    /**
 34    * Describes the data needed for object
 35    */
 36    public class ObjectDataHandler {
 37   
 38    static Log logger = LogFactory.getLog(ObjectDataHandler.class.getName() );
 39   
 40    private static I18n i18n =
 41    I18n.getI18n("org.globus.cas.impl.databaseAccess.errors",
 42    ObjectDataHandler.class.getClassLoader());
 43   
 44    /**
 45    * Returns object id given onject name and namespace
 46    * Returns -1 if object is not in database
 47    */
 48  0 public static int getObjectId(String objectName, String namespaceNick)
 49    throws CasDBException {
 50  0 Connection connection = CasDBStorage.getDBConnection();
 51  0 int result = getObjectId(connection, objectName, namespaceNick);
 52  0 CasDBStorage.returnDBConnection(connection);
 53  0 return result;
 54    }
 55   
 56  0 public static int getObjectId(Connection conn, String objectName,
 57    String namespaceNick)
 58    throws CasDBException {
 59  0 int result = -1;
 60  0 String query = "select " + CasDBConstants.COL_OBJECT_ID + " from"
 61    + CasDBConstants.TABLE_OBJECT + " where"
 62    + CasDBConstants.COL_OBJECT_NAME + "='" + objectName.trim()
 63    + "' and " + CasDBConstants.COL_NS_NICKNAME + "='"
 64    + namespaceNick.trim() + "'";
 65  0 Statement statement = null;
 66  0 ResultSet resultSet = null;
 67  0 try {
 68  0 statement = conn.createStatement();
 69  0 logger.debug("object exists query is " + query);
 70  0 resultSet = statement.executeQuery(query);
 71  0 if ((resultSet != null) && (resultSet.next())) {
 72  0 result = resultSet.getInt(CasDBConstants.COL_OBJECT_ID.trim());
 73    }
 74    }
 75    catch (SQLException exp) {
 76  0 CasDBStorage.returnDBConnection(conn);
 77  0 logger.error(i18n.getMessage("existenceErr", new Object[] {
 78    "object", query} ), exp);
 79  0 throw new CasDBException(i18n.getMessage("existenceErr",
 80    new Object[] { "object",
 81    "" })
 82    + exp.getMessage(),
 83    exp);
 84    }
 85    finally {
 86  0 try {
 87  0 if (resultSet != null)
 88  0 resultSet.close();
 89  0 if (statement != null)
 90  0 statement.close();
 91    }
 92    catch (SQLException exp) {
 93  0 logger.warn(i18n.getMessage("dataExistenceCloseErr"), exp);
 94    }
 95    }
 96  0 return result;
 97    }
 98   
 99    // Returns the object comparison class
 100  0 public static ObjectComparison getComparisonClass(String compAlgClassName)
 101    throws CasDBException {
 102   
 103  0 ObjectComparison objectComp = null;
 104  0 try {
 105  0 Class comparisonClass = Class.forName(compAlgClassName);
 106  0 objectComp = (ObjectComparison)comparisonClass.newInstance();
 107    }
 108    catch (ClassNotFoundException classNFE) {
 109  0 logger.error(i18n.getMessage("compClassNotFound",
 110    compAlgClassName), classNFE);
 111  0 throw new CasDBException(i18n.getMessage("compClassNotFound",
 112    compAlgClassName)
 113    + classNFE.getMessage(), classNFE);
 114    }
 115    catch (InstantiationException instantiateExp) {
 116  0 logger.error(i18n.getMessage("compClassInst", compAlgClassName),
 117    instantiateExp);
 118  0 throw new CasDBException(i18n.getMessage("compClassInst",
 119    compAlgClassName)
 120    + instantiateExp.getMessage(),
 121    instantiateExp);
 122    }
 123    catch (IllegalAccessException illegalExp) {
 124  0 logger.error(i18n.getMessage("compClassInst", compAlgClassName),
 125    illegalExp);
 126  0 throw new CasDBException(i18n.getMessage("compClassInst",
 127    compAlgClassName)
 128    + illegalExp.getMessage(),
 129    illegalExp);
 130    }
 131  0 return objectComp;
 132    }
 133   
 134    // Returns the comparison algorithm for the specified objectId
 135  0 public static String getComparisonAlg(String namespace)
 136    throws CasDBException {
 137   
 138  0 Connection connection = CasDBStorage.getDBConnection();
 139  0 try {
 140  0 return getComparisonAlg(connection, namespace);
 141    } finally {
 142    // returning connection irrespective of whether stmt
 143    // and result set are closed or not.
 144  0 CasDBStorage.returnDBConnection(connection);
 145    }
 146    }
 147   
 148  0 private static String getComparisonAlg(Connection connection,
 149    String namespace)
 150    throws CasDBException {
 151   
 152  0 String query = "select" + CasDBConstants.COL_COMPARISON_ALG + " from"
 153    + CasDBConstants.TABLE_NAMESPACE + " where"
 154    + CasDBConstants.COL_NS_NICKNAME + "='" + namespace + "'";
 155   
 156  0 String compAlg = null;
 157  0 Statement statement = null;
 158  0 ResultSet resultSet = null;
 159  0 try {
 160  0 statement = connection.createStatement();
 161  0 resultSet = statement.executeQuery(query);
 162  0 if ((resultSet!=null) && (resultSet.next())) {
 163  0 compAlg = resultSet.getString(
 164    CasDBConstants.COL_COMPARISON_ALG.trim());
 165    }
 166    }
 167    catch (SQLException exp) {
 168  0 logger.error(i18n.getMessage("retrCompAlgErr", namespace)
 169    + query, exp);
 170  0 throw new CasDBException(i18n.getMessage("retrCompAlgErr",
 171    namespace)
 172    + exp.getMessage(), exp);
 173    }
 174    finally {
 175   
 176  0 try {
 177  0 if (resultSet != null)
 178  0 resultSet.close();
 179  0 if (statement != null)
 180  0 statement.close();
 181    }
 182    catch (SQLException exp) {
 183  0 String err = i18n.getMessage("compAlgNSClose");
 184  0 logger.warn(err, exp);
 185    }
 186    }
 187   
 188  0 if (compAlg == null) {
 189  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 190    "Namespace", namespace });
 191  0 logger.error(err);
 192  0 throw new CasDBException(err);
 193    }
 194  0 if (compAlg.equals(CasConstants.EXACT_ALGNAME)) {
 195  0 logger.debug("Exact comparison");
 196  0 compAlg = CasConstants.EXACT_ALG_CLASSNAME;
 197    }
 198  0 if (compAlg.equals(CasConstants.WILD_ALGNAME)) {
 199  0 logger.debug("WildCard comparison");
 200  0 compAlg = CasConstants.WILD_ALG_CLASSNAME;
 201    }
 202  0 logger.debug("comparison Algorithm " + compAlg);
 203  0 return compAlg;
 204    }
 205   
 206  0 private static void storeInObjectTable(Connection conn, String objectName,
 207    String namespaceNick)
 208    throws CasDBException {
 209   
 210    // Insert object data
 211  0 String columnString = "insert into " + CasDBConstants.TABLE_OBJECT
 212    + " (" + CasDBConstants.COL_OBJECT_NAME + ","
 213    + CasDBConstants.COL_NS_NICKNAME + ") values ('";
 214  0 StringBuffer query = new StringBuffer(columnString);
 215  0 query.append(objectName).append("','").append(namespaceNick)
 216    .append("')");
 217  0 Statement statement = null;
 218  0 try {
 219  0 statement = conn.createStatement();
 220  0 logger.debug("store query is " + query.toString());
 221  0 statement.executeUpdate(query.toString());
 222    }
 223    catch (SQLException exp) {
 224  0 CasDBStorage.returnDBConnection(conn);
 225  0 logger.error(i18n.getMessage("storeErr", new Object[] { "object",
 226    query.toString() }), exp);
 227  0 throw new CasDBException(i18n.getMessage("storeErr", new Object[] {
 228    "object", "" } ) + exp.getMessage(), exp);
 229    }
 230    finally {
 231  0 try {
 232  0 if (statement != null)
 233  0 statement.close();
 234    }
 235    catch (SQLException exp) {
 236  0 logger.warn(i18n.getMessage("storeErrClose", "object"), exp);
 237    }
 238    }
 239    }
 240   
 241    /**
 242    * Insert into user data into object table
 243    * Assuming checks for object already existing we made
 244    */
 245  0 public static String storeObject(ObjectData objectData)
 246    throws CasDBException {
 247   
 248  0 String objectName = objectData.getObjectName();
 249  0 String namespaceNick = objectData.getObjectNamespace();
 250  0 Connection connection = CasDBStorage.getDBConnection();
 251  0 storeInObjectTable(connection, objectName, namespaceNick);
 252  0 int result = getObjectId(connection, objectName, namespaceNick);
 253  0 CasDBStorage.returnDBConnection(connection);
 254  0 return Integer.toString(result);
 255    }
 256   
 257    /**
 258    * List of ObjectData
 259    */
 260  0 public static String[] list() throws CasDBException {
 261  0 String query = "select" + CasDBConstants.COL_OBJECT_NAME + " from"
 262    + CasDBConstants.TABLE_OBJECT;
 263  0 return CasDBStorage.runListQuery(query, CasDBConstants.COL_OBJECT_NAME);
 264    }
 265   
 266    /**
 267    * Retrieves object data given onjectName and namespace
 268    * Picks up values from obejct table and object group entry table
 269    */
 270  0 public static CasObjectData retrieveObject(String objectName,
 271    String namespaceNick)
 272    throws CasDBException {
 273   
 274  0 ObjectData returnObject = null;
 275  0 Connection connection = CasDBStorage.getDBConnection();
 276  0 int result = getObjectId(connection, objectName, namespaceNick);
 277  0 if (result <= 0) {
 278  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 279    "Object data ", namespaceNick + "/" + objectName });
 280  0 logger.debug(err);
 281  0 throw new CasDBException(err);
 282    }
 283  0 returnObject = retrieveObjectForId(Integer.toString(result));
 284  0 CasDBStorage.returnDBConnection(connection);
 285  0 return returnObject;
 286    }
 287   
 288    /**
 289    * Retrieves object data given objectId
 290    * Picks up values from obejct table and object group entry table
 291    */
 292  0 public static ObjectData retrieveObjectForId(String objectId)
 293    throws CasDBException {
 294   
 295  0 ObjectData objData = null;
 296  0 Vector temp = null;
 297  0 String query = "select" + CasDBConstants.COL_OBJECTGP_NAME + " from"
 298    + CasDBConstants.TABLE_OBJECTGP_ENTRY + " where "
 299    + CasDBConstants.COL_OBJECT_SPEC + " ='" + objectId + "'";
 300  0 Connection connection = CasDBStorage.getDBConnection();
 301  0 Statement statement = null;
 302  0 ResultSet resultSet = null;
 303  0 try {
 304  0 statement = connection.createStatement();
 305  0 resultSet = statement.executeQuery(query);
 306  0 if ((resultSet!=null) && (resultSet.next())) {
 307  0 temp = new Vector(resultSet.getFetchSize());
 308  0 do {
 309  0 temp.add(
 310    resultSet.getString(
 311    CasDBConstants.COL_OBJECTGP_NAME.trim()));
 312  0 } while (resultSet.next());
 313    }
 314    }
 315    catch (SQLException exp) {
 316  0 logger.error(i18n.getMessage("stmtExecErr", query), exp);
 317  0 throw new CasDBException(i18n.getMessage("stmtExecErr", query)
 318    + exp.getMessage(), exp);
 319    }
 320    finally {
 321    // returning connection irrespective of whether stmt
 322    // and result set are closed or not.
 323  0 CasDBStorage.returnDBConnection(connection);
 324  0 try {
 325  0 if (resultSet != null)
 326  0 resultSet.close();
 327  0 if (statement != null)
 328  0 statement.close();
 329    }
 330    catch (SQLException exp) {
 331  0 String err = "Error retrieving object. Could not "
 332    + " close SQL statement.";
 333  0 logger.warn(err, exp);
 334    }
 335    }
 336   
 337  0 ArrayOfString arrayOfGpNames = null;
 338  0 if (temp != null) {
 339  0 String groupNamesString[] = new String[temp.size()];
 340  0 temp.toArray(groupNamesString);
 341  0 arrayOfGpNames = new ArrayOfString(groupNamesString);
 342    }
 343   
 344  0 connection = CasDBStorage.getDBConnection();
 345  0 query = "select * from" + CasDBConstants.TABLE_OBJECT + " where"
 346    + CasDBConstants.COL_OBJECT_ID + " = " + objectId.trim();
 347  0 try {
 348  0 statement = connection.createStatement();
 349  0 resultSet = statement.executeQuery(query);
 350  0 if (resultSet !=null) {
 351  0 if (resultSet.next()) {
 352  0 objData = new ObjectData();
 353  0 objData.setObjectId(objectId);
 354  0 objData.setObjectName(
 355    resultSet.getString(
 356    CasDBConstants.COL_OBJECT_NAME.trim()));
 357  0 objData.setObjectNamespace(
 358    resultSet.getString(
 359    CasDBConstants.COL_NS_NICKNAME.trim()));
 360  0 objData.setGroupNames(arrayOfGpNames);
 361    }
 362    }
 363    }
 364    catch (SQLException exp) {
 365  0 logger.error(i18n.getMessage("retrieveErr", new Object[] {
 366    "object", query }), exp);
 367  0 throw new CasDBException(i18n
 368    .getMessage("retrieveErr",
 369    new Object[] {"object", ""})
 370    + exp.getMessage(), exp);
 371    }
 372    finally {
 373    // returning connection irrespective of whether stmt
 374    // and result set are closed or not.
 375  0 CasDBStorage.returnDBConnection(connection);
 376  0 try {
 377  0 if (resultSet != null)
 378  0 resultSet.close();
 379  0 if (statement != null)
 380  0 statement.close();
 381    }
 382    catch (SQLException exp) {
 383  0 logger.warn(i18n.getMessage("retrieveErrClose", "object"),
 384    exp);
 385    }
 386    }
 387  0 return objData;
 388    }
 389   
 390    /**
 391    * Deletes object data gievn ojectName and namespace
 392    * Manipulates object table
 393    */
 394  0 public static int deleteObject(String objectName, String namespaceNick)
 395    throws CasDBException {
 396  0 int newObjectId = -1;
 397  0 Connection connection = CasDBStorage.getDBConnection();
 398  0 newObjectId = getObjectId(connection, objectName, namespaceNick);
 399  0 deleteObjectForId(connection, newObjectId);
 400  0 CasDBStorage.returnDBConnection(connection);
 401  0 return newObjectId;
 402    }
 403   
 404    /**
 405    * Deletes object data given objectId
 406    * Manipulates object table
 407    */
 408  0 public static void deleteObjectForId(String objectId)
 409    throws CasDBException {
 410   
 411  0 Connection connection = CasDBStorage.getDBConnection();
 412  0 deleteObjectForId(connection, Integer.parseInt(objectId));
 413  0 CasDBStorage.returnDBConnection(connection);
 414    }
 415   
 416  0 private static void deleteObjectForId(Connection connection, int objectId)
 417    throws CasDBException {
 418   
 419    // Object can be a member of object group, no SQL constraint
 420    // defines that. So check.
 421  0 if (ObjectGroupDataHandler.isMember(connection,
 422    Integer.toString(objectId),
 423    CasConstants.OBJECT_SPEC)) {
 424  0 String errMesg =
 425    i18n.getMessage("memberOfGpErr",
 426    new Object[] { "object",
 427    new Integer(objectId),
 428    "object" });
 429  0 logger.debug(errMesg);
 430  0 throw new CasDBException(errMesg);
 431    }
 432   
 433  0 String queryString = "delete from" + CasDBConstants.TABLE_OBJECT
 434    + " where" + CasDBConstants.COL_OBJECT_ID + " =" + objectId;
 435  0 Statement statement = null;
 436  0 try {
 437  0 statement = connection.createStatement();
 438  0 statement.executeUpdate(queryString);
 439    }
 440    catch (SQLException exp) {
 441  0 CasDBStorage.returnDBConnection(connection);
 442  0 logger.error(i18n.getMessage("delObjErr",
 443    new Object[] { "object",
 444    queryString }),
 445    exp);
 446  0 throw new CasDBException(i18n.getMessage("delObjErr",
 447    new Object[] { "object",
 448    "" })
 449    + exp.getMessage(), exp);
 450    }
 451    finally {
 452  0 try {
 453  0 if (statement != null)
 454  0 statement.close();
 455    }
 456    catch (SQLException exp) {
 457  0 logger.warn(i18n.getMessage("delObjErrClose", "object"), exp);
 458    }
 459    }
 460    }
 461   
 462    // Returns id of all objects in the db that match this object. Relevant
 463    // comparison algorithm is used.
 464  0 public static Vector retrieveMatchingObjects(String namespace,
 465    String objectName)
 466    throws CasDBException {
 467   
 468  0 logger.debug("Namespace " + namespace + " objName " + objectName);
 469  0 Vector nsMatch = null;
 470    // Retrieve all objects that belong to the same namespace, then use
 471    // macthes method to match.
 472  0 String query = "select * from" + CasDBConstants.TABLE_OBJECT
 473    + " where " + CasDBConstants.COL_NS_NICKNAME + "='" + namespace
 474    + "'";
 475  0 logger.debug("Retrieve Matching objects query: "+ query);
 476  0 Connection connection = CasDBStorage.getDBConnection();
 477  0 Statement statement = null;
 478  0 ResultSet resultSet = null;
 479  0 try {
 480  0 statement = connection.createStatement();
 481  0 resultSet = statement.executeQuery(query);
 482  0 if ((resultSet != null) && (resultSet.next())) {
 483  0 nsMatch = new Vector(resultSet.getFetchSize());
 484  0 do {
 485  0 ObjectData objData = new ObjectData();
 486  0 objData.setObjectId(resultSet.getString(
 487    CasDBConstants.COL_OBJECT_ID.trim()));
 488  0 objData.setObjectName(resultSet.getString(
 489    CasDBConstants.COL_OBJECT_NAME.trim()));
 490  0 objData.setObjectNamespace(resultSet.getString(
 491    CasDBConstants.COL_NS_NICKNAME.trim()));
 492  0 nsMatch.add(objData);
 493  0 } while (resultSet.next());
 494    }
 495   
 496  0 if (nsMatch == null) {
 497  0 logger.debug("No objects in the same namesapce, "
 498    + "so no matches");
 499  0 return null;
 500    } else {
 501  0 Vector matchedObjects = null;
 502  0 ObjectData objToBeCompared = new ObjectData();
 503  0 objToBeCompared.setObjectNamespace(namespace);
 504  0 objToBeCompared.setObjectName(objectName);
 505  0 String compAlgClassName =
 506    ObjectDataHandler.getComparisonAlg(connection, namespace);
 507  0 ObjectComparison objectComp =
 508    ObjectDataHandler.getComparisonClass(compAlgClassName);
 509  0 for (int i=0; i<nsMatch.size(); i++) {
 510  0 ObjectData objInDb = (ObjectData)nsMatch.get(i);
 511  0 if (objectComp.objectMatches(objInDb, objToBeCompared)) {
 512  0 logger.debug("Object matches "
 513    + objInDb.getObjectId());
 514  0 if (matchedObjects == null) {
 515  0 matchedObjects = new Vector();
 516    }
 517  0 matchedObjects.add(objInDb.getObjectId());
 518    } else
 519  0 logger.debug("Object does not match "
 520    + objInDb.getObjectId());
 521    }
 522  0 return matchedObjects;
 523    }
 524    }
 525    catch (SQLException exp) {
 526  0 logger.error(i18n.getMessage("dataExistenceErr", query), exp);
 527  0 throw new CasDBException(i18n.getMessage("dataExistenceErr", query)
 528    + exp.getMessage(), exp);
 529    }
 530    finally {
 531    // returning connection irrespective of whether stmt
 532    // and result set are closed or not.
 533  0 CasDBStorage.returnDBConnection(connection);
 534  0 try {
 535  0 if (resultSet != null)
 536  0 resultSet.close();
 537  0 if (statement != null)
 538  0 statement.close();
 539    }
 540    catch (SQLException exp) {
 541  0 String err = i18n.getMessage("dataExistenceCloseErr");
 542  0 logger.warn(err, exp);
 543    }
 544    }
 545    }
 546   
 547    // Assumed parameter is an object name
 548  0 public static String getObjectDescription(String objectName)
 549    throws CasDBException {
 550  0 ObjectData tempObject = retrieveObjectForId(objectName);
 551  0 if (tempObject == null) {
 552  0 String err = i18n.getMessage("objIdRetrErr", objectName);
 553  0 logger.error(err);
 554  0 throw new CasDBException(err);
 555    }
 556  0 return tempObject.getObjectNamespace() +
 557    CasConstants.OBJECTSPEC_DELIMITER
 558    + tempObject.getObjectName();
 559    }
 560    }