| 1 package org.enhydra.shark; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.Collections ; 7 import java.util.HashMap ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Set ; 13 14 import org.enhydra.shark.api.ApplicationMappingTransaction; 15 import org.enhydra.shark.api.ParticipantMappingTransaction; 16 import org.enhydra.shark.api.RepositoryTransaction; 17 import org.enhydra.shark.api.RootException; 18 import org.enhydra.shark.api.SharkTransaction; 19 import org.enhydra.shark.api.client.wfbase.BaseException; 20 import org.enhydra.shark.api.client.wfservice.ExternalPackageInvalid; 21 import org.enhydra.shark.api.client.wfservice.PackageAdministration; 22 import org.enhydra.shark.api.client.wfservice.PackageHasActiveProcesses; 23 import org.enhydra.shark.api.client.wfservice.PackageInUse; 24 import org.enhydra.shark.api.client.wfservice.PackageInvalid; 25 import org.enhydra.shark.api.client.wfservice.PackageUpdateNotAllowed; 26 import org.enhydra.shark.api.common.SharkConstants; 27 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager; 28 import org.enhydra.shark.api.internal.partmappersistence.ParticipantMappingManager; 29 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryPersistenceManager; 30 import org.enhydra.shark.api.internal.working.CallbackUtilities; 31 import org.enhydra.shark.api.internal.working.WfProcessMgrInternal; 32 import org.enhydra.shark.xpdl.Version; 33 import org.enhydra.shark.xpdl.XMLInterface; 34 import org.enhydra.shark.xpdl.XMLInterfaceForJDK13; 35 import org.enhydra.shark.xpdl.XMLUtil; 36 import org.enhydra.shark.xpdl.elements.Application; 37 import org.enhydra.shark.xpdl.elements.Applications; 38 import org.enhydra.shark.xpdl.elements.ExternalPackage; 39 import org.enhydra.shark.xpdl.elements.Package; 40 import org.enhydra.shark.xpdl.elements.Participant; 41 import org.enhydra.shark.xpdl.elements.Participants; 42 import org.enhydra.shark.xpdl.elements.WorkflowProcess; 43 import org.enhydra.shark.xpdl.elements.WorkflowProcesses; 44 45 49 public class PackageAdmin implements PackageAdministration { 50 51 private XMLInterface xmlInterface=SharkEngineManager.getInstance().getXMLInterface(); 52 private CallbackUtilities cus; 53 private RepositoryPersistenceManager repMgr; 54 private String userId="Unknown"; 55 56 protected PackageAdmin () { 57 this.cus=SharkEngineManager.getInstance().getCallbackUtilities(); 58 this.repMgr=SharkEngineManager.getInstance().getRepositoryPersistenceManager(); 59 } 60 61 public void connect (String userId) { 62 this.userId=userId; 63 } 64 65 public String [] getOpenedPackageIds () throws BaseException { 66 String [] ret = null; 67 RepositoryTransaction t = null; 68 try { 69 t = SharkUtilities.createRepositoryTransaction(); 70 ret = getOpenedPackageIds(t); 71 } catch (RootException e) { 73 if (e instanceof BaseException) 75 throw (BaseException)e; 76 else 77 throw new BaseException(e); 78 } finally { 79 SharkUtilities.releaseRepositoryTransaction(t); 80 } 81 return ret; 82 } 83 84 public String [] getOpenedPackageIds (RepositoryTransaction t) throws BaseException { 85 try { 86 List pkgIds=repMgr.getExistingXPDLIds(t); 87 Collections.sort(pkgIds); 88 String [] ret=new String [pkgIds.size()]; 89 pkgIds.toArray(ret); 90 return ret; 91 } catch (Exception ex) { 92 throw new BaseException(ex); 93 } 94 } 95 96 public String [] getPackageVersions (String pkgId) throws BaseException { 97 String [] ret = null; 98 RepositoryTransaction t = null; 99 try { 100 t = SharkUtilities.createRepositoryTransaction(); 101 ret = getPackageVersions(t,pkgId); 102 } catch (RootException e) { 104 if (e instanceof BaseException) 106 throw (BaseException)e; 107 else 108 throw new BaseException(e); 109 } finally { 110 SharkUtilities.releaseRepositoryTransaction(t); 111 } 112 return ret; 113 } 114 115 public String [] getPackageVersions (RepositoryTransaction t,String pkgId) throws BaseException { 116 try { 117 List pkgVers=repMgr.getXPDLVersions(t,pkgId); 118 Collections.sort(pkgVers); 119 String [] ret=new String [pkgVers.size()]; 120 pkgVers.toArray(ret); 121 return ret; 122 } catch (Exception ex) { 123 throw new BaseException(ex); 124 } 125 } 126 127 public boolean isPackageOpened (String pkgId) throws BaseException { 128 boolean ret = false; 129 RepositoryTransaction t = null; 130 try { 131 t = SharkUtilities.createRepositoryTransaction(); 132 ret = isPackageOpened(t,pkgId); 133 } catch (RootException e) { 135 if (e instanceof BaseException) 137 throw (BaseException)e; 138 else 139 throw new BaseException(e); 140 } finally { 141 SharkUtilities.releaseRepositoryTransaction(t); 142 } 143 return ret; 144 } 145 146 public boolean isPackageOpened (RepositoryTransaction t,String pkgId) throws BaseException { 147 try { 148 return repMgr.doesXPDLExist(t,pkgId); 149 } catch (Exception ex) { 150 throw new BaseException(ex); 151 } 152 } 153 154 public byte[] getPackageContent (String pkgId) throws BaseException { 155 byte[] ret = null; 156 RepositoryTransaction t = null; 157 try { 158 t = SharkUtilities.createRepositoryTransaction(); 159 ret = getPackageContent(t,pkgId); 160 } catch (RootException e) { 162 if (e instanceof BaseException) 164 throw (BaseException)e; 165 else 166 throw new BaseException(e); 167 } finally { 168 SharkUtilities.releaseRepositoryTransaction(t); 169 } 170 return ret; 171 } 172 173 public byte[] getPackageContent (RepositoryTransaction t,String pkgId) throws BaseException { 174 try { 175 return convertSharkPackageContentXPDLByteArray(repMgr.getSerializedXPDLObject(t,pkgId)); 176 } catch (Throwable ex) { 177 ex.printStackTrace(); 178 throw new BaseException(ex); 179 } 180 } 181 182 public byte[] getPackageContent (String pkgId,String pkgVer) throws BaseException { 183 byte[] ret = null; 184 RepositoryTransaction t = null; 185 try { 186 t = SharkUtilities.createRepositoryTransaction(); 187 ret = getPackageContent(t,pkgId,pkgVer); 188 } catch (RootException e) { 190 if (e instanceof BaseException) 192 throw (BaseException)e; 193 else 194 throw new BaseException(e); 195 } finally { 196 SharkUtilities.releaseRepositoryTransaction(t); 197 } 198 return ret; 199 } 200 201 public byte[] getPackageContent (RepositoryTransaction t,String pkgId,String pkgVer) throws BaseException { 202 try { 203 return convertSharkPackageContentXPDLByteArray(repMgr.getSerializedXPDLObject(t,pkgId,pkgVer)); 204 } catch (Throwable ex) { 205 ex.printStackTrace(); 206 throw new BaseException(ex); 207 } 208 } 209 210 public String getCurrentPackageVersion (String pkgId) throws BaseException { 211 String ret = null; 212 RepositoryTransaction t = null; 213 try { 214 t = SharkUtilities.createRepositoryTransaction(); 215 ret = getCurrentPackageVersion(t,pkgId); 216 } catch (RootException e) { 218 if (e instanceof BaseException) 220 throw (BaseException)e; 221 else 222 throw new BaseException(e); 223 } finally { 224 SharkUtilities.releaseRepositoryTransaction(t); 225 } 226 return ret; 227 } 228 229 public String getCurrentPackageVersion (RepositoryTransaction t,String pkgId) throws BaseException { 230 try { 231 return repMgr.getCurrentVersion(t,pkgId); 232 } catch (Exception ex) { 233 throw new BaseException(ex); 234 } 235 } 236 237 public String openPackage (String relativePath) throws BaseException, PackageInvalid, ExternalPackageInvalid { 238 String ret=null; 239 RepositoryTransaction t = null; 240 try { 241 t = SharkUtilities.createRepositoryTransaction(); 242 ret=openPackage(t,relativePath); 243 SharkUtilities.commitRepositoryTransaction(t); 244 } catch (RootException e) { 245 SharkUtilities.rollbackRepositoryTransaction(t,e); 246 if (e instanceof PackageInvalid) 247 throw (PackageInvalid)e; 248 else if (e instanceof ExternalPackageInvalid) 249 throw (ExternalPackageInvalid)e; 250 else if (e instanceof BaseException) 251 throw (BaseException)e; 252 else 253 throw new BaseException(e); 254 } finally { 255 SharkUtilities.releaseRepositoryTransaction(t); 256 } 257 return ret; 258 } 259 260 public String openPackage (RepositoryTransaction t,String relativePath) throws BaseException, PackageInvalid, ExternalPackageInvalid { 261 return openPackage(t,null,relativePath); 262 } 263 264 public void updatePackage (String id, String relativePathToNewPackage) throws BaseException, PackageUpdateNotAllowed, PackageInvalid, ExternalPackageInvalid { 265 RepositoryTransaction t = null; 266 try { 267 t = SharkUtilities.createRepositoryTransaction(); 268 updatePackage(t,id,relativePathToNewPackage); 269 SharkUtilities.commitRepositoryTransaction(t); 270 } catch (RootException e) { 271 SharkUtilities.rollbackRepositoryTransaction(t,e); 272 if (e instanceof PackageUpdateNotAllowed) 273 throw (PackageUpdateNotAllowed)e; 274 else if (e instanceof PackageInvalid) 275 throw (PackageInvalid)e; 276 else if (e instanceof ExternalPackageInvalid) 277 throw (ExternalPackageInvalid)e; 278 else if (e instanceof BaseException) 279 throw (BaseException)e; 280 else 281 throw new BaseException(e); 282 } finally { 283 SharkUtilities.releaseRepositoryTransaction(t); 284 } 285 } 286 287 public void updatePackage (RepositoryTransaction t,String id, String relativePathToNewPackage) throws BaseException, PackageUpdateNotAllowed, PackageInvalid, ExternalPackageInvalid { 288 if (id==null) throw new BaseException("Invalid package Id"); 289 openPackage(t,id,relativePathToNewPackage); 290 } 291 292 public void closePackage (String pkgId) throws BaseException, PackageInUse, PackageHasActiveProcesses { 293 RepositoryTransaction t = null; 294 try { 295 t = SharkUtilities.createRepositoryTransaction(); 296 closePackage(t,pkgId); 297 SharkUtilities.commitRepositoryTransaction(t); 298 } catch (RootException e) { 299 SharkUtilities.rollbackRepositoryTransaction(t,e); 300 if (e instanceof PackageInUse) 301 throw (PackageInUse)e; 302 else if (e instanceof PackageHasActiveProcesses) 303 throw (PackageHasActiveProcesses)e; 304 else if (e instanceof BaseException) 305 throw (BaseException)e; 306 else 307 throw new BaseException(e); 308 } finally { 309 SharkUtilities.releaseRepositoryTransaction(t); 310 } 311 } 312 313 public synchronized void closePackage (RepositoryTransaction t,String pkgId) throws BaseException, PackageInUse, PackageHasActiveProcesses { 314 cus.info("PackageAdmin -> User "+userId+" is trying to close all package versions of Package with id "+pkgId); 315 316 try { 317 318 SharkUtilities.synchronizeXPDLCache(t); 319 320 if (!repMgr.doesXPDLExist(t,pkgId)) { 321 throw new BaseException("Package with Id="+pkgId+" does not exist"); 322 } 323 if (repMgr.getReferringXPDLIds(t,pkgId).size()>0) { 326 throw new PackageInUse("The package can't be unloaded because it is referenced"); 327 } 328 329 List pkgVers=repMgr.getXPDLVersions(t,pkgId); 330 String curVer=repMgr.getCurrentVersion(t,pkgId); 331 Package curPkg=null; 332 Map pkgs=new HashMap (); 333 for (int i=0; i<pkgVers.size(); i++) { 334 String pkgVer=(String )pkgVers.get(i); 335 Package pkg=SharkUtilities.getPackage(pkgId,pkgVer); 336 if (pkg==null) { 337 throw new BaseException("Package with Id="+pkgId+" - something went wrong while getting XPDL object"); 338 } 339 pkgs.put(pkgVer,pkg); 340 if (pkgVer.equals(curVer)) { 341 curPkg=pkg; 342 } 343 } 344 SharkTransaction st=null; 345 boolean dpe; 346 try { 347 st=SharkUtilities.createTransaction(); 348 Iterator it=pkgs.entrySet().iterator(); 349 while (it.hasNext()) { 350 Map.Entry me=(Map.Entry )it.next(); 351 String pkgVer=(String )me.getKey(); 352 Package pkg=(Package )me.getValue(); 353 354 if (checkDBProcesses(st,pkg,pkgId,pkgVer)) { 357 throw new PackageHasActiveProcesses("Can't remove package for which exists process instance in DB"); } 359 } 360 } catch (Exception ex) { 361 SharkUtilities.emptyCaches(st); 362 throw ex; 363 } finally { 364 SharkUtilities.releaseTransaction(st); 365 } 366 367 368 Iterator it=pkgs.entrySet().iterator(); 369 while (it.hasNext()) { 370 Map.Entry me=(Map.Entry )it.next(); 371 String pkgVer=(String )me.getKey(); 372 Package pkg=(Package )me.getValue(); 373 repMgr.moveToHistory(t,pkgId,pkgVer); 374 375 WfPackageEventAuditImpl pea=new WfPackageEventAuditImpl(pkg,SharkConstants.EVENT_PACKAGE_UNLOADED,userId); 376 xmlInterface.closePackageVersion(pkgId, pkgVer); 378 } 379 removeParticipantMappingsForPackage(curPkg); 380 removeApplicationMappingsForPackage(curPkg); 381 382 removeManagersForPackages(pkgs.values()); 383 384 cus.info("PackageAdmin -> User "+userId+" has closed Package with id "+pkgId); 385 } catch (Exception ex) { 386 cus.warn("PackageAdmin -> User "+userId+" failed to close the Package with id "+pkgId+" because it is in use"); 387 if (ex instanceof BaseException) { 390 throw (BaseException)ex; 391 } else if (ex instanceof PackageInUse) { 392 throw (PackageInUse)ex; 393 } else if (ex instanceof PackageHasActiveProcesses) { 394 throw (PackageHasActiveProcesses)ex; 395 } else { 396 throw new BaseException(ex); 397 } 398 } 399 } 400 401 public void closePackage (String pkgId,String pkgVer) throws BaseException, PackageInUse, PackageHasActiveProcesses { 402 RepositoryTransaction t = null; 403 try { 404 t = SharkUtilities.createRepositoryTransaction(); 405 closePackage(t,pkgId,pkgVer); 406 SharkUtilities.commitRepositoryTransaction(t); 407 } catch (RootException e) { 408 SharkUtilities.rollbackRepositoryTransaction(t,e); 409 if (e instanceof PackageInUse) 410 throw (PackageInUse)e; 411 else if (e instanceof PackageHasActiveProcesses) 412 throw (PackageHasActiveProcesses)e; 413 else if (e instanceof BaseException) 414 throw (BaseException)e; 415 else 416 throw new BaseException(e); 417 } finally { 418 SharkUtilities.releaseRepositoryTransaction(t); 419 } 420 } 421 422 public synchronized void closePackage (RepositoryTransaction t,String pkgId,String pkgVer) throws BaseException, PackageInUse, PackageHasActiveProcesses { 423 cus.info("PackageAdmin -> User "+userId+" is trying to close Package with id "+pkgId+", and version "+pkgVer); 424 425 try { 426 SharkUtilities.synchronizeXPDLCache(t); 427 428 if (!repMgr.doesXPDLExist(t,pkgId,pkgVer)) { 429 throw new BaseException("Package with Id="+pkgId+" and version="+pkgVer+" does not exist"); 430 } 431 432 int hm=repMgr.getXPDLVersions(t,pkgId).size(); 433 434 Package pkg=SharkUtilities.getPackage(pkgId,pkgVer); 435 if (pkg==null) { 436 throw new BaseException("Package with Id="+pkgId+" - something went wrong while getting XPDL object"); 437 } 438 439 String curVer=repMgr.getCurrentVersion(t,pkgId); 440 if (repMgr.getReferringXPDLIds(t,pkgId).size()>0 && pkgVer.equals(curVer)) { 443 throw new PackageInUse("The package can't be unloaded because it is referenced"); 444 } 445 446 SharkTransaction st=null; 447 boolean dpe; 448 try { 449 st=SharkUtilities.createTransaction(); 450 dpe=checkDBProcesses(st,pkg,pkgId,pkgVer); 453 } catch (Exception ex) { 454 SharkUtilities.emptyCaches(st); 455 throw ex; 456 } finally { 457 SharkUtilities.releaseTransaction(st); 458 } 459 460 if (dpe) { 461 throw new PackageHasActiveProcesses("Can't remove package with processes instances in DB"); } 463 464 repMgr.moveToHistory(t,pkgId,pkgVer); 465 466 WfPackageEventAuditImpl pea=new WfPackageEventAuditImpl(pkg,SharkConstants.EVENT_PACKAGE_UNLOADED,userId); 467 xmlInterface.closePackageVersion(pkgId, pkgVer); 469 470 if (hm==1) { 472 removeParticipantMappingsForPackage(pkg); 473 removeApplicationMappingsForPackage(pkg); 474 } 475 removeManagersForPackage(pkg); 476 477 cus.info("PackageAdmin -> User "+userId+" has closed Package with id "+pkgId+" and version "+pkgVer); 478 } catch (Exception ex) { 479 cus.warn("PackageAdmin -> User "+userId+" failed to close the Package with id "+pkgId+" and version "+pkgVer+" because it is in use"); 480 if (ex instanceof BaseException) { 483 throw (BaseException)ex; 484 } else if (ex instanceof PackageInUse) { 485 throw (PackageInUse)ex; 486 } else if (ex instanceof PackageHasActiveProcesses) { 487 throw (PackageHasActiveProcesses)ex; 488 } else { 489 throw new BaseException(ex); 490 } 491 } 492 } 493 494 private boolean checkDBProcesses (SharkTransaction st,Package pkg,String pkgId,String pkgVer) throws Exception { 496 Iterator processes=((WorkflowProcesses)pkg.get("WorkflowProcesses")). 499 toElements().iterator(); 500 while (processes.hasNext()) { 501 WorkflowProcess wp=(WorkflowProcess)processes.next(); 502 String mgrName=SharkUtilities.createProcessMgrKey(pkgId,pkgVer,wp.getId()); 503 WfProcessMgrInternal mgr =SharkUtilities.getProcessMgr(st,mgrName); 504 List procs=SharkEngineManager 505 .getInstance() 506 .getInstancePersistenceManager() 507 .getAllProcessesForMgr(mgrName,st); 508 if (procs!=null && procs.size()>0) { 509 return true; 510 } 511 } 512 return false; 513 } 514 515 516 public boolean isPackageReferenced(String pkgId) throws BaseException { 517 boolean ret=false; 518 RepositoryTransaction t = null; 519 try { 520 t = SharkUtilities.createRepositoryTransaction(); 521 ret=isPackageReferenced(t,pkgId); 522 } catch (RootException e) { 524 if (e instanceof BaseException) 526 throw (BaseException)e; 527 else 528 throw new BaseException(e); 529 } finally { 530 SharkUtilities.releaseRepositoryTransaction(t); 531 } 532 return ret; 533 } 534 535 public boolean isPackageReferenced (RepositoryTransaction t,String pkgId) throws BaseException { 536 try { 537 return repMgr.getReferringXPDLIds(t,pkgId).size()>0; 538 } catch (Exception ex) { 539 throw new BaseException(ex); 540 } 541 } 542 543 public void synchronizeXPDLCache () throws BaseException { 544 RepositoryTransaction t = null; 545 try { 546 t = SharkUtilities.createRepositoryTransaction(); 547 synchronizeXPDLCache(t); 548 } catch (RootException e) { 550 if (e instanceof BaseException) 552 throw (BaseException)e; 553 else 554 throw new BaseException(e); 555 } finally { 556 SharkUtilities.releaseRepositoryTransaction(t); 557 } 558 } 559 560 public synchronized void synchronizeXPDLCache (RepositoryTransaction t) throws BaseException { 561 SharkUtilities.synchronizeXPDLCache(t); 562 } 563 564 public void clearXPDLCache () throws BaseException { 565 try { 566 SharkUtilities.clearProcessCache(); 567 } catch (Exception ex) {} 568 xmlInterface.closeAllPackages(); 569 } 570 571 public synchronized void clearXPDLCache (RepositoryTransaction t) throws BaseException { 572 try { 573 SharkUtilities.clearProcessCache(); 574 } catch (Exception ex) {} 575 xmlInterface.closeAllPackages(); 576 } 577 578 public void refreshXPDLCache () throws BaseException { 579 RepositoryTransaction t = null; 580 try { 581 t = SharkUtilities.createRepositoryTransaction(); 582 refreshXPDLCache(t); 583 } catch (RootException e) { 585 if (e instanceof BaseException) 587 throw (BaseException)e; 588 else 589 throw new BaseException(e); 590 } finally { 591 SharkUtilities.releaseRepositoryTransaction(t); 592 } 593 } 594 595 public synchronized void refreshXPDLCache (RepositoryTransaction t) throws BaseException { 596 try { 597 SharkUtilities.clearProcessCache(); 598 } catch (Exception ex) {} 599 xmlInterface.closeAllPackages(); 600 SharkUtilities.synchronizeXPDLCache(t); 601 } 602 603 private synchronized String openPackage (RepositoryTransaction t,String id,String relativePath) throws BaseException, PackageInvalid, ExternalPackageInvalid { 604 boolean update=(id!=null); 605 if (update) { 606 cus.info("PackageAdmin -> Trying to update Package with id="+id+" with content of file at "+relativePath); 607 } else { 608 cus.info("PackageAdmin -> Trying to open Package file "+relativePath); 609 } 610 int exceptionHappened=0; 612 XMLInterface xpdlManager=new XMLInterfaceForJDK13(); 613 String pkgId=null; 614 Map pkgInfo=new HashMap (); 615 616 relativePath=XMLUtil.convertToSystemPath(relativePath); 617 618 try { 619 pkgId=Shark.getInstance().getRepositoryManager().getPackageId(relativePath); 620 621 if (id!=null && !id.equals(pkgId)) { 622 String msg="The package at path="+relativePath+" does not have the id="+id; 623 cus.warn("PackageAdmin -> "+msg); 624 BaseException bex=new BaseException(msg); 625 throw bex; 626 627 } 628 629 Set currentPkgIds=new HashSet (repMgr.getExistingXPDLIds(t)); 631 632 if (id!=null && !currentPkgIds.contains(pkgId)) { 634 String msg="The package with id "+pkgId+" is not open"; 635 cus.warn("PackageAdmin -> "+msg); 636 BaseException bex=new BaseException(msg); 637 throw bex; 638 } 639 640 641 if (id==null && currentPkgIds.contains(pkgId)) { 643 String msg="The package with id "+pkgId+" is already open"; 644 cus.warn("PackageAdmin -> "+msg); 645 BaseException bex=new BaseException(msg); 646 throw bex; 647 } 648 649 String pkgVersion=repMgr.getNextVersion(t,pkgId); 650 651 relativePath=SharkUtilities.convertToAbsolutePath(relativePath); 652 653 654 Package pkg=xpdlManager.openPackage(relativePath,true); 655 pkg.setInternalVersion(pkgVersion); 656 SharkPackageValidator pv=new SharkPackageValidator(pkg,xpdlManager,true); 658 String xpdlValidationErrors=""; 659 if (pkg==null || !pv.validateAll(true)) { 660 cus.info("PackageAdmin -> Package file "+relativePath+" failed to open"); 661 &nb
|