1 19 20 package org.apache.cayenne.tools; 21 22 import java.io.File ; 23 import java.util.Iterator ; 24 25 import org.apache.cayenne.project.DataNodeConfigInfo; 26 import org.apache.cayenne.project.ProjectConfigInfo; 27 import org.apache.cayenne.project.ProjectConfigurator; 28 import org.apache.cayenne.project.ProjectException; 29 import org.apache.cayenne.util.Util; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Task; 32 33 39 public class DeploymentConfigurator extends Task { 40 41 protected ProjectConfigInfo info; 42 43 46 public DeploymentConfigurator() { 47 super(); 48 info = new ProjectConfigInfo(); 49 } 50 51 public ProjectConfigInfo getInfo() { 52 return info; 53 } 54 55 58 public void execute() throws BuildException { 59 60 validateAttributes(); 61 62 try { 63 processProject(); 64 } 65 catch (Exception ex) { 66 Throwable th = Util.unwindException(ex); 67 String message = th.getMessage(); 68 StringBuffer buf = new StringBuffer (); 69 70 if (message != null && message.trim().length() > 0) { 71 buf.append("Error: [").append(message).append("]."); 72 } 73 else { 74 buf.append("Error reconfiguring jar file."); 75 } 76 77 buf 78 .append(" Source: ") 79 .append(info.getSourceJar()) 80 .append("; target: ") 81 .append(info.getDestJar()); 82 83 String errorMessage = buf.toString(); 84 super.log(errorMessage); 85 throw new BuildException(errorMessage, ex); 86 } 87 } 88 89 92 protected void validateAttributes() throws BuildException { 93 if (info.getSourceJar() == null) { 94 throw new BuildException("'src' attribute is required."); 95 } 96 97 if (!info.getSourceJar().isFile()) { 98 throw new BuildException("'src' must be a valid file: " + info.getSourceJar()); 99 } 100 101 if (info.getAltProjectFile() != null && !info.getAltProjectFile().isFile()) { 102 throw new BuildException("'altProjectFile' must be a valid file: " 103 + info.getAltProjectFile()); 104 } 105 106 Iterator nodes = info.getNodes().iterator(); 107 while (nodes.hasNext()) { 108 DataNodeConfigInfo node = (DataNodeConfigInfo) nodes.next(); 109 if (node.getName() == null) { 110 throw new BuildException("'node.name' attribute is required."); 111 } 112 113 if (node.getDataSource() != null && node.getDriverFile() != null) { 114 throw new BuildException( 115 "'node.dataSource' and 'node.driverFile' are mutually exclusive."); 116 } 117 118 if (node.getDriverFile() != null && !node.getDriverFile().isFile()) { 119 throw new BuildException("'node.driverFile' does not exist."); 120 } 121 } 122 } 123 124 127 protected void processProject() throws ProjectException { 128 ProjectConfigurator conf = new ProjectConfigurator(info); 129 conf.execute(); 130 } 131 132 public void setSrc(File file) { 133 info.setSourceJar(file); 134 } 135 136 public void setDest(File file) { 137 info.setDestJar(file); 138 } 139 140 public void setAltProjectFile(File file) { 141 info.setAltProjectFile(file); 142 } 143 144 public void addNode(DataNodeConfigInfo node) { 145 info.addToNodes(node); 146 } 147 } 148 | Popular Tags |