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 artifactId (aka database schema)
035 *
036 * @parameter expression="${impex.artifactId}" default-value="${project.artifactId}"
037 * @required
038 */
039 private String artifactId;
040
041 /**
042 * The file that will contain the morphed contents
043 */
044 private File newFile;
045
046 /**
047 * The file containing the contents to be morphed
048 */
049 private File oldFile;
050
051 /**
052 * Add logic to the basic skip() method for skipping based on a morph being needed
053 */
054 @Override
055 protected boolean skipMojo() {
056 // We may be skipping based on packaging of type 'pom'
057 if (super.skipMojo()) {
058 return true;
059 }
060
061 // If a morph is needed, we can't skip
062 boolean morphNeeded = isMorphNeeded();
063 if (morphNeeded) {
064 return false;
065 } else {
066 getLog().info("Skipping morph. Nothing has changed");
067 return true;
068 }
069 }
070
071 protected boolean isMorphNeeded() {
072 // The file they asked to morph does not exist
073 if (!getOldFile().exists()) {
074 getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
075 return false;
076 }
077
078 // The new file does not exist, we need to morph
079 if (!getNewFile().exists()) {
080 return true;
081 }
082
083 // Extract the last modified timestamps
084 long oldLastModified = getOldFile().lastModified();
085 long newLastModified = getNewFile().lastModified();
086
087 // If old file has been modified since the new file was last modified,
088 // we need to morph
089 return oldLastModified > newLastModified;
090 }
091
092 protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
093
094 @Override
095 protected void executeMojo() throws MojoExecutionException {
096 try {
097 getLog().info("From: " + oldFile.getAbsolutePath());
098 getLog().info(" To: " + newFile.getAbsolutePath());
099 FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
100 MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile), new FileOutputStream(newFile));
101 request.setEncoding(getEncoding());
102 Morpher morpher = getMorpher(request, artifactId);
103 morpher.executeMorph();
104 } catch (IOException e) {
105 throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(), e);
106 }
107 }
108
109 public File getNewFile() {
110 return newFile;
111 }
112
113 public void setNewFile(final File newFile) {
114 this.newFile = newFile;
115 }
116
117 public File getOldFile() {
118 return oldFile;
119 }
120
121 public void setOldFile(final File oldFile) {
122 this.oldFile = oldFile;
123 }
124
125 public String getArtifactId() {
126 return artifactId;
127 }
128
129 public void setArtifactId(String artifactId) {
130 this.artifactId = artifactId;
131 }
132 }