org.sonatype.nexus.transaction.UnitOfWork Java Examples

The following examples show how to use org.sonatype.nexus.transaction.UnitOfWork. 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: NpmPackageRootMetadataUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private static String getPackageRootLatestVersion(final NestedAttributesMap packageJson,
                                                  final Repository repository)
{
  StorageTx tx = UnitOfWork.currentTx();
  NpmPackageId packageId = NpmPackageId.parse((String) packageJson.get(P_NAME));

  try {
    NestedAttributesMap packageRoot = getPackageRoot(tx, repository, packageId);
    if(nonNull(packageRoot)) {

      String latestVersion = getLatestVersionFromPackageRoot(packageRoot);
      if (nonNull(latestVersion)) {
        return latestVersion;
      }
    }
  }
  catch (IOException ignored) { // NOSONAR
  }
  return "";
}
 
Example #2
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Content doPut(final MavenPath path,
                        final Payload payload,
                        final TempBlob tempBlob)
    throws IOException
{
  final StorageTx tx = UnitOfWork.currentTx();

  final AssetBlob assetBlob = tx.createBlob(
      path.getPath(),
      tempBlob,
      null,
      payload.getContentType(),
      false
  );
  AttributesMap contentAttributes = null;
  if (payload instanceof Content) {
    contentAttributes = ((Content) payload).getAttributes();
  }

  return doPutAssetBlob(path, contentAttributes, tx, assetBlob);
}
 
Example #3
Source File: ElasticSearchCleanupComponentBrowse.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public PagedResponse<Component> browseByPage(final CleanupPolicy policy,
                                             final Repository repository,
                                             final QueryOptions options)
{
  checkNotNull(options.getStart());
  checkNotNull(options.getLimit());

  StorageTx tx = UnitOfWork.currentTx();

  QueryBuilder query = convertPolicyToQuery(policy, options);

  log.debug("Searching for components to cleanup using policy {}", policy);

  SearchResponse searchResponse = invokeSearchByPage(policy, repository, options, query);

  List<Component> components = stream(searchResponse.getHits().spliterator(), false)
      .map(searchHit -> tx.findComponent(new DetachedEntityId(searchHit.getId())))
      .filter(Objects::nonNull)
      .collect(toList());

  return new PagedResponse<>(searchResponse.getHits().getTotalHits(), components);
}
 
Example #4
Source File: NpmAuditTarballFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Optional<String> getComponentHashsumForProxyRepo(final Repository repository, final Context context)
    throws TarballLoadingException
{
  try {
    UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
    Content content = repository.facet(ProxyFacet.class).get(context);
    if (content != null) {
      return getHashsum(content.getAttributes());
    }
  }
  catch (IOException e) {
    throw new TarballLoadingException(e.getMessage());
  }
  finally {
    UnitOfWork.end();
  }

  return Optional.empty();
}
 
Example #5
Source File: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreMetadata
public Asset getOrCreateAsset(final String path,
                              final String group,
                              final String name,
                              final String version)
{
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());

  Component component = findComponent(tx, group, name, version);
  if (component == null) {
    component = tx.createComponent(bucket, format).group(group).name(name).version(version);
    tx.saveComponent(component);
  }

  Asset asset = findAsset(tx, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);
  }

  asset.markAsDownloaded();

  return asset;
}
 
Example #6
Source File: PurgeUnusedFacetImplTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Thread createTaskThread(final Runnable action, final AtomicBoolean cancelFlag) {
  Thread t = new Thread(() -> {
    CancelableHelper.set(cancelFlag);
    UnitOfWork.beginBatch(tx);

    action.run();
  });

  t.setUncaughtExceptionHandler((thread, ex) -> {
    if (ex instanceof TaskInterruptedException) {
      return;
    }
    uncaught.add(ex);
  });

  return t;
}
 
Example #7
Source File: ComposerContentFacetImpl.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
@TransactionalTouchBlob
public Content get(final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();

  final Asset asset = findAsset(tx, path);
  if (asset == null) {
    return null;
  }
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }

  final Blob blob = tx.requireBlob(asset.requireBlobRef());
  return toContent(asset, blob);
}
 
