1 22 package org.objectweb.petals.repository; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.URI ; 27 import java.util.zip.ZipException ; 28 import java.util.zip.ZipFile ; 29 30 import org.objectweb.petals.PetalsException; 31 import org.objectweb.petals.util.FileUtil; 32 import org.objectweb.petals.util.SystemUtil; 33 import org.objectweb.petals.util.ZipUtil; 34 35 42 public class RepositoryImpl implements RepositoryService { 43 44 47 private final static String REPOSITORY_PATH = "repository"; 48 49 52 private final static String INSTALL_PATH = "install"; 53 54 57 private final static String WORK_PATH = "work"; 58 59 60 63 private static String repositoryDirectory = null; 64 65 66 70 73 public boolean containsPackage(String uuid) { 74 75 File installationRoot = getComponentRoot(uuid); 76 77 boolean contain = (installationRoot.exists() && installationRoot.isDirectory()); 78 79 return contain; 80 } 81 82 85 public URI addPackage(String uuid, URI zipArchiveLocation) 86 throws PetalsException { 87 88 if (containsPackage(uuid)) { 90 removePackage(uuid); 91 } 92 93 File installationRoot = new File (getComponentRoot(uuid),INSTALL_PATH); 94 95 File workRoot = new File (new File (installationRoot,".."),WORK_PATH); 96 97 98 if (!installationRoot.mkdirs()) { 100 throw new PetalsException( 101 "Failed to create installation root dir: " + 102 installationRoot.getAbsolutePath()); 103 } 104 if (!workRoot.mkdirs()) { 106 throw new PetalsException( 107 "Failed to create installation root dir: " + 108 workRoot.getAbsolutePath()); 109 } 110 111 ZipFile zipArchive = null; 113 114 try { 115 File archiveFile = new File (zipArchiveLocation); 116 zipArchive = new ZipFile (archiveFile); 117 } catch (ZipException ze) { 118 throw new PetalsException("Unable to open Zip archive file: " + 119 zipArchiveLocation.toString(), ze); 120 } catch (IOException ioe) { 121 throw new PetalsException("Unexpected exception.", ioe); 122 } 123 124 ZipUtil.explodeIntoDirectory(zipArchive, installationRoot); 125 return installationRoot.toURI(); 126 127 } 128 129 132 public URI explodeSUIntoSAInstallFolder(String suId, 133 URI suZipLocation, 134 String saId) 135 throws PetalsException { 136 137 File installationRoot = new File (getComponentInstallRoot(saId),suId); 138 139 if (!installationRoot.mkdirs()) { 141 throw new PetalsException( 142 "Failed to create installation root dir: " + 143 installationRoot.getAbsolutePath()); 144 } 145 146 ZipFile zipArchive = null; 148 149 try { 150 File archiveFile = new File (suZipLocation); 151 zipArchive = new ZipFile (archiveFile); 152 } catch (ZipException ze) { 153 throw new PetalsException("Unable to open Zip archive file: " + 154 suZipLocation.toString(), ze); 155 } catch (IOException ioe) { 156 throw new PetalsException("Unexpected exception.", ioe); 157 } 158 159 ZipUtil.explodeIntoDirectory(zipArchive, installationRoot); 160 return installationRoot.toURI(); 161 162 } 163 164 167 public boolean removePackage(String componentId) { 168 169 boolean removed = true; 170 File installationRoot = getComponentRoot(componentId); 171 172 if (installationRoot.exists()) { 174 removed = FileUtil.removeDirectory(installationRoot); 175 } 176 177 return removed; 178 } 179 180 184 private String getRepositoryDirectory() { 185 if (repositoryDirectory == null) { 186 repositoryDirectory = SystemUtil.getPetalsInstallDirectory() 187 .getAbsolutePath() + File.separator + REPOSITORY_PATH; 188 } 189 return repositoryDirectory; 190 } 191 192 public File getComponentRoot(String componentId) { 193 194 String path = getRepositoryDirectory() + File.separator + componentId; 195 196 File installationRoot = new File (path); 197 198 return installationRoot; 199 } 200 201 public File getComponentInstallRoot(String componentId) { 202 203 String path = getRepositoryDirectory() + File.separator + componentId + File.separator + INSTALL_PATH; 204 205 File installationRoot = new File (path); 206 207 return installationRoot; 208 } 209 210 public File getComponentWorkRoot(String componentId) { 211 212 String path = getRepositoryDirectory() + File.separator + componentId+ File.separator +WORK_PATH; 213 214 File installationRoot = new File (path); 215 216 return installationRoot; 217 } 218 219 } 220 | Popular Tags |