Java Code Examples for org.sonatype.nexus.repository.storage.Asset#componentId()

The following examples show how to use org.sonatype.nexus.repository.storage.Asset#componentId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: P2ComponentMaintenance.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Deletes the asset and its component if it's the only asset in it.
 */
@Override
@Guarded(by = STARTED)
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
  if (asset == null) {
    return Collections.emptySet();
  }

  final EntityId componentId = asset.componentId();
  if (componentId == null) {
    // Assets without components should be deleted on their own
    return super.deleteAssetTx(assetId, deleteBlob);
  }

  final Component component = tx.findComponent(componentId);
  if (component == null) {
    return Collections.emptySet();
  }
  return deleteComponentTx(componentId, deleteBlob).getAssets();
}
 
Example 2
Source File: HelmComponentMaintenanceFacet.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return Collections.emptySet();
  }

  tx.deleteAsset(asset, deleteBlobs);

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }
  return Collections.singleton(asset.name());
}
 
Example 3
Source File: RComponentMaintenance.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Deletes the asset and its component if it's the only asset in it.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
  if (asset == null) {
    return Collections.emptySet();
  }

  final EntityId componentId = asset.componentId();
  if (componentId == null) {
    // Assets without components should be deleted on their own
    return super.deleteAssetTx(assetId, deleteBlob);
  }

  final Component component = tx.findComponent(componentId);
  if (Iterables.size(tx.browseAssets(component)) == 1) {
    // Component with only one asset should be deleted as well with its asset
    return deleteComponentTx(componentId, deleteBlob).getAssets();
  }
  else {
    return super.deleteAssetTx(assetId, deleteBlob);
  }
}
 
Example 4
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content doPutPackage(final TempBlob tempBlob,
                               final Payload content,
                               final ConanCoords coords,
                               final AssetKind assetKind) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = getOrCreateComponent(tx, bucket, coords);

  String assetPath = getProxyAssetPath(coords, assetKind);
  Asset asset = findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, CONAN_PACKAGE.name());
  }
  else if (!asset.componentId().equals(EntityHelper.id(component))) {
    asset.componentId(EntityHelper.id(component));
  }
  return saveAsset(tx, asset, tempBlob, content, null);
}
 
Example 5
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content doSaveMetadata(final TempBlob metadataContent,
                                 final Payload payload,
                                 final AssetKind assetKind,
                                 final ConanCoords coords) throws IOException
{
  HashCode hash = null;
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = getOrCreateComponent(tx, bucket, coords);

  String assetPath = getProxyAssetPath(coords, assetKind);
  Asset asset = findAsset(tx, bucket, assetPath);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(assetPath);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind.name());
    hash = hashVerifier.lookupHashFromAsset(tx, bucket, assetPath);
  }
  else if (!asset.componentId().equals(EntityHelper.id(component))) {
    asset.componentId(EntityHelper.id(component));
  }
  return saveAsset(tx, asset, metadataContent, payload, hash);
}
 
Example 6
Source File: CondaComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return emptySet();
  }

  tx.deleteAsset(asset, deleteBlobs);

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }
  return Collections.singleton(asset.name());
}
 
Example 7
Source File: MavenComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalDeleteBlob
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = tx.findAsset(assetId);
  if (asset == null) {
    return Collections.emptySet();
  }

  Set<String> deletedAssets = new HashSet<>(super.deleteAssetTx(assetId, deleteBlob));

  final EntityId componentId = asset.componentId();
  if (componentId != null) {
    Component component = tx.findComponent(componentId);
    if (component != null && !tx.browseAssets(component).iterator().hasNext()) {
      deletedAssets.addAll(deleteComponentTx(componentId, deleteBlob).getAssets());
    }
  }

  return deletedAssets;
}
 
Example 8
Source File: DeleteFolderServiceImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return EntityId of associated component if exists
 */
