protected String getStoragePid() throws IOException { File pidFile = getStoragePidFile(); if (pidFile.exists()) { return StreamUtil.slurp(new FileReader(pidFile)); } return null; }
protected String getReadmeContent() { // see if the command has a readme file whose content we will print; if not, just abort String readmeFilename = getReadmeFilename(); if (readmeFilename == null) { return null; } File readmeFile = new File(getBaseDir(), readmeFilename); if (!readmeFile.canRead()) { return null; } // dump the readme try { FileReader fileReader = new FileReader(readmeFile); String readmeContent = StreamUtil.slurp(fileReader); return readmeContent; } catch (Exception e) { // just quietly abort, don't be noisy about this, though this should never happen return null; } }
private long readShutdownTimeLogFile() throws Exception { File timeFile = shutdownListener.getShutdownTimeLogFile(); if (!timeFile.exists()) { // this is probably ok, perhaps its the first time we started this server, so this exception // just forces the caller to use startup time instead throw new FileNotFoundException(); } try { FileInputStream input = new FileInputStream(timeFile); String timeString = new String(StreamUtil.slurp(input)); return Long.parseLong(timeString); } catch (Exception e) { if (log.isDebugEnabled()) { log.warn("Failed to read the shutdown time log file", e); } else { log.warn("Failed to read the shutdown time log file: " + e.getMessage()); } throw e; } finally { // since we are starting again, we want to remove the now obsolete shutdown time file timeFile.delete(); } }
private String readFile(File file) throws Exception { return new String(StreamUtil.slurp(new FileInputStream(file))); }
private String getRecipeFromFile(String filename) { InputStream stream = getClass().getClassLoader().getResourceAsStream(filename); byte[] contents = StreamUtil.slurp(stream); return new String(contents); }
private void upgrade(boolean clean) throws Exception { testAntBundleInitialInstall(); // install a bundle first cleanPluginDirs(); // clean everything but the dest dir - we want to upgrade the destination prepareBeforeTestMethod(); // prepare for our new test // deploy upgrade and test it ResourceType resourceType = new ResourceType("testSimpleBundle2Type", "plugin", ResourceCategory.SERVER, null); BundleType bundleType = new BundleType("testSimpleBundle2BType", resourceType); Repo repo = new Repo("test-bundle-two"); PackageType packageType = new PackageType("test-bundle-two", resourceType); Bundle bundle = new Bundle("test-bundle-two", bundleType, repo, packageType); BundleVersion bundleVersion = new BundleVersion( "test-bundle-two", "3.0", bundle, getRecipeFromFile("test-bundle-three.xml")); BundleDestination destination = new BundleDestination( bundle, "testSimpleBundle2Dest", new ResourceGroup("testSimpleBundle2Group"), DEST_BASE_DIR_NAME, this.destDir.getAbsolutePath()); Configuration config = new Configuration(); String customPropName = "custom.prop"; String customPropValue = "DEF"; String onePropName = "one.prop"; String onePropValue = "one-one-one"; String threePropName = "three.prop"; String threePropValue = "333"; config.put(new PropertySimple(customPropName, customPropValue)); config.put(new PropertySimple(onePropName, onePropValue)); config.put(new PropertySimple(threePropName, threePropValue)); BundleDeployment deployment = new BundleDeployment(); deployment.setId(456); deployment.setName("test bundle 3 deployment name - upgrades test bundle 2"); deployment.setBundleVersion(bundleVersion); deployment.setConfiguration(config); deployment.setDestination(destination); // copy the test archive file to the bundle files dir FileUtil.copyFile( new File("src/test/resources/test-bundle-three-archive.zip"), new File(this.bundleFilesDir, "test-bundle-three-archive.zip")); // create test.properties file in the bundle files dir File file1 = new File(this.bundleFilesDir, "test.properties"); Properties props = new Properties(); props.setProperty(customPropName, "@@" + customPropName + "@@"); FileOutputStream outputStream = new FileOutputStream(file1); props.store(outputStream, "test.properties comment"); outputStream.close(); // create some additional files - note: recipe says to ignore "ignore/**" File ignoreDir = new File(this.destDir, "ignore"); File extraDir = new File(this.destDir, "extra"); ignoreDir.mkdirs(); extraDir.mkdirs(); File ignoredFile = new File(ignoreDir, "ignore-file.txt"); File extraFile = new File(extraDir, "extra-file.txt"); FileUtil.writeFile(new ByteArrayInputStream("ignore".getBytes()), ignoredFile); FileUtil.writeFile(new ByteArrayInputStream("extra".getBytes()), extraFile); BundleDeployRequest request = new BundleDeployRequest(); request.setBundleFilesLocation(this.bundleFilesDir); request.setResourceDeployment(new BundleResourceDeployment(deployment, null)); request.setBundleManagerProvider(new MockBundleManagerProvider()); request.setAbsoluteDestinationDirectory(this.destDir); request.setCleanDeployment(clean); BundleDeployResult results = plugin.deployBundle(request); assertResultsSuccess(results); // test that the prop was replaced in raw file test.properties Properties realizedProps = new Properties(); loadProperties( realizedProps, new FileInputStream(new File(this.destDir, "config/test.properties"))); assert customPropValue.equals(realizedProps.getProperty(customPropName)) : "didn't replace prop"; // test that the archive was extracted properly. These are the files in the archive or removed // from original: // REMOVED: zero-file.txt // one/one-file.txt (content: "@@one.prop@@") <-- recipe says this is to be replaced // two/two-file.txt (content: "@@two.prop@@") <-- recipe does not say to replace this // three/three-file.txt (content: "@@three.prop@@") <-- recipe says this is to be replaced File zeroFile = new File(this.destDir, "zero-file.txt"); File oneFile = new File(this.destDir, "one/one-file.txt"); File twoFile = new File(this.destDir, "two/two-file.txt"); File threeFile = new File(this.destDir, "three/three-file.txt"); assert !zeroFile.exists() : "zero file should have been removed during upgrade"; assert oneFile.exists() : "one file missing"; assert twoFile.exists() : "two file missing"; assert threeFile.exists() : "three file missing"; if (clean) { assert !ignoredFile.exists() : "ignored file should have been deleted due to clean deployment request"; assert !extraFile.exists() : "extra file should have been deleted due to clean deployment request"; } else { assert ignoredFile.exists() : "ignored file wasn't ignored, it was deleted"; assert !extraFile.exists() : "extra file ignored, but it should have been deleted/backed up"; } assert readFile(oneFile).startsWith(onePropValue); assert readFile(twoFile).startsWith("@@two.prop@@"); assert readFile(threeFile).startsWith(threePropValue); DeploymentsMetadata metadata = new DeploymentsMetadata(this.destDir); DeploymentProperties deploymentProps = metadata.getDeploymentProperties(deployment.getId()); assert deploymentProps.getDeploymentId() == deployment.getId(); assert deploymentProps.getBundleName().equals(bundle.getName()); assert deploymentProps.getBundleVersion().equals(bundleVersion.getVersion()); assert deploymentProps.getManageRootDir() == true; DeploymentProperties currentProps = metadata.getCurrentDeploymentProperties(); assert deploymentProps.equals(currentProps); // check the backup directory - note, clean flag is irrelevent when determining what should be // backed up File backupDir = metadata.getDeploymentBackupDirectory(deployment.getId()); File extraBackupFile = new File(backupDir, extraDir.getName() + File.separatorChar + extraFile.getName()); File ignoredBackupFile = new File(backupDir, ignoreDir.getName() + File.separatorChar + ignoredFile.getName()); assert !ignoredBackupFile.exists() : "ignored file was backed up but it should not have been"; assert extraBackupFile.exists() : "extra file was not backed up"; assert "extra".equals(new String(StreamUtil.slurp(new FileInputStream(extraBackupFile)))) : "bad backup of extra"; DeploymentProperties previousProps = metadata.getPreviousDeploymentProperties(456); assert previousProps != null : "There should be previous deployment metadata"; assert previousProps.getDeploymentId() == 123 : "bad previous deployment metadata"; // testAntBundleInitialInstall used 123 assert previousProps.getBundleName().equals(deploymentProps.getBundleName()); assert previousProps .getBundleVersion() .equals("2.5"); // testAntBundleInitialInstall deployed version 2.5 assert previousProps.getManageRootDir() == true; }
public void testPersistStreamContent2() throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; boolean done = false; getTransactionManager().begin(); EntityManager em = getEntityManager(); try { byte[] content = "this is the test content".getBytes(); String path = "/test/Persist"; // persist a content-less plugin ServerPlugin plugin = new ServerPlugin("ServerPluginTest-testPersist", path); plugin.setDisplayName("Server Plugin Test - testPersist"); plugin.setEnabled(true); plugin.setMD5(MessageDigestGenerator.getDigestString(new String(content))); em.persist(plugin); assert plugin.getId() > 0; getTransactionManager() .commit(); // must commit since we are going to use a second connection now getTransactionManager().begin(); // test that we can get a null content stream InitialContext context = getInitialContext(); DataSource ds = (DataSource) context.lookup(JNDI_RHQDS); assert ds != null : "Could not get the data source!"; conn = ds.getConnection(); ps = conn.prepareStatement( "SELECT PATH, CONTENT FROM " + ServerPlugin.TABLE_NAME + " WHERE ID = ?"); ps.setInt(1, plugin.getId()); rs = ps.executeQuery(); rs.next(); String dbPath = rs.getString(1); assert dbPath.equals(path); InputStream dbStream = rs.getBinaryStream(2); assert dbStream == null : "Was expecting a null stream but got a non-null stream from db"; rs.close(); ps.close(); conn.close(); rs = null; ps = null; conn = null; getTransactionManager().commit(); getTransactionManager().begin(); // now stream the content into the plugin's table conn = ds.getConnection(); ps = conn.prepareStatement( "UPDATE " + ServerPlugin.TABLE_NAME + " SET CONTENT = ? WHERE ID = ?"); ps.setBinaryStream(1, new ByteArrayInputStream(content), content.length); ps.setInt(2, plugin.getId()); int updateResults = ps.executeUpdate(); assert updateResults == 1 : "Failed to stream the content blob: " + updateResults; ps.close(); ps = null; conn.close(); conn = null; getTransactionManager().commit(); getTransactionManager().begin(); // verify we can get the content stream along with another column in the same query conn = ds.getConnection(); ps = conn.prepareStatement( "SELECT PATH, CONTENT FROM " + ServerPlugin.TABLE_NAME + " WHERE ID = ?"); ps.setInt(1, plugin.getId()); rs = ps.executeQuery(); rs.next(); dbPath = rs.getString(1); assert dbPath.equals(path); dbStream = rs.getBinaryStream(2); assert dbStream != null : "Could not read the plugin content stream from the db"; byte[] contentFromDb = StreamUtil.slurp(dbStream); assert contentFromDb.length == content.length; assert new String(contentFromDb).equals(new String(content)); assert MessageDigestGenerator.getDigestString(new String(contentFromDb)) .equals(MessageDigestGenerator.getDigestString(new String(content))); rs.close(); rs = null; ps.close(); ps = null; conn.close(); conn = null; // clean up - delete our test plugin getTransactionManager().commit(); getTransactionManager().begin(); Query q = em.createNamedQuery(ServerPlugin.QUERY_FIND_ANY_BY_NAME); q.setParameter("name", plugin.getName()); ServerPlugin doomed = (ServerPlugin) q.getSingleResult(); doomed = em.getReference(ServerPlugin.class, doomed.getId()); em.remove(doomed); assert q.getResultList().size() == 0 : "didn't remove the plugin"; getTransactionManager().commit(); done = true; } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } if (!done) { getTransactionManager().rollback(); } } }
public void testPersistStreamContent() throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; boolean done = false; getTransactionManager().begin(); EntityManager em = getEntityManager(); try { String name = "ServerPluginTest-testPersist"; String path = "/test/Persist"; String displayName = "Server Plugin Test - testPersist"; boolean enabled = true; String version = "1.0"; String description = "the test description is here"; String help = "the test help string is here"; byte[] content = "this is the test content".getBytes(); String md5 = MessageDigestGenerator.getDigestString(new String(content)); // persist the plugin, but without any content ServerPlugin plugin = new ServerPlugin(name, path); plugin.setDisplayName(displayName); plugin.setEnabled(enabled); plugin.setMD5(md5); plugin.setVersion(version); plugin.setDescription(description); plugin.setHelp(help); em.persist(plugin); assert plugin.getId() > 0; // verify we have a content-less plugin in the db plugin = em.find(ServerPlugin.class, plugin.getId()); assert plugin != null; assert plugin.getId() > 0; assert plugin.getName().equals(name); assert plugin.getPath().equals(path); assert plugin.getDisplayName().equals(displayName); assert plugin.isEnabled() == enabled; assert plugin.getMD5().equals(md5); assert plugin.getVersion().equals(version); assert plugin.getDescription().equals(description); assert plugin.getHelp().equals(help); assert plugin.getContent() == null; getTransactionManager() .commit(); // must commit since we are going to use a second connection now getTransactionManager().begin(); // now stream the content into the plugin's table InitialContext context = getInitialContext(); DataSource ds = (DataSource) context.lookup(JNDI_RHQDS); assert ds != null : "Could not get the data source!"; conn = ds.getConnection(); ps = conn.prepareStatement( "UPDATE " + ServerPlugin.TABLE_NAME + " SET CONTENT = ? WHERE ID = ?"); ps.setBinaryStream(1, new ByteArrayInputStream(content), content.length); ps.setInt(2, plugin.getId()); int updateResults = ps.executeUpdate(); assert updateResults == 1 : "Failed to stream the content blob: " + updateResults; ps.close(); ps = null; conn.close(); conn = null; getTransactionManager().commit(); getTransactionManager().begin(); // verify the content made it into the database via hibernate plugin = em.find(ServerPlugin.class, plugin.getId()); assert new String(plugin.getContent()).equals(new String(content)); getTransactionManager().commit(); getTransactionManager().begin(); // verify the content made it into the database via jdbc streaming conn = ds.getConnection(); ps = conn.prepareStatement("SELECT CONTENT FROM " + ServerPlugin.TABLE_NAME + " WHERE ID = ?"); ps.setInt(1, plugin.getId()); rs = ps.executeQuery(); rs.next(); InputStream dbStream = rs.getBinaryStream(1); assert dbStream != null : "Could not read the plugin content stream from the db"; byte[] contentFromDb = StreamUtil.slurp(dbStream); assert contentFromDb.length == content.length; assert new String(contentFromDb).equals(new String(content)); assert MessageDigestGenerator.getDigestString(new String(contentFromDb)).equals(md5); rs.close(); rs = null; ps.close(); ps = null; conn.close(); conn = null; // clean up - delete our test plugin getTransactionManager().commit(); getTransactionManager().begin(); Query q = em.createNamedQuery(ServerPlugin.QUERY_FIND_ANY_BY_NAME); q.setParameter("name", plugin.getName()); ServerPlugin doomed = (ServerPlugin) q.getSingleResult(); doomed = em.getReference(ServerPlugin.class, doomed.getId()); em.remove(doomed); assert q.getResultList().size() == 0 : "didn't remove the plugin"; getTransactionManager().commit(); done = true; } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } if (!done) { getTransactionManager().rollback(); } } }