1 19 20 package org.apache.cayenne.project; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 26 import org.apache.cayenne.conf.Configuration; 27 import org.apache.cayenne.util.Util; 28 import org.apache.cayenne.util.ZipUtil; 29 30 35 public class ProjectConfigurator { 36 protected ProjectConfigInfo info; 37 38 public ProjectConfigurator(ProjectConfigInfo info) { 39 this.info = info; 40 } 41 42 47 public void execute() throws ProjectException { 48 File tmpDir = null; 49 File tmpDest = null; 50 try { 51 if (info.getDestJar() == null) { 53 info.setDestJar(info.getSourceJar()); 54 } 55 56 validate(); 58 59 tmpDir = makeTempDirectory(); 61 ZipUtil.unzip(info.getSourceJar(), tmpDir); 62 63 reconfigureProject(tmpDir); 64 65 tmpDest = makeTempDestJar(); 66 ZipUtil.zip(tmpDest, tmpDir, tmpDir.listFiles(), '/'); 67 68 if (info.getDestJar().exists() && !info.getDestJar().delete()) { 70 throw new IOException ( 71 "Can't delete old jar file: " + info.getDestJar()); 72 } 73 74 if (!tmpDest.renameTo(info.getDestJar())) { 75 throw new IOException ( 76 "Error renaming: " + tmpDest + " to " + info.getDestJar()); 77 } 78 } catch (IOException ex) { 79 throw new ProjectException("Error performing reconfiguration.", ex); 80 } finally { 81 if (tmpDir != null) { 82 cleanup(tmpDir); 83 } 84 85 if (tmpDest != null) { 86 tmpDest.delete(); 87 } 88 } 89 } 90 91 97 protected void reconfigureProject(File projectDir) 98 throws ProjectException { 99 File projectFile = new File (projectDir, Configuration.DEFAULT_DOMAIN_FILE); 100 101 if (info.getAltProjectFile() != null) { 103 if (!Util.copy(info.getAltProjectFile(), projectFile)) { 104 throw new ProjectException( 105 "Can't copy project file: " + info.getAltProjectFile()); 106 } 107 } 108 109 Iterator it = info.getNodes().iterator(); 111 boolean needFix = it.hasNext(); 112 while (it.hasNext()) { 113 DataNodeConfigInfo nodeInfo = (DataNodeConfigInfo) it.next(); 114 String name = nodeInfo.getName(); 115 116 File targetDriverFile = 117 new File (projectDir, name + DataNodeFile.LOCATION_SUFFIX); 118 119 if (nodeInfo.getDataSource() != null 121 || nodeInfo.getDriverFile() != null) { 122 if (targetDriverFile.exists()) { 123 targetDriverFile.delete(); 124 } 125 } 126 127 if (nodeInfo.getDriverFile() != null 128 && !nodeInfo.getDriverFile().equals(targetDriverFile)) { 129 if (!Util.copy(nodeInfo.getDriverFile(), targetDriverFile)) { 131 throw new ProjectException( 132 "Can't copy driver file from " 133 + nodeInfo.getDriverFile()); 134 } 135 } 136 } 137 138 if (needFix) { 140 PartialProject project = new PartialProject(projectFile); 142 project.updateNodes(info.getNodes()); 143 project.save(); 144 } 145 } 146 147 150 protected File makeTempDestJar() throws IOException { 151 File destFolder = info.getDestJar().getParentFile(); 152 if (destFolder != null && !destFolder.isDirectory()) { 153 if (!destFolder.mkdirs()) { 154 throw new IOException ( 155 "Can't create directory: " + destFolder.getCanonicalPath()); 156 } 157 } 158 159 String baseName = "tmp_" + info.getDestJar().getName(); 160 161 for (int i = 0; i < 50; i++) { 164 File tmpFile = 165 (destFolder != null) 166 ? new File (destFolder, baseName + i) 167 : new File (baseName + i); 168 if (!tmpFile.exists()) { 169 return tmpFile; 170 } 171 } 172 173 throw new IOException ("Problems creating temporary file."); 174 } 175 176 179 protected void cleanup(File dir) { 180 Util.delete(dir.getPath(), true); 181 } 182 183 189 protected File makeTempDirectory() throws IOException { 190 File destFolder = info.getDestJar().getParentFile(); 191 if (destFolder != null && !destFolder.isDirectory()) { 192 if (!destFolder.mkdirs()) { 193 throw new IOException ( 194 "Can't create directory: " + destFolder.getCanonicalPath()); 195 } 196 } 197 198 String baseName = info.getDestJar().getName(); 199 if (baseName.endsWith(".jar")) { 200 baseName = baseName.substring(0, baseName.length() - 4); 201 } 202 203 for (int i = 0; i < 50; i++) { 206 File tmpDir = 207 (destFolder != null) 208 ? new File (destFolder, baseName + i) 209 : new File (baseName + i); 210 if (!tmpDir.exists()) { 211 if (!tmpDir.mkdir()) { 212 throw new IOException ( 213 "Can't create directory: " + tmpDir.getCanonicalPath()); 214 } 215 216 return tmpDir; 217 } 218 } 219 220 throw new IOException ("Problems creating temporary directory."); 221 } 222 223 226 protected void validate() throws IOException , ProjectException { 227 if (info == null) { 228 throw new ProjectException("ProjectConfig info is not set."); 229 } 230 231 if (info.getSourceJar() == null) { 232 throw new ProjectException("Source jar file is not set."); 233 } 234 235 if (!info.getSourceJar().isFile()) { 236 throw new IOException (info.getSourceJar() + " is not a file."); 237 } 238 239 if (!info.getSourceJar().canRead()) { 240 throw new IOException ("Can't read file: " + info.getSourceJar()); 241 } 242 } 243 } 244 | Popular Tags |