1 58 59 package org.apache.jetspeed.services.cms.repository.slide; 60 61 import java.lang.reflect.Constructor ; 62 import java.util.Hashtable ; 63 import java.util.Enumeration ; 64 import java.util.Vector ; 65 import java.util.Date ; 66 import java.io.FileWriter ; 67 import java.io.IOException ; 68 import java.sql.*; 69 import javax.transaction.xa.XAException ; 70 import javax.transaction.xa.Xid ; 71 import org.apache.slide.common.*; 72 import org.apache.slide.store.*; 73 import org.apache.slide.structure.*; 74 import org.apache.slide.security.*; 75 import org.apache.slide.lock.*; 76 import org.apache.slide.content.*; 77 import org.apache.slide.util.logger.Logger; 78 79 92 93 public class JDBCDescriptorsStore 94 extends AbstractSimpleService 95 implements LockStore, NodeStore, RevisionDescriptorsStore, 96 RevisionDescriptorStore, SecurityStore 97 { 98 99 101 103 105 protected static final int OBJECTS_URI = 1; 106 protected static final int OBJECTS_CLASS = 2; 107 108 protected static final int CHILDREN_URI = 1; 109 protected static final int CHILDREN_CHILDURI = 2; 110 111 protected static final int LINKS_LINK = 1; 112 protected static final int LINKS_LINKTO = 2; 113 114 116 protected static final int PERMISSIONS_OBJECT = 1; 117 protected static final int PERMISSIONS_REVISION_NUMBER = 2; 118 protected static final int PERMISSIONS_SUBJECT = 3; 119 protected static final int PERMISSIONS_ACTION = 4; 120 protected static final int PERMISSIONS_INHERITABLE = 5; 121 protected static final int PERMISSIONS_NEGATIVE = 6; 122 123 125 protected static final int LOCKS_ID = 1; 126 protected static final int LOCKS_OBJECT = 2; 127 protected static final int LOCKS_SUBJECT = 3; 128 protected static final int LOCKS_TYPE = 4; 129 protected static final int LOCKS_EXPIRATIONDATE = 5; 130 protected static final int LOCKS_INHERITABLE = 6; 131 protected static final int LOCKS_EXCLUSIVE = 7; 132 133 135 protected static final int REVISIONS_URI = 1; 136 protected static final int REVISIONS_ISVERSIONED = 2; 137 protected static final int REVISIONS_INITIALREVISION = 3; 138 139 protected static final int WORKINGREVISION_URI = 1; 140 protected static final int WORKINGREVISION_BASEREVISION = 2; 141 protected static final int WORKINGREVISION_NUMBER = 3; 142 143 protected static final int LATESTREVISIONS_URI = 1; 144 protected static final int LATESTREVISIONS_BRANCHNAME = 2; 145 protected static final int LATESTREVISIONS_NUMBER = 3; 146 147 protected static final int BRANCHES_URI = 1; 148 protected static final int BRANCHES_NUMBER = 2; 149 protected static final int BRANCHES_CHILDNUMBER = 3; 150 151 protected static final int REVISION_URI = 1; 152 protected static final int REVISION_NUMBER = 2; 153 protected static final int REVISION_BRANCHNAME = 3; 154 155 protected static final int LABEL_URI = 1; 156 protected static final int LABEL_NUMBER = 2; 157 protected static final int LABEL_LABEL = 3; 158 159 protected static final int PROPERTY_URI = 1; 160 protected static final int PROPERTY_NUMBER = 2; 161 protected static final int PROPERTY_NAME = 3; 162 protected static final int PROPERTY_VALUE = 4; 163 protected static final int PROPERTY_NAMESPACE = 5; 164 protected static final int PROPERTY_TYPE = 6; 165 protected static final int PROPERTY_PROTECTED = 7; 166 167 168 170 171 174 protected Connection connection; 175 176 177 180 protected String driver; 181 182 183 186 protected String url; 187 188 189 192 protected String user; 193 194 195 198 protected String password; 199 200 201 204 protected int jdbcVersion; 205 206 210 protected boolean alreadyEnlisted=false; 211 212 214 217 protected String [] getDatabaseCreateStatements() 218 { 219 String [] statements = { 220 "create table objects(uri varchar(65536) primary key," + 221 " classname varchar(4096))", 222 "create table children(uri varchar(65536), " + 223 " childuri varchar(65536))", 224 "create table links(link varchar(65536), " + 225 " linkto varchar(65536))", 226 "create table permissions(object varchar(65536)," + 227 " revisionnumber varchar(20), " + 228 " subject varchar(65536), action varchar(65536), " + 229 " inheritable int, negative int)", 230 "create table locks(id varchar(65536), object varchar(4096)," + 231 " subject varchar(4096), type varchar(4096), " + 232 " expirationdate varchar(15), inheritable int, " + 233 " xexclusive int)", 234 "create table revisions(uri varchar(65536) primary key, " + 235 " isversioned int, initialrevision varchar(10))", 236 "create table workingrevision(uri varchar(65536), " + 237 " baserevision varchar(20), xnumber varchar(20))", 238 "create table latestrevisions(uri varchar(65536), " + 239 " branchname varchar(4096), xnumber varchar(20))", 240 "create table branches(uri varchar(65536), xnumber varchar(20)," + 241 " childnumber varchar(20))", 242 "create table revision(uri varchar(65536), xnumber varchar(20)," + 243 " branchname varchar(4096))", 244 "create table label(uri varchar(65536), xnumber varchar(20)," + 245 " label varchar(4096))", 246 "create table property(uri varchar(65536), xnumber varchar(20)," + 247 " name varchar(4096), value varchar(65536), " + 248 " namespace varchar(4096), type varchar(100), protected int)"}; 249 250 return statements; 251 } 252 253 261 public void setParameters(Hashtable parameters) 262 throws ServiceParameterErrorException, 263 ServiceParameterMissingException 264 { 265 266 driver = (String ) parameters.get("driver"); 268 269 url = (String ) parameters.get("url"); 271 272 if (!url.startsWith("jdbc:")) 276 { 277 url="jdbc:" + url; 278 } 279 281 user = (String ) parameters.get("user"); 283 if (user == null) 284 { 285 user = new String (); 286 } 287 288 password = (String ) parameters.get("password"); 290 if (password == null) 291 { 292 password = new String (); 293 } 294 295 jdbcVersion = 1; 297 String version = (String ) parameters.get("jdbcversion"); 298 if (version != null) 299 { 300 jdbcVersion = (new Integer (version)).intValue(); 301 } 302 } 303 304 310 public synchronized void connect() 311 throws ServiceConnectionFailedException 312 { 313 getLogger().log("Connecting to \"" + url + "\" as user \"" + user + "\"",LOG_CHANNEL,Logger.INFO); 314 try 315 { 316 connection = DriverManager.getConnection(url, user, password); 317 } 318 catch (SQLException e) 319 { 320 getLogger().log("Connecting to \"" + url + "\" as user \"" + user + "\" failed",LOG_CHANNEL,Logger.ERROR); 321 getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 322 throw new ServiceConnectionFailedException(this, e); 323 } 324 325 try 327 { 328 connection.setAutoCommit(false); 329 } 330 catch (SQLException e) 331 { 332 } 333 351 alreadyEnlisted=false; 353 } 354 355 361 public void disconnect() 362 throws ServiceDisconnectionFailedException 363 { 364 getLogger().log("Disconnecting from \"" + url + "\" as user \"" + user + "\"",LOG_CHANNEL,Logger.INFO); 365 try 366 { 367 if (connection != null) 368 { 369 connection.close(); 370 } 371 connection = null; 372 } 373 catch (SQLException e) 374 { 375 getLogger().log("Disconnecting from \"" + url + "\" as user \"" + user + "\" failed",LOG_CHANNEL,Logger.ERROR); 376 getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 377 throw new ServiceDisconnectionFailedException(this, e); 378 } 379 } 380 381 393 public synchronized void initialize(NamespaceAccessToken token) 394 throws ServiceInitializationFailedException 395 { 396 try 397 { 398 token.getLogger().log("Loading and registering driver: " + driver,LOG_CHANNEL,Logger.INFO); 400 Class driverClass = Class.forName(driver); 401 Driver databaseDriver = (Driver) driverClass.newInstance(); 402 DriverManager.registerDriver(databaseDriver); 403 } 404 catch (ClassNotFoundException e) 405 { 406 token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR); 407 token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 408 throw new ServiceInitializationFailedException(this, e.getMessage()); 409 } 410 catch (InstantiationException e) 411 { 412 token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR); 413 token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 414 throw new ServiceInitializationFailedException(this, e.getMessage()); 415 } 416 catch (IllegalAccessException e) 417 { 418 token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR); 419 token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 420 throw new ServiceInitializationFailedException(this, e.getMessage()); 421 } 422 catch (SQLException e) 423 { 424 token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR); 425 token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 426 throw new ServiceInitializationFailedException(this, e.getMessage()); 427 } 428 catch (ClassCastException e) 429 { 430 token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR); 431 token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 432 throw new ServiceInitializationFailedException(this, e.getMessage()); 433 } 434 catch (Exception e) 435 { 436 token.getLogger().log("Loading and registering driver " + driver + " failed",LOG_CHANNEL,Logger.ERROR); 437 token.getLogger().log(e.toString(),LOG_CHANNEL,Logger.ERROR); 438 throw new ServiceInitializationFailedException(this, e.getMessage()); 439 } 440 } 441 442 443 448 public synchronized void reset() 449 throws ServiceResetFailedException 450 { 451 Statement statement = null; 452 509 } 510 511 512 518 public boolean isConnected() 519 throws ServiceAccessException 520 { 521 try 522 { 523 return ((connection != null) && (!connection.isClosed())); 524 } 525 catch (SQLException e) 526 { 527 throw new ServiceAccessException(this, e); 528 } 529 } 530 531 532 534 535 538 public void commit(Xid xid, boolean onePhase) 539 throws XAException 540 { 541 super.commit(xid, onePhase); 542 543 try 544 { 545 connection.commit(); 547 } 548 catch (SQLException e) 549 { 550 throw new XAException (XAException.XA_RBCOMMFAIL); 551 } 552 alreadyEnlisted=false; 553 } 554 555 556 560 public void rollback(Xid xid) 561 throws XAException 562 { 563 super.rollback(xid); 564 565 try 566 { 567 connection.rollback(); 569 } 570 catch (SQLException e) 571 { 572 throw new XAException (XAException.XA_HEURCOM); 573 } 574 alreadyEnlisted=false; 575 } 576 577 578 581 public void start(Xid xid, int flags) 582 throws XAException 583 { 584 super.start(xid, flags); 585 if (!alreadyEnlisted) 586 { 587 try 588 { 589 connection.rollback(); 592 } 593 catch (SQLException e) 594 { 595 throw new XAException (XAException.XAER_RMERR); 596 } 597 alreadyEnlisted=true; 598 } 599 } 600 601 602 604 605 612 public ObjectNode retrieveObject(Uri uri) 613 throws ServiceAccessException, ObjectNotFoundException 614 { 615 616 ObjectNode result = null; 617 PreparedStatement statement = null; 618 619 try 620 { 621 622 statement = connection.prepareStatement 623 ("select * from objects where uri= ?"); 624 statement.setString(1, uri.toString()); 625 626 ResultSet res = statement.executeQuery(); 627 628 630 String className; 631 632 if (res.next()) 633 { 634 className = res.getString(OBJECTS_CLASS); 636 } 637 else 638 { 639 throw new ObjectNotFoundException(uri); 641 } 642 643 closeStatement(statement); 644 645 statement = connection.prepareStatement 647 ("select * from children where uri= ?"); 648 statement.setString(1,uri.toString()); 649 res = statement.executeQuery(); 650 651 Vector childrenVector = new Vector (); 652 653 while (res.next()) 655 { 656 childrenVector.addElement(res.getString(CHILDREN_CHILDURI)); 658 } 659 closeStatement(statement); 660 661 statement = connection.prepareStatement 662 ("select * from links where linkto= ?"); 663 statement.setString(1,uri.toString()); 664 res = statement.executeQuery(); 665 666 Vector linksVector = new Vector (); 667 668 while (res.next()) 670 { 671 linksVector.addElement(res.getString(LINKS_LINKTO)); 673 } 674 675 closeStatement(statement); 676 677 if (className.equals("org.apache.slide.structure.LinkNode")) 678 { 679 680 String linkTo = new String (); 681 statement = connection.prepareStatement 682 ("select * from links where link= ?"); 683 statement.setString(1,uri.toString()); 684 res = statement.executeQuery(); 685 686 if(res.next()) 687 { 688 linkTo = res.getString(LINKS_LINKTO); 689 } 690 691 closeStatement(statement); 692 693 result = new LinkNode(uri.toString(), childrenVector, 694 linksVector, linkTo); 695 696 } 697 else 698 { 699 700 try 701 { 702 Class objclass = Class.forName(className); 703 704 Class [] argClasses = 705 { 706 Class.forName("java.lang.String"), 707 Class.forName("java.util.Vector"), 708 Class.forName("java.util.Vector") 709 }; 710 Object [] arguments = 711 { 712 uri.toString(), 713 childrenVector, 714 linksVector 715 }; 716 717 Constructor constructor = 718 objclass.getConstructor(argClasses); 719 result = (ObjectNode)constructor.newInstance(arguments); 720 } 721 catch(Exception e) 722 { 723 throw new ServiceAccessException(this, e); 725 } 726 } 727 } 728 catch (SQLException e) 729 { 730 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 731 throw new ServiceAccessException(this, e); 732 } 733 finally 734 { 735 closeStatement(statement); 736 } 737 return result; 738 } 739 740 741 748 public void storeObject(Uri uri, ObjectNode object) 749 throws ServiceAccessException, ObjectNotFoundException 750 { 751 752 PreparedStatement statement = null; 753 754 try 755 { 756 statement = connection.prepareStatement 757 ("select * from objects where uri= ?"); 758 statement.setString(1, uri.toString()); 759 760 ResultSet res = statement.executeQuery(); 761 762 764 if (!res.next()) 765 { 766 throw new ObjectNotFoundException(uri); 767 } 768 769 closeStatement(statement); 770 771 statement = connection.prepareStatement 773 ("delete from children where uri= ?"); 774 statement.setString(1, object.getUri()); 775 statement.execute(); 776 closeStatement(statement); 777 778 statement = null; 779 Enumeration children = object.enumerateChildren(); 780 while (children.hasMoreElements()) 781 { 782 if (statement == null) 783 { 784 statement = connection.prepareStatement 785 ("insert into children values(?, ?)"); 786 } 787 statement.setString(1, object.getUri()); 788 statement.setString(2, (String )children.nextElement()); 789 statement.execute(); 790 } 791 closeStatement(statement); 792 793 805 806 statement = connection.prepareStatement 808 ("delete from links where link= ?"); 809 statement.setString(1, object.getUri()); 810 statement.execute(); 811 closeStatement(statement); 812 813 if (object instanceof LinkNode) 814 { 815 statement = connection.prepareStatement 816 ("insert into links values(?,?)"); 817 statement.setString(1, object.getUri()); 818 statement.setString(2, ((LinkNode) object).getLinkedUri()); 819 statement.execute(); 820 closeStatement(statement); 821 } 822 } 823 catch (SQLException e) 824 { 825 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 826 throw new ServiceAccessException(this, e); 827 } 828 finally 829 { 830 closeStatement(statement); 831 } 832 } 833 834 835 844 public void createObject(Uri uri, ObjectNode object) 845 throws ServiceAccessException, ObjectAlreadyExistsException 846 { 847 848 PreparedStatement statement = null; 849 850 try 851 { 852 853 String className = object.getClass().getName(); 854 855 statement = connection.prepareStatement 856 ("select * from objects where uri= ?"); 857 statement.setString(1, uri.toString()); 858 859 ResultSet res = statement.executeQuery(); 860 861 863 if (res.next()) 864 { 865 throw new ObjectAlreadyExistsException(uri.toString()); 866 } 867 868 closeStatement(statement); 869 870 statement = connection.prepareStatement 871 ("insert into objects values(?,?)"); 872 statement.setString(1, uri.toString()); 873 statement.setString(2, className ); 874 875 statement.execute(); 876 closeStatement(statement); 877 878 statement = null; 879 Enumeration children = object.enumerateChildren(); 881 while (children.hasMoreElements()) 882 { 883 if (statement == null) 884 { 885 statement = connection.prepareStatement 886 ("insert into children values(?,?)"); 887 } 888 statement.setString(1, uri.toString()); 889 statement.setString(2, (String ) children.nextElement()); 890 statement.execute(); 891 } 892 closeStatement(statement); 893 894 904 905 if (object instanceof LinkNode) 907 { 908 statement = connection.prepareStatement 909 ("insert into links values(?,?)"); 910 statement.setString(1, uri.toString()); 911 statement.setString(2, ((LinkNode) object).getLinkedUri()); 912 statement.execute(); 913 closeStatement(statement); 914 } 915 } 916 catch (SQLException e) 917 { 918 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 919 throw new ServiceAccessException(this, e); 920 } 921 finally 922 { 923 closeStatement(statement); 924 } 925 926 } 927 928 929 936 public void removeObject(Uri uri, ObjectNode object) 937 throws ServiceAccessException, ObjectNotFoundException 938 { 939 940 PreparedStatement statement = null; 941 942 try 943 { 944 statement = connection.prepareStatement 946 ("delete from objects where uri= ?"); 947 statement.setString(1,object.getUri()); 948 statement.execute(); 949 closeStatement(statement); 950 951 statement = connection.prepareStatement 953 ("delete from children where uri=?"); 954 statement.setString(1, object.getUri()); 955 statement.execute(); 956 closeStatement(statement); 957 958 963 964 statement = connection.prepareStatement 966 ("delete from links where link= ?"); 967 statement.setString(1, object.getUri()); 968 statement.execute(); 969 closeStatement(statement); 970 } 971 catch (SQLException e) 972 { 973 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 974 throw new ServiceAccessException(this, e); 975 } 976 } 977 978 979 985 public void grantPermission(Uri uri, NodePermission permission) 986 throws ServiceAccessException 987 { 988 989 PreparedStatement statement = null; 990 991 try 992 { 993 int inheritable = 0; 994 if (permission.isInheritable()) 995 { 996 inheritable = 1; 997 } 998 999 int negative = 0; 1000 if (permission.isNegative()) 1001 { 1002 negative = 1; 1003 } 1004 1005 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 1006 String revisionNumberStr = 1007 (revisionNumber == null) ? null : revisionNumber.toString(); 1008 1009 statement = connection.prepareStatement 1010 ("insert into permissions values(?,?,?,?,?,?)"); 1011 statement.setString(1, permission.getObjectUri()); 1012 statement.setString(2, revisionNumberStr); 1013 statement.setString(3, permission.getSubjectUri()); 1014 statement.setString(4, permission.getActionUri()); 1015 statement.setInt(5, inheritable); 1016 statement.setInt(6, negative); 1017 statement.execute(); 1018 } 1019 catch (SQLException e) 1020 { 1021 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1022 throw new ServiceAccessException(this, e); 1023 } 1024 finally 1025 { 1026 closeStatement(statement); 1027 } 1028 } 1029 1030 1031 1037 public void revokePermission(Uri uri, NodePermission permission) 1038 throws ServiceAccessException 1039 { 1040 1041 PreparedStatement statement = null; 1042 1043 try 1044 { 1045 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 1046 1047 if(revisionNumber != null) 1048 { 1049 statement = connection.prepareStatement 1050 ("delete from permissions where object= ? and subject = ? and \"ACTION\" = ? and revisionnumber = ? "); 1051 statement.setString(4, revisionNumber.toString()); 1052 } 1053 else 1054 { 1055 statement = connection.prepareStatement 1056 ("delete from permissions where object = ? and subject = ? and \"ACTION\" = ? and revisionnumber is NULL"); 1057 } 1058 1059 statement.setString(1, permission.getObjectUri()); 1060 statement.setString(2, permission.getSubjectUri()); 1061 statement.setString(3, permission.getActionUri()); 1062 1063 statement.execute(); 1064 } 1065 catch (SQLException e) 1066 { 1067 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1068 throw new ServiceAccessException(this, e); 1069 } 1070 finally 1071 { 1072 closeStatement(statement); 1073 } 1074 } 1075 1076 1077 1083 public void revokePermissions(Uri uri) 1084 throws ServiceAccessException 1085 { 1086 1087 PreparedStatement statement = null; 1088 1089 try 1090 { 1091 statement = connection.prepareStatement 1092 ("delete from permissions where object= ?"); 1093 statement.setString(1, uri.toString()); 1094 statement.execute(); 1095 } 1096 catch (SQLException e) 1097 { 1098 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1099 throw new ServiceAccessException(this, e); 1100 } 1101 finally 1102 { 1103 closeStatement(statement); 1104 } 1105 } 1106 1107 1108 1114 public Enumeration enumeratePermissions(Uri uri) 1115 throws ServiceAccessException 1116 { 1117 1118 Vector permissionVector = new Vector (); 1119 PreparedStatement statement = null; 1120 1121 try 1122 { 1123 statement = connection.prepareStatement 1124 ("select * from permissions where object= ?"); 1125 statement.setString(1, uri.toString()); 1126 ResultSet res = statement.executeQuery(); 1127 1128 while (res.next()) 1129 { 1130 String object = res.getString(PERMISSIONS_OBJECT); 1131 String revision = res.getString(PERMISSIONS_REVISION_NUMBER); 1132 String subject = res.getString(PERMISSIONS_SUBJECT); 1133 String action = res.getString(PERMISSIONS_ACTION); 1134 1135 boolean inheritable = false; 1136 if (res.getInt(PERMISSIONS_INHERITABLE) == 1) 1137 { 1138 inheritable = true; 1139 } 1140 boolean negative = false; 1141 if (res.getInt(PERMISSIONS_NEGATIVE) == 1) 1142 { 1143 negative = true; 1144 } 1145 NodePermission permission = 1146 new NodePermission(object,revision,subject, 1147 action,inheritable,negative); 1148 permissionVector.addElement(permission); 1149 } 1150 } 1151 catch (SQLException e) 1152 { 1153 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1154 throw new ServiceAccessException(this, e); 1155 } 1156 finally 1157 { 1158 closeStatement(statement); 1159 } 1160 1161 return permissionVector.elements(); 1162 } 1163 1164 1165 1171 public void putLock(Uri uri, NodeLock lock) 1172 throws ServiceAccessException 1173 { 1174 1175 PreparedStatement statement = null; 1176 1177 try 1178 { 1179 int inheritable = 0; 1180 if (lock.isInheritable()) 1181 { 1182 inheritable = 1; 1183 } 1184 1185 int exclusive = 0; 1186 if (lock.isExclusive()) 1187 { 1188 exclusive = 1; 1189 } 1190 1191 statement = connection.prepareStatement 1192 ("insert into locks values(?,?,?,?,?,?,?)"); 1193 statement.setString(1, lock.getLockId()); 1194 statement.setString(2, lock.getObjectUri()); 1195 statement.setString(3, lock.getSubjectUri()); 1196 statement.setString(4, lock.getTypeUri()); 1197 statement.setString 1198 (5, String.valueOf(lock.getExpirationDate().getTime())); 1199 statement.setInt(6,inheritable); 1200 statement.setInt(7, exclusive); 1201 statement.execute(); 1202 } 1203 catch (SQLException e) 1204 { 1205 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1206 throw new ServiceAccessException(this, e); 1207 } 1208 finally 1209 { 1210 closeStatement(statement); 1211 } 1212 1213 } 1214 1215 1216 1223 public void renewLock(Uri uri, NodeLock lock) 1224 throws ServiceAccessException, LockTokenNotFoundException 1225 { 1226 1227 PreparedStatement statement = null; 1228 1229 try 1230 { 1231 1232 int inheritable = 0; 1233 if (lock.isInheritable()) 1234 { 1235 inheritable = 1; 1236 } 1237 1238 int exclusive = 0; 1239 if (lock.isExclusive()) 1240 { 1241 exclusive = 1; 1242 } 1243 1244 statement = connection.prepareStatement 1245 ("delete from locks where id=?"); 1246 statement.setString(1, lock.getLockId()); 1247 statement.execute(); 1248 closeStatement(statement); 1249 1250 statement = connection.prepareStatement 1251 ("insert into locks values(?,?,?,?,?,?,?)"); 1252 statement.setString(1, lock.getLockId()); 1253 statement.setString(2, lock.getObjectUri()); 1254 statement.setString(3, lock.getSubjectUri()); 1255 statement.setString(4, lock.getTypeUri()); 1256 statement.setString 1257 (5, String.valueOf(lock.getExpirationDate().getTime())); 1258 statement.setInt(6, inheritable); 1259 statement.setInt(7, exclusive); 1260 statement.execute(); 1261 1262 } 1263 catch (SQLException e) 1264 { 1265 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1266 throw new ServiceAccessException(this, e); 1267 } 1268 finally 1269 { 1270 closeStatement(statement); 1271 } 1272 } 1273 1274 1275 1282 public void removeLock(Uri uri, NodeLock lock) 1283 throws ServiceAccessException, LockTokenNotFoundException 1284 { 1285 1286 Statement statement = null; 1287 1288 try 1289 { 1290 1291 statement = connection.createStatement(); 1292 1293 int inheritable = 0; 1294 if (lock.isInheritable()) 1295 { 1296 inheritable = 1; 1297 } 1298 1299 String s = null; 1300 1301 s = "delete from locks where id='" + lock.getLockId() + "'"; 1302 statement.execute(s); 1303 1304 } 1305 catch (SQLException e) 1306 { 1307 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1308 throw new ServiceAccessException(this, e); 1309 } 1310 finally 1311 { 1312 closeStatement(statement); 1313 } 1314 } 1315 1316 1317 1324 public void killLock(Uri uri, NodeLock lock) 1325 throws ServiceAccessException, LockTokenNotFoundException 1326 { 1327 removeLock(uri, lock); 1328 } 1329 1330 1331 1338 public Enumeration enumerateLocks(Uri uri) 1339 throws ServiceAccessException 1340 { 1341 1342 Vector lockVector = new Vector (); 1343 PreparedStatement statement = null; 1344 1345 try 1346 { 1347 statement = connection.prepareStatement 1348 ("select * from locks where object= ?"); 1349 statement.setString(1, uri.toString()); 1350 statement.execute(); 1351 ResultSet res = statement.getResultSet(); 1352 1353 while (res.next()) 1354 { 1355 Date expirationDate = null; 1356 try 1357 { 1358 Long timeValue = new Long (res.getString 1359 (LOCKS_EXPIRATIONDATE)); 1360 expirationDate = new Date (timeValue.longValue()); 1361 } 1362 catch (NumberFormatException e) 1363 { 1364 expirationDate = new Date (); 1365 } 1366 NodeLock lock = 1367 new NodeLock(res.getString(LOCKS_ID), 1368 res.getString(LOCKS_OBJECT), 1369 res.getString(LOCKS_SUBJECT), 1370 res.getString(LOCKS_TYPE), 1371 expirationDate, 1372 (res.getInt(LOCKS_INHERITABLE) == 1), 1373 (res.getInt(LOCKS_EXCLUSIVE) == 1)); 1374 lockVector.addElement(lock); 1375 } 1376 } 1377 catch (SQLException e) 1378 { 1379 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1380 throw new ServiceAccessException(this, e); 1381 } 1382 finally 1383 { 1384 closeStatement(statement); 1385 } 1386 1387 return lockVector.elements(); 1388 } 1389 1390 1391 1399 public NodeRevisionDescriptors retrieveRevisionDescriptors(Uri uri) 1400 throws ServiceAccessException, RevisionDescriptorNotFoundException 1401 { 1402 1403 NodeRevisionDescriptors revisionDescriptors = null; 1404 PreparedStatement statement = null; 1405 PreparedStatement statement2 = null; 1406 1407 try 1408 { 1409 ResultSet res = null; 1410 1411 NodeRevisionNumber initialRevision = new NodeRevisionNumber(); 1412 Hashtable workingRevisions = new Hashtable (); 1413 Hashtable latestRevisionNumbers = new Hashtable (); 1414 Hashtable branches = new Hashtable (); 1415 boolean isVersioned = false; 1416 1417 statement = connection.prepareStatement 1418 ("select * from revisions where uri= ?"); 1419 statement.setString(1, uri.toString()); 1420 res = statement.executeQuery(); 1421 1422 if (res.next()) 1423 { 1424 int isVersionedInt = res.getInt(REVISIONS_ISVERSIONED); 1425 if (isVersionedInt == 1) 1426 { 1427 isVersioned = true; 1428 } 1429 } 1430 else 1431 { 1432 throw new RevisionDescriptorNotFoundException(uri.toString()); 1433 } 1434 1435 closeStatement(statement); 1436 1437 statement = connection.prepareStatement 1438 ("select * from workingrevision where uri= ?"); 1439 statement.setString(1, uri.toString()); 1440 res = statement.executeQuery(); 1441 1442 while(res.next()) 1443 { 1444 } 1446 1447 closeStatement(statement); 1448 1449 statement = connection.prepareStatement 1450 ("select * from latestrevisions where uri=?"); 1451 statement.setString(1, uri.toString()); 1452 res = statement.executeQuery(); 1453 1454 while(res.next()) 1455 { 1456 latestRevisionNumbers 1457 .put(res.getString(LATESTREVISIONS_BRANCHNAME), 1458 new NodeRevisionNumber 1459 (res.getString(LATESTREVISIONS_NUMBER))); 1460 } 1461 closeStatement(statement); 1462 1463 statement = connection.prepareStatement 1464 ("select * from revision where uri= ?"); 1465 statement.setString(1, uri.toString()); 1466 res = statement.executeQuery(); 1467 1468 while(res.next()) 1469 { 1470 String currentRevisionNumber = res.getString(REVISION_NUMBER); 1471 1472 if (statement2 == null) 1474 { 1475 statement2 = connection.prepareStatement 1476 ("select * from branches where uri = ? and xnumber = ?"); 1477 } 1478 statement2.setString(1, uri.toString()); 1479 statement2.setString(2, currentRevisionNumber); 1480 ResultSet res2 = statement2.executeQuery(); 1481 Vector childList = new Vector (); 1482 1483 while (res2.next()) 1484 { 1485 childList.addElement(new NodeRevisionNumber 1486 (res2.getString(BRANCHES_CHILDNUMBER))); 1487 } 1488 1489 branches.put(new NodeRevisionNumber(currentRevisionNumber), 1490 childList); 1491 1492 res2.close(); 1493 } 1494 closeStatement(statement2); 1495 1496 revisionDescriptors = new NodeRevisionDescriptors 1497 (uri.toString(), initialRevision, workingRevisions, 1498 latestRevisionNumbers, branches, isVersioned); 1499 1500 } 1501 catch (SQLException e) 1502 { 1503 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1504 throw new ServiceAccessException(this, e); 1505 } 1506 finally 1507 { 1508 closeStatement(statement); 1509 closeStatement(statement2); 1510 } 1511 return revisionDescriptors; 1512 } 1513 1514 1515 1522 public void createRevisionDescriptors 1523 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 1524 throws ServiceAccessException 1525 { 1526 1527 1530 PreparedStatement statement = null; 1531 1532 try 1533 { 1534 ResultSet res = null; 1535 1536 1538 int isVersioned = 0; 1539 if (revisionDescriptors.isVersioned()) 1540 { 1541 isVersioned = 1; 1542 } 1543 1544 statement = connection.prepareStatement 1545 ("insert into revisions values(?,?,?)"); 1546 statement.setString(1,uri.toString()); 1547 statement.setInt(2, isVersioned); 1548 statement.setString 1549 (3, revisionDescriptors.getInitialRevision().toString()); 1550 statement.execute(); 1551 closeStatement(statement); 1552 1553 1556 1558 if (revisionDescriptors.getLatestRevision() != null) 1560 { 1561 statement = connection.prepareStatement 1562 ("insert into latestrevisions values(?,?,?)"); 1563 statement.setString(1, uri.toString()); 1564 statement.setString 1565 (2, NodeRevisionDescriptors.MAIN_BRANCH.toString()); 1566 statement.setString 1567 (3, revisionDescriptors.getLatestRevision().toString()); 1568 statement.execute(); 1569 closeStatement(statement); 1570 } 1571 1572 1575 } 1576 catch (SQLException e) 1577 { 1578 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1579 throw new ServiceAccessException(this, e); 1580 } 1581 finally 1582 { 1583 closeStatement(statement); 1584 } 1585 1586 } 1587 1588 1589 1598 public void storeRevisionDescriptors 1599 (Uri uri, NodeRevisionDescriptors revisionDescriptors) 1600 throws ServiceAccessException, RevisionDescriptorNotFoundException 1601 { 1602 removeRevisionDescriptors(uri); 1603 createRevisionDescriptors(uri, revisionDescriptors); 1604 } 1605 1606 1607 1613 public void removeRevisionDescriptors(Uri uri) 1614 throws ServiceAccessException 1615 { 1616 1617 PreparedStatement statement = null; 1618 1619 try 1620 { 1621 statement = connection.prepareStatement 1622 ("delete from revisions where uri= ?"); 1623 statement.setString(1, uri.toString()); 1624 statement.execute(); 1625 closeStatement(statement); 1626 1627 statement = connection.prepareStatement 1628 ("delete from workingrevision where uri= ?"); 1629 statement.setString(1, uri.toString()); 1630 statement.execute(); 1631 closeStatement(statement); 1632 1633 statement = connection.prepareStatement 1634 ("delete from latestrevisions where uri= ?"); 1635 statement.setString(1, uri.toString()); 1636 statement.execute(); 1637 closeStatement(statement); 1638 1639 statement = connection.prepareStatement 1640 ("delete from branches where uri= ?"); 1641 statement.setString(1, uri.toString()); 1642 statement.execute(); 1643 closeStatement(statement); 1644 } 1645 catch (SQLException e) 1646 { 1647 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1648 throw new ServiceAccessException(this, e); 1649 } 1650 finally 1651 { 1652 closeStatement(statement); 1653 } 1654 } 1655 1656 1657 1663 public NodeRevisionDescriptor retrieveRevisionDescriptor 1664 (Uri uri, NodeRevisionNumber revisionNumber) 1665 throws ServiceAccessException, RevisionDescriptorNotFoundException 1666 { 1667 1668 NodeRevisionDescriptor revisionDescriptor = null; 1669 PreparedStatement statement = null; 1670 1671 if(revisionNumber == null) 1672 { 1673 throw new RevisionDescriptorNotFoundException(uri.toString()); 1674 } 1675 1676 try 1677 { 1678 ResultSet res = null; 1679 1680 String branchName = null; 1681 Vector labels = new Vector (); 1682 Hashtable properties = new Hashtable (); 1683 1684 1687 statement = connection.prepareStatement 1688 ("select * from revision where uri= ? and xnumber = ?"); 1689 statement.setString(1, uri.toString()); 1690 statement.setString(2, revisionNumber.toString()); 1691 res = statement.executeQuery(); 1692 1693 if (res.next()) 1694 { 1695 branchName = res.getString(REVISION_BRANCHNAME); 1696 } 1697 else 1698 { 1699 throw new RevisionDescriptorNotFoundException(uri.toString()); 1700 } 1701 1702 closeStatement(statement); 1703 1704 1706 statement = connection.prepareStatement 1707 ("select * from label where uri= ? and xnumber = ?"); 1708 statement.setString(1, uri.toString()); 1709 statement.setString(2, revisionNumber.toString()); 1710 res = statement.executeQuery(); 1711 1712 while (res.next()) 1713 { 1714 labels.addElement(res.getString(LABEL_LABEL)); 1715 } 1716 1717 closeStatement(statement); 1718 1719 1721 statement = connection.prepareStatement 1722 ("select * from property where uri= ? and xnumber = ?"); 1723 statement.setString(1, uri.toString()); 1724 statement.setString(2, revisionNumber.toString()); 1725 res = statement.executeQuery(); 1726 1727 while (res.next()) 1728 { 1729 String propertyName = res.getString(PROPERTY_NAME); 1730 String propertyNamespace = res.getString(PROPERTY_NAMESPACE); 1731 NodeProperty property = 1732 new NodeProperty(propertyName, 1733 res.getString(PROPERTY_VALUE), 1734 propertyNamespace, 1735 res.getString(PROPERTY_TYPE), 1736 (res.getInt(PROPERTY_PROTECTED) == 1)); 1737 properties.put(propertyNamespace + propertyName, property); 1738 } 1739 1740 revisionDescriptor = 1741 new NodeRevisionDescriptor(revisionNumber, branchName, 1742 labels, properties); 1743 } 1744 catch (SQLException e) 1745 { 1746 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1747 throw new ServiceAccessException(this, e); 1748 } 1749 finally 1750 { 1751 closeStatement(statement); 1752 } 1753 1754 return revisionDescriptor; 1755 } 1756 1757 1758 1765 public void createRevisionDescriptor 1766 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 1767 throws ServiceAccessException 1768 { 1769 1770 PreparedStatement statement = null; 1771 1772 try 1773 { 1774 1775 ResultSet res = null; 1776 1777 statement = connection.prepareStatement 1778 ("insert into revision values(?, ?, ?)"); 1779 statement.setString(1, uri.toString()); 1780 statement.setString 1781 (2, revisionDescriptor.getRevisionNumber().toString()); 1782 statement.setString(3, revisionDescriptor.getBranchName()); 1783 statement.execute(); 1784 closeStatement(statement); 1785 1786 statement = null; 1788 Enumeration labels = revisionDescriptor.enumerateLabels(); 1789 while (labels.hasMoreElements()) 1790 { 1791 if (statement == null) 1792 { 1793 statement = connection.prepareStatement 1794 ("insert into label values(?,?,?)"); 1795 } 1796 statement.setString(1, uri.toString()); 1797 statement.setString 1798 (2, revisionDescriptor.getRevisionNumber().toString()); 1799 statement.setString(3, (String )labels.nextElement()); 1800 statement.execute(); 1801 } 1802 closeStatement(statement); 1803 1804 statement = null; 1806 Enumeration properties = revisionDescriptor.enumerateProperties(); 1807 while (properties.hasMoreElements()) 1808 { 1809 NodeProperty property = 1810 (NodeProperty) properties.nextElement(); 1811 int protectedProperty = 0; 1812 if (property.isProtected()) 1813 { 1814 protectedProperty = 1; 1815 } 1816 if (statement == null) 1817 { 1818 statement = connection.prepareStatement 1819 ("insert into property values(?,?,?,?,?,?,?)"); 1820 } 1821 statement.setString(1, uri.toString()); 1822 statement.setString 1823 (2, revisionDescriptor.getRevisionNumber().toString()); 1824 statement.setString(3, property.getName()); 1825 statement.setString(4, property.getValue().toString()); 1826 statement.setString(5, property.getNamespace()); 1827 statement.setString(6, property.getType()); 1828 statement.setInt(7, protectedProperty); 1829 statement.execute(); 1830 } 1831 closeStatement(statement); 1832 1833 } 1834 catch (SQLException e) 1835 { 1836 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1837 throw new ServiceAccessException(this, e); 1838 } 1839 finally 1840 { 1841 closeStatement(statement); 1842 } 1843 } 1844 1845 1846 1855 public void storeRevisionDescriptor 1856 (Uri uri, NodeRevisionDescriptor revisionDescriptor) 1857 throws ServiceAccessException, RevisionDescriptorNotFoundException 1858 { 1859 removeRevisionDescriptor(uri, revisionDescriptor.getRevisionNumber()); 1860 createRevisionDescriptor(uri, revisionDescriptor); 1861 } 1862 1863 1864 1871 public void removeRevisionDescriptor(Uri uri, NodeRevisionNumber number) 1872 throws ServiceAccessException 1873 { 1874 1875 PreparedStatement statement = null; 1876 1877 try 1878 { 1879 statement = connection.prepareStatement 1880 ("delete from revision where uri= ? and xnumber = ?"); 1881 statement.setString(1, uri.toString()); 1882 statement.setString(2, number.toString()); 1883 statement.execute(); 1884 closeStatement(statement); 1885 1886 1888 statement = connection.prepareStatement 1889 ("delete from label where uri= ? and xnumber = ?"); 1890 statement.setString(1, uri.toString()); 1891 statement.setString(2, number.toString()); 1892 statement.execute(); 1893 closeStatement(statement); 1894 1895 1897 statement = connection.prepareStatement 1898 ("delete from property where uri= ? and xnumber = ?"); 1899 statement.setString(1, uri.toString()); 1900 statement.setString(2, number.toString()); 1901 statement.execute(); 1902 1903 } 1904 catch (SQLException e) 1905 { 1906 getLogger().log(e,LOG_CHANNEL,Logger.ERROR); 1907 throw new ServiceAccessException(this, e); 1908 } 1909 finally 1910 { 1911 closeStatement(statement); 1912 } 1913 1914 } 1915 1916 1917 1919 1920 1923 protected void closeStatement(Statement statement) 1924 { 1925 if (statement != null) 1926 { 1927 try 1928 { 1929 statement.close(); 1930 } 1931 catch (SQLException e) 1932 { 1933 } 1934 } 1935 } 1936} 1937 | Popular Tags |