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), 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 }