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 artifactId (aka database schema)
35       *
36       * @parameter expression="${impex.artifactId}" default-value="${project.artifactId}"
37       * @required
38       */
39      private String artifactId;
40  
41      /**
42       * The file that will contain the morphed contents
43       */
44      private File newFile;
45  
46      /**
47       * The file containing the contents to be morphed
48       */
49      private File oldFile;
50  
51      /**
52       * Add logic to the basic skip() method for skipping based on a morph being needed
53       */
54      @Override
55      protected boolean skipMojo() {
56          // We may be skipping based on packaging of type 'pom'
57          if (super.skipMojo()) {
58              return true;
59          }
60  
61          // If a morph is needed, we can't skip
62          boolean morphNeeded = isMorphNeeded();
63          if (morphNeeded) {
64              return false;
65          } else {
66              getLog().info("Skipping morph.  Nothing has changed");
67              return true;
68          }
69      }
70  
71      protected boolean isMorphNeeded() {
72          // The file they asked to morph does not exist
73          if (!getOldFile().exists()) {
74              getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
75              return false;
76          }
77  
78          // The new file does not exist, we need to morph
79          if (!getNewFile().exists()) {
80              return true;
81          }
82  
83          // Extract the last modified timestamps
84          long oldLastModified = getOldFile().lastModified();
85          long newLastModified = getNewFile().lastModified();
86  
87          // If old file has been modified since the new file was last modified,
88          // we need to morph
89          return oldLastModified > newLastModified;
90      }
91  
92      protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
93  
94      @Override
95      protected void executeMojo() throws MojoExecutionException {
96          try {
97              getLog().info("From: " + oldFile.getAbsolutePath());
98              getLog().info("  To: " + newFile.getAbsolutePath());
99              FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
100             MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile),
101                     new FileOutputStream(newFile));
102             request.setEncoding(getEncoding());
103             Morpher morpher = getMorpher(request, artifactId);
104             morpher.executeMorph();
105         } catch (IOException e) {
106             throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(),
107                     e);
108         }
109     }
110 
111     public File getNewFile() {
112         return newFile;
113     }
114 
115     public void setNewFile(final File newFile) {
116         this.newFile = newFile;
117     }
118 
119     public File getOldFile() {
120         return oldFile;
121     }
122 
123     public void setOldFile(final File oldFile) {
124         this.oldFile = oldFile;
125     }
126 
127     public String getArtifactId() {
128         return artifactId;
129     }
130 
131     public void setArtifactId(String artifactId) {
132         this.artifactId = artifactId;
133     }
134 }