KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > RepositoryManager


1 package org.enhydra.shark;
2
3 import java.util.*;
4
5 import java.io.File JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
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 /**
19  * @author Nenad Stefanovic, Sasa Bojanic
20  *
21  */

22 public class RepositoryManager implements RepositoryMgr {
23
24    private XMLInterface xmlInterface=SharkEngineManager.getInstance().getXMLInterface();
25
26    private CallbackUtilities cus;
27    private String JavaDoc userId="Unknown";
28
29    protected RepositoryManager () {
30       this.cus=SharkEngineManager.getInstance().getCallbackUtilities();
31    }
32
33    public void connect (String JavaDoc userId) {
34       this.userId=userId;
35    }
36
37    public void deletePackage (String JavaDoc relativePath) throws BaseException, RepositoryInvalid {
38       String JavaDoc path=SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY+File.separator+relativePath;
39       File JavaDoc pkgFile=new File JavaDoc(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 JavaDoc 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 JavaDoc relativePath) throws BaseException, RepositoryInvalid {
59       String JavaDoc path=SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY+File.separator+relativePath;
60       File JavaDoc pkgFile=new File JavaDoc(path);
61       File JavaDoc parentFile=pkgFile.getParentFile();
62       // if the file exists, or the parent file exists and it is not directory -> can't create
63
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 JavaDoc ex) {
72             throw new BaseException(ex);
73          }
74       }
75
76       try {
77          FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(pkgFile);
78          fos.write(pkgContent);
79          // Write to file
80
fos.flush();
81          fos.close();
82       } catch (Exception JavaDoc ex) {
83          cus.error("RepositoryManager -> upload of the file "+path+" failed");
84          throw new BaseException(ex);
85       }
86
87       // if the package user wants to upload is not valid, delete it and
88
// throw proper exception
89
XMLInterface xpdlManager=new XMLInterfaceForJDK13();
90       Package JavaDoc updatedPkg=xpdlManager.openPackage(path,false);
91       SharkPackageValidator pv=new SharkPackageValidator(updatedPkg,xpdlManager,true);
92       String JavaDoc 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          // delete the package file
102
try {
103             pkgFile.delete();
104          } catch (Exception JavaDoc 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 JavaDoc[] 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 JavaDoc f=(File JavaDoc)pfi.next();
121          String JavaDoc fileName;
122          try {
123             fileName=f.getCanonicalPath();
124          } catch (Exception JavaDoc ex) {
125             fileName=f.getAbsolutePath();
126          }
127          fileName=fileName.substring(SharkUtilities.EXTERNAL_PACKAGES_REPOSITORY.length()+1);
128          pfls.add(fileName);
129       }
130       String JavaDoc[] pfs=new String JavaDoc[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 JavaDoc f=(File JavaDoc)pfi.next();
142          String JavaDoc fileName;
143          try {
144             fileName=f.getCanonicalPath();
145          } catch (Exception JavaDoc ex) {
146             fileName=f.getAbsolutePath();
147          }
148          String JavaDoc 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 JavaDoc getPackageId (String JavaDoc relativePath) throws BaseException {
158       String JavaDoc 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 JavaDoc f=(File JavaDoc)pfi.next();
165          String JavaDoc fileName;
166          try {
167             fileName=f.getCanonicalPath();
168          } catch (Exception JavaDoc ex) {
169             fileName=f.getAbsolutePath();
170          }
171          String JavaDoc 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