View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.torque.mojo;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.torque.mojo.morph.MorphRequest;
25  import org.apache.torque.mojo.morph.Morpher;
26  import org.codehaus.plexus.util.FileUtils;
27  
28  /**
29   * Common logic for morphing one file to another file
30   */
31  public abstract class AbstractMorphSingleMojo extends BaseMojo {
32  
33      /**
34       * The file that will contain the morphed contents
35       */
36      private File newFile;
37  
38      /**
39       * The file containing the contents to be morphed
40       */
41      private File oldFile;
42  
43      /**
44       * Add logic to the basic skip() method for skipping based on a morph being needed
45       */
46      @Override
47      protected boolean skipMojo() {
48          // We may be skipping based on packaging of type 'pom'
49          if (super.skipMojo()) {
50              return true;
51          }
52  
53          // If a morph is needed, we can't skip
54          boolean morphNeeded = isMorphNeeded();
55          if (morphNeeded) {
56              return false;
57          } else {
58              getLog().info("Skipping morph.  Nothing has changed");
59              return true;
60          }
61      }
62  
63      protected boolean isMorphNeeded() {
64          // The file they asked to morph does not exist
65          if (!getOldFile().exists()) {
66              getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
67              return false;
68          }
69  
70          // The new file does not exist, we need to morph
71          if (!getNewFile().exists()) {
72              return true;
73          }
74  
75          // Extract the last modified timestamps
76          long oldLastModified = getOldFile().lastModified();
77          long newLastModified = getNewFile().lastModified();
78  
79          // If old file has been modified since the new file was last modified,
80          // we need to morph
81          return oldLastModified > newLastModified;
82      }
83  
84      protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
85  
86      @Override
87      protected void executeMojo() throws MojoExecutionException {
88          try {
89              getLog().info("From: " + oldFile.getAbsolutePath());
90              getLog().info("  To: " + newFile.getAbsolutePath());
91              FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
92              MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile),
93                      new FileOutputStream(newFile));
94              request.setEncoding(getEncoding());
95              Morpher morpher = getMorpher(request, getProject().getArtifactId());
96              morpher.executeMorph();
97          } catch (IOException e) {
98              throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(),
99                      e);
100         }
101     }
102 
103     public File getNewFile() {
104         return newFile;
105     }
106 
107     public void setNewFile(final File newFile) {
108         this.newFile = newFile;
109     }
110 
111     public File getOldFile() {
112         return oldFile;
113     }
114 
115     public void setOldFile(final File oldFile) {
116         this.oldFile = oldFile;
117     }
118 }