private Optional<EntityId> deleteAsset(final Repository repository,
                                       final EntityId assetId,
                                       final DateTime timestamp,
                                       final ComponentMaintenance componentMaintenance)
{
  EntityId componenetId = null;
  Asset asset = assetStore.getById(assetId);
  if (timestamp.isAfter(asset.blobCreated()) && canDeleteAsset(repository, asset)) {
    try {
      componentMaintenance.deleteAsset(assetId);
      componenetId = asset.componentId();
    }
    catch (Exception e) {
      log.error("Failed to delete an asset - skipping.", e);
    }
  }
  return Optional.ofNullable(componenetId);
}
 
Example 9
Source File: OrientBrowseNodeManager.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates an asset browse node and optional component browse node if the asset has a component.
 */
@SuppressWarnings("unchecked")
private void createBrowseNodes(final String repositoryName,
                               final String format,
                               final BrowseNodeGenerator generator,
                               final Asset asset)
{
  try {
    Component component = asset.componentId() != null ? componentStore.read(asset.componentId()) : null;

    List<? extends BrowsePath> assetPaths = generator.computeAssetPaths(asset, component);
    if (!assetPaths.isEmpty()) {
      browseNodeStore.createAssetNode(repositoryName, format, (List<BrowsePath>) assetPaths, asset);
    }

    if (component != null) {
      List<? extends BrowsePath> componentPaths = generator.computeComponentPaths(asset, component);
      if (!componentPaths.isEmpty()) {
        browseNodeStore.createComponentNode(repositoryName, format, (List<BrowsePath>) componentPaths, component);
      }
    }
  }
  catch (RuntimeException e) {
    log.warn("Problem generating browse nodes for {}", asset, e);
  }
}
 
Example 10
Source File: BrowseNodeEntityAdapterTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void createEntities(final ODatabaseDocumentTx db) {
  bucket = new Bucket();
  bucket.setRepositoryName(REPOSITORY_NAME);
  bucket.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>()));
  bucketEntityAdapter.addEntity(db, bucket);

  component = new DefaultComponent();
  component.bucketId(EntityHelper.id(bucket));
  component.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>()));
  component.format(FORMAT_NAME);
  component.group("org").name("foo").version("1.0");
  componentEntityAdapter.addEntity(db, component);

  asset = new Asset();
  asset.bucketId(EntityHelper.id(bucket));
  asset.componentId(EntityHelper.id(component));
  asset.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>()));
  asset.format(FORMAT_NAME);
  asset.name("/org/foo/1.0/foo-1.0.jar");
  assetEntityAdapter.addEntity(db, asset);
}
 
Example 11
Source File: OrientBlobstoreRestoreTestHelper.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void assertForComponentId(final Repository repository, final Component component, final String path) {
  EntityId compId = component.getEntityMetadata().getId();
  assertThat(compId, notNullValue());

  try (StorageTx tx = getStorageTx(repository)) {
    tx.begin();

    Asset asset = tx.findAssetWithProperty(AssetEntityAdapter.P_NAME, path, tx.findBucket(repository));
    assertThat(asset, notNullValue());

    final EntityId assertEntityId = asset.componentId();
    assertThat(assertEntityId, notNullValue());
    assertEquals(assertEntityId, compId);
  }
}
 
Example 12
Source File: OrientPyPiComponentMaintenance.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes the asset. If the associated component has no additional assets, then the component is also deleted.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());
  final Asset asset = tx.findAsset(assetId, bucket);
  if (asset == null) {
    return Collections.emptySet();
  }
  Set<String> deletedAssets = new HashSet<>();
  deletedAssets.addAll(super.deleteAssetTx(assetId, deleteBlob));

  final EntityId componentId = asset.componentId();
  if (componentId != null) {
    deleteRootIndex();

    deleteCachedIndexForPackage(asset);

    final Component component = tx.findComponentInBucket(componentId, bucket);
    if (component != null && !tx.browseAssets(component).iterator().hasNext()) {
      deletedAssets.addAll(deleteComponentTx(componentId, deleteBlob).getAssets());
    }
  }

  return deletedAssets;
}
 
