001 /**
002 * Copyright 2004-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.torque.mojo;
017
018 import java.io.File;
019 import java.io.FileInputStream;
020 import java.io.FileOutputStream;
021 import java.io.IOException;
022
023 import org.apache.maven.plugin.MojoExecutionException;
024 import org.apache.torque.mojo.morph.MorphRequest;
025 import org.apache.torque.mojo.morph.Morpher;
026 import org.codehaus.plexus.util.FileUtils;
027
028 /**
029 * Common logic for morphing one file to another file
030 */
031 public abstract class AbstractMorphSingleMojo extends BaseMojo {
032
033 /**
034 * The file that will contain the morphed contents
035 */
036 private File newFile;
037
038 /**
039 * The file containing the contents to be morphed
040 */
041 private File oldFile;
042
043 /**
044 * Add logic to the basic skip() method for skipping based on a morph being needed
045 */
046 @Override
047 protected boolean skipMojo() {
048 // We may be skipping based on packaging of type 'pom'
049 if (super.skipMojo()) {
050 return true;
051 }
052
053 // If a morph is needed, we can't skip
054 boolean morphNeeded = isMorphNeeded();
055 if (morphNeeded) {
056 return false;
057 } else {
058 getLog().info("Skipping morph. Nothing has changed");
059 return true;
060 }
061 }
062
063 protected boolean isMorphNeeded() {
064 // The file they asked to morph does not exist
065 if (!getOldFile().exists()) {
066 getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
067 return false;
068 }
069
070 // The new file does not exist, we need to morph
071 if (!getNewFile().exists()) {
072 return true;
073 }
074
075 // Extract the last modified timestamps
076 long oldLastModified = getOldFile().lastModified();
077 long newLastModified = getNewFile().lastModified();
078
079 // If old file has been modified since the new file was last modified,
080 // we need to morph
081 return oldLastModified > newLastModified;
082 }
083
084 protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
085
086 @Override
087 protected void executeMojo() throws MojoExecutionException {
088 try {
089 getLog().info("From: " + oldFile.getAbsolutePath());
090 getLog().info(" To: " + newFile.getAbsolutePath());
091 FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
092 MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile),
093 new FileOutputStream(newFile));
094 request.setEncoding(getEncoding());
095 Morpher morpher = getMorpher(request, getProject().getArtifactId());
096 morpher.executeMorph();
097 } catch (IOException e) {
098 throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(),
099 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 }