001: package org.enhydra.shark.admin.repositorymanagement;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005: import java.io.FileOutputStream;
006: import java.util.ArrayList;
007: import java.util.Collection;
008: import java.util.Collections;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012:
013: import org.enhydra.shark.api.admin.RepositoryMgr;
014: import org.enhydra.shark.utilities.MiscUtilities;
015: import org.enhydra.shark.utilities.SequencedHashMap;
016: import org.enhydra.shark.xpdl.XMLUtil;
017:
018: /**
019: * @author Nenad Stefanovic, Sasa Bojanic
020: */
021: public class RepositoryManager implements RepositoryMgr {
022: private static PackageFileFilter packageFileFilter = new PackageFileFilter();
023: private static ArisxmlFileFilterAris arisxmlFileFilter = new ArisxmlFileFilterAris();
024:
025: private static RepositoryMgr anInstance;
026:
027: private static String EXTERNAL_PACKAGES_REPOSITORY = "repository/external";
028:
029: private RepositoryManager() {
030: }
031:
032: public static RepositoryMgr getInstance() {
033: if (null == anInstance) {
034: synchronized (RepositoryManager.class) {
035: if (null == anInstance) {
036: anInstance = new RepositoryManager();
037: try {
038: anInstance
039: .setPathToXPDLRepositoryFolder(EXTERNAL_PACKAGES_REPOSITORY);
040: } catch (Exception ex) {
041: }
042: }
043: }
044: }
045: return anInstance;
046: }
047:
048: public void setPathToXPDLRepositoryFolder(String xpdlRepFolder)
049: throws Exception {
050: RepositoryManager.EXTERNAL_PACKAGES_REPOSITORY = xpdlRepFolder;
051: File f = new File(xpdlRepFolder);
052: if (!f.isAbsolute()) {
053: f = f.getAbsoluteFile();
054: }
055:
056: if (!f.exists()) {
057: throw new Exception("Folder " + xpdlRepFolder
058: + " does not exist");
059: }
060: try {
061: EXTERNAL_PACKAGES_REPOSITORY = f.getCanonicalPath();
062: } catch (Exception ex) {
063: EXTERNAL_PACKAGES_REPOSITORY = f.getAbsolutePath();
064: }
065: }
066:
067: public String getPathToXPDLRepositoryFolder() throws Exception {
068: return EXTERNAL_PACKAGES_REPOSITORY;
069: }
070:
071: public void deletePackage(String relativePath) throws Exception {
072: String path = EXTERNAL_PACKAGES_REPOSITORY + File.separator
073: + relativePath;
074: File pkgFile = new File(path);
075: if (!pkgFile.exists() || pkgFile.isDirectory()) {
076: System.err
077: .println("RepositoryManager -> deleting of the file "
078: + path
079: + " failed because it does not exist, or it is a directory");
080: throw new Exception(
081: "RepositoryManager -> deleting of the file "
082: + path
083: + " failed because it does not exist, or it is a directory");
084: }
085: boolean isDeleted = false;
086: try {
087: isDeleted = pkgFile.delete();
088: } catch (Exception ex) {
089: System.err
090: .println("RepositoryManager -> deleting of the file "
091: + path + " failed - it might be locked");
092: throw new Exception(
093: "RepositoryManager -> deleting of the file " + path
094: + " failed - it might be locked");
095: }
096: if (!isDeleted) {
097: throw new Exception(
098: "RepositoryManager -> deleting of the file " + path
099: + " NOT deleted");
100: }
101: System.out.println("RepositoryManager -> file " + path
102: + " is successfully deleted from repository");
103:
104: }
105:
106: public void uploadPackage(byte[] pkgContent, String relativePath)
107: throws Exception {
108: String path = EXTERNAL_PACKAGES_REPOSITORY + File.separator
109: + relativePath;
110: File pkgFile = new File(path);
111: File parentFile = pkgFile.getParentFile();
112: // if the file exists, or the parent file exists and it is not directory -> can't
113: // create
114: if (pkgFile.exists()
115: || (parentFile.exists() && !parentFile.isDirectory())) {
116: System.err
117: .println("RepositoryManager -> upload of the file "
118: + path
119: + " failed because it already exists or its parent is not directory");
120: throw new Exception(
121: "RepositoryManager -> upload of the file "
122: + path
123: + " failed because it already exists or its parent is not directory");
124: }
125:
126: if (!parentFile.exists()) {
127: parentFile.mkdirs();
128: }
129:
130: try {
131: FileOutputStream fos = new FileOutputStream(pkgFile);
132: fos.write(pkgContent);
133: // Write to file
134: fos.flush();
135: fos.close();
136: } catch (Exception ex) {
137: System.err
138: .println("RepositoryManager -> upload of the file "
139: + path + " failed");
140: throw ex;
141: }
142:
143: System.out.println("RepositoryManager -> file " + path
144: + " is successfully uploaded into repository");
145: }
146:
147: public String[] getPackagePaths() throws Exception {
148: List packageFiles = getDefinedPackageFiles(EXTERNAL_PACKAGES_REPOSITORY);
149: Collection pfls = new ArrayList();
150: Iterator pfi = packageFiles.iterator();
151: Collections.sort(packageFiles);
152: while (pfi.hasNext()) {
153: File f = (File) pfi.next();
154: String fileName;
155: try {
156: fileName = f.getCanonicalPath();
157: } catch (Exception ex) {
158: fileName = f.getAbsolutePath();
159: }
160: fileName = fileName.substring(EXTERNAL_PACKAGES_REPOSITORY
161: .length() + 1);
162: pfls.add(fileName);
163: }
164: String[] pfs = new String[pfls.size()];
165: pfls.toArray(pfs);
166: return pfs;
167: }
168:
169: public String[] getArisxmlPaths() throws Exception {
170: List arisxmlFiles = getDefinedArisXmlFiles(EXTERNAL_PACKAGES_REPOSITORY);
171: Collection pfls = new ArrayList();
172: Iterator pfi = arisxmlFiles.iterator();
173: Collections.sort(arisxmlFiles);
174: while (pfi.hasNext()) {
175: File f = (File) pfi.next();
176: String fileName;
177: try {
178: fileName = f.getCanonicalPath();
179: } catch (Exception ex) {
180: fileName = f.getAbsolutePath();
181: }
182: fileName = fileName.substring(EXTERNAL_PACKAGES_REPOSITORY
183: .length() + 1);
184: pfls.add(fileName);
185: }
186: String[] pfs = new String[pfls.size()];
187: pfls.toArray(pfs);
188: return pfs;
189: }
190:
191: public Map getPackagePathToIdMapping() throws Exception {
192: List packageFiles = getDefinedPackageFiles(EXTERNAL_PACKAGES_REPOSITORY);
193: Map m = new SequencedHashMap();
194: Iterator pfi = packageFiles.iterator();
195: Collections.sort(packageFiles);
196: while (pfi.hasNext()) {
197: File f = (File) pfi.next();
198: String fileName;
199: try {
200: fileName = f.getCanonicalPath();
201: } catch (Exception ex) {
202: fileName = f.getAbsolutePath();
203: }
204: String pkgId = XMLUtil.getIdFromFile(fileName);
205: if (pkgId != null && pkgId.length() > 0) {
206: fileName = fileName
207: .substring(EXTERNAL_PACKAGES_REPOSITORY
208: .length() + 1);
209: m.put(fileName, pkgId);
210: }
211: }
212: return m;
213: }
214:
215: public String getPackageId(String relativePath) throws Exception {
216: relativePath = XMLUtil.convertToSystemPath(relativePath);
217: List packageFiles = getDefinedPackageFiles(EXTERNAL_PACKAGES_REPOSITORY);
218: Iterator pfi = packageFiles.iterator();
219: while (pfi.hasNext()) {
220: File f = (File) pfi.next();
221: String fileName;
222: try {
223: fileName = f.getCanonicalPath();
224: } catch (Exception ex) {
225: fileName = f.getAbsolutePath();
226: }
227: String relFileName = fileName
228: .substring(EXTERNAL_PACKAGES_REPOSITORY.length() + 1);
229: if (relFileName.equals(relativePath)) {
230: return XMLUtil.getIdFromFile(fileName);
231: }
232: }
233: return null;
234: }
235:
236: static List getDefinedPackageFiles(String repository) {
237: File startingFolder = new File(repository);
238: List packageFiles = new ArrayList();
239: if (!startingFolder.exists()) {
240: System.out.println("RepositoryManager -> Repository "
241: + startingFolder + " doesn't exist");
242: }
243: MiscUtilities.traverse(startingFolder, packageFiles,
244: packageFileFilter);
245: // packageFiles = Arrays.asList(startingFolder.listFiles(packageFileFilter));
246:
247: return packageFiles;
248: }
249:
250: static List getDefinedArisXmlFiles(String repository) {
251: File startingFolder = new File(repository);
252: List packageFiles = new ArrayList();
253: if (!startingFolder.exists()) {
254: System.out.println("RepositoryManager -> Repository "
255: + startingFolder + " doesn't exist");
256: }
257: MiscUtilities.traverse(startingFolder, packageFiles,
258: arisxmlFileFilter);
259: // packageFiles = Arrays.asList(startingFolder.listFiles(packageFileFilter));
260:
261: return packageFiles;
262: }
263:
264: }
265:
266: class PackageFileFilter implements FileFilter {
267: public boolean accept(File pathname) {
268: return pathname.isDirectory()
269: || pathname.getName().endsWith(".xpdl");
270: }
271: }
272:
273: class ArisxmlFileFilterAris implements FileFilter {
274: public boolean accept(File pathname) {
275: return pathname.isDirectory()
276: || pathname.getName().endsWith(".aml");
277: }
278: }
|