Example #1
0
  @SuppressWarnings("deprecation")
  @Override
  public Bundle assemble(IssueSubmissionRequest request) throws IssueSubmissionException {
    OutputStream out = null;
    try {
      final ManagedBundle bundle = storageManager.createBundle("nexus.xml", "application/xml");
      final Configuration configuration =
          configHelper.maskPasswords(nexusConfig.getConfigurationModel());

      // No config ?
      if (configuration != null) {
        NexusConfigurationXpp3Writer writer = new NexusConfigurationXpp3Writer();
        out = bundle.getOutputStream();
        writer.write(out, configuration);
        out.close();
      } else {
        ByteStreams.write(
            "Got no configuration from config helper".getBytes("utf-8"),
            new OutputSupplier<OutputStream>() {
              @Override
              public OutputStream getOutput() throws IOException {
                return bundle.getOutputStream();
              }
            });
      }
      return bundle;
    } catch (IOException e) {
      IOUtil.close(out);
      throw new IssueSubmissionException("Could not assemble nexus.xml: " + e.getMessage(), e);
    }
  }
  /**
   * Load configuration.
   *
   * @param file the file
   * @return the configuration
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void loadConfiguration(File file) throws IOException, ConfigurationException {
    getLogger().info("Loading Nexus configuration from " + file.getAbsolutePath());

    FileInputStream fis = null;
    try {
      fis = new FileInputStream(file);

      loadConfiguration(fis);

      // seems a bit dirty, but the config might need to be upgraded.
      if (this.getConfiguration() != null) {
        // decrypt the passwords
        setConfiguration(configHelper.encryptDecryptPasswords(getConfiguration(), false));
      }
    } finally {
      if (fis != null) {
        fis.close();
      }
    }
  }
  /**
   * Save configuration.
   *
   * @param file the file
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void saveConfiguration(File file) throws IOException {
    FileOutputStream fos = null;

    File backupFile = new File(file.getParentFile(), file.getName() + ".old");

    try {
      // Create the dir if doesn't exist, throw runtime exception on failure
      // bad bad bad
      if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
        String message =
            "\r\n******************************************************************************\r\n"
                + "* Could not create configuration file [ "
                + file.toString()
                + "]!!!! *\r\n"
                + "* Nexus cannot start properly until the process has read+write permissions to this folder *\r\n"
                + "******************************************************************************";

        getLogger().error(message);
      }

      // copy the current nexus config file as file.bak
      if (file.exists()) {
        FileUtils.copyFile(file, backupFile);
      }

      // Clone the conf so we can encrypt the passwords
      Configuration copyOfConfig = configHelper.encryptDecryptPasswords(getConfiguration(), true);

      fos = new FileOutputStream(file);

      saveConfiguration(fos, copyOfConfig);

      fos.flush();
    } finally {
      IOUtil.close(fos);
    }

    // if all went well, delete the bak file
    backupFile.delete();
  }