1 package org.enhydra.shark.eventaudit; 2 3 4 5 import java.util.*; 6 7 8 9 import org.enhydra.shark.api.RootException; 10 11 import org.enhydra.shark.api.SharkTransaction; 12 13 import org.enhydra.shark.api.internal.eventaudit.*; 14 15 import org.enhydra.shark.api.internal.working.CallbackUtilities; 16 17 18 19 30 31 public class NotifyingEventAuditManager implements EventAuditManagerInterface { 32 33 34 35 public static final EventType DATA_EVENT_TYPE = new EventType(DataEventAuditPersistenceInterface.class); 36 37 38 39 public static final EventType ASSIGNMENT_EVENT_TYPE = new EventType(AssignmentEventAuditPersistenceInterface.class); 40 41 42 43 public static final EventType STATE_EVENT_TYPE = new EventType(StateEventAuditPersistenceInterface.class); 44 45 46 47 public static final EventType CREATION_EVENT_TYPE = new EventType(CreateProcessEventAuditPersistenceInterface.class); 48 49 50 51 53 55 57 58 59 private static EventAuditManagerInterface delegate; 60 61 62 63 private static final Map listeners = new HashMap(); 64 65 66 67 private static CallbackUtilities cus; 68 69 70 71 private static boolean DEBUG; 72 73 74 75 public void configure(CallbackUtilities _cus) throws RootException { 76 77 cus = _cus; 78 79 delegate.configure(cus); 80 81 DEBUG = Boolean.valueOf(cus.getProperty("NotifyingEventAuditManager.Debug", 82 83 "false")) 84 85 .booleanValue(); 86 87 final String clazzName = cus.getProperty("NotifyingEventAuditManager.Delegate", 88 89 "org.enhydra.shark.eventaudit.DODSEventAuditManager"); 90 91 try { 92 93 final Class clazz = Class.forName(clazzName); 94 95 delegate = (EventAuditManagerInterface) clazz.newInstance(); 96 97 } catch (ClassNotFoundException e) { 98 99 cus.error("could not find delegate class", e.getMessage()); 100 101 } catch (IllegalAccessException e) { 102 103 cus.error("cannot access delegate class", e.getMessage()); 104 105 } catch (InstantiationException e) { 106 107 cus.error("cannot instantiate delegate class", e.getMessage()); 108 109 } 110 111 } 112 113 114 115 public void persist(AssignmentEventAuditPersistenceInterface aea, 116 117 SharkTransaction ti) throws EventAuditException { 118 119 delegate.persist(aea, ti); 120 121 fire(aea, ASSIGNMENT_EVENT_TYPE); 122 123 } 124 125 126 127 public void persist(DataEventAuditPersistenceInterface dea, 128 129 SharkTransaction ti) throws EventAuditException { 130 131 delegate.persist(dea, ti); 132 133 fire(dea, DATA_EVENT_TYPE); 134 135 } 136 137 138 139 public void persist(StateEventAuditPersistenceInterface sea, 140 141 SharkTransaction ti) throws EventAuditException { 142 143 delegate.persist(sea, ti); 144 145 fire(sea, STATE_EVENT_TYPE); 146 147 } 148 149 150 151 public void persist(CreateProcessEventAuditPersistenceInterface cpea, 152 153 SharkTransaction ti) throws EventAuditException { 154 155 delegate.persist(cpea, ti); 156 157 fire(cpea, CREATION_EVENT_TYPE); 158 159 } 160 161 162 163 public boolean restore(AssignmentEventAuditPersistenceInterface aea, 164 165 SharkTransaction ti) throws EventAuditException { 166 167 final boolean b = delegate.restore(aea, ti); 168 169 fire(aea, ASSIGNMENT_EVENT_TYPE); 170 171 return b; 172 173 } 174 175 176 177 public boolean restore(DataEventAuditPersistenceInterface dea, 178 179 SharkTransaction ti) throws EventAuditException { 180 181 final boolean b = delegate.restore(dea, ti); 182 183 fire(dea, DATA_EVENT_TYPE); 184 185 return b; 186 187 } 188 189 190 191 public boolean restore(StateEventAuditPersistenceInterface sea, 192 193 SharkTransaction ti) throws EventAuditException { 194 195 final boolean b = delegate.restore(sea, ti); 196 197 fire(sea, STATE_EVENT_TYPE); 198 199 return b; 200 201 } 202 203 204 205 public boolean restore(CreateProcessEventAuditPersistenceInterface cpea, 206 207 SharkTransaction ti) throws EventAuditException { 208 209 final boolean b = delegate.restore(cpea, ti); 210 211 fire(cpea, CREATION_EVENT_TYPE); 212 213 return b; 214 215 } 216 217 218 219 public List restoreProcessHistory(String procId, SharkTransaction ti) throws EventAuditException { 220 221 final List list = delegate.restoreProcessHistory(procId, ti); 222 223 return list; 224 225 } 226 227 228 229 public List restoreActivityHistory(String procId, 230 231 String actId, 232 233 SharkTransaction ti) throws EventAuditException { 234 235 final List list = delegate.restoreActivityHistory(procId, actId, ti); 236 237 return list; 238 239 } 240 241 242 243 public void delete(AssignmentEventAuditPersistenceInterface aea, 244 245 SharkTransaction ti) throws EventAuditException { 246 247 delegate.delete(aea, ti); 248 249 fire(aea, ASSIGNMENT_EVENT_TYPE); 250 251 } 252 253 254 255 public void delete(DataEventAuditPersistenceInterface dea, 256 257 SharkTransaction ti) throws EventAuditException { 258 259 delegate.delete(dea, ti); 260 261 fire(dea, DATA_EVENT_TYPE); 262 263 } 264 265 266 267 public void delete(StateEventAuditPersistenceInterface sea, 268 269 SharkTransaction ti) throws EventAuditException { 270 271 delegate.delete(sea, ti); 272 273 fire(sea, STATE_EVENT_TYPE); 274 275 } 276 277 278 279 public void delete(CreateProcessEventAuditPersistenceInterface cpea, 280 281 SharkTransaction ti) throws EventAuditException { 282 283 delegate.delete(cpea, ti); 284 285 fire(cpea, CREATION_EVENT_TYPE); 286 287 } 288 289 290 291 public AssignmentEventAuditPersistenceInterface createAssignmentEventAudit() { 292 293 final AssignmentEventAuditPersistenceInterface audit = delegate.createAssignmentEventAudit(); 294 295 return audit; 296 297 } 298 299 300 301 public CreateProcessEventAuditPersistenceInterface createCreateProcessEventAudit() { 302 303 final CreateProcessEventAuditPersistenceInterface audit = delegate.createCreateProcessEventAudit(); 304 305 return audit; 306 307 } 308 309 310 311 public DataEventAuditPersistenceInterface createDataEventAudit() { 312 313 final DataEventAuditPersistenceInterface audit = delegate.createDataEventAudit(); 314 315 return audit; 316 317 } 318 319 320 321 public StateEventAuditPersistenceInterface createStateEventAudit() { 322 323 final StateEventAuditPersistenceInterface audit = delegate.createStateEventAudit(); 324 325 return audit; 326 327 } 328 329 330 331 public String getNextId(String idName) throws EventAuditException { 332 333 final String id = delegate.getNextId(idName); 334 335 return id; 336 337 } 338 339 340 341 private static void fire(EventAuditPersistenceInterface persister, 342 343 EventType type) { 344 345 346 347 synchronized (listeners) { 348 349 if (DEBUG) { 350 351 cus.debug("firing event for " 352 353 + (type == null ? "all types " : "type \"" 354 355 + type + "\"") 356 357 + (persister.getActivityId() == null ? "" 358 359 : ", activity \"" 360 361 + persister.getActivityId() 362 363 + "\"") 364 365 + (persister.getProcessId() == null ? "" 366 367 : ", instance \"" 368 369 + persister.getProcessId() 370 371 + "\"") 372 373 + (persister.getProcessDefinitionId() == null ? "" 374 375 : ", process \"" 376 377 + persister.getProcessDefinitionId() 378 379 + "\"") 380 381 + (persister.getPackageId() == null ? "" 382 383 : ", package \"" 384 385 + persister.getPackageId() 386 387 + "\"")); 388 389 } 390 391 392 393 final Set listenerset = new HashSet(); 394 395 listenerset.addAll(collectListeners((Map) listeners.get(null), 396 397 persister)); 398 399 listenerset.addAll(collectListeners((Map) listeners.get(type), 400 401 persister)); 402 403 listenerset.remove(null); 404 405 406 407 for (final Iterator iterator = listenerset.iterator(); iterator.hasNext();) { 408 409 final EventAuditListener listener = (EventAuditListener) iterator.next(); 410 411 final EventAuditEvent event = new EventAuditEvent(NotifyingEventAuditManager.class); 412 413 event.setPersister(persister); 414 415 listener.eventAuditChanged(event); 416 417 } 418 419 } 420 421 } 422 423 424 425 private static Set collectListeners(final Map allTypes, 426 427 EventAuditPersistenceInterface persister) { 428 429 430 431 List list; 432 433 final Set listenerset = new HashSet(); 434 435 if (allTypes != null) { 436 437 list = (List) allTypes.get(null); 438 439 if (list != null) { 440 441 listenerset.addAll(list); 442 443 } 444 445 list = (List) allTypes.get(persister.getPackageId()); 446 447 if (list != null) { 448 449 listenerset.addAll(list); 450 451 } 452 453 list = (List) allTypes.get(persister.getActivityId()); 454 455 if (list != null) { 456 457 listenerset.addAll(list); 458 459 } 460 461 list = (List) allTypes.get(persister.getProcessId()); 462 463 if (list != null) { 464 465 listenerset.addAll(list); 466 467 } 468 469 list = (List) allTypes.get(persister.getProcessDefinitionId()); 470 471 if (list != null) { 472 473 listenerset.addAll(list); 474 475 } 476 477 } 478 479 480 481 return listenerset; 482 483 } 484 485 486 487 public static void addEventAuditListener(final EventAuditListener listener) { 488 489 addEventAuditListener(listener, null); 490 491 } 492 493 494 495 public static void addEventAuditListener(final EventAuditListener listener, 496 497 final EventType type) { 498 499 addEventAuditListener(listener, type, null); 500 501 } 502 503 504 505 public static void addEventAuditListener(final EventAuditListener listener, 506 507 final EventType type, 508 509 final String id) { 510 511 Map hash = (Map) listeners.get(type); 512 513 if (hash == null) { 514 515 hash = new HashMap(); 516 517 listeners.put(type, hash); 518 519 } 520 521 List list = (List) hash.get(id); 522 523 if (list == null) { 524 525 list = new ArrayList(); 526 527 hash.put(id, list); 528 529 } 530 531 list.add(listener); 532 533 } 534 535 536 537 public static void removeEventAuditListener(final EventAuditListener listener) { 538 539 removeEventAuditListener(listener, null, null); 540 541 } 542 543 544 545 public static void removeEventAuditListener(final EventAuditListener listener, 546 547 final EventType type) { 548 549 removeEventAuditListener(listener, type, null); 550 551 } 552 553 554 555 public static void removeEventAuditListener(final EventAuditListener listener, 556 557 final EventType type, 558 559 final String id) { 560 561 final Map hash = (Map) listeners.get(type); 562 563 if (hash == null) { return; } 564 565 final List list = (List) hash.get(id); 566 567 if (list == null) { return; } 568 569 list.remove(listener); 570 571 } 572 573 574 575 private static class EventType { 576 577 578 579 private String name; 580 581 582 583 private EventType(Class type) { 584 585 final String full = type.getName(); 586 587 name = full.substring(full.lastIndexOf('.') + 1); 588 589 } 590 591 592 593 public String toString() { 594 595 return name; 596 597 } 598 599 } 600 601 } | Popular Tags |