CAS Unit Tests
Clover coverage report - CAS Unit Tests
Coverage timestamp: Mon Jul 4 2005 18:13:17 CDT
file stats: LOC: 323   Methods: 8
NCLOC: 261   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WildCardComparison.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    import java.util.StringTokenizer;
 20   
 21    import org.apache.commons.logging.Log;
 22    import org.apache.commons.logging.LogFactory;
 23   
 24    import org.globus.cas.types.ObjectData;
 25   
 26    import org.globus.cas.impl.service.ObjectComparison;
 27   
 28    import org.globus.util.I18n;
 29   
 30    /**
 31    * Comparison class for objects that are within the realm of
 32    * "CAS Simple Policy Language"
 33    * Wild card character is "*" and file delimiter is "/"
 34    */
 35    public class WildCardComparison extends ObjectComparison {
 36   
 37    static Log logger = LogFactory.getLog(WildCardComparison.class.getName());
 38   
 39    private static I18n i18n =
 40    I18n.getI18n("org.globus.cas.impl.databaseAccess.errors",
 41    WildCardComparison.class.getClassLoader());
 42   
 43    final static String wildCardChar = "*";
 44    final static String fileDelimiter = "/";
 45   
 46    // Set of all objects that are a subset
 47  0 public ObjectData[] matchingSubset(ObjectData objData)
 48    throws CasDBException {
 49   
 50  0 logger.debug("matching subset " + objData.getObjectNamespace());
 51  0 Vector objects = getObjectsInNamespace(objData.getObjectNamespace());
 52  0 return matchForSubset(objects, objData);
 53    }
 54   
 55  0 public ObjectData[] matchForSubset(Vector objects, ObjectData objData) {
 56   
 57  0 if ((objects == null) || (objects.size() <= 0)) {
 58  0 logger.debug("No namespace match");
 59  0 return null;
 60    }
 61  0 String reqName = objData.getObjectName();
 62  0 logger.debug("Subset, Request name " + reqName);
 63  0 String toMatch = reqName;
 64  0 Vector matchedObjects = new Vector();
 65  0 if (toMatch.endsWith(fileDelimiter + wildCardChar)) {
 66    // it needs to match till /
 67  0 toMatch = reqName.substring(0, reqName.indexOf(wildCardChar));
 68  0 logger.debug("toMatch " + toMatch);
 69   
 70  0 for (int i=0; i<objects.size(); i++) {
 71  0 ObjectData obj = (ObjectData)objects.get(i);
 72  0 String objInDb = obj.getObjectName();
 73  0 if ((!objInDb.equals(reqName)) &&
 74    (objInDb.startsWith(toMatch))) {
 75  0 logger.debug("adding " + obj.getObjectName());
 76  0 matchedObjects.add(obj);
 77    }
 78    }
 79    }
 80   
 81  0 ObjectData[] retData = null;
 82  0 if (matchedObjects.size() > 0) {
 83  0 retData = new ObjectData[matchedObjects.size()];
 84  0 matchedObjects.toArray(retData);
 85    }
 86  0 return retData;
 87    }
 88   
 89    // Set of all objects that are a super set
 90  0 public ObjectData[] matchingSuperset(ObjectData objData)
 91    throws CasDBException {
 92  0 logger.debug("Matching super set");
 93  0 Vector objects = getObjectsInNamespace(objData.getObjectNamespace());
 94  0 return matchForSuperset(objects, objData);
 95    }
 96   
 97  0 public ObjectData[] matchForSuperset(Vector objects, ObjectData objData) {
 98   
 99  0 if ((objects == null) || (objects.size() <= 0)) {
 100  0 logger.debug("No namespace match");
 101  0 return null;
 102    }
 103  0 String reqName = objData.getObjectName();
 104  0 logger.debug("Superset, Request name " + reqName);
 105  0 if (reqName.endsWith(fileDelimiter + wildCardChar))
 106  0 reqName = reqName.substring(0, reqName.indexOf(fileDelimiter
 107    + wildCardChar));
 108  0 logger.debug("Altered reqName " + reqName);
 109  0 StringTokenizer strTok = new StringTokenizer(reqName, fileDelimiter);
 110  0 int numberOfRelevantToks = strTok.countTokens()-1;
 111  0 logger.debug("Number of relevant tokens " + numberOfRelevantToks);
 112  0 String[] tokensToMatch = null;
 113  0 if (numberOfRelevantToks == 0) {
 114  0 tokensToMatch = new String[1];
 115  0 tokensToMatch[0] = "*";
 116    } else {
 117  0 tokensToMatch = new String[numberOfRelevantToks];
 118  0 for (int i=0; i<numberOfRelevantToks; i++) {
 119  0 if (i == 0) {
 120  0 tokensToMatch[i] = strTok.nextToken();
 121    } else {
 122  0 tokensToMatch[i] = tokensToMatch[i-1] + fileDelimiter
 123    + strTok.nextToken();
 124    }
 125  0 logger.debug("TOKEN: " + tokensToMatch[i]);
 126    }
 127    }
 128  0 int lengthToCompare = reqName.length();
 129  0 Vector matchedObjects = new Vector();
 130  0 for (int i=0; i<objects.size(); i++) {
 131    // if the object has length greater than reqObj length, not a match
 132  0 ObjectData obj = (ObjectData)objects.get(i);
 133  0 String objInDb = obj.getObjectName();
 134  0 if (objInDb.length() <= lengthToCompare) {
 135  0 logger.debug("Shorter string, chance its a super set "
 136    + objInDb);
 137  0 for (int j=0; j<tokensToMatch.length; j++) {
 138  0 logger.debug("Token to match" + tokensToMatch[j]
 139    + fileDelimiter + wildCardChar);
 140  0 if (objInDb.equals(tokensToMatch[j] + fileDelimiter
 141    + wildCardChar)) {
 142  0 logger.debug("Added obj " + objInDb);
 143  0 matchedObjects.add(obj);
 144  0 break;
 145    }
 146    }
 147    }
 148    }
 149   
 150  0 ObjectData[] retData = null;
 151  0 if (matchedObjects.size() > 0) {
 152  0 retData = new ObjectData[matchedObjects.size()];
 153  0 matchedObjects.toArray(retData);
 154    }
 155  0 return retData;
 156    }
 157   
 158    // Assuming namespace compAlg is wildcard when this method is invoked.
 159  0 public boolean objectMatches(ObjectData policyObj, ObjectData requestObj)
 160    throws CasDBException {
 161   
 162    // if not same namespace return false
 163  0 if (!policyObj.getObjectNamespace().equals(
 164    requestObj.getObjectNamespace()))
 165  0 return false;
 166  0 return matchObjectNames(policyObj.getObjectName(),
 167    requestObj.getObjectName());
 168    }
 169   
 170  0 private boolean matchObjectNames(String policyObjectName,
 171    String requestObjectName) {
 172   
 173  0 logger.debug("matchObjectNames called with policy " + policyObjectName
 174    + " request " + requestObjectName);
 175   
 176  0 if (requestObjectName.length() < policyObjectName.length()) {
 177  0 logger.debug("Less chars");
 178  0 return false;
 179    }
 180   
 181  0 int index = policyObjectName.length();
 182  0 if (policyObjectName.endsWith(fileDelimiter + wildCardChar)) {
 183  0 logger.debug("policy object name ends with /*");
 184  0 index =
 185    policyObjectName.lastIndexOf(fileDelimiter + wildCardChar);
 186  0 logger.debug("index is " + index);
 187  0 requestObjectName = requestObjectName.substring(0, index);
 188  0 logger.debug("altered request obj Name " + requestObjectName);
 189    }
 190   
 191  0 if (!requestObjectName.equals(policyObjectName.substring(0, index))) {
 192  0 logger.debug("leading part is not equal");
 193  0 return false;
 194    }
 195  0 return true;
 196    }
 197   
 198  0 public boolean objectExists(ObjectData reqObj)
 199    throws CasDBException {
 200   
 201  0 logger.debug("object exists " + reqObj.getObjectName());
 202   
 203    // get all objects that match the name space of the reqObj
 204  0 String query = "select" + CasDBConstants.COL_OBJECT_NAME + " from"
 205    + CasDBConstants.TABLE_OBJECT + " where"
 206    + CasDBConstants.COL_NS_NICKNAME + "='"
 207    + reqObj.getObjectNamespace() + "'";
 208  0 Connection connection = CasDBStorage.getDBConnection();
 209  0 Vector objectName = null;
 210  0 Statement statement = null;
 211  0 ResultSet resultSet = null;
 212  0 try {
 213  0 statement = connection.createStatement();
 214  0 resultSet = statement.executeQuery(query);
 215  0 if ((resultSet!=null) && (resultSet.next())) {
 216  0 objectName = new Vector(resultSet.getFetchSize());
 217  0 do {
 218  0 objectName.add(resultSet.getString(
 219    CasDBConstants.COL_OBJECT_NAME.trim()));
 220  0 } while (resultSet.next());
 221    }
 222    }
 223    catch (SQLException exp) {
 224  0 logger.error(i18n.getMessage("existenceErr", new Object[]
 225    { "object", query} ),
 226    exp);
 227  0 throw new CasDBException(i18n.getMessage("objExistenceErr",
 228    new Object[] {
 229    "object", "" })
 230    + exp.getMessage(), exp);
 231    }
 232    finally {
 233    // returning connection irrespective of whether stmt
 234    // and result set are closed or not.
 235  0 CasDBStorage.returnDBConnection(connection);
 236  0 try {
 237  0 if (resultSet != null)
 238  0 resultSet.close();
 239  0 if (statement != null)
 240  0 statement.close();
 241    }
 242    catch (SQLException exp) {
 243  0 logger.warn(i18n.getMessage("existenceCloseErr", "object"),
 244    exp);
 245    }
 246    }
 247   
 248  0 if (objectName == null) {
 249  0 logger.debug("No object in this namesapce");
 250  0 return false;
 251    }
 252   
 253  0 String reqObjName = reqObj.getObjectName();
 254  0 for (int i=0; i<objectName.size(); i++) {
 255    // if a match is found return true
 256  0 if (matchObjectNames( reqObjName, (String)objectName.get(i))) {
 257  0 logger.debug("requestName, objName matched");
 258  0 return true;
 259    }
 260  0 if (matchObjectNames((String)objectName.get(i), reqObjName)) {
 261  0 logger.debug("objName, requestName matched");
 262  0 return true;
 263    }
 264    }
 265  0 return false;
 266    }
 267   
 268  0 private Vector getObjectsInNamespace(String namespace)
 269    throws CasDBException {
 270   
 271    // get all objects that match the name space of the objData
 272  0 String query = "select * from" + CasDBConstants.TABLE_OBJECT
 273    + " where" + CasDBConstants.COL_NS_NICKNAME + "='" + namespace
 274    + "'";
 275  0 logger.debug("Query is " + query);
 276  0 Connection connection = CasDBStorage.getDBConnection();
 277  0 Vector objects = null;
 278  0 Statement statement = null;
 279  0 ResultSet resultSet = null;
 280  0 try {
 281  0 statement = connection.createStatement();
 282  0 resultSet = statement.executeQuery(query);
 283  0 if ((resultSet!=null) && (resultSet.next())) {
 284  0 objects = new Vector(resultSet.getFetchSize());
 285  0 do {
 286  0 ObjectData objData = new ObjectData();
 287  0 objData.setObjectId(resultSet.getString(
 288    CasDBConstants.COL_OBJECT_ID.trim()));
 289  0 objData.setObjectName(resultSet.getString(
 290    CasDBConstants.COL_OBJECT_NAME.trim()));
 291  0 objData.setObjectNamespace(resultSet.getString(
 292    CasDBConstants.COL_NS_NICKNAME.trim()));
 293  0 objects.add(objData);
 294  0 } while (resultSet.next());
 295    }
 296    }
 297    catch (SQLException exp) {
 298  0 logger.error(i18n.getMessage("existenceErr", new Object[]
 299    { "object", query} ),
 300    exp);
 301  0 throw new CasDBException(i18n.getMessage("objExistenceErr",
 302    new Object[] {
 303    "object", "" })
 304    + exp.getMessage(), exp);
 305    }
 306    finally {
 307    // returning connection irrespective of whether stmt
 308    // and result set are closed or not.
 309  0 CasDBStorage.returnDBConnection(connection);
 310  0 try {
 311  0 if (resultSet != null)
 312  0 resultSet.close();
 313  0 if (statement != null)
 314  0 statement.close();
 315    }
 316    catch (SQLException exp) {
 317  0 logger.warn(i18n.getMessage("existenceCloseErr", "object"),
 318    exp);
 319    }
 320    }
 321  0 return objects;
 322    }
 323    }