Example #8
Source File: OrientMavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void writeWithoutValidation(
    final Repository repository,
    final String path,
    final Payload payload) throws IOException
{
  final MavenFacet mavenFacet = repository.facet(MavenFacet.class);
  final StorageFacet storageFacet = repository.facet(StorageFacet.class);

  final MavenPath mavenPath = mavenFacet.getMavenPathParser().parsePath(path);
  UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
  try {
    try (TempBlob tempBlob = storageFacet.createTempBlob(payload, HashType.ALGORITHMS)) {

      mavenFacet.put(mavenPath, tempBlob, MavenMimeRulesSource.METADATA_TYPE, new AttributesMap());
    }
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #9
Source File: MavenIndexPublisher.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Publishes MI index into {@code target}, sourced from repository's own CMA structures.
 */
public static void publishHostedIndex(final Repository repository,
                                      final DuplicateDetectionStrategy<Record> duplicateDetectionStrategy)
    throws IOException
{
  checkNotNull(repository);
  Transactional.operation.throwing(IOException.class).call(
      () -> {
        final StorageTx tx = UnitOfWork.currentTx();
        try (Maven2WritableResourceHandler resourceHandler = new Maven2WritableResourceHandler(repository)) {
          try (IndexWriter indexWriter = new IndexWriter(resourceHandler, repository.getName(), false)) {
            indexWriter.writeChunk(
                transform(
                    decorate(
                        filter(getHostedRecords(tx, repository), duplicateDetectionStrategy),
                        repository.getName()
                    ),
                    RECORD_COMPACTOR::apply
                ).iterator()
            );
          }
        }
        return null;
      }
  );
}
 
Example #10
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalStoreBlob
@Nullable
public Content getRootIndex() {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, bucket, INDEX_PATH_PREFIX);
  if (asset == null) {
    try {
      return createAndSaveRootIndex(bucket);
    }
    catch (IOException e) {
      log.error("Unable to create root index for repository: {}", getRepository().getName(), e);
      return null;
    }
  }

  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example #11
Source File: AptSnapshotFacetSupport.java    From nexus-repository-apt with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = { ONeedRetryException.class })
@Override
public Content getSnapshotFile(String id, String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.findComponentWithProperty(P_NAME, id, bucket);
  if (component == null) {
    return null;
  }

  final Asset asset = tx.findAssetWithProperty(P_NAME, createAssetPath(id, path), component);
  if (asset == null) {
    return null;
  }

  final Blob blob = tx.requireBlob(asset.requireBlobRef());
  return FacetHelper.toContent(asset, blob);
}
 
Example #12
Source File: ElasticSearchCleanupComponentBrowseTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
  underTest = new ElasticSearchCleanupComponentBrowse(ImmutableMap.of(
      LAST_DOWNLOADED_KEY, new LastDownloadedCriteriaAppender(),
      LAST_BLOB_UPDATED_KEY, new LastBlobUpdatedCriteriaAppender(),
      IS_PRERELEASE_KEY, new PrereleaseCriteriaAppender(),
      REGEX_KEY, new RegexCriteriaAppender()
  ), searchQueryService, metricRegistry);

  when(repository.getName()).thenReturn(REPO_NAME);

  when(searchHit1.getId()).thenReturn(COMPONENT_ID_1);
  when(searchHit2.getId()).thenReturn(COMPONENT_ID_2);

  when(metricRegistry.timer(anyString())).thenReturn(timer);

  UnitOfWork.beginBatch(tx);
}
 
