Package org.rhq.core.util

Examples of org.rhq.core.util.MessageDigestGenerator


     * @return SHA256 of the archive
     */
    private String computeSHAForArchivedContent(File contentFile) {
        if (!contentFile.isDirectory()) {
            try {
                MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256);
                return messageDigest.calcDigestString(contentFile);
            } catch (Exception ex) {
                log.error("Not able to compute SHA256 for " + contentFile.getPath() + " .");
            }
        }

View Full Code Here


     * @return SHA256 of the content
     */
    private String computeSHAForExplodedContent(File deploymentDirectory) {
        try {
            if (deploymentDirectory.isDirectory()) {
                MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256);

                Stack<File> unvisitedFolders = new Stack<File>();
                unvisitedFolders.add(deploymentDirectory);
                while (!unvisitedFolders.empty()) {
                    File[] files = unvisitedFolders.pop().listFiles();
                    Arrays.sort(files, new Comparator<File>() {
                        public int compare(File f1, File f2) {
                            try {
                                return f1.getCanonicalPath().compareTo(f2.getCanonicalPath());
                            } catch (IOException e) {
                                //do nothing if the sort fails at this point
                            }

                            return 0;
                        }
                    });

                    for (File file : files) {
                        if (file.isDirectory()) {
                            unvisitedFolders.add(file);
                        } else {
                            FileInputStream inputStream = null;
                            try {
                                inputStream = new FileInputStream(file);
                                messageDigest.add(inputStream);
                            } finally {
                                if (inputStream != null) {
                                    inputStream.close();
                                }
                            }
                        }
                    }
                }

                return messageDigest.getDigestString();
            }
        } catch (IOException e) {
            throw new RuntimeException("Error creating artifact for contentFile: " + deploymentDirectory, e);
        }

View Full Code Here

     *
     * @param prefixKey extra key to be appended at the beginning of the digest process
     * @return key
     */
    private String getAncestryBasedResourceKey(String prefixKey) {
        MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256);

        if (prefixKey != null) {
            messageDigest.add(prefixKey.getBytes());
        }

        messageDigest.add(this.resource.getResourceKey().getBytes());

        ResourceContext<?> ancestor = this.parentResourceContext;
        while (ancestor != null) {
            messageDigest.add(ancestor.getResourceKey().getBytes());
            ancestor = ancestor.getParentResourceContext();
        }

        return messageDigest.getDigestString();
    }
View Full Code Here

                    config.put(new PropertySimple("sealed", attributes.getValue(Attributes.Name.SEALED)));
                }

                String sha256 = null;
                try {
                    sha256 = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(file);
                } catch (Exception e) {
                    // leave as null
                }

                ResourcePackageDetails details = new ResourcePackageDetails(new PackageDetailsKey(file.getName(),
View Full Code Here

        Assert.assertFalse(deploymentFile.isDirectory(), "Deployment was exploded when it should not have been.");

        File sha256File = new File(resourceDataDirectory, "application.sha256");
        Assert.assertFalse(sha256File.exists(), "SHA256 properties files was wrongly created for zipped deployment.");

        MessageDigestGenerator digest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256);
        String expectedSHA256 = digest.calcDigestString(sampleWithoutManifestWar);
        String actualSHA256OfDeployment = digest.calcDigestString(deploymentFile);

        Assert.assertEquals(actualSHA256OfDeployment, expectedSHA256);
        Assert.assertEquals(actualShaReturned, expectedSHA256);
    }
View Full Code Here

        //run code under test
        String actualShaReturned = objectUnderTest.retrieveDeploymentSHA(sampleWithoutManifestWar,
            resourceDataDirectory);

        //verify the results (Assert and mock verification)
        MessageDigestGenerator digest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256);
        String expectedSHA256 = digest.calcDigestString(sampleWithoutManifestWar);

        //cleanup resources created for this test
        Assert.assertEquals(actualShaReturned, expectedSHA256);
    }
View Full Code Here

        JarFile mockJarFile = mock(JarFile.class);
        PowerMockito.whenNew(JarFile.class).withParameterTypes(File.class).withArguments(any(File.class))
            .thenReturn(mockJarFile);

        MessageDigestGenerator mockMessageDigestGenerator = mock(MessageDigestGenerator.class);
        PowerMockito.whenNew(MessageDigestGenerator.class).withParameterTypes(String.class).withArguments(anyString())
            .thenReturn(mockMessageDigestGenerator);
        when(mockMessageDigestGenerator.calcDigestString(any(File.class))).thenReturn("abcd1234");

        //create object to test and inject required dependencies
        JarContentDelegate objectUnderTest = new JarContentDelegate(mockDirectory, "jar");

        //run code under test
View Full Code Here

        }
        return randomUuids;
    }

    private String computeSHA256(String... values) {
        MessageDigestGenerator messageDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256);
        for (String value : values) {
            messageDigest.add(value.getBytes());
        }
        return messageDigest.getDigestString();
    }
