| 1 | |
package com.aurea.maven.plugins.sonic.sdm; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileInputStream; |
| 5 | |
import java.util.Iterator; |
| 6 | |
import java.util.Properties; |
| 7 | |
|
| 8 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 9 | |
import org.apache.maven.plugin.MojoFailureException; |
| 10 | |
import org.codehaus.plexus.archiver.Archiver; |
| 11 | |
import org.codehaus.plexus.archiver.manager.ArchiverManager; |
| 12 | |
import org.codehaus.plexus.util.xml.XmlStreamReader; |
| 13 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
| 14 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
| 15 | |
|
| 16 | |
import com.aurea.maven.plugins.sonic.utils.FileUtilities; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | 0 | public class SdmValidatePropertiesMojo extends AbstractSdmMojo { |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
protected String sonicTarget; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
protected boolean failOnValidationWarning; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
protected ArchiverManager archiverManager; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
protected void doExecute() throws MojoExecutionException, MojoFailureException { |
| 59 | |
|
| 60 | |
try { |
| 61 | |
|
| 62 | |
|
| 63 | 0 | validateTopologies(); |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 0 | File sdmAssemblyFile = new File(getOutputDirectory(), getFinalAssemblyFileName()); |
| 71 | 0 | Archiver archiver = archiverManager.getArchiver("zip"); |
| 72 | 0 | archiver.setDestFile(sdmAssemblyFile); |
| 73 | 0 | archiver.addDirectory(getTargetSDMDir()); |
| 74 | |
|
| 75 | 0 | project.getArtifact().setFile(sdmAssemblyFile); |
| 76 | 0 | } catch (Exception e) { |
| 77 | 0 | throw new MojoExecutionException("Error creating SDM Model - " + e.getMessage(), e); |
| 78 | 0 | } |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
private void validateTopologies() throws MojoExecutionException { |
| 82 | 0 | getLog().info("Validating the Topology files"); |
| 83 | |
|
| 84 | 0 | boolean isValid = true; |
| 85 | |
|
| 86 | 0 | Properties generated = null; |
| 87 | 0 | Properties compare = null; |
| 88 | |
|
| 89 | |
|
| 90 | 0 | generated = buildPropertiesFromTopology(getGeneratedTopologyFile()); |
| 91 | |
|
| 92 | 0 | for (String toCompare : FileUtilities.getFileList(getTargetSDMDir().getAbsolutePath(), "*.Topology.xml", |
| 93 | |
"generated.Topology.xml")) { |
| 94 | 0 | getLog().info("------------------------------------------------------------------------"); |
| 95 | 0 | getLog().info("Verifying Topology file: " + toCompare); |
| 96 | 0 | compare = buildPropertiesFromTopology(new File(getTargetSDMDir() + "/" + toCompare)); |
| 97 | |
|
| 98 | |
|
| 99 | 0 | isValid = validateProperties(generated, compare) && isValid; |
| 100 | |
} |
| 101 | |
|
| 102 | 0 | if (!isValid) { |
| 103 | 0 | throw new MojoExecutionException("Properties validation failed"); |
| 104 | |
} |
| 105 | 0 | } |
| 106 | |
|
| 107 | |
private void validateProperties() throws MojoExecutionException { |
| 108 | |
|
| 109 | 0 | boolean isValid = true; |
| 110 | |
|
| 111 | 0 | Properties generated = null; |
| 112 | 0 | Properties compare = null; |
| 113 | |
|
| 114 | |
try { |
| 115 | 0 | generated = new Properties(); |
| 116 | 0 | FileInputStream fis = new FileInputStream(getGeneratedPropertiesFile()); |
| 117 | 0 | generated.load(fis); |
| 118 | 0 | fis.close(); |
| 119 | 0 | } catch (Exception e) { |
| 120 | 0 | throw new MojoExecutionException("Could not read generated properties files: " + getGeneratedPropertiesFile()); |
| 121 | 0 | } |
| 122 | |
|
| 123 | 0 | for (String toCompare : FileUtilities.getFileList(getTargetSDMDir().getAbsolutePath(), "*.tailoring.properties", |
| 124 | |
"generated.tailoring.properties")) { |
| 125 | |
try { |
| 126 | 0 | getLog().info("------------------------------------------------------------------------"); |
| 127 | 0 | getLog().info("Verifying property file: " + toCompare); |
| 128 | 0 | compare = new Properties(); |
| 129 | 0 | FileInputStream fis = new FileInputStream(getTargetSDMDir() + "/" + toCompare); |
| 130 | 0 | compare.load(fis); |
| 131 | 0 | fis.close(); |
| 132 | 0 | } catch (Exception e) { |
| 133 | 0 | getLog().warn("Could not read " + getTargetSDMDir() + "/" + toCompare); |
| 134 | 0 | isValid = false; |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
|
| 138 | 0 | isValid = validateProperties(generated, compare) && isValid; |
| 139 | |
} |
| 140 | |
|
| 141 | 0 | if (!isValid) { |
| 142 | 0 | throw new MojoExecutionException("Properties validation failed"); |
| 143 | |
} |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
private boolean validateProperties(final Properties _source, final Properties _toCompare) { |
| 147 | |
|
| 148 | 0 | boolean result = true; |
| 149 | |
|
| 150 | 0 | for (Iterator<Object> it = (Iterator<Object>) _source.keySet().iterator(); it.hasNext();) { |
| 151 | 0 | Object key = it.next(); |
| 152 | 0 | String value = _toCompare.getProperty(key.toString()); |
| 153 | |
|
| 154 | 0 | if (value == null) { |
| 155 | 0 | getLog().warn("Property file does not contain key: " + key); |
| 156 | |
|
| 157 | 0 | if (result) { |
| 158 | |
|
| 159 | 0 | result = !failOnValidationWarning; |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | 0 | } else if (value.trim().length() == 0) { |
| 164 | 0 | getLog().warn("Property has no value defined : " + key); |
| 165 | |
|
| 166 | 0 | if (result) { |
| 167 | |
|
| 168 | 0 | result = !failOnValidationWarning; |
| 169 | |
} |
| 170 | |
|
| 171 | |
} |
| 172 | 0 | } |
| 173 | |
|
| 174 | 0 | return result; |
| 175 | |
} |
| 176 | |
|
| 177 | |
} |