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