/*
   * (non-Javadoc)
   *
   * @see org.araqne.bundle.BundleManager#installBundle(org.araqne.pkg.
   * ProgressMonitor, java.lang.String, java.lang.String, java.lang.String)
   */
  @Override
  public long installBundle(
      ProgressMonitor monitor, String groupId, String artifactId, String version)
      throws MavenResolveException {
    MavenResolver resolver =
        new MavenResolver(
            getLocalRepository(), config.getRepositories(), monitor, getKeyStoreManager());
    Version v = (version != null) ? new Version(version) : null;
    MavenArtifact artifact = new MavenArtifact(groupId, artifactId, v);

    if (isBuiltinArtifact(artifact.getGroupId(), artifact.getArtifactId()))
      throw new IllegalStateException("provided in system bundle");

    if (monitor != null)
      monitor.writeln(
          String.format("Resolving %s/%s", artifact.getGroupId(), artifact.getArtifactId())
              + (artifact.getVersion() != null ? (" (" + artifact.getVersion() + ")") : ""));

    File file = resolver.resolve(artifact);
    if (!isBundleJar(file))
      throw new IllegalStateException(
          "invalid OSGi bundle: " + groupId + "/" + artifactId + " (" + version + ")");

    String filePath = file.getAbsolutePath();

    try {
      Bundle newBundle = context.installBundle(getPrefix() + filePath.replace('\\', '/'));
      if (newBundle.getSymbolicName() == null) newBundle.uninstall();

      return newBundle.getBundleId();
    } catch (BundleException e) {
      throw new IllegalStateException(e);
    }
  }
  @Override
  public void updateBundleVersion(
      long bundleId, String groupId, String artifactId, String version) {
    Bundle bundle = context.getBundle(bundleId);
    if (bundle == null) {
      logger.warn(String.format("bundle %d not found", bundleId));
      throw new IllegalStateException("bundle " + bundleId + " not found");
    }

    MavenResolver resolver =
        new MavenResolver(
            getLocalRepository(), config.getRepositories(), null, getKeyStoreManager());
    Version v = (version != null) ? new Version(version) : null;
    MavenArtifact artifact = new MavenArtifact(groupId, artifactId, v);

    if (isBuiltinArtifact(artifact.getGroupId(), artifact.getArtifactId()))
      throw new IllegalStateException("provided in system bundle");

    try {
      File file = resolver.resolve(artifact);
      if (!isBundleJar(file))
        throw new IllegalStateException(
            "invalid OSGi bundle: " + groupId + "/" + artifactId + " (" + version + ")");

      bundle.update(new FileInputStream(file));
    } catch (Exception e) {
      throw new IllegalStateException("bundleId: " + bundleId + ", version: " + version, e);
    }
  }