1 5 package org.codehaus.aspectwerkz.compiler; 6 7 import java.io.File ; 8 import java.util.ArrayList ; 9 import java.util.List ; 10 import java.util.Iterator ; 11 12 import org.apache.tools.ant.BuildException; 13 import org.apache.tools.ant.Task; 14 import org.apache.tools.ant.types.Path; 15 import org.apache.tools.ant.types.Reference; 16 import org.apache.tools.ant.types.FileSet; 17 import org.codehaus.aspectwerkz.transform.inlining.AspectModelManager; 18 19 57 public class AspectWerkzCTask extends Task { 58 59 private final static String AW_TRANSFORM_DETAILS = "aspectwerkz.transform.details"; 60 61 private final static String AW_TRANSFORM_VERBOSE = "aspectwerkz.transform.verbose"; 62 63 private static final String AW_DEFINITION_FILE = "aspectwerkz.definition.file"; 64 65 private boolean m_verbose; 66 private boolean m_details; 67 private boolean m_genjp; 68 private boolean m_taskVerbose = false; 69 private String m_aspectModels; 70 private File m_backupdir; 71 private String m_preprocessor; 72 private File m_definitionFile; 73 private Path m_classpath; 74 private Path m_target; 75 77 78 82 public void setDefinition(File defFile) { 83 m_definitionFile = defFile; 84 } 85 86 90 public void setVerbose(boolean verbose) { 91 m_verbose = verbose; 92 } 93 94 98 public void setDetails(boolean details) { 99 m_details = details; 100 } 101 102 106 public void setGenjp(boolean genjp) { 107 m_genjp = genjp; 108 } 109 110 114 public void setTaskVerbose(boolean verbose) { 115 m_taskVerbose = verbose; 116 } 117 118 122 public void setAspectModels(String aspectModels) { 123 m_aspectModels = aspectModels; 124 } 125 126 128 public Path createTarget() { 129 if (m_target == null) 130 m_target = new Path(getProject()); 131 return m_target.createPath(); 132 } 133 134 public void setTargetdir(Path srcDir) { 135 if (m_target == null) 136 m_target = srcDir; 137 else 138 m_target.append(srcDir); 139 } 140 141 public void setTargetpath(Path targetpath) { 142 if (m_target == null) 143 m_target = targetpath; 144 else 145 m_target.append(targetpath); 146 } 147 148 public Path createTargetpath() { 149 if (m_target == null) 150 m_target = new Path(getProject()); 151 return m_target.createPath(); 152 } 153 154 public void setTargetpathRef(Reference r) { 155 createTargetpath().setRefid(r); 156 } 157 158 162 public void setBackupdir(File backupDir) { 163 m_backupdir = backupDir; 164 } 165 166 170 public void setPreprocessor(String preprocessorFqn) { 171 m_preprocessor = preprocessorFqn; 172 } 173 174 176 public void setClasspath(Path classpath) { 177 if (m_classpath == null) 178 m_classpath = classpath; 179 else 180 m_classpath.append(classpath); 181 } 182 183 public Path createClasspath() { 184 if (m_classpath == null) 185 m_classpath = new Path(getProject()); 186 return m_classpath.createPath(); 187 } 188 189 public void setClasspathRef(Reference r) { 190 createClasspath().setRefid(r); 191 } 192 193 198 public void execute() throws BuildException { 199 try { 200 if (m_definitionFile!=null && !!m_definitionFile.exists() && !m_definitionFile.isFile()) { 201 throw new BuildException("Definition file provided does not exists"); 202 } 203 204 AspectWerkzC compiler = new AspectWerkzC(); 205 206 compiler.setHaltOnError(true); 207 compiler.setVerbose(m_taskVerbose); 208 compiler.setGenJp(m_genjp); 209 compiler.setVerify(false); 210 211 if (m_definitionFile != null) { 212 System.setProperty(AW_DEFINITION_FILE, m_definitionFile.getAbsolutePath()); 213 } 214 215 if (m_verbose) { 216 System.setProperty(AW_TRANSFORM_VERBOSE, m_verbose ? "true" : "false"); 217 } 218 219 if (m_details) { 220 System.setProperty(AW_TRANSFORM_DETAILS, m_details ? "true" : "false"); 221 } 222 223 if (m_aspectModels != null) { 224 System.setProperty(AspectModelManager.ASPECT_MODELS_VM_OPTION, m_aspectModels); 225 } 226 227 if (m_backupdir != null && m_backupdir.isDirectory()) { 228 compiler.setBackupDir(m_backupdir.getAbsolutePath()); 229 } 230 231 if (m_taskVerbose) { 232 System.out.println("Classpath : " + dump(getDirectories(m_classpath))); 233 System.out.println("Target : " + dump(getDirectories(m_target))); 234 System.out.println("Definition : " + m_definitionFile); 235 System.out.println("Backupdir : " + m_backupdir); 236 System.out.println("Preprocessor : " + m_preprocessor); 237 } 238 239 AspectWerkzC.compile(compiler, 240 getClass().getClassLoader(), 241 m_preprocessor, 242 getDirectories(m_classpath), 243 getDirectories(m_target) 244 ); 245 } catch (Exception e) { 246 e.printStackTrace(); 247 throw new BuildException(e, getLocation()); 248 } 249 } 250 251 private List getDirectories(Path path) throws BuildException { 252 List dirs = new ArrayList (); 253 if (path == null) 254 return dirs; 255 for (int i = 0; i < path.list().length; i++) { 256 File dir = getProject().resolveFile(path.list()[i]); 257 if (!dir.exists()) { 258 throw new BuildException(" \"" + dir.getPath() + "\" does not exist!", getLocation()); 259 } 260 dirs.add(dir); } 262 return dirs; 263 } 264 265 private String dump(List strings) { 266 StringBuffer sb = new StringBuffer (); 267 for (Iterator iterator = strings.iterator(); iterator.hasNext();) { 268 Object o = (Object ) iterator.next(); 269 sb.append(o.toString()).append(File.pathSeparator); 270 } 271 return sb.toString(); 272 } 273 } 274 | Popular Tags |