View Javadoc

1   /**
2    * Copyright 2004-2013 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), 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 }