View Javadoc

1   package org.apache.torque.mojo;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.IOException;
7   
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.torque.mojo.morph.MorphRequest;
10  import org.apache.torque.mojo.morph.Morpher;
11  import org.codehaus.plexus.util.FileUtils;
12  
13  /**
14   * Common logic for morphing one file to another file
15   */
16  public abstract class AbstractMorphSingleMojo extends BaseMojo {
17  
18      /**
19       * The file that will contain the morphed contents
20       */
21      private File newFile;
22  
23      /**
24       * The file containing the contents to be morphed
25       */
26      private File oldFile;
27  
28      /**
29       * Add logic to the basic skip() method for skipping based on a morph being needed
30       */
31      @Override
32      protected boolean skipMojo() {
33          // We may be skipping based on packaging of type 'pom'
34          if (super.skipMojo()) {
35              return true;
36          }
37  
38          // If a morph is needed, we can't skip
39          boolean morphNeeded = isMorphNeeded();
40          if (morphNeeded) {
41              return false;
42          } else {
43              getLog().info("Skipping morph.  Nothing has changed");
44              return true;
45          }
46      }
47  
48      protected boolean isMorphNeeded() {
49          // The file they asked to morph does not exist
50          if (!getOldFile().exists()) {
51              getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
52              return false;
53          }
54  
55          // The new file does not exist, we need to morph
56          if (!getNewFile().exists()) {
57              return true;
58          }
59  
60          // Extract the last modified timestamps
61          long oldLastModified = getOldFile().lastModified();
62          long newLastModified = getNewFile().lastModified();
63  
64          // If old file has been modified since the new file was last modified,
65          // we need to morph
66          return oldLastModified > newLastModified;
67      }
68  
69      protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
70  
71      @Override
72      protected void executeMojo() throws MojoExecutionException {
73          try {
74              getLog().info("From: " + oldFile.getAbsolutePath());
75              getLog().info("  To: " + newFile.getAbsolutePath());
76              FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
77              MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile),
78                      new FileOutputStream(newFile));
79              request.setEncoding(getEncoding());
80              Morpher morpher = getMorpher(request, getProject().getArtifactId());
81              morpher.executeMorph();
82          } catch (IOException e) {
83              throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(),
84                      e);
85          }
86      }
87  
88      public File getNewFile() {
89          return newFile;
90      }
91  
92      public void setNewFile(final File newFile) {
93          this.newFile = newFile;
94      }
95  
96      public File getOldFile() {
97          return oldFile;
98      }
99  
100     public void setOldFile(final File oldFile) {
101         this.oldFile = oldFile;
102     }
103 }