private long getPid(File nodeDir) throws IOException {
    File binDir = new File(nodeDir, "bin");
    StringWriter writer = new StringWriter();
    StreamUtil.copy(new FileReader(new File(binDir, "cassandra.pid")), writer);

    return Long.parseLong(writer.getBuffer().toString());
  }
  private Stats getLastCallTimeCollection(
      String requestName, Map<String, Map<String, Number>> fallbackValues, long fallbackStartTime)
      throws IOException {

    File dataFile = new File(context.getResourceDataDirectory(), requestName);
    if (!dataFile.exists()) {
      return Stats.fromMap(
          fallbackValues, requestName, System.currentTimeMillis(), fallbackStartTime);
    } else {
      ObjectInputStream in = null;
      try {
        in = new ObjectInputStream(new FileInputStream(dataFile));

        Stats stats = (Stats) in.readObject();
        if (stats.serverStartTime == 0) {
          // we might get serverStartTime == 0 if the datafile comes from the old version of the
          // plugin
          // in that case just fallback to the old behavior that assumed no server restarts.
          // After that we save the new version of the stats with the start time remembered and we
          // will
          // switch to the new correct behavior from the next collection.
          stats.serverStartTime = fallbackStartTime;
        }

        return stats;
      } catch (IOException e) {
        throw new IOException(
            "Couldn't read the stored calltime data from file " + dataFile + ".", e);
      } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Couldn't find plugin API classes. This is serious!", e);
      } finally {
        StreamUtil.safeClose(in);
      }
    }
  }
示例#3
0
  protected String getStoragePid() throws IOException {
    File pidFile = getStoragePidFile();

    if (pidFile.exists()) {
      return StreamUtil.slurp(new FileReader(pidFile));
    }
    return null;
  }
  private void saveCallTimeCollection(String requestName, Stats stats) throws IOException {
    File dataFile = new File(context.getResourceDataDirectory(), requestName);

    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream(new FileOutputStream(dataFile));
      out.writeObject(stats);
    } catch (IOException e) {
      throw new IOException(
          "Couldn't write the last collected calltime data to file " + dataFile + ".", e);
    } finally {
      StreamUtil.safeClose(out);
    }
  }
  private void updateStorageAuthConf(File basedir) {
    File confDir = new File(basedir, "conf");
    File authFile = new File(confDir, "rhq-storage-auth.conf");
    authFile.delete();

    Set<String> addresses = calculateLocalIPAddresses(deploymentOptions.getNumNodes());

    try {
      StreamUtil.copy(
          new StringReader(StringUtil.collectionToString(addresses, "\n")),
          new FileWriter(authFile),
          true);
    } catch (IOException e) {
      throw new RuntimeException("Failed to update " + authFile);
    }
  }
  private BundleVersion getBundleVersion(Bundle bundle) throws Exception {
    BundleVersionCriteria criteria = new BundleVersionCriteria();
    criteria.addFilterBundleId(bundle.getId());
    criteria.addFilterVersion(bundleVersion);
    criteria.fetchBundle(true);
    criteria.fetchBundleDeployments(true);

    PageList<BundleVersion> bundleVersions =
        bundleManager.findBundleVersionsByCriteria(overlord, criteria);

    if (bundleVersions.isEmpty()) {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      StreamUtil.copy(getClass().getResourceAsStream("cassandra-bundle.jar"), outputStream);
      return bundleManager.createBundleVersionViaByteArray(overlord, outputStream.toByteArray());
    }

    return bundleVersions.get(0);
  }
示例#7
0
  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;
    }
  }
示例#8
0
  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();
    }
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    HttpSession session = req.getSession();
    session.setMaxInactiveInterval(MAX_INACTIVE_INTERVAL);

    if (ServletFileUpload.isMultipartContent(req)) {

      DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
      // fileItemFactory.setSizeThreshold(0);

      ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

      List<FileItem> fileItemsList;
      try {
        fileItemsList = (List<FileItem>) servletFileUpload.parseRequest(req);
      } catch (FileUploadException e) {
        writeExceptionResponse(resp, "File upload failed", e);
        return;
      }

      List<FileItem> actualFiles = new ArrayList<FileItem>();
      Map<String, String> formFields = new HashMap<String, String>();
      boolean retrieve = false;
      Subject authenticatedSubject = null;

      for (FileItem fileItem : fileItemsList) {
        if (fileItem.isFormField()) {
          if (fileItem.getFieldName() != null) {
            formFields.put(fileItem.getFieldName(), fileItem.getString());
          }
          if ("retrieve".equals(fileItem.getFieldName())) {
            retrieve = true;
          } else if ("sessionid".equals(fileItem.getFieldName())) {
            int sessionid = Integer.parseInt(fileItem.getString());
            SubjectManagerLocal subjectManager = LookupUtil.getSubjectManager();
            try {
              authenticatedSubject = subjectManager.getSubjectBySessionId(sessionid);
            } catch (Exception e) {
              throw new ServletException("Cannot authenticate request", e);
            }
          }
          fileItem.delete();
        } else {
          // file item contains an actual uploaded file
          actualFiles.add(fileItem);
          log("file was uploaded: " + fileItem.getName());
        }
      }

      if (authenticatedSubject == null) {
        for (FileItem fileItem : actualFiles) {
          fileItem.delete();
        }
        throw new ServletException("Cannot process unauthenticated request");
      }

      if (retrieve && actualFiles.size() == 1) {
        // sending in "retrieve" form element with a single file means the client just wants the
        // content echoed back
        resp.setContentType("text/html");
        FileItem fileItem = actualFiles.get(0);

        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.print("<html>");
        InputStream inputStream = fileItem.getInputStream();
        try {
          // we have to HTML escape inputStream before writing it to outputStream
          StreamUtil.copy(inputStream, outputStream, false, true);
        } finally {
          inputStream.close();
        }
        outputStream.print("</html>");
        outputStream.flush();

        fileItem.delete();
      } else {
        Map<String, File> allUploadedFiles =
            new HashMap<String, File>(); // maps form field name to the actual file
        Map<String, String> allUploadedFileNames =
            new HashMap<String, String>(); // maps form field name to upload file name
        for (FileItem fileItem : actualFiles) {
          File theFile = forceToFile(fileItem);
          allUploadedFiles.put(fileItem.getFieldName(), theFile);
          allUploadedFileNames.put(
              fileItem.getFieldName(),
              (fileItem.getName() != null) ? fileItem.getName() : theFile.getName());
        }
        processUploadedFiles(
            authenticatedSubject, allUploadedFiles, allUploadedFileNames, formFields, req, resp);
      }
    }
  }
 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;
  }
示例#13
0
  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();
      }
    }
  }
示例#14
0
  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();
      }
    }
  }