1 package org.enhydra.shark; 2 3 import java.util.*; 4 5 import java.io.File ; 6 import java.io.FileOutputStream ; 7 import org.enhydra.shark.api.client.wfbase.BaseException; 8 import org.enhydra.shark.api.client.wfservice.RepositoryInvalid; 9 import org.enhydra.shark.api.client.wfservice.RepositoryMgr; 10 import org.enhydra.shark.api.internal.working.CallbackUtilities; 11 import org.enhydra.shark.utilities.SequencedHashMap; 12 import org.enhydra.shark.xpdl.XMLInterface; 13 import org.enhydra.shark.xpdl.XMLInterfaceForJDK13; 14 import org.enhydra.shark.xpdl.XMLUtil; 15 import org.enhydra.shark.xpdl.elements.Package; 16 17 18 22 public class RepositoryManager implements RepositoryMgr { 23 24 private XMLInterface xmlInterface=SharkEngineManager.getInstance().getXMLInterface(); 25 26 private CallbackUtilities cus; 27 private String userId="Unknown"; 28 29 protected RepositoryManager () { 30 this.cus=SharkEngineManager.getInstance().getCallbackUtilities(); 31 } 32 33 public void connect (String userId) { 34 this.userId=userId; 35 } 36 37 public void deletePackage (String relativePath) throws BaseException, RepositoryInvalid { 38 String path=SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY+File.separator+relativePath; 39 File pkgFile=new File (path); 40 if (!pkgFile.exists() || pkgFile.isDirectory()) { 41 cus.error("RepositoryManager -> deleting of the file "+path+" failed because it does not exist, or it is a directory"); 42 throw new BaseException("RepositoryManager -> deleting of the file "+path+" failed because it does not exist, or it is a directory"); 43 } 44 boolean isDeleted=false; 45 try { 46 isDeleted=pkgFile.delete(); 47 } catch (Exception ex) { 48 cus.error("RepositoryManager -> deleting of the file "+path+" failed - it might be locked"); 49 throw new BaseException("RepositoryManager -> deleting of the file "+path+" failed - it might be locked"); 50 } 51 if (!isDeleted) { 52 throw new BaseException("RepositoryManager -> deleting of the file "+path+" NOT deleted"); 53 } 54 cus.info("RepositoryManager -> file "+path+" is successfully deleted from repository"); 55 56 } 57 58 public void uploadPackage (byte[] pkgContent, String relativePath) throws BaseException, RepositoryInvalid { 59 String path=SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY+File.separator+relativePath; 60 File pkgFile=new File (path); 61 File parentFile=pkgFile.getParentFile(); 62 if (pkgFile.exists() || (parentFile.exists() && !parentFile.isDirectory())) { 64 cus.error("RepositoryManager -> upload of the file "+path+" failed because it already exists or its parent is not directory"); 65 throw new BaseException("RepositoryManager -> upload of the file "+path+" failed because it already exists or its parent is not directory"); 66 } 67 68 if (!parentFile.exists()) { 69 try { 70 parentFile.mkdirs(); 71 } catch (Exception ex) { 72 throw new BaseException(ex); 73 } 74 } 75 76 try { 77 FileOutputStream fos = new FileOutputStream (pkgFile); 78 fos.write(pkgContent); 79 fos.flush(); 81 fos.close(); 82 } catch (Exception ex) { 83 cus.error("RepositoryManager -> upload of the file "+path+" failed"); 84 throw new BaseException(ex); 85 } 86 87 XMLInterface xpdlManager=new XMLInterfaceForJDK13(); 90 Package updatedPkg=xpdlManager.openPackage(path,false); 91 SharkPackageValidator pv=new SharkPackageValidator(updatedPkg,xpdlManager,true); 92 String xpdlValidationErrors=""; 93 if (updatedPkg==null || !pv.validateAll(true)) { 94 if (updatedPkg!=null) { 95 xpdlValidationErrors=pv.createXPDLValidationErrorsString(); 96 } else { 97 xpdlValidationErrors="Fatal error while checking package"; 98 } 99 xpdlManager.closeAllPackages(); 100 101 try { 103 pkgFile.delete(); 104 } catch (Exception ex) {} 105 cus.error("RepositoryManager -> upload of the file "+path+" failed because the package is not valid"); 106 throw new RepositoryInvalid(xpdlValidationErrors,"Error while uploading package to repository"); 107 } else { 108 xpdlManager.closeAllPackages(); 109 } 110 cus.info("RepositoryManager -> file "+path+" is successfully uploaded into repository"); 111 } 112 113 114 public String [] getPackagePaths () throws BaseException { 115 List packageFiles=SharkUtilities.getDefinedPackageFiles(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY,true); 116 Collection pfls=new ArrayList(); 117 Iterator pfi=packageFiles.iterator(); 118 Collections.sort(packageFiles); 119 while (pfi.hasNext()) { 120 File f=(File )pfi.next(); 121 String fileName; 122 try { 123 fileName=f.getCanonicalPath(); 124 } catch (Exception ex) { 125 fileName=f.getAbsolutePath(); 126 } 127 fileName=fileName.substring(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY.length()+1); 128 pfls.add(fileName); 129 } 130 String [] pfs=new String [pfls.size()]; 131 pfls.toArray(pfs); 132 return pfs; 133 } 134 135 public Map getPackagePathToIdMapping () throws BaseException { 136 List packageFiles=SharkUtilities.getDefinedPackageFiles(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY,true); 137 Map m=new SequencedHashMap(); 138 Iterator pfi=packageFiles.iterator(); 139 Collections.sort(packageFiles); 140 while (pfi.hasNext()) { 141 File f=(File )pfi.next(); 142 String fileName; 143 try { 144 fileName=f.getCanonicalPath(); 145 } catch (Exception ex) { 146 fileName=f.getAbsolutePath(); 147 } 148 String pkgId=xmlInterface.getIdFromFile(fileName); 149 if (pkgId!=null && pkgId.length()>0) { 150 fileName=fileName.substring(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY.length()+1); 151 m.put(fileName,pkgId); 152 } 153 } 154 return m; 155 } 156 157 public String getPackageId (String relativePath) throws BaseException { 158 String pkgId=""; 159 relativePath=XMLUtil.convertToSystemPath(relativePath); 160 List packageFiles=SharkUtilities.getDefinedPackageFiles(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY,true); 161 Map m=new HashMap(); 162 Iterator pfi=packageFiles.iterator(); 163 while (pfi.hasNext()) { 164 File f=(File )pfi.next(); 165 String fileName; 166 try { 167 fileName=f.getCanonicalPath(); 168 } catch (Exception ex) { 169 fileName=f.getAbsolutePath(); 170 } 171 String relFileName=fileName.substring(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY.length()+1); 172 if (relFileName.equals(relativePath)) { 173 return xmlInterface.getIdFromFile(fileName); 174 } 175 } 176 return null; 177 } 178 179 } 180 | Popular Tags |