1 40 41 package org.jahia.services.acl; 42 43 import java.sql.Connection ; 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 import java.sql.Statement ; 48 import java.util.ArrayList ; 49 import java.util.Enumeration ; 50 import java.util.Hashtable ; 51 import java.util.Vector ; 52 53 import org.apache.log4j.Logger; 54 import org.jahia.data.JahiaDBDOMObject; 55 import org.jahia.data.JahiaDOMObject; 56 import org.jahia.data.fields.FieldTypes; 57 import org.jahia.exceptions.JahiaException; 58 import org.jahia.exceptions.JahiaInitializationException; 59 import org.jahia.exceptions.database.JahiaDatabaseException; 60 import org.jahia.registries.ServicesRegistry; 61 import org.jahia.services.cache.Cache; 62 import org.jahia.services.cache.CacheFactory; 63 import org.jahia.services.cache.CacheListener; 64 import org.jahia.utils.DBRowDataFilter; 65 66 75 class AclDBUtils implements ACLInfo, CacheListener { 76 protected static Logger logger = Logger.getLogger (AclDBUtils.class); 77 78 79 private static String JAHIA_ACL = "jahia_acl"; 80 81 82 private static String FIELD_ACL_ID = "id_jahia_acl"; 83 84 85 private static String FIELD_PARENT_ID = "parent_id_jahia_acl"; 86 87 88 private static String FIELD_INHERITANCE = "inheritance_jahia_acl"; 89 90 91 private static String JAHIA_ACL_ENTRIES = "jahia_acl_entries"; 92 93 94 private static String FIELD_ENTRY_ACL_ID = "id_jahia_acl"; 95 96 97 private static String FIELD_ENTRY_TYPE = "type_jahia_acl_entries"; 98 99 100 private static String FIELD_ENTRY_TARGET = "target_jahia_acl_entries"; 101 102 103 private static String FIELD_ENTRY_STATE = "entry_state_jahia_acl_entries"; 104 105 106 private static String FIELD_ENTRY_TRISTATE = "entry_trist_jahia_acl_entries"; 107 108 private static final int CHUNK_SIZE = 500; 109 110 111 static private AclDBUtils instance = null; 112 113 public static final String ACL_ENTRIES_CACHE = "ACLEntriesCache"; 115 116 Cache cacheACLEntries = null; 117 118 123 private AclDBUtils () 124 throws JahiaDatabaseException, JahiaInitializationException { 125 126 cacheACLEntries = CacheFactory.createCache (ACL_ENTRIES_CACHE); 129 cacheACLEntries.registerListener(this); 130 cacheAllACLEntries (); 131 } 132 133 134 139 public static synchronized AclDBUtils getInstance () { 140 try { 141 if (instance == null) { 142 instance = new AclDBUtils (); 143 } 144 145 } catch (JahiaDatabaseException ex) { 146 logger.warn ("!!!!!!! Problem reading ACL entries from database!", ex); 147 return null; 148 149 } catch (JahiaInitializationException ex) { 150 logger.warn ("An initialization occured while dating the ACL Cache Babe! :-/", ex); 151 return null; 152 } 153 154 return instance; 155 } 156 157 public JahiaACL getACL (int aclID) throws JahiaException { 158 159 JahiaACL result = null; 160 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 162 if (dbConn == null) { 163 return null; 164 } 165 Statement statement = null; 166 StringBuffer query = new StringBuffer (); 167 try { 168 statement = dbConn.createStatement (); 170 query.append ("SELECT * "); 171 query.append ("FROM "); 172 query.append (JAHIA_ACL); 173 query.append (" WHERE "); 174 query.append (FIELD_ACL_ID); 175 query.append ("="); 176 query.append (aclID); 177 ResultSet rs = statement.executeQuery (query.toString ()); 178 if (rs != null) { 179 if (rs.next ()) { 180 int parentID = rs.getInt (FIELD_PARENT_ID); 181 JahiaACL acl = new JahiaACL (aclID, parentID, 182 rs.getInt (FIELD_INHERITANCE)); 183 acl.setUserEntries (getACLEntries (aclID, USER_TYPE_ENTRY)); 184 acl.setGroupEntries (getACLEntries (aclID, GROUP_TYPE_ENTRY)); 185 result = acl; 186 } else { 187 logger.debug ("ACL " + aclID + " not found in database !"); 188 throw new JahiaException ("ACL " + aclID + " not found in database !", "ACL " + aclID + " not found in database !", JahiaException.ACL_ERROR 189 , JahiaException.ERROR_SEVERITY); 190 } 191 } 192 } catch (SQLException ex) { 193 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", 195 ex); 196 197 } finally { 198 query = null; 199 closeStatement (statement); 200 } 201 return result; 202 } 203 204 211 public void readAllACLs (Cache allACLs) throws JahiaException { 212 213 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 215 if (dbConn == null) { 216 return; 217 } 218 219 PreparedStatement statement = null; 220 StringBuffer query = new StringBuffer (); 221 try { 222 query.append ("SELECT * "); 224 query.append ("FROM "); 225 query.append (JAHIA_ACL); 226 query.append (" ORDER BY "); 227 query.append (FIELD_PARENT_ID); 228 query.append (", "); 229 query.append (FIELD_ACL_ID); 230 statement = dbConn.prepareStatement (query.toString ()); 231 ResultSet rs = statement.executeQuery (); 232 Hashtable relicatACL = new Hashtable (); 234 if (rs != null) { 235 while (rs.next ()) { 236 int aclID = rs.getInt (FIELD_ACL_ID); 237 int parentID = rs.getInt (FIELD_PARENT_ID); 238 239 JahiaACL parentACL = (JahiaACL) allACLs.get (new Integer (parentID)); 241 if ((parentACL == null) && (parentID > 0)) { 242 relicatACL.put (new Integer (aclID), new Integer (parentID)); 243 } 244 245 JahiaACL acl = new JahiaACL (aclID, parentID, 246 rs.getInt (FIELD_INHERITANCE)); 247 acl.setUserEntries (getACLEntries (aclID, USER_TYPE_ENTRY)); 248 acl.setGroupEntries (getACLEntries (aclID, GROUP_TYPE_ENTRY)); 249 allACLs.put (new Integer (aclID), acl); 250 } 251 } 252 while (!relicatACL.isEmpty ()) { 254 Enumeration enumeration = relicatACL.keys (); 255 int rest = relicatACL.size (); 256 while (enumeration.hasMoreElements ()) { 257 Integer aclID = (Integer ) enumeration.nextElement (); 258 Integer parentID = (Integer ) relicatACL.get (aclID); 259 260 JahiaACL parentACL = (JahiaACL) allACLs.get (parentID); 261 if (parentACL != null) { 263 JahiaACL acl = (JahiaACL) allACLs.get (aclID); 264 if (acl != null) { 265 acl.setParentACL (parentACL, false); 266 } 267 relicatACL.remove (aclID); 268 } 269 if (!enumeration.hasMoreElements () && rest == relicatACL.size ()) { 271 relicatACL.remove (aclID); 272 logger.debug ("WARNING !!! Parent ACL #" + parentID.toString () + 273 " does not exist for ACL #" + aclID.toString ()); 274 } 275 } 276 } 277 relicatACL = null; 278 } catch (SQLException ex) { 279 logger.warn ("!!! DB error, can't cache acls : " + ex.getMessage () + ". Query=[" + 280 query + "]", ex); 281 } finally { 283 query = null; 284 closeStatement (statement); 285 } 286 } 287 288 298 public void preloadACLs (int preloadCount, Cache aclCache) 299 throws JahiaException { 300 301 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 303 if (dbConn == null) { 304 return; 305 } 306 PreparedStatement statement = null; 307 StringBuffer query = new StringBuffer (); 308 try { 309 query.append ("SELECT * "); 311 query.append ("FROM "); 312 query.append (JAHIA_ACL); 313 query.append (" ORDER BY "); 314 query.append (FIELD_PARENT_ID); 315 query.append (", "); 316 query.append (FIELD_ACL_ID); 317 statement = dbConn.prepareStatement (query.toString ()); 318 ResultSet rs = statement.executeQuery (); 319 int resultCount = 0; 320 if (rs != null) { 321 while (rs.next () && (resultCount < preloadCount)) { 322 int aclID = rs.getInt (FIELD_ACL_ID); 323 int parentID = rs.getInt (FIELD_PARENT_ID); 324 JahiaACL acl = new JahiaACL (aclID, parentID, 325 rs.getInt (FIELD_INHERITANCE)); 326 acl.setUserEntries (getACLEntries (aclID, USER_TYPE_ENTRY)); 327 acl.setGroupEntries (getACLEntries (aclID, GROUP_TYPE_ENTRY)); 328 aclCache.put (new Integer (aclID), acl); 329 resultCount++; 330 } 331 } 332 } catch (SQLException ex) { 333 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", ex); 335 336 } finally { 337 query = null; 338 closeStatement (statement); 339 } 340 } 341 342 351 public void preloadPageACLs (Cache aclCache) 352 throws JahiaException { 353 354 logger.debug("Preloading Page ACL"); 355 356 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 358 if (dbConn == null) { 359 return; 360 } 361 int count = 0; 362 PreparedStatement statement = null; 363 StringBuffer query = new StringBuffer (); 364 try { 365 query.append ("SELECT DISTINCT b.id_jahia_acl, b.parent_id_jahia_acl, b.inheritance_jahia_acl "); 366 query.append ("FROM jahia_pages_data a, jahia_acl b "); 367 query.append ("WHERE (a.rights_jahia_pages_data=b.id_jahia_acl)"); 368 statement = dbConn.prepareStatement (query.toString ()); 369 ResultSet rs = statement.executeQuery (); 370 371 if (rs != null) { 372 while (rs.next()) { 373 int aclID = rs.getInt(1); 374 int parentID = rs.getInt (2); 375 if ( !aclCache.containsKey(new Integer (aclID)) ){ 376 JahiaACL acl = new JahiaACL(aclID, parentID, 377 rs.getInt(3)); 378 acl.setUserEntries(getACLEntries(aclID, USER_TYPE_ENTRY)); 379 acl.setGroupEntries(getACLEntries(aclID, 380 GROUP_TYPE_ENTRY)); 381 aclCache.put(new Integer (aclID), acl); 382 count++; 383 } 384 } 385 } 386 } catch (SQLException ex) { 387 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", ex); 389 390 } finally { 391 logger.debug("preloading Page ACL, has loaded " + count + " ACLs in cache"); 392 query = null; 393 closeStatement (statement); 394 } 395 } 396 397 406 public void preloadPageFieldACLs (Cache aclCache) 407 throws JahiaException { 408 409 logger.debug("Preloading Page Field ACL"); 410 411 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 413 if (dbConn == null) { 414 return; 415 } 416 int count = 0; 417 PreparedStatement statement = null; 418 StringBuffer query = new StringBuffer (); 419 try { 420 query.append ("SELECT DISTINCT b.id_jahia_acl, b.parent_id_jahia_acl, b.inheritance_jahia_acl "); 421 query.append ("FROM jahia_fields_data a, jahia_acl b "); 422 query.append ("WHERE (a.type_jahia_fields_data="); 423 query.append (FieldTypes.PAGE); 424 query.append(" AND a.rights_jahia_fields_data=b.id_jahia_acl)"); 425 statement = dbConn.prepareStatement (query.toString ()); 426 ResultSet rs = statement.executeQuery (); 427 428 if (rs != null) { 429 while (rs.next()) { 430 int aclID = rs.getInt (1); 431 int parentID = rs.getInt (2); 432 if ( !aclCache.containsKey(new Integer (aclID)) ){ 433 JahiaACL acl = new JahiaACL(aclID, parentID, 434 rs.getInt(3)); 435 acl.setUserEntries(getACLEntries(aclID, USER_TYPE_ENTRY)); 436 acl.setGroupEntries(getACLEntries(aclID, 437 GROUP_TYPE_ENTRY)); 438 aclCache.put(new Integer (aclID), acl); 439 count++; 440 } 441 } 442 } 443 } catch (SQLException ex) { 444 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", ex); 446 447 } finally { 448 logger.debug("preloading Page Field ACL, has loaded " + count + " ACLs in cache"); 449 query = null; 450 closeStatement (statement); 451 } 452 } 453 454 463 public void preloadContainerListACLs (Cache aclCache) 464 throws JahiaException { 465 466 logger.debug("Preloading Container List ACL"); 467 468 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 470 if (dbConn == null) { 471 return; 472 } 473 int count = 0; 474 PreparedStatement statement = null; 475 StringBuffer query = new StringBuffer (); 476 try { 477 query.append ("SELECT DISTINCT b.id_jahia_acl, b.parent_id_jahia_acl, b.inheritance_jahia_acl "); 478 query.append ("FROM jahia_ctn_lists a, jahia_acl b "); 479 query.append ("WHERE (a.rights_jahia_ctn_lists=b.id_jahia_acl)"); 480 statement = dbConn.prepareStatement (query.toString ()); 481 ResultSet rs = statement.executeQuery (); 482 483 if (rs != null) { 484 while (rs.next()) { 485 int aclID = rs.getInt (1); 486 int parentID = rs.getInt (2); 487 if ( !aclCache.containsKey(new Integer (aclID)) ){ 488 JahiaACL acl = new JahiaACL(aclID, parentID, 489 rs.getInt(3)); 490 acl.setUserEntries(getACLEntries(aclID, USER_TYPE_ENTRY)); 491 acl.setGroupEntries(getACLEntries(aclID, 492 GROUP_TYPE_ENTRY)); 493 aclCache.put(new Integer (aclID), acl); 494 count++; 495 } 496 } 497 } 498 } catch (SQLException ex) { 499 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", ex); 501 502 } finally { 503 logger.debug("preloading Container List ACL, has loaded " + count + " ACLs in cache"); 504 query = null; 505 closeStatement (statement); 506 } 507 } 508 509 519 public synchronized void preloadContainerACLsByPage (int pageID, 520 Cache aclCache) throws JahiaException { 521 logger.debug("Preloading Container ACL of page " + pageID); 522 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 524 if (dbConn == null || pageID<1) { 525 return; 526 } 527 int count = 0; 528 PreparedStatement statement = null; 529 StringBuffer query = new StringBuffer (); 530 try { 531 query.append ("SELECT DISTINCT b.id_jahia_acl, b.parent_id_jahia_acl, b.inheritance_jahia_acl "); 533 query.append ("FROM jahia_ctn_entries a, jahia_acl b "); 534 query.append ("WHERE (a.pageid_jahia_ctn_entries=?"); 535 query.append(" AND a.rights_jahia_ctn_entries=b.id_jahia_acl)"); 536 statement = dbConn.prepareStatement (query.toString ()); 537 statement.setInt(1, pageID); 538 ResultSet rs = statement.executeQuery (); 539 540 if (rs != null) { 541 while (rs.next()) { 542 int aclID = rs.getInt (1); 543 int parentID = rs.getInt (2); 544 if ( !aclCache.containsKey(new Integer (aclID)) ){ 545 JahiaACL acl = new JahiaACL(aclID, parentID, 546 rs.getInt(3)); 547 acl.setUserEntries(getACLEntries(aclID, USER_TYPE_ENTRY)); 548 acl.setGroupEntries(getACLEntries(aclID, 549 GROUP_TYPE_ENTRY)); 550 aclCache.put( new Integer (aclID), acl); 551 count++; 552 } 553 } 554 } 555 } catch (SQLException ex) { 556 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", ex); 558 559 } finally { 560 logger.debug("preloading Container ACL for page " + pageID + " has loaded " + count + " ACLs in cache"); 561 query = null; 562 closeStatement (statement); 563 } 564 } 565 566 576 public synchronized void preloadFieldACLsByPage (int pageID, 577 Cache aclCache) throws JahiaException { 578 579 logger.debug("Preloading Field ACL of page " + pageID); 580 581 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 583 if (dbConn == null && pageID<1) { 584 return; 585 } 586 int count = 0; 587 PreparedStatement statement = null; 588 StringBuffer query = new StringBuffer (); 589 try { 590 query.append ("SELECT DISTINCT b.id_jahia_acl, b.parent_id_jahia_acl, b.inheritance_jahia_acl "); 592 query.append ("FROM jahia_fields_data a, jahia_acl b "); 593 query.append ("WHERE (a.pageid_jahia_fields_data=?"); 594 query.append(" AND a.rights_jahia_fields_data=b.id_jahia_acl)"); 595 statement = dbConn.prepareStatement (query.toString ()); 596 statement.setInt(1, pageID); 597 ResultSet rs = statement.executeQuery (); 598 if (rs != null) { 599 while (rs.next()) { 600 int aclID = rs.getInt (1); 601 int parentID = rs.getInt (2); 602 if ( !aclCache.containsKey(new Integer (aclID)) ){ 603 JahiaACL acl = new JahiaACL(aclID, parentID, 604 rs.getInt(3)); 605 acl.setUserEntries(getACLEntries(aclID, USER_TYPE_ENTRY)); 606 acl.setGroupEntries(getACLEntries(aclID, 607 GROUP_TYPE_ENTRY)); 608 aclCache.put(new Integer (aclID), acl); 609 } 610 } 611 } 612 } catch (SQLException ex) { 613 logger.warn ("!!! DB error, can't cache acls : " + ex + ". Query=[" + query + "]", ex); 615 616 } finally { 617 logger.debug("preloading Field ACL for page " + pageID + " has loaded " + count + " ACLs in cache"); 618 query = null; 619 closeStatement (statement); 620 } 621 } 622 623 640 public boolean addACLEntry (int aclID, int typeID, String targetID, 641 int state, int tristate) 642 throws JahiaDatabaseException { 643 644 645 StringBuffer query = new StringBuffer (); 646 query.append ("INSERT INTO "); 647 query.append (JAHIA_ACL_ENTRIES); 648 query.append (" ("); 649 query.append (FIELD_ENTRY_ACL_ID); 650 query.append (","); 651 query.append (FIELD_ENTRY_TYPE); 652 query.append (","); 653 query.append (FIELD_ENTRY_TARGET); 654 query.append ("," + FIELD_ENTRY_STATE); 655 query.append (","); 656 query.append (FIELD_ENTRY_TRISTATE); 657 query.append (")"); 658 query.append (" VALUES ("); 659 query.append (aclID); 660 query.append (","); 661 query.append (typeID); 662 query.append (",'"); 663 query.append (targetID); 664 query.append ("',"); 665 query.append (state); 666 query.append (","); 667 query.append (tristate); 668 query.append (")"); 669 670 boolean result = makeQuery (query.toString ()); 671 if (result) { 672 JahiaACLEntry entry = new JahiaACLEntry (state, tristate); 673 Hashtable cachedType = null; 674 Hashtable cachedACL = (Hashtable ) cacheACLEntries.get (new Integer (aclID)); 675 if (cachedACL == null) { 676 cachedACL = new Hashtable (); 677 cacheACLEntries.put (new Integer (aclID), cachedACL); 678 } 679 cachedType = (Hashtable ) cachedACL.get (new Integer (typeID)); 680 if (cachedType == null) { 681 cachedType = new Hashtable (); 682 cachedACL.put (new Integer (typeID), cachedType); 683 } 684 cachedType.put (targetID, entry); 685 } 686 query = null; 687 return result; 688 } 689 690 691 708 public boolean updateACLEntry (int aclID, int typeID, String targetID, 709 int state, int tristate) 710 throws JahiaDatabaseException { 711 StringBuffer query = new StringBuffer ("UPDATE "); 712 query.append (JAHIA_ACL_ENTRIES); 713 query.append (" SET "); 714 query.append (FIELD_ENTRY_STATE); 715 query.append ("="); 716 query.append (state); 717 query.append (","); 718 query.append (FIELD_ENTRY_TRISTATE); 719 query.append ("="); 720 query.append (tristate); 721 query.append (" WHERE (("); 722 query.append (FIELD_ENTRY_ACL_ID); 723 query.append ("="); 724 query.append (aclID); 725 query.append (")"); 726 query.append (" AND ("); 727 query.append (FIELD_ENTRY_TYPE); 728 query.append ("="); 729 query.append (typeID); 730 query.append (")"); 731 query.append (" AND ("); 732 query.append (FIELD_ENTRY_TARGET); 733 query.append ("='"); 734 query.append (targetID); 735 query.append ("'))"); 736 737 boolean result = makeQuery (query.toString ()); 738 if (result) { 739 JahiaACLEntry entry = new JahiaACLEntry (state, tristate); 740 Hashtable cachedType = null; 741 Hashtable cachedACL = (Hashtable ) cacheACLEntries.get (new Integer (aclID)); 742 if (cachedACL == null) { 743 cachedACL = new Hashtable (); 744 cacheACLEntries.put (new Integer (aclID), cachedACL); 745 } 746 cachedType = (Hashtable ) cachedACL.get (new Integer (typeID)); 747 if (cachedType == null) { 748 cachedType = new Hashtable (); 749 cachedACL.put (new Integer (typeID), cachedType); 750 } 751 cachedType.put (targetID, entry); 752 } 753 query = null; 754 return result; 755 } 756 757 758 774 public boolean removeACLEntry (int aclID, int typeID, String targetID) 775 throws JahiaDatabaseException { 776 Hashtable cachedType = null; 779 Hashtable cachedACL = (Hashtable ) cacheACLEntries.get (new Integer (aclID)); 780 boolean result = true; 781 if (cachedACL != null) { 782 cachedType = (Hashtable ) cachedACL.get (new Integer (typeID)); 783 if (cachedType != null) { 784 StringBuffer query = new StringBuffer ("DELETE FROM "); 785 query.append (JAHIA_ACL_ENTRIES); 786 query.append (" WHERE "); 787 query.append (FIELD_ENTRY_ACL_ID); 788 query.append ("="); 789 query.append (aclID); 790 query.append (" AND "); 791 query.append (FIELD_ENTRY_TYPE); 792 query.append ("="); 793 query.append (typeID); 794 query.append (" AND "); 795 query.append (FIELD_ENTRY_TARGET); 796 query.append ("='"); 797 query.append (targetID); 798 query.append ("'"); 799 result = makeQuery (query.toString ()); 800 801 if (result) { 802 cachedType.remove (targetID); 803 } 804 } 805 } 806 return result; 807 } 808 809 810 823 public boolean removeACLEntries (int aclID, int typeID) 824 throws JahiaDatabaseException { 825 StringBuffer query = new StringBuffer ("DELETE FROM "); 826 query.append (JAHIA_ACL_ENTRIES); 827 query.append (" WHERE "); 828 query.append (FIELD_ENTRY_ACL_ID); 829 query.append ("="); 830 query.append (aclID); 831 query.append (" AND "); 832 query.append (FIELD_ENTRY_TYPE); 833 query.append ("="); 834 query.append (typeID); 835 836 boolean result = makeQuery (query.toString ()); 837 if (result) { 838 Hashtable cachedACL = (Hashtable ) cacheACLEntries.get (new Integer (aclID)); 839 if (cachedACL != null) { 840 cachedACL.remove (new Integer (typeID)); 841 } 842 } 843 query = null; 844 return result; 845 } 846 847 848 859 public Hashtable getACLEntries (int aclID, int typeID) { 860 try { 861 if (cacheACLEntries.isEmpty()) { 864 cacheAllACLEntries (); 865 } 866 } catch (JahiaDatabaseException jde) { 867 logger.debug ("Error while loading ACL entries for ACL " + aclID, 868 jde); 869 } 870 871 Hashtable aclEntries = (Hashtable ) cacheACLEntries.get (new Integer (aclID)); 873 if (aclEntries != null) { 874 Hashtable typeEntries = (Hashtable ) aclEntries.get (new Integer (typeID)); 875 if (typeEntries != null) { 876 return typeEntries; 877 } 878 } else { 879 896 } 897 return new Hashtable (); 899 } 900 901 private void loadACLEntries (int aclID) 902 throws JahiaDatabaseException { 903 904 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 906 if (dbConn == null) { 907 throw new JahiaDatabaseException ( 908 "ACL entry loading process could not get a database connection.", 909 JahiaDatabaseException.CRITICAL_SEVERITY); 910 } 911 912 PreparedStatement statement = null; 913 StringBuffer query = new StringBuffer (); 914 try { 915 916 query = new StringBuffer (); 917 query.append ("SELECT * FROM "); 918 query.append (JAHIA_ACL_ENTRIES); 919 query.append (" WHERE "); 920 query.append (FIELD_ENTRY_ACL_ID); 921 query.append ("=?"); 922 923 statement = dbConn.prepareStatement (query.toString ()); 925 statement.setInt (1, aclID); 926 927 ResultSet rs = statement.executeQuery (); 928 if (rs != null) { 929 while (rs.next ()) { 930 String target = rs.getString (FIELD_ENTRY_TARGET); 931 int state = rs.getInt (FIELD_ENTRY_STATE); 932 int tristate = rs.getInt (FIELD_ENTRY_TRISTATE); 933 int id = rs.getInt (FIELD_ENTRY_ACL_ID); 934 int type = rs.getInt (FIELD_ENTRY_TYPE); 935 936 JahiaACLEntry entry = new JahiaACLEntry (state, tristate); 937 Hashtable cachedType = null; 938 Hashtable cachedACL = (Hashtable ) cacheACLEntries.get (new Integer (id)); 939 if (cachedACL == null) { 940 cachedACL = new Hashtable (); 941 cacheACLEntries.put (new Integer (id), cachedACL); 942 } 943 944 cachedType = (Hashtable ) cachedACL.get (new Integer (type)); 945 if (cachedType == null) { 946 cachedType = new Hashtable (); 947 cachedACL.put (new Integer (type), cachedType); 948 } 949 950 cachedType.put (target, entry); 951 } 952 } 953 954 } catch (SQLException ex) { 955 throw new JahiaDatabaseException ( 956 "An SQL exception occrued while accessing an ACL entry.", 957 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 958 } finally { 959 query = null; 960 closeStatement (statement); 961 } 962 } 963 964 973 private void cacheAllACLEntries () 974 throws JahiaDatabaseException { 975 976 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 978 if (dbConn == null) { 979 throw new JahiaDatabaseException ( 980 "ACL entry loading process could not get a database connection.", 981 JahiaDatabaseException.CRITICAL_SEVERITY); 982 } 983 984 Statement statement = null; 985 StringBuffer query = new StringBuffer (); 986 try { 987 statement = dbConn.createStatement (); 989 query.append ("SELECT "); 990 query.append (FIELD_ENTRY_ACL_ID); 991 query.append (" FROM "); 992 query.append (JAHIA_ACL_ENTRIES); 993 query.append (" ORDER BY "); 994 query.append (FIELD_ACL_ID); 995 query.append (" ASC"); 996 997 ResultSet rs = statement.executeQuery (query.toString ()); 998 999 Vector entries = new Vector (); 1000 if (rs != null) { 1001 while (rs.next ()) { 1002 entries.add (new Integer (rs.getInt (FIELD_ACL_ID))); 1003 } 1004 } 1005 1006 1008 int ptr = 0; 1010 while (ptr < entries.size ()) { 1011 query = new StringBuffer (); 1012 query.append ("SELECT * FROM "); 1013 query.append (JAHIA_ACL_ENTRIES); 1014 query.append (" WHERE "); 1015 query.append (FIELD_ENTRY_ACL_ID); 1016 query.append (">="); 1017 query.append ((entries.elementAt (ptr)).toString ()); 1018 query.append (" AND "); 1019 query.append (FIELD_ENTRY_ACL_ID); 1020 query.append ("<="); 1021 int max = (ptr + CHUNK_SIZE < entries.size () ? 1022 ptr + CHUNK_SIZE : entries.size () - 1); 1023 query.append ((entries.elementAt (max)).toString ()); 1024 1025 rs = statement.executeQuery (query.toString ()); 1026 if (rs != null) { 1027 while (rs.next ()) { 1028 String target = rs.getString (FIELD_ENTRY_TARGET); 1029 int state = rs.getInt (FIELD_ENTRY_STATE); 1030 int tristate = rs.getInt (FIELD_ENTRY_TRISTATE); 1031 int id = rs.getInt (FIELD_ENTRY_ACL_ID); 1032 int type = rs.getInt (FIELD_ENTRY_TYPE); 1033 1034 JahiaACLEntry entry = new JahiaACLEntry (state, tristate); 1035 Hashtable cachedType = null; 1036 Hashtable cachedACL = (Hashtable ) cacheACLEntries.get ( 1037 new Integer (id)); 1038 if (cachedACL == null) { 1039 cachedACL = new Hashtable (); 1040 cacheACLEntries.put (new Integer (id), cachedACL); 1041 } 1042 1043 cachedType = (Hashtable ) cachedACL.get (new Integer (type)); 1044 if (cachedType == null) { 1045 cachedType = new Hashtable (); 1046 cachedACL.put (new Integer (type), cachedType); 1047 } 1048 1049 cachedType.put (target, entry); 1050 } 1051 } 1052 1053 ptr += CHUNK_SIZE; 1054 } 1055 } catch (SQLException ex) { 1056 throw new JahiaDatabaseException ( 1057 "An SQL exception occrued while accessing an ACL entry.", 1058 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 1059 } finally { 1060 query = null; 1061 closeStatement (statement); 1062 } 1063 } 1064 1065 1081 1082 public boolean addACLEntries (int aclID, int typeID, Hashtable entries) 1083 throws JahiaDatabaseException { 1084 if (entries == null) { 1085 return false; 1086 } 1087 1088 Enumeration keys = entries.keys (); 1089 while (keys.hasMoreElements ()) { 1090 String key = (String ) keys.nextElement (); 1091 JahiaACLEntry entry = (JahiaACLEntry) entries.get (key); 1092 1093 addACLEntry (aclID, typeID, key, 1094 entry.getState (), entry.getTriState ()); 1095 } 1097 return true; 1098 } 1099 1100 1109 public ArrayList getACLDirectChilds (int parentACL_ID) 1110 throws JahiaDatabaseException { 1111 ArrayList aclChilds = new ArrayList (); 1112 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1113 if (dbConn == null) { 1114 throw new JahiaDatabaseException ( 1115 "ACL entry loading process could not get a database connection.", 1116 JahiaDatabaseException.CRITICAL_SEVERITY); 1117 } 1118 Statement statement = null; 1119 StringBuffer query = new StringBuffer (); 1120 try { 1121 statement = dbConn.createStatement (); 1122 query.append ("SELECT "); 1123 query.append (FIELD_ACL_ID); 1124 query.append (" FROM "); 1125 query.append (JAHIA_ACL); 1126 query.append (" WHERE "); 1127 query.append (FIELD_PARENT_ID); 1128 query.append ("="); 1129 query.append (parentACL_ID); 1130 ResultSet rs = statement.executeQuery (query.toString ()); 1131 while (rs.next ()) { 1132 int child = rs.getInt (FIELD_ACL_ID); 1133 aclChilds.add (new Integer (child)); 1134 } 1135 } catch (SQLException ex) { 1136 throw new JahiaDatabaseException ( 1137 "An SQL exception occured while accessing an ACL.", 1138 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 1139 } finally { 1140 query = null; 1141 closeStatement (statement); 1142 } 1143 return aclChilds; 1144 } 1145 1146 1155 public ArrayList getACLAllChilds (int parentACL_ID) 1156 throws JahiaDatabaseException { 1157 1158 ArrayList aclAllChilds = getACLDirectChilds (parentACL_ID); 1159 int childNb = aclAllChilds.size (); 1160 for (int i = 0; i < childNb; i++) { 1161 aclAllChilds.add (getACLAllChilds (((Integer ) aclAllChilds.get (i)).intValue ())); 1162 } 1163 return aclAllChilds; 1164 } 1165 1166 1179 public boolean addACLInDB (JahiaACL acl) 1180 throws JahiaDatabaseException { 1181 String parentIDStr = Integer.toString (acl.getParentID ()); 1182 StringBuffer query = new StringBuffer ("INSERT INTO "); 1183 query.append (JAHIA_ACL); 1184 query.append (" ("); 1185 query.append (FIELD_ACL_ID); 1186 query.append (","); 1187 query.append (FIELD_PARENT_ID); 1188 query.append (","); 1189 query.append (FIELD_INHERITANCE); 1190 query.append (") VALUES ("); 1191 query.append (acl.getID ()); 1192 query.append (","); 1193 query.append (parentIDStr); 1194 query.append (","); 1195 query.append (Integer.toString (acl.getInheritance ())); 1196 query.append (")"); 1197 1198 boolean result = makeQuery (query.toString ()); 1199 1200 if (result) { 1202 result = addACLEntries (acl.getID (), USER_TYPE_ENTRY, 1204 acl.getAllUserEntries ()); 1205 if (result) { 1206 result = addACLEntries (acl.getID (), GROUP_TYPE_ENTRY, 1207 acl.getAllGroupEntries ()); 1208 } 1209 1210 } 1215 query = null; 1216 return result; 1217 } 1218 1219 1220 1233 public synchronized boolean deleteACLFromDB (JahiaACL acl) 1234 throws JahiaDatabaseException { 1235 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1237 if (dbConn == null) { 1238 throw new JahiaDatabaseException ( 1239 "ACL deletion process could not get a database connection.", 1240 JahiaDatabaseException.CRITICAL_SEVERITY); 1241 } 1242 1243 boolean result = false; 1244 Statement statement = null; 1245 StringBuffer query = new StringBuffer (); 1246 try { 1247 statement = dbConn.createStatement (); 1248 1249 query.append ("DELETE FROM "); 1251 query.append (JAHIA_ACL_ENTRIES); 1252 query.append (" WHERE "); 1253 query.append (FIELD_ENTRY_ACL_ID); 1254 query.append ("="); 1255 query.append (acl.getID ()); 1256 1257 cacheACLEntries.remove (new Integer (acl.getID ())); 1258 1259 statement.executeUpdate (query.toString ()); 1260 1261 query.delete (0, query.length ()); 1263 1264 query.append ("DELETE FROM "); 1266 query.append (JAHIA_ACL); 1267 query.append (" WHERE "); 1268 query.append (FIELD_ACL_ID); 1269 query.append ("="); 1270 query.append (acl.getID ()); 1271 1272 statement.executeUpdate (query.toString ()); 1273 acl = null; 1274 result = true; 1275 } catch (SQLException ex) { 1276 throw new JahiaDatabaseException ( 1277 "An SQL exception occured while deleting an ACL.", 1278 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 1279 } finally { 1280 query = null; 1281 closeStatement (statement); 1282 } 1283 1284 return result; 1285 } 1286 1287 1297 public synchronized boolean updateACL (JahiaACL acl) 1298 throws JahiaDatabaseException { 1299 if (acl == null) { 1300 return false; 1301 } 1302 1303 StringBuffer query = new StringBuffer ("UPDATE "); 1304 query.append (JAHIA_ACL); 1305 query.append (" SET "); 1306 query.append (FIELD_PARENT_ID); 1307 query.append ("="); 1308 query.append (acl.getParentID ()); 1309 query.append (","); 1310 query.append (FIELD_INHERITANCE); 1311 query.append ("="); 1312 query.append (acl.getInheritance ()); 1313 query.append (" WHERE "); 1314 query.append (FIELD_ACL_ID); 1315 query.append ("="); 1316 query.append (acl.getID ()); 1317 1318 boolean result = makeQuery (query.toString ()); 1319 query = null; 1320 return result; 1321 } 1322 1323 1324 1335 public synchronized boolean removeACLUserEntries (String user_key) 1336 throws JahiaDatabaseException { 1337 return removeACLEntriesByTarget (user_key, JahiaACL.USER_TYPE_ENTRY); 1338 } 1339 1340 1341 1352 public synchronized boolean removeACLGroupEntries (String group_key) 1353 throws JahiaDatabaseException { 1354 return removeACLEntriesByTarget (group_key, JahiaACL.GROUP_TYPE_ENTRY); 1355 } 1356 1357 1358 1372 private boolean removeACLEntriesByTarget ( 1373 String targetName, int targetType) 1374 throws JahiaDatabaseException { 1375 1376 StringBuffer query = new StringBuffer ("DELETE FROM "); 1377 query.append (JAHIA_ACL_ENTRIES); 1378 query.append (" WHERE "); 1379 query.append (FIELD_ENTRY_TYPE); 1380 query.append ("="); 1381 query.append (targetType); 1382 query.append (" AND "); 1383 query.append (FIELD_ENTRY_TARGET); 1384 query.append ("='"); 1385 query.append (targetName); 1386 query.append ("'"); 1387 1388 boolean result = makeQuery (query.toString ()); 1389 query = null; 1390 return result; 1391 } 1392 1393 1404 private Vector getAclParents (Vector ids) 1405 throws JahiaException { 1406 1407 1408 Vector v = new Vector (); 1409 1410 if ((ids == null) || (ids.size () == 0)) { 1411 return v; 1412 } 1413 1414 Connection dbConn = null; 1415 Statement statement = null; 1416 1417 int aclIDMax = getBiggestAclID (); 1418 if (aclIDMax == -1) { 1419 return v; 1420 } 1421 1422 int low = 0; 1423 int high = 0; 1424 Integer I = null; 1425 while (high <= aclIDMax) { 1426 low = high; 1427 high += 5000; 1428 1429 StringBuffer buff = new StringBuffer 1430 ( 1431 "SELECT DISTINCT id_jahia_acl,parent_id_jahia_acl, inheritance_jahia_acl FROM jahia_acl WHERE id_jahia_acl "); 1432 buff.append (" > "); 1433 buff.append (low); 1434 buff.append (" AND id_jahia_acl <= "); 1435 buff.append (high); 1436 1437 try { 1438 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1439 statement = dbConn.createStatement (); 1440 if (statement != null) { 1441 ResultSet rs = statement.executeQuery (buff.toString ()); 1442 if (rs != null) { 1443 while (rs.next ()) { 1444 try { 1445 int aclID = rs.getInt ("id_jahia_acl"); 1446 int parentID = rs.getInt ("parent_id_jahia_acl"); 1447 1448 if (parentID > 0) { 1449 if (ids.contains (new Integer (aclID))) { 1450 1451 I = new Integer (parentID); 1452 if (!v.contains (I)) { 1453 v.add (I); 1454 } 1455 } 1456 } 1457 } catch (Throwable t) { 1458 t.printStackTrace (); 1459 } 1460 } 1461 } 1462 } 1463 } catch (SQLException se) { 1464 String msg = "Cannot load acl from the database."; 1465 logger.warn (msg, se); 1466 throw new JahiaException (msg, se.getMessage (), 1467 JahiaException.DATABASE_ERROR, 1468 JahiaException.CRITICAL_SEVERITY, se); 1469 } finally { 1470 closeStatement (statement); 1471 } 1472 } 1473 return v; 1474 } 1475 1476 1477 1488 public JahiaDOMObject getAclsAsDOM (Vector ids, boolean withParents) 1489 throws JahiaException { 1490 1491 1492 JahiaDBDOMObject dom = null; 1493 1494 Connection dbConn = null; 1495 Statement statement = null; 1496 1497 try { 1498 1499 1500 if ((ids == null) || (ids.size () == 0)) { 1501 dom = new JahiaDBDOMObject (); 1502 dom.addTable ("jahia_acl", null); 1503 return dom; 1504 } 1505 1506 Vector v = new Vector (); 1508 1509 if (withParents) { 1510 v.addAll (ids); 1511 Vector aclParents = getAclParents (v); 1512 Vector newAcls = new Vector (); 1513 while (aclParents.size () > 0) { 1514 newAcls = new Vector (); 1515 int size = v.size (); 1516 int size2 = aclParents.size (); 1517 Integer I = null; 1518 Integer J = null; 1519 for (int i = 0; i < size2; i++) { 1520 boolean isNew = true; 1521 I = (Integer ) aclParents.get (i); 1522 for (int j = 0; j < size; j++) { 1523 J = (Integer ) v.get (j); 1524 if (I.intValue () == J.intValue ()) { 1525 isNew = false; 1526 break; 1527 } 1528 } 1529 if (isNew) { 1530 newAcls.add (I); 1531 v.add (I); 1532 } 1533 size = v.size (); 1534 } 1535 aclParents = new Vector (); 1537 aclParents = getAclParents (newAcls); 1538 } 1539 } else { 1540 v = ids; 1541 } 1542 1543 int aclIDMax = getBiggestAclID (); 1544 if (aclIDMax == -1) { 1545 dom = new JahiaDBDOMObject (); 1546 dom.addTable ("jahia_acl", null); 1547 return dom; 1548 } 1549 1550 AclFilter filter = new AclFilter (v); 1551 1552 dom = new JahiaDBDOMObject (); 1553 int low = 0; 1554 int high = 0; 1555 while (high <= aclIDMax) { 1556 low = high; 1557 high += 5000; 1558 StringBuffer buff = new StringBuffer 1559 ( 1560 "SELECT DISTINCT id_jahia_acl,parent_id_jahia_acl, inheritance_jahia_acl FROM jahia_acl WHERE id_jahia_acl"); 1561 buff.append (" > "); 1562 buff.append (low); 1563 buff.append (" AND id_jahia_acl <= "); 1564 buff.append (high); 1565 1566 try { 1567 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1568 statement = dbConn.createStatement (); 1569 if (statement != null) { 1570 ResultSet rs = statement.executeQuery (buff.toString ()); 1571 if (rs != null) { 1572 dom.addTable ("jahia_acl", rs, filter); 1573 } 1574 } 1575 } catch (SQLException se) { 1576 String msg = "Cannot load acl from the database"; 1577 logger.warn (msg, se); 1578 1579 throw new JahiaException (msg, se.getMessage (), 1580 JahiaException.DATABASE_ERROR, 1581 JahiaException.CRITICAL_SEVERITY, se); 1582 } finally { 1583 closeStatement (statement); 1584 } 1585 } 1586 1587 } catch (SQLException se) { 1588 String msg = "Cannot load acl from the database"; 1589 logger.warn (msg, se); 1590 1591 throw new JahiaException (msg, se.getMessage (), 1592 JahiaException.DATABASE_ERROR, 1593 JahiaException.CRITICAL_SEVERITY, se); 1594 } 1595 1596 return dom; 1597 } 1598 1599 1600 1610 public JahiaDOMObject getAclEntriesAsDOM (Vector ids) 1611 throws JahiaException { 1612 1613 1614 JahiaDBDOMObject dom = new JahiaDBDOMObject (); 1615 1616 1617 try { 1618 1619 if (ids == null) { 1620 dom.addTable ("jahia_acl_entries", null); 1621 return dom; 1622 } 1623 1624 Connection dbConn = null; 1625 Statement statement = null; 1626 1627 int aclIDMax = getBiggestAclEntryID (); 1628 if (aclIDMax == -1) { 1629 dom = new JahiaDBDOMObject (); 1630 dom.addTable ("jahia_acl_entries", null); 1631 return dom; 1632 } 1633 1634 AclFilter filter = new AclFilter (ids); 1635 1636 dom = new JahiaDBDOMObject (); 1637 int low = 0; 1638 int high = 0; 1639 while (high <= aclIDMax) { 1640 low = high; 1641 high += 5000; 1642 1643 StringBuffer buff = new StringBuffer ( 1644 "SELECT DISTINCT id_jahia_acl,type_jahia_acl_entries,target_jahia_acl_entries,entry_state_jahia_acl_entries,entry_trist_jahia_acl_entries FROM jahia_acl_entries WHERE id_jahia_acl"); 1645 buff.append (" > "); 1646 buff.append (low); 1647 buff.append (" AND id_jahia_acl <= "); 1648 buff.append (high); 1649 1650 try { 1651 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1652 statement = dbConn.createStatement (); 1653 if (statement != null) { 1654 ResultSet rs = statement.executeQuery (buff.toString ()); 1655 if (rs != null) { 1656 dom.addTable ("jahia_acl_entries", rs, filter); 1657 } 1658 } 1659 } catch (SQLException se) { 1660 String msg = "Cannot load acl from the database"; 1661 logger.warn (msg, se); 1662 1663 throw new JahiaException (msg, se.getMessage (), 1664 JahiaException.DATABASE_ERROR, 1665 JahiaException.CRITICAL_SEVERITY, se); 1666 1667 } finally { 1668 closeStatement (statement); 1669 } 1670 } 1671 1672 } catch (SQLException se) { 1673 String msg = "Cannot load acl from the database"; 1674 logger.warn (msg, se); 1675 1676 throw new JahiaException (msg, se.getMessage (), 1677 JahiaException.DATABASE_ERROR, 1678 JahiaException.CRITICAL_SEVERITY, se); 1679 1680 1681 } 1682 1683 return dom; 1684 1685 } 1686 1687 1688 1700 public boolean makeQuery (String query) 1701 throws JahiaDatabaseException { 1702 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1704 if (dbConn == null) { 1705 throw new JahiaDatabaseException ( 1706 "ACL SQL query execution process could not get a database connection.", 1707 JahiaDatabaseException.CRITICAL_SEVERITY); 1708 } 1709 1710 boolean result = false; 1711 Statement statement = null; 1712 1713 try { 1714 statement = dbConn.createStatement (); 1715 if (statement != null) { 1716 synchronized (this) { 1717 statement.executeUpdate (query); 1718 result = true; 1719 } 1720 } 1721 } catch (SQLException ex) { 1722 throw new JahiaDatabaseException ( 1723 "An SQL exception occrued while executing an SQL query in the ACL DB Utilities.", 1724 query, ex, JahiaDatabaseException.ERROR_SEVERITY); 1725 } finally { 1726 closeStatement (statement); 1727 } 1728 1729 return result; 1730 } 1731 1732 1733 1740 public synchronized int getBiggestAclID () 1741 throws JahiaDatabaseException { 1742 final String MAX_ID = "MaxID"; 1743 int val = -1; 1744 1745 Connection connection = org.jahia.services.database.ConnectionDispenser.getConnection (); 1746 if (connection != null) { 1747 Statement statement = null; 1748 StringBuffer query = new StringBuffer (); 1749 1750 try { 1751 query.append ("SELECT MAX("); 1752 query.append (FIELD_ACL_ID); 1753 query.append (") as "); 1754 query.append (MAX_ID); 1755 query.append (" FROM "); 1756 query.append (JAHIA_ACL); 1757 1758 statement = connection.createStatement (); 1759 if (statement != null) { 1760 ResultSet rs = statement.executeQuery (query.toString ()); 1761 if (rs != null) { 1762 if (rs.next ()) { 1763 val = rs.getInt (MAX_ID); 1765 } 1766 } 1767 } 1768 } catch (SQLException ex) { 1769 throw new JahiaDatabaseException ( 1770 "Could not get the max acl ID", 1771 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 1772 } finally { 1773 closeStatement (statement); 1774 } 1775 } else { 1776 throw new JahiaDatabaseException ( 1777 "Could not get a database connection while getting the max acl ID", 1778 JahiaDatabaseException.CRITICAL_SEVERITY); 1779 } 1780 1781 return val; 1782 } 1783 1784 1792 public synchronized int getBiggestAclEntryID () 1793 throws JahiaDatabaseException { 1794 final String MAX_ID = "MaxID"; 1795 int val = -1; 1796 1797 Connection connection = org.jahia.services.database.ConnectionDispenser.getConnection (); 1798 if (connection != null) { 1799 Statement statement = null; 1800 StringBuffer query = new StringBuffer (); 1801 1802 try { 1803 query.append ("SELECT MAX("); 1804 query.append (FIELD_ACL_ID); 1805 query.append (") as "); 1806 query.append (MAX_ID); 1807 query.append (" FROM "); 1808 query.append (JAHIA_ACL_ENTRIES); 1809 1810 statement = connection.createStatement (); 1811 if (statement != null) { 1812 ResultSet rs = statement.executeQuery (query.toString ()); 1813 if (rs != null) { 1814 if (rs.next ()) { 1815 val = rs.getInt (MAX_ID); 1817 } 1818 } 1819 } 1820 } catch (SQLException ex) { 1821 throw new JahiaDatabaseException ( 1822 "Could not get the max acl entry ID", 1823 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 1824 } finally { 1825 closeStatement (statement); 1826 } 1827 } else { 1828 throw new JahiaDatabaseException ( 1829 "Could not get a database connection while getting the max acl entry ID", 1830 JahiaDatabaseException.CRITICAL_SEVERITY); 1831 } 1832 1833 return val; 1834 } 1835 1836 1843 public synchronized int getNextID () 1844 throws JahiaDatabaseException { 1845 final String MAX_ID = "MaxID"; 1846 1847 int counter = -1; 1848 1849 Connection connection = org.jahia.services.database.ConnectionDispenser.getConnection (); 1850 if (connection != null) { 1851 Statement statement = null; 1852 StringBuffer query = new StringBuffer (); 1853 1854 try { 1855 query.append ("SELECT MAX("); 1856 query.append (FIELD_ACL_ID); 1857 query.append (") as "); 1858 query.append (MAX_ID); 1859 query.append (" FROM "); 1860 query.append (JAHIA_ACL); 1861 1862 statement = connection.createStatement (); 1863 if (statement != null) { 1864 ResultSet rs = statement.executeQuery (query.toString ()); 1865 if (rs != null) { 1866 if (rs.next ()) { 1867 counter = rs.getInt (MAX_ID); 1869 1870 counter++; 1872 } 1873 } 1874 } 1875 } catch (SQLException ex) { 1876 throw new JahiaDatabaseException ( 1877 "Could not get the next ID in the account table", 1878 query.toString (), ex, JahiaDatabaseException.ERROR_SEVERITY); 1879 } finally { 1880 closeStatement (statement); 1881 } 1882 } else { 1883 throw new JahiaDatabaseException ( 1884 "Could not get a database connection while getting the next available account ID", 1885 JahiaDatabaseException.CRITICAL_SEVERITY); 1886 } 1887 1888 return counter; 1889 } 1890 1891 1892 1898 private void closeStatement (Statement statement) { 1899 try { 1901 if (statement != null) { 1902 statement.close (); 1903 } 1904 1905 } catch (SQLException sqlEx) { 1906 logger.warn("Cannot close a statement", sqlEx); 1907 } 1908 } 1909 1910 1915 public void onCacheFlush(String cacheName) { 1916 if (ACL_ENTRIES_CACHE.equals(cacheName)) { 1917 try { 1918 cacheACLEntries.flush(false); 1919 cacheAllACLEntries(); 1920 1921 } catch (JahiaDatabaseException e) { 1922 logger.warn("Could not reload all ACL Entries after cache flush!", e); 1923 } 1924 } 1925 } 1926 1927 public void onCachePut(String cacheName, Object entryKey) { 1928 } 1930 1931 class AclFilter implements DBRowDataFilter { 1932 1933 private Vector acls; 1934 1935 public AclFilter (Vector aclList) { 1936 this.acls = aclList; 1937 } 1938 1939 public boolean inValue (Hashtable vals) { 1940 if (vals == null) { 1941 return false; 1942 } 1943 1944 if (acls == null) { 1945 return true; 1946 } 1947 String val = null; 1948 try { 1949 val = (String ) vals.get ("id_jahia_acl"); 1950 Integer aclID = new Integer (val); 1951 return (acls.contains (aclID)); 1952 1953 } catch (Throwable t) { 1954 logger.warn ("Error parsing " + val, t); 1955 } 1956 return false; 1957 } 1958 } 1959 1960} 1961 | Popular Tags |