Example #13
Source File: HelmHostedFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@Override
@TransactionalTouchBlob
public Content get(final String path) {
  checkNotNull(path);
  StorageTx tx = UnitOfWork.currentTx();

  Optional<Asset> assetOpt = helmFacet.findAsset(tx, path);
  if (!assetOpt.isPresent()) {
    return null;
  }
  Asset asset = assetOpt.get();
  if (asset.markAsDownloaded()) {
    tx.saveAsset(asset);
  }

  return helmFacet.toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example #14
Source File: ConanHostedMetadataFacetSupport.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable
@TransactionalTouchBlob
public String getHash(final String path) {
  checkNotNull(path);

  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findAsset(tx, tx.findBucket(getRepository()), path);
  if (asset == null) {
    return null;
  }
  HashCode checksum = asset.getChecksum(HashAlgorithm.MD5);
  if (checksum == null) {
    return null;
  }
  return checksum.toString();
}
 
Example #15
Source File: IndexGroupFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void publishIndex() throws IOException {
  UnitOfWork.begin(getRepository().facet(StorageFacet.class).txSupplier());
  try (DuplicateDetectionStrategy<Record> strategy = duplicateDetectionStrategyProvider.get()) {
    List<Repository> leafMembers = facet(GroupFacet.class).leafMembers();
    ArrayList<String> withoutIndex = new ArrayList<>();
    for (Iterator<Repository> ri = leafMembers.iterator(); ri.hasNext(); ) {
      Repository repository = ri.next();
      if (repository.facet(MavenIndexFacet.class).lastPublished() == null) {
        withoutIndex.add(repository.getName());
        ri.remove();
      }
    }
    if (!withoutIndex.isEmpty()) {
      log.info("Following members of group {} have no index, will not participate in merged index: {}",
          getRepository().getName(),
          withoutIndex
      );
    }
    MavenIndexPublisher.publishMergedIndex(getRepository(), leafMembers, strategy);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #16
Source File: NpmSearchIndexFacetGroup.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void pauseTransactionAndProcessMemberRepositories(final StorageTx tx,
                                                          final List<Repository> members,
                                                          final Closer closer,
                                                          final ArrayList<JsonParser> parsers)
    throws IOException
{
  UnitOfWork groupWork = UnitOfWork.pause();
  try {
    tx.commit();
    for (Repository repository : Lists.reverse(members)) {
      processMember(closer, parsers, repository);
    }
  }
  finally {
    UnitOfWork.resume(groupWork);
    tx.begin();
  }
}
 
Example #17
Source File: OrientNpmGroupFacetTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private int concurrentlyBuildPackageRoot(final boolean cooperationEnabled) throws Exception
{
  int instancesCount = 100;

  setupCooperation(cooperationEnabled, instancesCount);

  final int iterations = 2;
  final int iterationTimeoutSeconds = 60;

  ConcurrentRunner runner = new ConcurrentRunner(iterations, iterationTimeoutSeconds);
  runner.addTask(instancesCount, () -> {
    UnitOfWork.beginBatch(storageTx);
    try {
      underTest.buildPackageRoot(newHashMap(), context);
    }
    finally {
      UnitOfWork.end();
    }
  });
  runner.go();

  int invocationsCount = runner.getTaskCount() * runner.getIterations();
  assertThat(runner.getRunInvocations(), is(invocationsCount));
  return invocationsCount;
}
 
Example #18
Source File: RUploadHandler.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public UploadResponse handle(final Repository repository, final ComponentUpload upload) throws IOException {
  final AssetUpload assetUpload = upload.getAssetUploads().get(0);
  final PartPayload payload = assetUpload.getPayload();
  final Map<String, String> fields = assetUpload.getFields();
  final String uploadPath = removeInitialSlashFromPath(fields.get(PATH_ID));
  final String assetPath = buildPath(uploadPath, payload.getName());

  ensurePermitted(repository.getName(), RFormat.NAME, assetPath, Collections.emptyMap());
  validateArchiveUploadPath(assetPath);

  try {
    UnitOfWork.begin(repository.facet(StorageFacet.class).txSupplier());
    Asset asset = repository.facet(RHostedFacet.class).upload(assetPath, payload);
    return new UploadResponse(asset);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #19
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 #20
Source File: NpmComponentDirector.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional
protected void updatePackageRoot(final NpmHostedFacet npmHostedFacet,
                                 final Component component,
                                 final Repository destination)
{
  final StorageTx tx = UnitOfWork.currentTx();
  tx.browseAssets(component).forEach(asset -> {
    Blob blob = checkNotNull(tx.getBlob(asset.blobRef()));
    final Map<String, Object> packageJson = npmPackageParser.parsePackageJson(blob::getInputStream);
    final NpmPackageId packageId = NpmPackageId.parse((String) packageJson.get(P_NAME));

    try {
      final NestedAttributesMap updatedMetadata = createFullPackageMetadata(
          new NestedAttributesMap("metadata", packageJson),
          destination.getName(),
          blob.getMetrics().getSha1Hash(),
          destination,
          extractNewestVersion);
      npmHostedFacet.putPackageRoot(packageId, null, updatedMetadata);
    }
    catch (IOException e) {
      log.error("Failed to update package root, packageId: {}", packageId, e);
    }
  });
}
 
Example #21
Source File: PurgeUnusedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void purgeUnused(final int numberOfDays) {
  checkArgument(numberOfDays > 0, "Number of days must be greater then zero");
  log.info("Purging unused components from repository {}", getRepository().getName());

  Date olderThan = DateTime.now().minusDays(numberOfDays).withTimeAtStartOfDay().toDate();

  UnitOfWork.beginBatch(facet(StorageFacet.class).txSupplier());
  try {
    deleteUnusedComponents(olderThan);
    deleteUnusedAssets(olderThan);
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #22
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
@TransactionalDeleteBlob
public boolean delete(final MavenPath... paths) throws IOException {
  final StorageTx tx = UnitOfWork.currentTx();

  boolean result = false;
  for (MavenPath path : paths) {
    log.trace("DELETE {} : {}", getRepository().getName(), path.getPath());
    if (path.getCoordinates() != null) {
      result = deleteArtifact(path, tx) || result;
    }
    else {
      result = deleteFile(path, tx) || result;
    }
  }
  return result;
}
 
Example #23
Source File: AptSnapshotFacetSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = {ONeedRetryException.class})
@Override
@Nullable
public Content getSnapshotFile(final String id, final String path) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  final Asset asset = tx.findAssetWithProperty(P_NAME, createAssetPath(id, path), bucket);
  if (asset == null) {
    return null;
  }

  final Blob blob = tx.requireBlob(asset.requireBlobRef());
  return FacetHelper.toContent(asset, blob);
}
 
Example #24
Source File: NpmFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
@Override
public Asset findTarballAsset(final String packageId,
                              final String tarballName)
{
  final StorageTx tx = UnitOfWork.currentTx();
  return NpmFacetUtils
      .findTarballAsset(tx, tx.findBucket(getRepository()), NpmPackageId.parse(packageId), tarballName);
}
 
Example #25
Source File: ConanHostedFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreBlob
protected void doPutArchive(final ConanCoords coord,
                            final String path,
                            final TempBlob tempBlob,
                            final AssetKind assetKind) throws IOException
{
  checkNotNull(path);
  checkNotNull(tempBlob);

  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Map<String, String> attributes = new HashMap<>();
  attributes.put(GROUP, coord.getGroup());
  attributes.put(PROJECT, coord.getProject());
  attributes.put(VERSION, coord.getVersion());
  attributes.put(STATE, coord.getChannel());

  Component component = findComponent(tx, getRepository(), coord);
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(coord.getGroup())
        .name(coord.getProject())
        .version(getComponentVersion(coord));
  }
  tx.saveComponent(component);

  Asset asset = findAsset(tx, bucket, path);
  if (asset == null) {
    asset = tx.createAsset(bucket, component);
    asset.name(path);
    asset.formatAttributes().set(P_ASSET_KIND, assetKind);
  }

  saveAsset(tx, asset, tempBlob);
}
 
Example #26
Source File: ConanHostedMetadataFacetSupport.java    From nexus-repository-conan with Eclipse Public License 1.0 5 votes vote down vote up
private Map<String, String> generateDownloadUrls(
    final List<AssetKind> assetKinds,
    final ConanCoords coords)
{
  Repository repository = getRepository();
  StorageTx tx = UnitOfWork.currentTx();
  return assetKinds
      .stream().filter(x -> tx.assetExists(getHostedAssetPath(coords, x), repository))
      .collect(toMap(AssetKind::getFilename,
          x -> repository.getUrl() + "/" + getHostedAssetPath(coords, x)));
}
 
Example #27
Source File: SearchFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void rebuildIndex() {
  log.info("Rebuilding index of repository {}", getRepository().getName());
  searchIndexService.rebuildIndex(getRepository());
  UnitOfWork.begin(facet(StorageFacet.class).txSupplier());
  try {
    rebuildComponentIndex();
  }
  finally {
    UnitOfWork.end();
  }
}
 
Example #28
Source File: NpmHostedComponentMaintenanceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes depending on what it is.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
  if (asset == null) {
    return Collections.emptySet();
  }
  return deleteAssetTx(asset, deleteBlob);
}
 
Example #29
Source File: AptFacetImpl.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Nullable
@TransactionalTouchBlob
public Content get(String path) throws IOException {
  final StorageTx tx = UnitOfWork.currentTx();
  final Asset asset = tx.findAssetWithProperty(P_NAME, path, tx.findBucket(getRepository()));
  if (asset == null) {
    return null;
  }

  return FacetHelper.toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}
 
Example #30
Source File: OrientNpmProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
@TransactionalTouchBlob
public Content getRepositoryRoot() throws IOException {
  StorageTx tx = UnitOfWork.currentTx();

  Asset asset = findRepositoryRootAsset(tx, tx.findBucket(getRepository()));
  if (asset == null) {
    return null;
  }

  return toContent(asset, tx.requireBlob(asset.requireBlobRef()));
}