| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.tracing; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.IOException; |
| 37 | | | import java.util.ArrayList; |
| 38 | | | import java.util.Iterator; |
| 39 | | | import java.util.LinkedList; |
| 40 | | | import java.util.List; |
| 41 | | | import java.util.logging.Handler; |
| 42 | | | import java.util.logging.Level; |
| 43 | | | import java.util.logging.Logger; |
| 44 | | | |
| 45 | | | import org.apache.tools.ant.BuildException; |
| 46 | | | import org.apache.tools.ant.DirectoryScanner; |
| 47 | | | import org.apache.tools.ant.Project; |
| 48 | | | import org.apache.tools.ant.Task; |
| 49 | | | import org.apache.tools.ant.types.FileSet; |
| 50 | | | import org.jcoderz.commons.tracing.TracingInjector.Matcher; |
| 51 | | | import org.jcoderz.commons.tracing.TracingInjector.MethodMatcher; |
| 52 | | | import org.jcoderz.commons.util.IoUtil; |
| 53 | | | |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | <p><code></code> |
| 58 | | | <code></code> |
| 59 | | | |
| 60 | | | |
| 61 | | | </p> |
| 62 | | | |
| 63 | | | <p> |
| 64 | | | <code></code> |
| 65 | | | |
| 66 | | | </p> |
| 67 | | | |
| 68 | | | <p><code></code><code></code> |
| 69 | | | |
| 70 | | | |
| 71 | | | <code></code> |
| 72 | | | <code></code> |
| 73 | | | <code></code> |
| 74 | | | </p> |
| 75 | | | |
| 76 | | | <p><code></code> |
| 77 | | | |
| 78 | | | |
| 79 | | (1) | <a href="http://aspectwerkz.codehaus.org/definition_issues.html"> |
| 80 | | | </a> |
| 81 | | | </p> |
| 82 | | | |
| 83 | | | <p><code></code> |
| 84 | | | |
| 85 | | | |
| 86 | | | </p> |
| 87 | | | |
| 88 | | | |
| 89 | | | <code></code><pre> |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | </pre> |
| 94 | | | |
| 95 | | | <pre> |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | </pre> |
| 102 | | | |
| 103 | | (2) | |
| 104 | | | |
| 105 | | | @author |
| 106 | | | |
| 107 | 0 | | public class TracingInjectorTask extends Task |
| 108 | | | { |
| 109 | | | |
| 110 | | (3) | |
| 111 | | (4) | |
| 112 | | (5) | |
| 113 | | (6) | |
| 114 | | | |
| 115 | | | |
| 116 | 0 | | private final List mSrcFiles = new ArrayList(); |
| 117 | | | private String mDestDirName; |
| 118 | | | private File mDestDir; |
| 119 | 0 | | private final List mSourceFiles = new LinkedList(); |
| 120 | | | private File mPatternFile; |
| 121 | | | private Matcher mMatcher; |
| 122 | 0 | | private boolean mJava5 = false; |
| 123 | 0 | | private boolean mPai = false; |
| 124 | 0 | | private boolean mVerbose = false; |
| 125 | | | |
| 126 | | | |
| 127 | | | |
| 128 | | | @param |
| 129 | | | |
| 130 | | (7) | public void addFileset(FileSet fileset) |
| 131 | | | { |
| 132 | 0 | | mSrcFiles.add(fileset); |
| 133 | 0 | | } |
| 134 | | | |
| 135 | | (8)(9) | public void setDestDir(String destDir) |
| 136 | | | { |
| 137 | 0 | | mDestDirName = destDir; |
| 138 | 0 | | } |
| 139 | | | |
| 140 | | (10)(11) | public void setPatternFile(File patternFile) |
| 141 | | | { |
| 142 | 0 | | mPatternFile = patternFile; |
| 143 | 0 | | } |
| 144 | | | |
| 145 | | (12)(13) | public void setJava5(boolean java5) |
| 146 | | | { |
| 147 | 0 | | mJava5 = java5; |
| 148 | 0 | | } |
| 149 | | | |
| 150 | | (14)(15) | public void setPai(boolean pai) |
| 151 | | | { |
| 152 | 0 | | mPai = pai; |
| 153 | 0 | | } |
| 154 | | | |
| 155 | | (16)(17) | public void setVerbose(boolean verbose) |
| 156 | | | { |
| 157 | 0 | | mVerbose = verbose; |
| 158 | 0 | | } |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | |
| 163 | | (18) | public void execute() |
| 164 | | | { |
| 165 | 0 | | validate(); |
| 166 | | | |
| 167 | | | |
| 168 | | | try |
| 169 | | | { |
| 170 | 0 | | mMatcher = new MethodMatcher(mPatternFile); |
| 171 | | | |
| 172 | 0 | | log("Read matcher from " + mPatternFile, Project.MSG_VERBOSE); |
| 173 | 0 | | log(String.valueOf(mMatcher), Project.MSG_VERBOSE); |
| 174 | | | } |
| 175 | 0 | | catch (IOException ex) |
| 176 | | | { |
| 177 | 0 | | throw new BuildException("Could not build matcher from file.", ex); |
| 178 | 0 | | } |
| 179 | | | |
| 180 | 0 | | for (final Iterator itFSets = mSrcFiles.iterator(); itFSets.hasNext();) |
| 181 | | | { |
| 182 | 0 | | final FileSet fs = (FileSet) itFSets.next(); |
| 183 | 0 | | final DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
| 184 | 0 | | final File base = ds.getBasedir(); |
| 185 | 0 | | final String[] includedFiles = ds.getIncludedFiles(); |
| 186 | 0 | | for (int i = 0; i < includedFiles.length; i++) |
| 187 | | | { |
| 188 | 0 | | mSourceFiles.add(new SourceFile( |
| 189 | | | new File(base, includedFiles[i]), includedFiles[i])); |
| 190 | | | } |
| 191 | 0 | | } |
| 192 | | | try |
| 193 | | | { |
| 194 | 0 | | inject(); |
| 195 | | | } |
| 196 | 0 | | catch (IOException ex) |
| 197 | | | { |
| 198 | 0 | | throw new BuildException(ex); |
| 199 | 0 | | } |
| 200 | 0 | | } |
| 201 | | | |
| 202 | | | |
| 203 | | | |
| 204 | | | @throws |
| 205 | | | |
| 206 | | (19) | public void inject() |
| 207 | | (20) | throws IOException |
| 208 | | | { |
| 209 | 0 | | while (!mSourceFiles.isEmpty()) |
| 210 | | | { |
| 211 | 0 | | final SourceFile mySource = (SourceFile) mSourceFiles.remove(0); |
| 212 | | | |
| 213 | 0 | | final File targetFile = getTargetForSource(mySource); |
| 214 | | | |
| 215 | 0 | | if (!targetFile.exists() |
| 216 | | | || mySource.getAbsoluteFile().lastModified() |
| 217 | | | > targetFile.lastModified()) |
| 218 | | | { |
| 219 | 0 | | ensurePath(targetFile); |
| 220 | 0 | | if (mySource.getRelativeFileName().endsWith(".class")) |
| 221 | | | { |
| 222 | | | try |
| 223 | | | { |
| 224 | 0 | | log("About to work on " + mySource.getAbsoluteFile() |
| 225 | | | + ".", Project.MSG_VERBOSE); |
| 226 | 0 | | TracingInjector.inject(mySource.getAbsoluteFile(), targetFile, |
| 227 | | | mMatcher, mJava5, mPai); |
| 228 | | | } |
| 229 | 0 | | catch (RuntimeException ex) |
| 230 | | | { |
| 231 | 0 | | log("Failed with " + mySource.mAbsoluteFile + " got " |
| 232 | | | + ex, Project.MSG_DEBUG); |
| 233 | 0 | | if (ex.getMessage() != null |
| 234 | | | && ex.getMessage().startsWith("JSR/RET are not supported ")) |
| 235 | | | { |
| 236 | 0 | | IoUtil.copy(mySource.getAbsoluteFile(), targetFile); |
| 237 | | | } |
| 238 | | | else |
| 239 | | | { |
| 240 | | | |
| 241 | 0 | | log("About to only copy " + mySource.getAbsoluteFile() |
| 242 | | | + " cause " + ex + ".", ex, Project.MSG_ERR); |
| 243 | 0 | | IoUtil.copy(mySource.getAbsoluteFile(), targetFile); |
| 244 | | | } |
| 245 | 0 | | } |
| 246 | | | } |
| 247 | | | else |
| 248 | | | { |
| 249 | 0 | | log("About to copy " + mySource.getAbsoluteFile() |
| 250 | | | + ".", Project.MSG_VERBOSE); |
| 251 | 0 | | IoUtil.copy(mySource.getAbsoluteFile(), targetFile); |
| 252 | | | } |
| 253 | | | } |
| 254 | 0 | | } |
| 255 | 0 | | } |
| 256 | | | |
| 257 | | | |
| 258 | | | |
| 259 | | | @param |
| 260 | | | @throws |
| 261 | | | |
| 262 | | (21) | private void ensurePath(File file) |
| 263 | | | throws IOException |
| 264 | | | { |
| 265 | 0 | | if (!file.exists()) |
| 266 | | | { |
| 267 | 0 | | if (!file.getParentFile().exists()) |
| 268 | | | { |
| 269 | 0 | | if (!file.getParentFile().mkdirs()) |
| 270 | | | { |
| 271 | 0 | (22) | throw new IOException("Could not create dir for target files."); |
| 272 | | | } |
| 273 | | | } |
| 274 | | | } |
| 275 | 0 | | } |
| 276 | | | |
| 277 | | | |
| 278 | | | |
| 279 | | | @param |
| 280 | | | @return |
| 281 | | | |
| 282 | | (23) | private File getTargetForSource(SourceFile mySource) |
| 283 | | | { |
| 284 | 0 | | return new File(mDestDir, mySource.getRelativeFileName()); |
| 285 | | | } |
| 286 | | | |
| 287 | | | |
| 288 | | | |
| 289 | | | |
| 290 | | (24) | private void validate() |
| 291 | | | { |
| 292 | 0 | | mDestDir = new File(mDestDirName); |
| 293 | 0 | | if (mVerbose) |
| 294 | | | { |
| 295 | 0 | | Logger.getLogger( |
| 296 | | (25) | TracingInjectorTask.class.getPackage().getName()).setLevel(Level.FINEST); |
| 297 | 0 | | final Handler[] handlers = Logger.getLogger("").getHandlers(); |
| 298 | 0 | | final int amountOfHandlers = handlers.length; |
| 299 | 0 | | for (int i = 0; i < amountOfHandlers; i++) |
| 300 | | | { |
| 301 | | | try |
| 302 | | | { |
| 303 | 0 | | handlers[i].setLevel(Level.FINEST); |
| 304 | | | } |
| 305 | 0 | (26) | catch (Exception ex) |
| 306 | | | { |
| 307 | | | |
| 308 | 0 | | } |
| 309 | | | } |
| 310 | | | } |
| 311 | 0 | | } |
| 312 | | | |
| 313 | | | |
| 314 | | | |
| 315 | | | @author |
| 316 | | | |
| 317 | 0 | | private static class SourceFile |
| 318 | | | { |
| 319 | | | private final File mAbsoluteFile; |
| 320 | | | private final String mRelativeFileName; |
| 321 | | | |
| 322 | | | |
| 323 | | | |
| 324 | | | @param |
| 325 | | | @param |
| 326 | | | |
| 327 | | (27) | public SourceFile(File absoluteFile, String relativeFileName) |
| 328 | 0 | | { |
| 329 | 0 | | mAbsoluteFile = absoluteFile; |
| 330 | 0 | | mRelativeFileName = relativeFileName; |
| 331 | 0 | | } |
| 332 | | | |
| 333 | | (28) | public File getAbsoluteFile() |
| 334 | | | { |
| 335 | 0 | | return mAbsoluteFile; |
| 336 | | | } |
| 337 | | | |
| 338 | | (29) | public String getRelativeFileName() |
| 339 | | | { |
| 340 | 0 | | return mRelativeFileName; |
| 341 | | | } |
| 342 | | | } |
| 343 | | | } |