CAS Unit Tests
Clover coverage report - CAS Unit Tests
Coverage timestamp: Mon Jul 4 2005 18:13:17 CDT
file stats: LOC: 774   Methods: 9
NCLOC: 658   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PolicyQueryHandler.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 org.globus.cas.types.UserData;
 14    import org.globus.cas.types.PolicyData;
 15    import org.globus.cas.types.ObjectData;
 16    import org.globus.cas.types.ArrayOfString;
 17   
 18    import org.globus.cas.impl.CasConstants;
 19   
 20    import java.sql.ResultSet;
 21    import java.sql.SQLException;
 22    import java.sql.Connection;
 23    import java.sql.Statement;
 24   
 25    import java.util.Vector;
 26   
 27    import org.globus.util.I18n;
 28   
 29    import org.apache.commons.logging.Log;
 30    import org.apache.commons.logging.LogFactory;
 31   
 32    /**
 33    * Used to retrieve all policies (on external objects and CAS server)
 34    * on a specific object or all policies for a user.
 35    */
 36    public class PolicyQueryHandler {
 37   
 38    static Log logger =
 39    LogFactory.getLog(PolicyQueryHandler.class.getName());
 40   
 41    private static I18n i18n =
 42    I18n.getI18n("org.globus.cas.impl.databaseAccess.errors",
 43    PolicyQueryHandler.class.getClassLoader());
 44   
 45    /**
 46    * Returns all polcies for a given trust anchor.
 47    * a) part of objectSpec and objectSpecDesc = TrustAnchor
 48    * b) part of "ObjectGroup" (of which it is a member)
 49    */
 50  0 private static Vector getAllPoliciesForTrustAnchor(String nickName)
 51    throws CasDBException {
 52  0 logger.debug("getAllPolicies for " + nickName);
 53  0 String errMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 54    "trust anchor", nickName });
 55  0 String existsQuery = "select * from" + CasDBConstants.TABLE_TRUSTANCHOR
 56    + " where" + CasDBConstants.COL_TRUST_NICKNAME + "='"
 57    + nickName + "'";
 58  0 if (!CasDBStorage.rowExists(existsQuery)) {
 59  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 60    "trust anchor", nickName });
 61  0 logger.error(err);
 62  0 throw new CasDBException(err);
 63    }
 64  0 Vector policiesVector = new Vector();
 65    // TrustAnchor data could be in object specification of a policy
 66  0 String query = "select * from" + CasDBConstants.TABLE_POLICY
 67    + " where (" + CasDBConstants.COL_OBJECT_SPEC + "='"
 68    + nickName + "') and (" + CasDBConstants.COL_OBJECT_SPEC_DESC
 69    + "='" + CasConstants.TRUSTANCHOR_SPEC + "')";
 70  0 Connection connection = CasDBStorage.getDBConnection();
 71  0 Statement statement = null;
 72  0 ResultSet resultSet = null;
 73  0 try {
 74  0 statement = connection.createStatement();
 75  0 resultSet = statement.executeQuery(query);
 76  0 Vector temp =
 77    PolicyDataHandler.constructPolicyData(connection, resultSet,
 78    false);
 79  0 if (temp != null) {
 80  0 logger.debug("adding policies");
 81  0 policiesVector.addAll(temp);
 82    }
 83    }
 84    catch (SQLException exp) {
 85  0 logger.error(errMesg + "\n" + query, exp);
 86  0 throw new CasDBException(errMesg + exp.getMessage(), exp);
 87    }
 88    catch (CasDBException exp) {
 89  0 logger.error(exp);
 90  0 throw new CasDBException(errMesg + exp.getMessage(), exp);
 91    }
 92    finally {
 93    // returning connection irrespective of whether stmt
 94    // and result set are closed or not.
 95  0 CasDBStorage.returnDBConnection(connection);
 96  0 try {
 97  0 if (resultSet != null)
 98  0 resultSet.close();
 99  0 if (statement != null)
 100  0 statement.close();
 101    }
 102    catch (SQLException exp) {
 103  0 logger.warn(errMesg, exp);
 104    }
 105    }
 106   
 107    // add obj group data
 108  0 String[] groupNames =
 109    ObjectGroupDataHandler.getObjectGroupsForMember(
 110    nickName,
 111    CasConstants.TRUSTANCHOR_SPEC);
 112  0 if (groupNames != null) {
 113  0 for (int i=0; i<groupNames.length; i++) {
 114  0 Vector temp =
 115    getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 116    groupNames[i]);
 117  0 if (temp != null) {
 118  0 logger.debug("adding policies");
 119  0 policiesVector.addAll(temp);
 120    }
 121    }
 122    }
 123  0 if ((policiesVector != null) && (policiesVector.size() > 0)) {
 124  0 logger.debug("policies size " + policiesVector.size());
 125  0 PolicyData[] allPolicies = new PolicyData[policiesVector.size()];
 126  0 for (int i=0; i<policiesVector.size(); i++) {
 127  0 logger.debug("polices " + i);
 128  0 allPolicies[i] = (PolicyData)policiesVector.get(i);
 129    }
 130  0 return policiesVector;
 131    }
 132    else
 133  0 return null;
 134    }
 135   
 136    /**
 137    * Returns all polcies for a given namespace
 138    * a) part of objectSpec and objectSpecDesc = Namespace
 139    * b) part of "ObjectGroup" (of which it is a member)
 140    */
 141  0 private static Vector getAllPoliciesForNamespace(String nickName)
 142    throws CasDBException {
 143  0 logger.debug("getAllPolicies for " + nickName);
 144   
 145  0 String errMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 146    "namespace", nickName });
 147   
 148  0 String existsQuery = "select * from" + CasDBConstants.TABLE_NAMESPACE
 149    + " where" + CasDBConstants.COL_NS_NICKNAME + "='"
 150    + nickName + "'";
 151  0 if (!CasDBStorage.rowExists(existsQuery)) {
 152  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 153    "Namespace ", nickName });
 154  0 logger.error(err);
 155  0 throw new CasDBException(err);
 156    }
 157  0 Vector policiesVector = new Vector();
 158    // Namespace data could be in object specification of a policy
 159  0 String query = "select * from" + CasDBConstants.TABLE_POLICY
 160    + " where (" + CasDBConstants.COL_OBJECT_SPEC + "='"
 161    + nickName + "') and (" + CasDBConstants.COL_OBJECT_SPEC_DESC
 162    + "='" + CasConstants.NAMESPACE_SPEC + "')";
 163  0 Connection connection = CasDBStorage.getDBConnection();
 164  0 Statement statement = null;
 165  0 ResultSet resultSet = null;
 166  0 try {
 167  0 statement = connection.createStatement();
 168  0 resultSet = statement.executeQuery(query);
 169  0 Vector temp =
 170    PolicyDataHandler.constructPolicyData(connection, resultSet,
 171    false);
 172  0 if (temp != null) {
 173  0 logger.debug("adding policies");
 174  0 policiesVector.addAll(temp);
 175    }
 176    }
 177    catch (SQLException exp) {
 178  0 logger.error(errMesg + "\n" + query, exp);
 179  0 throw new CasDBException(errMesg + exp.getMessage(), exp);
 180    }
 181    catch (CasDBException exp) {
 182  0 logger.error(exp);
 183  0 throw new CasDBException(errMesg + exp.getMessage(), exp);
 184    }
 185    finally {
 186    // returning connection irrespective of whether stmt
 187    // and result set are closed or not.
 188  0 CasDBStorage.returnDBConnection(connection);
 189  0 try {
 190  0 if (resultSet != null)
 191  0 resultSet.close();
 192  0 if (statement != null)
 193  0 statement.close();
 194    }
 195    catch (SQLException exp) {
 196  0 logger.warn(errMesg, exp);
 197    }
 198    }
 199    // add obj group data
 200  0 String[] groupNames =
 201    ObjectGroupDataHandler.getObjectGroupsForMember(
 202    nickName,
 203    CasConstants.NAMESPACE_SPEC);
 204  0 if (groupNames != null) {
 205  0 for (int i=0; i<groupNames.length; i++) {
 206  0 Vector temp =
 207    getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 208    groupNames[i]);
 209  0 if (temp != null) {
 210  0 logger.debug("adding policies");
 211  0 policiesVector.addAll(temp);
 212    }
 213    }
 214    }
 215  0 if ((policiesVector != null) && (policiesVector.size() > 0))
 216  0 return policiesVector;
 217    else
 218  0 return null;
 219    }
 220   
 221    /**
 222    * Returns all polcies for a given user. Both CAS and External policies
 223    * are retuned.
 224    * b) part of objectSpec and objectSpecDesc = user
 225    * c) part "UserGroup" (of which it is a member), userSpec or objectSpec
 226    * d) part of "ObjectGroup" (of which it is a member)
 227    */
 228  0 private static Vector getAllPoliciesForUser(String userNick)
 229    throws CasDBException {
 230  0 logger.debug("getAllPolicies for " + userNick);
 231  0 String baseErrMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 232    "user", userNick });
 233  0 UserData userData =
 234    (UserData)UserDataHandler.retrieveObject(userNick);
 235  0 if (userData == null) {
 236  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 237    "user ", userNick });
 238  0 logger.error(err);
 239  0 throw new CasDBException(err);
 240    }
 241  0 ArrayOfString arrayOfGpnames = userData.getGroupNames();
 242  0 Vector policiesVector = new Vector();
 243  0 if (arrayOfGpnames != null) {
 244  0 String gpNames[] = arrayOfGpnames.getStrings();
 245  0 logger.debug("User: gpNames is not null " + gpNames.length);
 246  0 for (int i=0; i<gpNames.length; i++) {
 247  0 Vector temp =
 248    getAllPoliciesForGroup(CasConstants.USERGP_SPEC,
 249    gpNames[i]);
 250  0 if (temp != null) {
 251  0 logger.debug("adding policies for "
 252    + gpNames[i]);
 253  0 policiesVector.addAll(temp);
 254    }
 255    }
 256    }
 257    // User data could be in object specification of a policy
 258    // Pick up policy only if it has already not been picked up
 259  0 String query = "select * from" + CasDBConstants.TABLE_POLICY
 260    + " where (" + CasDBConstants.COL_OBJECT_SPEC + "='"
 261    + userNick + "') and (" + CasDBConstants.COL_OBJECT_SPEC_DESC
 262    + "='" + CasConstants.USER_SPEC + "')" ;
 263  0 Connection connection = CasDBStorage.getDBConnection();
 264  0 Statement statement = null;
 265  0 ResultSet resultSet = null;
 266  0 try {
 267  0 statement = connection.createStatement();
 268  0 resultSet = statement.executeQuery(query);
 269  0 Vector temp =
 270    PolicyDataHandler.constructPolicyData(connection, resultSet,
 271    false);
 272  0 if (temp != null) {
 273  0 logger.debug("adding policies");
 274  0 policiesVector.addAll(temp);
 275    }
 276    }
 277    catch (SQLException exp) {
 278  0 logger.error(baseErrMesg + "\n" + query, exp);
 279  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 280    }
 281    catch (CasDBException exp) {
 282  0 logger.error(exp);
 283  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 284    }
 285    finally {
 286    // returning connection irrespective of whether stmt
 287    // and result set are closed or not.
 288  0 CasDBStorage.returnDBConnection(connection);
 289  0 try {
 290  0 if (resultSet != null)
 291  0 resultSet.close();
 292  0 if (statement != null)
 293  0 statement.close();
 294    }
 295    catch (SQLException exp) {
 296  0 logger.warn(baseErrMesg, exp);
 297    }
 298    }
 299    // user could be member of a object group
 300  0 String[] groupNames =
 301    ObjectGroupDataHandler.getObjectGroupsForMember(
 302    userNick,
 303    CasConstants.USER_SPEC);
 304  0 if (groupNames != null) {
 305  0 for (int i=0; i<groupNames.length; i++) {
 306  0 Vector temp =
 307    getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 308    groupNames[i]);
 309  0 if (temp != null) {
 310  0 logger.debug("adding policies");
 311  0 policiesVector.addAll(temp);
 312    }
 313    }
 314    }
 315  0 if (policiesVector.size() > 0)
 316  0 return policiesVector;
 317    else
 318  0 return null;
 319    }
 320   
 321    /**
 322    * Returns all polcies for a given object. Both CAS and External policies
 323    * are returned.
 324    * a) part of objectSpec and objectSpecDesc = object
 325    * b) part of "ObjectGroup" (of which it is a member)
 326    */
 327  0 private static Vector getAllPoliciesForObject(String objectId)
 328    throws CasDBException {
 329  0 logger.debug("getAllPolicies for " + objectId);
 330  0 String baseErrMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 331    "object", objectId });
 332  0 Vector policiesVector = new Vector();
 333    // policies on object group
 334  0 ObjectData objectData =
 335    (ObjectData)ObjectDataHandler.retrieveObjectForId(objectId);
 336  0 if (objectData == null) {
 337  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 338    "object ", objectId});
 339  0 logger.error(err);
 340  0 throw new CasDBException(err);
 341    }
 342  0 ArrayOfString arrayOfGpnames = objectData.getGroupNames();
 343  0 if (arrayOfGpnames != null) {
 344  0 String gpNames[] = arrayOfGpnames.getStrings();
 345  0 logger.debug("gpNames is not null");
 346  0 for (int i=0; i<gpNames.length; i++) {
 347  0 Vector temp =
 348    getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 349    gpNames[i]);
 350  0 if (temp != null) {
 351  0 logger.debug("adding policies for " + gpNames[i]);
 352  0 policiesVector.addAll(temp);
 353    }
 354    }
 355    }
 356   
 357    // policies on object
 358  0 String query = "select * from" + CasDBConstants.TABLE_POLICY
 359    + " where (" + CasDBConstants.COL_OBJECT_SPEC + "='"
 360    + objectId + "') and (" + CasDBConstants.COL_OBJECT_SPEC_DESC
 361    + "='" + CasConstants.OBJECT_SPEC + "')";
 362  0 Connection connection = CasDBStorage.getDBConnection();
 363  0 Statement statement = null;
 364  0 ResultSet resultSet = null;
 365  0 try {
 366  0 statement = connection.createStatement();
 367  0 resultSet = statement.executeQuery(query);
 368  0 Vector temp =
 369    PolicyDataHandler.constructPolicyData(connection, resultSet,
 370    false);
 371  0 if (temp != null) {
 372  0 logger.debug("adding policies");
 373  0 policiesVector.addAll(temp);
 374    }
 375    }
 376    catch (SQLException exp) {
 377  0 logger.error(baseErrMesg + "\n" + query, exp);
 378  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 379    }
 380    catch (CasDBException exp) {
 381  0 logger.error(exp);
 382  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 383    }
 384    finally {
 385    // returning connection irrespective of whether stmt
 386    // and result set are closed or not.
 387  0 CasDBStorage.returnDBConnection(connection);
 388  0 try {
 389  0 if (resultSet != null)
 390  0 resultSet.close();
 391  0 if (statement != null)
 392  0 statement.close();
 393    }
 394    catch (SQLException exp) {
 395  0 logger.warn(baseErrMesg, exp);
 396    }
 397    }
 398  0 if (policiesVector.size() > 0)
 399  0 return policiesVector;
 400    else
 401  0 return null;
 402    }
 403   
 404    /**
 405    * Returns all polcies for a given service. Both CAS and External policies
 406    * are returned.
 407    * a) objectSpec
 408    * b) part of "ObjectGroup" (of which it is a member)
 409    */
 410  0 private static Vector getAllPoliciesForServiceType(String serviceName)
 411    throws CasDBException {
 412  0 logger.debug("getAllPolicies for " + serviceName);
 413  0 String baseErrMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 414    "service type", serviceName });
 415    // Ensure service type exists
 416  0 String existsQuery = "select * from" + CasDBConstants.TABLE_SERVICETYPE
 417    + " where" + CasDBConstants.COL_SERVICETYPE_NAME + "='"
 418    + serviceName + "'";
 419  0 if (!CasDBStorage.rowExists(existsQuery)) {
 420  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 421    "service type", serviceName });
 422  0 logger.error(err);
 423  0 throw new CasDBException(err);
 424    }
 425  0 Vector policiesVector = new Vector();
 426  0 String query = "select * from" + CasDBConstants.TABLE_POLICY
 427    + " where (" + CasDBConstants.COL_OBJECT_SPEC + "='"
 428    + serviceName + "') and ("
 429    + CasDBConstants.COL_OBJECT_SPEC_DESC + "='"
 430    + CasConstants.SERVICETYPE_SPEC + "')";
 431  0 Connection connection = CasDBStorage.getDBConnection();
 432  0 Statement statement = null;
 433  0 ResultSet resultSet = null;
 434  0 try {
 435  0 statement = connection.createStatement();
 436  0 resultSet = statement.executeQuery(query);
 437  0 Vector temp =
 438    PolicyDataHandler.constructPolicyData(connection, resultSet,
 439    false);
 440  0 if (temp != null) {
 441  0 logger.debug("adding policies");
 442  0 policiesVector.addAll(temp);
 443    }
 444    }
 445    catch (SQLException exp) {
 446  0 logger.error(baseErrMesg + "\n" + query, exp);
 447  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 448    }
 449    catch (CasDBException exp) {
 450  0 logger.error(exp);
 451  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 452    }
 453    finally {
 454    // returning connection irrespective of whether stmt
 455    // and result set are closed or not.
 456  0 CasDBStorage.returnDBConnection(connection);
 457  0 try {
 458  0 if (resultSet != null)
 459  0 resultSet.close();
 460  0 if (statement != null)
 461  0 statement.close();
 462    }
 463    catch (SQLException exp) {
 464  0 logger.warn(baseErrMesg, exp);
 465    }
 466    }
 467   
 468    // serviceType could be member of a object group
 469  0 String[] groupNames =
 470    ObjectGroupDataHandler.getObjectGroupsForMember(
 471    serviceName,
 472    CasConstants.SERVICETYPE_SPEC);
 473  0 if (groupNames != null) {
 474  0 for (int i=0; i<groupNames.length; i++) {
 475  0 Vector temp =
 476    getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 477    groupNames[i]);
 478  0 if (temp != null) {
 479  0 logger.debug("adding policies");
 480  0 policiesVector.addAll(temp);
 481    }
 482    }
 483    }
 484  0 if (policiesVector.size() > 0)
 485  0 return policiesVector;
 486    else
 487  0 return null;
 488    }
 489   
 490    /**
 491    * Returns all polcies for a given service/action mapping.
 492    * a) part of actionSpec and actionSpecDesc = serviceTypeAction
 493    * b) part of "ServiceActionGroup" (of which it is a member)
 494    */
 495  0 private static Vector
 496    getAllPoliciesForServiceAction(String serviceActionId)
 497    throws CasDBException {
 498  0 logger.debug("getAllPolicies for " + serviceActionId);
 499   
 500  0 String baseErrMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 501    "service type/action", serviceActionId });
 502    // Ensure service type exists
 503  0 String existsQuery = "select * from"
 504    + CasDBConstants.TABLE_SERVICETYPE_ACTION
 505    + " where" + CasDBConstants.COL_SERVICEACTION_ID + "='"
 506    + serviceActionId + "'";
 507  0 if (!CasDBStorage.rowExists(existsQuery)) {
 508  0 String err = i18n.getMessage("doesNotExist", new Object[] {
 509    "Service Action id ", serviceActionId });
 510  0 logger.error(err);
 511  0 throw new CasDBException(err);
 512    }
 513  0 Vector policiesVector = new Vector();
 514  0 String query = "select * from" + CasDBConstants.TABLE_POLICY
 515    + " where (" + CasDBConstants.COL_ACTION_SPEC_DESC + "='"
 516    + CasConstants.SERVICEACTION_SPEC + "') and ("
 517    + CasDBConstants.COL_ACTION_SPEC + "='" + serviceActionId + "')";
 518  0 Connection connection = CasDBStorage.getDBConnection();
 519  0 Statement statement = null;
 520  0 ResultSet resultSet = null;
 521  0 try {
 522  0 statement = connection.createStatement();
 523  0 resultSet = statement.executeQuery(query);
 524  0 Vector temp =
 525    PolicyDataHandler.constructPolicyData(connection, resultSet,
 526    false);
 527  0 if (temp != null) {
 528  0 logger.debug("adding policies");
 529  0 policiesVector.addAll(temp);
 530    }
 531    }
 532    catch (SQLException exp) {
 533  0 logger.error(baseErrMesg + "\n" + query, exp);
 534  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 535    }
 536    catch (CasDBException exp) {
 537  0 logger.error(exp);
 538  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 539    }
 540    finally {
 541    // returning connection irrespective of whether stmt
 542    // and result set are closed or not.
 543  0 CasDBStorage.returnDBConnection(connection);
 544  0 try {
 545  0 if (resultSet != null)
 546  0 resultSet.close();
 547  0 if (statement != null)
 548  0 statement.close();
 549    }
 550    catch (SQLException exp) {
 551  0 logger.warn(baseErrMesg, exp);
 552    }
 553    }
 554   
 555    // Retrieve groups for these
 556  0 String gpNames[] = ServiceTypeActionHandler
 557    .retrieveServiceActionGroups(serviceActionId);
 558  0 if (gpNames != null) {
 559  0 for (int j=0; j<gpNames.length; j++) {
 560  0 Vector temp =
 561    getAllPoliciesForGroup(CasConstants.SERVICEACTIONGP_SPEC,
 562    gpNames[j]);
 563  0 if (temp != null) {
 564  0 logger.debug("adding action group policies");
 565  0 policiesVector.addAll(temp);
 566    }
 567    }
 568    }
 569  0 if (policiesVector.size() > 0)
 570  0 return policiesVector;
 571    else
 572  0 return null;
 573    }
 574   
 575    /**
 576    * Returns all polcies for a given userGroup/obejctGroup/serviceActionGroup
 577    * Both CAS and External policies are returned
 578    */
 579  0 private static Vector getAllPoliciesForGroup(String type, String groupName)
 580    throws CasDBException {
 581   
 582  0 logger.debug("type " + type + "groupName " + groupName);
 583  0 String baseErrMesg = i18n.getMessage("retrPolicyFor", new Object[] {
 584    "group", "" });
 585  0 String queryInit = "select * from" + CasDBConstants.TABLE_POLICY
 586    + " where ";
 587  0 String queryTail = null;
 588  0 String existsQuery = null;
 589  0 if (type.equals(CasConstants.USERGP_SPEC)) {
 590  0 logger.debug("usergroup processing");
 591  0 queryTail = "(" + CasDBConstants.COL_USERGP_NAME + "='"
 592    + groupName + "') or ((" + CasDBConstants.COL_OBJECT_SPEC
 593    + "='" + groupName + "') and ("
 594    + CasDBConstants.COL_OBJECT_SPEC_DESC + "='"
 595    + CasConstants.USERGP_SPEC + "'))";
 596  0 existsQuery = "select * from" + CasDBConstants.TABLE_USERGP
 597    + " where" + CasDBConstants.COL_USERGP_NAME + "='"
 598    + groupName + "'";
 599    }
 600  0 else if (type.equals(CasConstants.OBJECTGP_SPEC)) {
 601  0 logger.debug("obejct group");
 602  0 queryTail = "(" + CasDBConstants.COL_OBJECT_SPEC + "='"
 603    + groupName + "') and (" + CasDBConstants.COL_OBJECT_SPEC_DESC
 604    + "='" + CasConstants.OBJECTGP_SPEC + "')";
 605  0 existsQuery = "select * from" + CasDBConstants.TABLE_OBJECTGP
 606    + " where" + CasDBConstants.COL_OBJECTGP_NAME + "='"
 607    + groupName + "'";
 608  0 } else if (type.equals(CasConstants.SERVICEACTIONGP_SPEC)) {
 609  0 logger.debug("service action group");
 610  0 queryTail = "((" + CasDBConstants.COL_ACTION_SPEC + "='"
 611    + groupName + "') and (" + CasDBConstants.COL_ACTION_SPEC_DESC
 612    + "='" + CasConstants.SERVICEACTIONGP_SPEC + "')) or (("
 613    + CasDBConstants.COL_OBJECT_SPEC + "='" + groupName
 614    + "') and (" + CasDBConstants.COL_OBJECT_SPEC_DESC + "='"
 615    + CasConstants.SERVICEACTIONGP_SPEC + "'))";
 616  0 existsQuery = "select * from"
 617    + CasDBConstants.TABLE_SERVICEACTIONGP + " where"
 618    + CasDBConstants.COL_SERVICEACTIONGP_NAME + "='"
 619    + groupName + "'";
 620    } else {
 621  0 String errMesg = "Type must be " + CasConstants.USERGP_SPEC
 622    + " " + CasConstants.OBJECTGP_SPEC + " "
 623    + CasConstants.SERVICEACTIONGP_SPEC;
 624  0 logger.error(errMesg);
 625  0 throw new CasDBException(baseErrMesg + errMesg);
 626    }
 627   
 628  0 if (!CasDBStorage.rowExists(existsQuery)) {
 629  0 String errMesg = type + "group " + groupName
 630    + " does not exist";
 631  0 logger.error(errMesg);
 632  0 throw new CasDBException(baseErrMesg + errMesg);
 633    }
 634   
 635  0 String query = queryInit + queryTail;
 636  0 logger.debug("Query is "+ query);
 637  0 Connection connection = CasDBStorage.getDBConnection();
 638  0 Vector policies = null;
 639  0 Statement statement = null;
 640  0 ResultSet resultSet = null;
 641  0 try {
 642  0 statement = connection.createStatement();
 643  0 resultSet = statement.executeQuery(query);
 644  0 policies =
 645    PolicyDataHandler.constructPolicyData(connection, resultSet,
 646    false);
 647    }
 648    catch (SQLException exp) {
 649  0 logger.error(baseErrMesg + "\n" + query, exp);
 650  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 651    }
 652    catch (CasDBException exp) {
 653  0 logger.error(exp);
 654  0 throw new CasDBException(baseErrMesg + exp.getMessage(), exp);
 655    }
 656    finally {
 657    // returning connection irrespective of whether stmt
 658    // and result set are closed or not.
 659  0 CasDBStorage.returnDBConnection(connection);
 660  0 try {
 661  0 if (resultSet != null)
 662  0 resultSet.close();
 663  0 if (statement != null)
 664  0 statement.close();
 665    }
 666    catch (SQLException exp) {
 667  0 logger.warn(baseErrMesg, exp);
 668    }
 669    }
 670   
 671    // UserGp can be a part of objectGp
 672  0 if (type.equals(CasConstants.USERGP_SPEC)) {
 673  0 logger.debug("User group can be part of object group");
 674  0 String[] groupNames =
 675    ObjectGroupDataHandler.getObjectGroupsForMember(
 676    groupName,
 677    CasConstants.USERGP_SPEC);
 678  0 if (groupNames != null) {
 679  0 for (int i=0; i<groupNames.length; i++) {
 680  0 Vector temp =
 681    getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 682    groupName);
 683  0 if (temp != null) {
 684  0 logger.debug("adding policies");
 685  0 policies.addAll(temp);
 686    }
 687   
 688    }
 689    }
 690    }
 691  0 return policies;
 692    }
 693   
 694    /**
 695    * Returns all applicable policies for a given type and name
 696    */
 697  0 public static PolicyData[] getAllApplicablePolicy(String type,
 698    String name)
 699    throws CasDBException {
 700  0 Vector policyVector = null;
 701  0 if (type.trim().equals(CasConstants.USER_SPEC)) {
 702  0 logger.debug("Policy for user " + name);
 703  0 policyVector = getAllPoliciesForUser(name.trim());
 704  0 } else if (type.trim().equals(CasConstants.USERGP_SPEC)) {
 705  0 logger.debug("Policy for user group" + name);
 706  0 policyVector = getAllPoliciesForGroup(CasConstants.USERGP_SPEC,
 707    name.trim());
 708  0 } else if (type.trim().equals(CasConstants.OBJECT_SPEC)) {
 709  0 logger.debug("Policy for object group" + name);
 710  0 policyVector = getAllPoliciesForObject(name.trim());
 711  0 } else if (type.trim().equals(CasConstants.OBJECTGP_SPEC)) {
 712  0 logger.debug("Policy for object group" + name);
 713  0 policyVector = getAllPoliciesForGroup(CasConstants.OBJECTGP_SPEC,
 714    name.trim());
 715  0 } else if (type.trim().equals(CasConstants.SERVICETYPE_SPEC)) {
 716  0 logger.debug("Policy for service type" + name);
 717  0 policyVector = getAllPoliciesForServiceType(name.trim());
 718  0 } else if (type.trim().equals(CasConstants.SERVICEACTIONGP_SPEC)) {
 719  0 logger.debug("Policy for service/action gp" + name);
 720  0 policyVector =
 721    getAllPoliciesForGroup(CasConstants.SERVICEACTIONGP_SPEC,
 722    name.trim());
 723  0 } else if (type.trim().equals(CasConstants.TRUSTANCHOR_SPEC)) {
 724  0 logger.debug("Policy for trust anchor" + name);
 725  0 policyVector = getAllPoliciesForTrustAnchor(name.trim());
 726  0 } else if (type.trim().equals(CasConstants.NAMESPACE_SPEC)) {
 727  0 logger.debug("Policy for namespace" + name);
 728  0 policyVector = getAllPoliciesForNamespace(name.trim());
 729  0 } else if (type.trim().equals(CasConstants.SERVICEACTION_SPEC)) {
 730  0 logger.debug("Policy for serviceAction" + name);
 731  0 policyVector = getAllPoliciesForServiceAction(name.trim());
 732    } else {
 733  0 String err = i18n.getMessage("retrPolicyFor", new Object[]
 734    { "erroneous type", type.trim() });
 735  0 logger.error(err);
 736  0 throw new CasDBException(err);
 737    }
 738  0 if (policyVector != null) {
 739  0 PolicyData[] allPolicies = new PolicyData[policyVector.size()];
 740  0 logger.debug("Policies vector size " + policyVector.size());
 741  0 for (int i=0; i<policyVector.size(); i++) {
 742  0 logger.debug("polices " + i);
 743  0 PolicyData externalPolicy =
 744    replaceInternalIds((PolicyData)policyVector.get(i));
 745  0 allPolicies[i] = externalPolicy;
 746    }
 747  0 return allPolicies;
 748    }
 749  0 return null;
 750    }
 751   
 752  0 private static PolicyData replaceInternalIds(PolicyData policyData)
 753    throws CasDBException {
 754   
 755  0 if (policyData == null)
 756  0 return null;
 757   
 758  0 if (policyData.getActionSpecDesc().equals(
 759    CasConstants.SERVICEACTION_SPEC)) {
 760  0 String actionSpec = policyData.getActionSpec();
 761  0 String serviceAction =
 762    ServiceTypeActionHandler.getServiceActionMapping(actionSpec);
 763  0 policyData.setActionSpec(serviceAction);
 764    }
 765   
 766  0 if (policyData.getObjectSpecDesc().equals(CasConstants.OBJECT_SPEC)) {
 767  0 String objSpec = policyData.getObjectSpec();
 768  0 String objNsName =
 769    ObjectDataHandler.getObjectDescription(objSpec);
 770  0 policyData.setObjectSpec(objNsName);
 771    }
 772  0 return policyData;
 773    }
 774    }