1 23 24 package org.apache.slide.lock; 25 26 import java.util.Date ; 27 28 import org.apache.commons.codec.digest.DigestUtils; 29 import org.apache.slide.common.ObjectValidationFailedException; 30 import org.apache.slide.structure.ActionNode; 31 import org.apache.slide.structure.ObjectNode; 32 import org.apache.slide.structure.SubjectNode; 33 import org.apache.slide.util.Messages; 34 35 40 public final class NodeLock implements Cloneable , java.io.Serializable { 41 public final static int SHARED = 0; 42 43 public final static int EXCLUSIVE = 1; 44 45 48 public final static int LOCAL = 2; 49 50 53 protected String objectUri; 54 55 58 protected String subjectUri; 59 60 63 protected String typeUri; 64 65 68 protected Date expirationDate; 69 70 73 protected boolean inheritance; 74 75 78 protected int scope; 79 80 83 protected String lockId; 84 85 88 protected String ownerInfo; 89 90 93 public NodeLock() { 94 } 95 96 110 public NodeLock(ObjectNode locked, SubjectNode user, ActionNode lockType, 111 Date expirationDate, boolean inheritance) { 112 this(locked.getUri(), user.getUri(), lockType.getUri(), expirationDate, 113 inheritance); 114 } 115 116 130 public NodeLock(ObjectNode locked, SubjectNode user, ActionNode lockType, 131 Date expirationDate, boolean inheritance, boolean exclusive) { 132 this(locked.getUri(), user.getUri(), lockType.getUri(), expirationDate, 133 inheritance, exclusive); 134 } 135 136 150 public NodeLock(ObjectNode locked, SubjectNode user, ActionNode lockType, 151 Date expirationDate, boolean inheritance, boolean exclusive, 152 String ownerInfo) { 153 this(locked.getUri(), user.getUri(), lockType.getUri(), expirationDate, 154 inheritance, exclusive, ownerInfo); 155 } 156 157 171 public NodeLock(String objectUri, String subjectUri, String typeUri, 172 Date expirationDate, boolean inheritance) { 173 this(objectUri, subjectUri, typeUri, expirationDate, inheritance, true); 174 } 175 176 190 public NodeLock(String objectUri, String subjectUri, String typeUri, 191 Date expirationDate, boolean inheritance, boolean exclusive) { 192 this(objectUri, subjectUri, typeUri, expirationDate, inheritance, 193 exclusive, null); 194 } 195 196 214 public NodeLock(String objectUri, String subjectUri, String typeUri, 215 Date expirationDate, boolean inheritance, boolean exclusive, 216 String ownerInfo) { 217 this(generateLockID(objectUri.hashCode(), subjectUri.hashCode(), 218 typeUri.hashCode(), (expirationDate == null) ? 0 219 : expirationDate.getTime()), objectUri, subjectUri, 220 typeUri, expirationDate, inheritance, exclusive, ownerInfo); 221 } 222 223 229 public NodeLock(NodeLock lock, String typeUri) { 230 this(lock.getLockId(), lock.getObjectUri(), lock.getSubjectUri(), 231 typeUri, lock.getExpirationDate(), lock.isInheritable(), lock 232 .isExclusive(), lock.getOwnerInfo()); 233 } 234 235 249 public NodeLock(String lockId, String objectUri, String subjectUri, 250 String typeUri, Date expirationDate, boolean inheritance, 251 boolean exclusive) { 252 this(lockId, objectUri, subjectUri, typeUri, expirationDate, 253 inheritance, exclusive, null); 254 } 255 256 274 public NodeLock(String lockId, String objectUri, String subjectUri, 275 String typeUri, Date expirationDate, boolean inheritance, 276 boolean exclusive, String ownerInfo) { 277 this(lockId, objectUri, subjectUri, typeUri, expirationDate,inheritance, exclusive ? EXCLUSIVE : SHARED, ownerInfo); 278 } 279 280 public NodeLock(String objectUri, String subjectUri, String typeUri, 281 Date expirationDate, boolean inheritance, int scope, 282 String ownerInfo) { 283 this(generateLockID(objectUri.hashCode(), subjectUri.hashCode(), 284 typeUri.hashCode(), (expirationDate == null) ? 0 285 : expirationDate.getTime()), objectUri, subjectUri, 286 typeUri, expirationDate, inheritance, scope, ownerInfo); 287 } 288 289 public NodeLock(String lockId, String objectUri, String subjectUri, 290 String typeUri, Date expirationDate, boolean inheritance, 291 int scope, String ownerInfo) { 292 this.objectUri = objectUri; 293 this.subjectUri = subjectUri; 294 this.typeUri = typeUri; 295 this.expirationDate = expirationDate; 296 this.inheritance = inheritance; 297 this.lockId = lockId; 298 this.scope = scope; 299 this.ownerInfo = ownerInfo; 300 } 301 302 315 private static final String generateLockID(int object, int subject, 316 int type, long expires) { 317 long current = System.currentTimeMillis(); 318 byte[] input = new byte[4 + 4 + 4 + 8 + 8]; 319 encode(input, 0, object); 320 encode(input, 4, subject); 321 encode(input, 8, type); 322 if (expires != 0) 323 encode(input, 12, expires); 324 encode(input, 20, current); 325 return new String (DigestUtils.md5Hex(input)); 326 } 327 328 332 private static final void encode(byte[] buf, int offset, int number) { 333 buf[offset + 0] = (byte) ((number) & 0xff); 334 buf[offset + 1] = (byte) ((number >> 8) & 0xff); 335 buf[offset + 2] = (byte) ((number >> 16) & 0xff); 336 buf[offset + 3] = (byte) ((number >> 24) & 0xff); 337 } 338 339 343 private static final void encode(byte[] buf, int offset, long number) { 344 buf[offset + 0] = (byte) ((number) & 0xff); 345 buf[offset + 1] = (byte) ((number >> 8) & 0xff); 346 buf[offset + 2] = (byte) ((number >> 16) & 0xff); 347 buf[offset + 3] = (byte) ((number >> 24) & 0xff); 348 buf[offset + 4] = (byte) ((number >> 32) & 0xff); 349 buf[offset + 5] = (byte) ((number >> 40) & 0xff); 350 buf[offset + 6] = (byte) ((number >> 48) & 0xff); 351 buf[offset + 7] = (byte) ((number >> 56) & 0xff); 352 } 353 354 359 public String getObjectUri() { 360 return this.objectUri; 361 } 362 363 369 public void setObjectUri(String objectUri) { 370 this.objectUri = objectUri; 371 } 372 373 378 public String getSubjectUri() { 379 return this.subjectUri; 380 } 381 382 388 void setSubjectUri(String subjectUri) { 389 this.subjectUri = subjectUri; 390 } 391 392 397 public String getTypeUri() { 398 return this.typeUri; 399 } 400 401 407 void setTypeUri(String typeUri) { 408 this.typeUri = typeUri; 409 } 410 411 416 public Date getExpirationDate() { 417 return expirationDate; 418 } 419 420 426 void setExpirationDate(Date expirationDate) { 427 this.expirationDate = expirationDate; 428 } 429 430 435 public boolean hasExpired() { 436 return (expirationDate.before(new Date ())); 438 } 439 440 445 public boolean isInheritable() { 446 return inheritance; 447 } 448 449 455 void setInheritable(boolean inheritance) { 456 this.inheritance = inheritance; 457 } 458 459 464 public boolean isExclusive() { 465 return (scope == EXCLUSIVE); 466 } 467 468 473 public boolean isShared() { 474 return (scope == SHARED); 475 } 476 477 482 public boolean isLocal() { 483 return (scope == LOCAL); 484 } 485 486 492 void setExclusive(boolean exclusive) { 493 if (exclusive) { 494 scope = EXCLUSIVE; 495 } else { 496 scope = SHARED; 497 } 498 } 499 500 505 public String getLockId() { 506 return lockId; 507 } 508 509 516 public void setOwnerInfo(String ownerInfo) { 517 this.ownerInfo = ownerInfo; 518 } 519 520 526 public String getOwnerInfo() { 527 return ownerInfo; 528 } 529 530 532 537 public NodeLock cloneObject() { 538 NodeLock result = null; 539 try { 540 result = (NodeLock) super.clone(); 541 } catch (CloneNotSupportedException e) { 542 e.printStackTrace(); 543 } 544 return result; 545 } 546 547 556 public boolean equals(Object obj) { 557 boolean result = false; 558 if ((obj != null) && (obj instanceof NodeLock)) { 559 NodeLock lock = (NodeLock) obj; 560 result = this.getLockId().equals(lock.getLockId()); 561 } 562 return result; 563 } 564 565 571 public void validate(String expectedUri) { 572 573 if (objectUri == null) 574 throw new ObjectValidationFailedException(expectedUri, Messages 575 .message(NodeLock.class.getName() + ".nullObjectUri")); 576 577 if (!objectUri.equals(expectedUri)) 578 throw new ObjectValidationFailedException(expectedUri, Messages 579 .message(NodeLock.class.getName() + ".incorrectObjectUri")); 580 581 if (subjectUri == null) 582 throw new ObjectValidationFailedException(expectedUri, Messages 583 .message(NodeLock.class.getName() + ".nullSubjectUri")); 584 585 if (typeUri == null) 586 throw new ObjectValidationFailedException(expectedUri, Messages 587 .message(NodeLock.class.getName() + ".nullTypeUri")); 588 589 if (expirationDate == null) 590 throw new ObjectValidationFailedException(expectedUri, Messages 591 .message(NodeLock.class.getName() + ".nullExpirationDate")); 592 593 if (lockId == null) 594 throw new ObjectValidationFailedException(expectedUri, Messages 595 .message(NodeLock.class.getName() + ".nullLockId")); 596 597 } 598 599 } | Popular Tags |