View Full Code Here

                            visitor.getFileHashcodeMap());
                    }
                }

                // Copy the archive to the destination dir if we need to. Generate its hashcode and add it to the new file hashcode map
                MessageDigestGenerator hashcodeGenerator = new MessageDigestGenerator();
                String compressedFileHashcode;
                if (!dryRun) {
                    if (!compressedFile.exists()) {
                        if (compressedFile.getParentFile() != null) {
                            compressedFile.getParentFile().mkdirs();
                        }
                        FileUtil.copyFile(zipFile, compressedFile);
                    }
                    compressedFileHashcode = hashcodeGenerator.calcDigestString(compressedFile);
                } else {
                    // use source zip for hash - should be the same as the would-be compressed file since we aren't realizing files in it
                    compressedFileHashcode = hashcodeGenerator.calcDigestString(zipFile);
                }
                newFileHashCodeMap.put(zipPath, compressedFileHashcode);
            }
        }

        // copy all raw files
        StreamCopyDigest copyDigester = new StreamCopyDigest();
        for (Map.Entry<File, File> rawFile : this.deploymentData.getRawFiles().entrySet()) {
            // determine where the original file is and where it needs to go
            File currentLocationFile = rawFile.getKey();
            File newLocationFile = rawFile.getValue();
            String newLocationPath = rawFile.getValue().getPath();
            newLocationPath = newFileHashCodeMap.convertPath(newLocationPath);
            if (currentFilesToLeaveAlone != null && currentFilesToLeaveAlone.containsKey(newLocationPath)) {
                continue;
            }
            if (!newLocationFile.isAbsolute()) {
                newLocationFile = new File(this.deploymentData.getDestinationDir(), newLocationFile.getPath());
            }

            if (!dryRun) {
                File newLocationParentDir = newLocationFile.getParentFile();
                newLocationParentDir.mkdirs();
                if (!newLocationParentDir.isDirectory()) {
                    throw new Exception("Failed to create new parent directory for raw file [" + newLocationFile + "]");
                }
            }

            String hashcode;

            boolean realize = false;
            if (this.deploymentData.getRawFilesToRealize() != null) {
                realize = this.deploymentData.getRawFilesToRealize().contains(currentLocationFile);
            }

            if (realize) {
                debug("Realizing file [", currentLocationFile, "] to [", newLocationFile, "]. dryRun=", dryRun);

                // this entry needs to be realized, do it now in-memory (we assume realizable files will not be large)
                // note: tempateEngine will never be null if we got here
                FileInputStream in = new FileInputStream(currentLocationFile);
                byte[] rawFileContent = StreamUtil.slurp(in);
                String content = this.deploymentData.getTemplateEngine().replaceTokens(new String(rawFileContent));

                if (diff != null) {
                    diff.addRealizedFile(newLocationPath, content);
                }

                // now write the realized content to the filesystem
                byte[] bytes = content.getBytes();

                if (!dryRun) {
                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newLocationFile));
                    try {
                        out.write(bytes);
                    } finally {
                        out.close();
                    }
                }

                MessageDigestGenerator hashcodeGenerator = copyDigester.getMessageDigestGenerator();
                hashcodeGenerator.add(bytes);
                hashcode = hashcodeGenerator.getDigestString();
            } else {
                debug("Copying raw file [", currentLocationFile, "] to [", newLocationFile, "]. dryRun=", dryRun);

                FileInputStream in = new FileInputStream(currentLocationFile);
                try {
View Full Code Here

            visitor = new InMemoryZipFileVisitor(realizeRegex, this.deploymentData.getTemplateEngine());
            ZipUtil.walkZipFile(zipFile, visitor);
            fileHashcodeMap.putAll(visitor.getFileHashcodeMap());
        }

        MessageDigestGenerator generator = new MessageDigestGenerator();

        // calculate hashcodes for all raw files, perform in-memory realization when necessary
        for (Map.Entry<File, File> rawFile : this.deploymentData.getRawFiles().entrySet()) {
            // determine where the original file is and where it would go if we were writing it to disk
            File currentLocationFile = rawFile.getKey();
            File newLocationFile = rawFile.getValue();
            String newLocationPath = rawFile.getValue().getPath();
            if (!newLocationFile.isAbsolute()) {
                newLocationFile = new File(this.deploymentData.getDestinationDir(), newLocationFile.getPath());
            }

            String hashcode;

            boolean realize = false;
            if (this.deploymentData.getRawFilesToRealize() != null) {
                realize = this.deploymentData.getRawFilesToRealize().contains(currentLocationFile);
            }

            if (realize) {
                debug("Realizing file [", currentLocationFile, "] in-memory to determine its hashcode");

                // this entry needs to be realized, do it now in-memory (we assume realizable files will not be large)
                // note: tempateEngine will never be null if we got here
                FileInputStream in = new FileInputStream(currentLocationFile);
                byte[] rawFileContent = StreamUtil.slurp(in);
                String content = this.deploymentData.getTemplateEngine().replaceTokens(new String(rawFileContent));

                // now calculate the hashcode of the realized content
                generator.add(content.getBytes());
                hashcode = generator.getDigestString();
            } else {
                debug("Streaming file [", currentLocationFile, "] in-memory to determine its hashcode");

                BufferedInputStream in = new BufferedInputStream(new FileInputStream(currentLocationFile));
                try {
                    hashcode = generator.calcDigestString(in);
                } finally {
                    in.close();
                }
            }
View Full Code Here

TOP

Related Classes of org.rhq.core.util.MessageDigestGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.