Example 13
Source File: AptHostedComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return Collections.emptySet();
  }

  String assetKind = asset.formatAttributes().get(P_ASSET_KIND, String.class);
  Set<String> result = super.deleteAssetTx(assetId, deleteBlobs);
  if ("DEB".equals(assetKind)) {
    try {
      getRepository().facet(AptHostedFacet.class)
          .rebuildIndexes(Collections.singletonList(new AptHostedFacet.AssetChange(AssetAction.REMOVED, asset)));
    }
    catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }

  return result;
}
 
Example 14
Source File: BrowseNodeEntityAdapterTest.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void browseNestedAssets() throws Exception {
  List<String> path = Splitter.on('/').omitEmptyStrings().splitToList(asset.name());

  try (ODatabaseDocumentTx db = database.getInstance().acquire()) {

    Asset parentAsset = new Asset();
    parentAsset.bucketId(EntityHelper.id(bucket));
    parentAsset.componentId(EntityHelper.id(component));
    parentAsset.attributes(new NestedAttributesMap(P_ATTRIBUTES, new HashMap<>()));
    parentAsset.format(FORMAT_NAME);
    parentAsset.name("/org/foo");
    assetEntityAdapter.addEntity(db, parentAsset);

    underTest.createAssetNode(db, REPOSITORY_NAME, FORMAT_NAME, toBrowsePaths(path.subList(0, 2)), parentAsset);
    underTest.createAssetNode(db, REPOSITORY_NAME, FORMAT_NAME, toBrowsePaths(path), asset);

    assertThat(underTest.getByPath(db, REPOSITORY_NAME, path.subList(0, 0), 1, "", emptyMap()),
        contains(
            allOf(
                hasProperty("repositoryName", is(REPOSITORY_NAME)),
                hasProperty("parentPath", is("/")),
                hasProperty("name", is("org")),
                hasProperty("leaf", is(false)),
                hasProperty("componentId", nullValue()),
                hasProperty("assetId", nullValue()),
                hasProperty("path", is("org/")))
            ));

    assertThat(underTest.getByPath(db, REPOSITORY_NAME, path.subList(0, 1), 1, "", emptyMap()),
        contains(
            allOf(
                hasProperty("repositoryName", is(REPOSITORY_NAME)),
                hasProperty("parentPath", is("/org/")),
                hasProperty("name", is("foo")),
                hasProperty("leaf", is(false)),
                hasProperty("componentId", nullValue()),
                hasProperty("assetId", is(EntityHelper.id(parentAsset))),
                hasProperty("path", is("org/foo/")))
            ));

    assertThat(underTest.getByPath(db, REPOSITORY_NAME, path.subList(0, 2), 1, "", emptyMap()),
        contains(
            allOf(
                hasProperty("repositoryName", is(REPOSITORY_NAME)),
                hasProperty("parentPath", is("/org/foo/")),
                hasProperty("name", is("1.0")),
                hasProperty("leaf", is(false)),
                hasProperty("componentId", nullValue()),
                hasProperty("assetId", nullValue()),
                hasProperty("path", is("org/foo/1.0/")))
            ));

    assertThat(underTest.getByPath(db, REPOSITORY_NAME, path.subList(0, 3), 1, "", emptyMap()),
        contains(
            allOf(
                hasProperty("repositoryName", is(REPOSITORY_NAME)),
                hasProperty("parentPath", is("/org/foo/1.0/")),
                hasProperty("name", is("foo-1.0.jar")),
                hasProperty("leaf", is(true)),
                hasProperty("componentId", nullValue()),
                hasProperty("assetId", is(EntityHelper.id(asset))),
                hasProperty("path", is("org/foo/1.0/foo-1.0.jar")))
            ));

    assertThat(underTest.getByPath(db, REPOSITORY_NAME, path, 1, "", emptyMap()), is(empty()));
  }
}