1 56 package org.objectstyle.cayenne.tools; 57 58 import java.io.File ; 59 import java.util.Iterator ; 60 61 import org.apache.tools.ant.BuildException; 62 import org.objectstyle.cayenne.project.DataNodeConfigInfo; 63 import org.objectstyle.cayenne.project.ProjectConfigInfo; 64 import org.objectstyle.cayenne.project.ProjectConfigurator; 65 import org.objectstyle.cayenne.project.ProjectException; 66 import org.objectstyle.cayenne.util.Util; 67 68 74 public class DeploymentConfigurator extends CayenneTask { 75 76 protected ProjectConfigInfo info; 77 78 81 public DeploymentConfigurator() { 82 super(); 83 info = new ProjectConfigInfo(); 84 } 85 86 public ProjectConfigInfo getInfo() { 87 return info; 88 } 89 90 93 public void execute() throws BuildException { 94 configureLogging(); 95 96 validateAttributes(); 97 98 try { 99 processProject(); 100 } 101 catch (Exception ex) { 102 Throwable th = Util.unwindException(ex); 103 String message = th.getMessage(); 104 StringBuffer buf = new StringBuffer (); 105 106 if (message != null && message.trim().length() > 0) { 107 buf.append("Error: [").append(message).append("]."); 108 } 109 else { 110 buf.append("Error reconfiguring jar file."); 111 } 112 113 buf 114 .append(" Source: ") 115 .append(info.getSourceJar()) 116 .append("; target: ") 117 .append(info.getDestJar()); 118 119 String errorMessage = buf.toString(); 120 super.log(errorMessage); 121 throw new BuildException(errorMessage, ex); 122 } 123 } 124 125 128 protected void validateAttributes() throws BuildException { 129 if (info.getSourceJar() == null) { 130 throw new BuildException("'src' attribute is required."); 131 } 132 133 if (!info.getSourceJar().isFile()) { 134 throw new BuildException("'src' must be a valid file: " + info.getSourceJar()); 135 } 136 137 if (info.getAltProjectFile() != null && !info.getAltProjectFile().isFile()) { 138 throw new BuildException("'altProjectFile' must be a valid file: " 139 + info.getAltProjectFile()); 140 } 141 142 Iterator nodes = info.getNodes().iterator(); 143 while (nodes.hasNext()) { 144 DataNodeConfigInfo node = (DataNodeConfigInfo) nodes.next(); 145 if (node.getName() == null) { 146 throw new BuildException("'node.name' attribute is required."); 147 } 148 149 if (node.getDataSource() != null && node.getDriverFile() != null) { 150 throw new BuildException( 151 "'node.dataSource' and 'node.driverFile' are mutually exclusive."); 152 } 153 154 if (node.getDriverFile() != null && !node.getDriverFile().isFile()) { 155 throw new BuildException("'node.driverFile' does not exist."); 156 } 157 } 158 } 159 160 163 protected void processProject() throws ProjectException { 164 ProjectConfigurator conf = new ProjectConfigurator(info); 165 conf.execute(); 166 } 167 168 public void setSrc(File file) { 169 info.setSourceJar(file); 170 } 171 172 public void setDest(File file) { 173 info.setDestJar(file); 174 } 175 176 public void setAltProjectFile(File file) { 177 info.setAltProjectFile(file); 178 } 179 180 public void addNode(DataNodeConfigInfo node) { 181 info.addToNodes(node); 182 } 183 } | Popular Tags |