modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/TomcatWarComponent.java | 37 --- modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/helper/FileContentDelegate.java | 103 ++++++---- 2 files changed, 77 insertions(+), 63 deletions(-)
New commits: commit 59550355b8059df1ad63587309426032c2dd108a Author: Stefan Negrea snegrea@redhat.com Date: Tue Dec 13 11:15:45 2011 -0600
[BZ 761593] Move all the code for computation and retrieval of SHA256 to file content delegate. This way the manifest and digest code is in one location only.
diff --git a/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/TomcatWarComponent.java b/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/TomcatWarComponent.java index 27591ba..9a60a57 100644 --- a/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/TomcatWarComponent.java +++ b/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/TomcatWarComponent.java @@ -34,7 +34,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; -import java.util.jar.Manifest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -71,7 +70,6 @@ import org.rhq.core.pluginapi.operation.OperationResult; import org.rhq.core.pluginapi.util.FileUtils; import org.rhq.core.pluginapi.util.ResponseTimeConfiguration; import org.rhq.core.pluginapi.util.ResponseTimeLogParser; -import org.rhq.core.util.MessageDigestGenerator; import org.rhq.core.util.ZipUtil; import org.rhq.core.util.exception.ThrowableUtil; import org.rhq.plugins.jmx.MBeanResourceComponent; @@ -633,43 +631,18 @@ public class TomcatWarComponent extends MBeanResourceComponent<TomcatVHostCompon }
/** - * Retrieve SHA 256 for a deployed app. - * 1) If the app is exploded then look into the manifest file for the RHQ-Sha265 attribute. - * The SHA should have been computed and the attribute added during package deployment. - * 2) If the app is not exploded then compute the SHA256 for the archive. + * Retrieve SHA256 for a deployed app. * * @param file application file - * @return the SHA 256 + * @return SHA256 of the content */ private String getSHA256(File file) { - String sha256 = null;
try { - //if the filesize of discovered package is null then it's an exploded and deployed war/ear. - File app = new File(file.getPath()); - if (app.isDirectory()) { - File manifestFile = new File(app.getAbsolutePath(), "META-INF/MANIFEST.MF"); - if (manifestFile.exists()) { - InputStream manifestStream = new FileInputStream(manifestFile); - Manifest manifest = null; - try { - manifest = new Manifest(manifestStream); - sha256 = manifest.getMainAttributes().getValue("RHQ-Sha256"); - } finally { - manifestStream.close(); - } - } - - if (sha256 == null || sha256.trim().isEmpty()) { - FileContentDelegate fileContentDelegate = new FileContentDelegate(app); - sha256 = fileContentDelegate.calculateSHAForCotent(app); - } - } else { - sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(app); - } - } catch (IOException iex) { - //log exception but move on, discovery happens often. No reason to hold up anything. + FileContentDelegate fileContentDelegate = new FileContentDelegate(file); + sha256 = fileContentDelegate.getSHA(file); + } catch (Exception iex) { if (log.isDebugEnabled()) { log.debug("Problem calculating digest of package [" + file.getPath() + "]." + iex.getMessage()); } diff --git a/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/helper/FileContentDelegate.java b/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/helper/FileContentDelegate.java index 5699ab4..6f914db 100644 --- a/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/helper/FileContentDelegate.java +++ b/modules/plugins/tomcat/src/main/java/org/jboss/on/plugins/tomcat/helper/FileContentDelegate.java @@ -51,6 +51,9 @@ import org.rhq.core.util.file.FileUtil; */ public class FileContentDelegate {
+ private static final String RHQ_SHA_256 = "RHQ-Sha256"; + private static final String MANIFEST_RELATIVE_PATH = "META-INF/MANIFEST.MF"; + private File deployDirectory;
public FileContentDelegate(File directory) { @@ -92,44 +95,38 @@ public class FileContentDelegate { }
/** - * Calculate SHA256 for the content of an exploded war deployment. This method should be used to - * compute the SHA256 for content deployed outside RHQ or for the initial content delivered - * with the server. + * Retrieves the SHA256 for a deployed application. + * 1) If the app is exploded then return RHQ-Sha256 manifest attribute. + * 1.1) If RHQ-Sha256 is missing then compute it, save it and return the result. + * 2) If the app is an archive then compute SHA256 on fly and return it. * - * @param deploymentDirectory app deployment folder + * @param deploymentFile deployment file * @return */ - public String calculateSHAForCotent(File deploymentDirectory) { + public String getSHA(File deploymentFile) { String sha = null; try { - if (deploymentDirectory.isDirectory()) { - MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256); - - Stack<File> unvisitedFolders = new Stack<File>(); - unvisitedFolders.add(deploymentDirectory); - while (!unvisitedFolders.empty()) { - for (File file : unvisitedFolders.pop().listFiles()) { - if (file.isDirectory()) { - unvisitedFolders.add(file); - } else { - FileInputStream inputStream = null; - try { - inputStream = new FileInputStream(file); - messageDigest.add(inputStream); - } finally { - if (inputStream != null) { - inputStream.close(); - } - } - } + if (deploymentFile.isDirectory()) { + File manifestFile = new File(deploymentFile.getAbsolutePath(), MANIFEST_RELATIVE_PATH); + if (manifestFile.exists()) { + InputStream manifestStream = new FileInputStream(manifestFile); + Manifest manifest = null; + try { + manifest = new Manifest(manifestStream); + sha = manifest.getMainAttributes().getValue(RHQ_SHA_256); + } finally { + manifestStream.close(); } }
- sha = messageDigest.getDigestString(); - writeSHAToManifest(deploymentDirectory, sha); + if (sha == null || sha.trim().isEmpty()) { + sha = computeAndSaveSHA(deploymentFile); + } + } else { + sha = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(deploymentFile); } - } catch (IOException e) { - throw new RuntimeException("Error creating artifact for contentFile: " + deploymentDirectory, e); + } catch (IOException ex) { + throw new RuntimeException("Problem calculating digest of package [" + deploymentFile.getPath() + "].", ex); }
return sha; @@ -200,6 +197,50 @@ public class FileContentDelegate { }
/** + * Compute SHA256 for the content of an exploded war deployment. This method should be used to + * compute the SHA256 for content deployed outside RHQ or for the initial content delivered + * with the server. + * + * @param deploymentDirectory app deployment folder + * @return + */ + private String computeAndSaveSHA(File deploymentDirectory) { + String sha = null; + try { + if (deploymentDirectory.isDirectory()) { + MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256); + + Stack<File> unvisitedFolders = new Stack<File>(); + unvisitedFolders.add(deploymentDirectory); + while (!unvisitedFolders.empty()) { + for (File file : unvisitedFolders.pop().listFiles()) { + if (file.isDirectory()) { + unvisitedFolders.add(file); + } else { + FileInputStream inputStream = null; + try { + inputStream = new FileInputStream(file); + messageDigest.add(inputStream); + } finally { + if (inputStream != null) { + inputStream.close(); + } + } + } + } + } + + sha = messageDigest.getDigestString(); + writeSHAToManifest(deploymentDirectory, sha); + } + } catch (IOException e) { + throw new RuntimeException("Error creating artifact for contentFile: " + deploymentDirectory, e); + } + + return sha; + } + + /** * Write the SHA256 to the manifest using the RHQ-Sha256 attribute tag. * * @param deploymentFolder app deployment folder @@ -207,7 +248,7 @@ public class FileContentDelegate { * @throws IOException */ private void writeSHAToManifest(File deploymentFolder, String sha) throws IOException { - File manifestFile = new File(deploymentFolder, "META-INF/MANIFEST.MF"); + File manifestFile = new File(deploymentFolder, MANIFEST_RELATIVE_PATH); Manifest manifest; if (manifestFile.exists()) { FileInputStream inputStream = new FileInputStream(manifestFile); @@ -231,7 +272,7 @@ public class FileContentDelegate { attribs.putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0"); }
- attribs.putValue("RHQ-Sha256", sha); + attribs.putValue(RHQ_SHA_256, sha);
FileOutputStream outputStream = new FileOutputStream(manifestFile); try {
rhq-commits@lists.fedorahosted.org