Java Code Examples for org.sonatype.nexus.repository.storage.StorageTx#saveComponent()

The following examples show how to use org.sonatype.nexus.repository.storage.StorageTx#saveComponent() . 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: NpmFacetUtils.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets or creates a {@link Component} for npm package tarball.
 */
@Nonnull
static Component getOrCreateTarballComponent(final StorageTx tx,
                                             final Repository repository,
                                             final NpmPackageId packageId,
                                             final String version)
{
  Component tarballComponent = findPackageTarballComponent(tx, repository, packageId, version);
  if (tarballComponent == null) {
    tarballComponent = tx.createComponent(tx.findBucket(repository), repository.getFormat())
        .group(packageId.scope())
        .name(packageId.name())
        .version(version);
    tx.saveComponent(tarballComponent);
  }
  return tarballComponent;
}
 
Example 2
Source File: CocoapodsFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Component findOrCreateComponent(final StorageTx tx, final Bucket bucket, final String path) {
  PodInfo podInfo = podPathParser.parse(path);

  Iterable<Component> components = tx.findComponents(
      builder()
          .where(P_NAME).eq(podInfo.getName())
          .and(P_VERSION).eq(podInfo.getVersion())
          .build(),
      singletonList(getRepository())
  );

  Component component = Iterables.getFirst(components, null); // NOSONAR
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .name(podInfo.getName())
        .version(podInfo.getVersion());
    tx.saveComponent(component);
  }
  return component;
}
 
Example 3
Source File: AptFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private Component findOrCreateComponent(final StorageTx tx, final Bucket bucket, final PackageInfo info) {
  String name = info.getPackageName();
  String version = info.getVersion();
  String architecture = info.getArchitecture();

  Iterable<Component> components = tx.findComponents(
      builder()
          .where(P_NAME).eq(name)
          .and(P_VERSION).eq(version)
          .and(P_GROUP).eq(architecture)
          .build(),
      singletonList(getRepository())
  );

  Component component = Iterables.getFirst(components, null);
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .name(name)
        .version(version)
        .group(architecture);
    tx.saveComponent(component);
  }

  return component;
}
 
Example 4
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 5
Source File: CondaProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalStoreBlob
protected Component findOrCreateComponent(final String arch, final String name, final String version)
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  Component component = getCondaFacet().findComponent(tx, getRepository(), arch, name, version);

  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(arch)
        .name(name)
        .version(version);
  }
  tx.saveComponent(component);

  return component;
}
 
Example 6
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 void createSnapshot(String id, SnapshotComponentSelector selector) throws IOException {
  StorageTx tx = UnitOfWork.currentTx();
  StorageFacet storageFacet = facet(StorageFacet.class);
  Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.createComponent(bucket, getRepository().getFormat()).name(id);
  tx.saveComponent(component);
  for (SnapshotItem item : collectSnapshotItems(selector)) {
    String assetName = createAssetPath(id, item.specifier.path);
    Asset asset = tx.createAsset(bucket, component).name(assetName);
    try (final TempBlob streamSupplier = storageFacet.createTempBlob(item.content.openInputStream(), FacetHelper.hashAlgorithms)) {
      AssetBlob blob = tx.createBlob(item.specifier.path, streamSupplier, FacetHelper.hashAlgorithms, null,
          FacetHelper.determineContentType(item), true);
      tx.attachBlob(asset, blob);
    }
    finally {
      item.content.close();
    }
    tx.saveAsset(asset);
  }
}
 
Example 7
Source File: ConanProxyFacet.java    From nexus-repository-conan with Eclipse Public License 1.0 6 votes vote down vote up
private Component getOrCreateComponent(final StorageTx tx,
                                       final Bucket bucket,
                                       final ConanCoords coords)
{
  Component component = findComponent(tx, getRepository(), coords);
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(coords.getGroup())
        .name(coords.getProject())
        .version(getComponentVersion(coords));
    component.formatAttributes().set("baseVersion", coords.getVersion());
    component.formatAttributes().set("channel", coords.getChannel());
  }
  tx.saveComponent(component);
  return component;
}
 
Example 8
Source File: RFacetImpl.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Component findOrCreateComponent(final StorageTx tx,
                                       final String path,
                                       final Map<String, String> attributes)
{
  String name = attributes.get(P_PACKAGE);
  String version = attributes.get(P_VERSION);
  String group = getBasePath(path);

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

  return component;
}
 
Example 9
Source File: P2FacetImpl.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Component findOrCreateComponent(final StorageTx tx, final P2Attributes attributes) {
  String name = attributes.getComponentName();
  String version = attributes.getComponentVersion();

  Component component = findComponent(tx, getRepository(), name, version);
  if (component == null) {
    Bucket bucket = tx.findBucket(getRepository());
    component = tx.createComponent(bucket, getRepository().getFormat())
        .name(name)
        .version(version);
    if (attributes.getPluginName() != null) {
      component.formatAttributes().set(PLUGIN_NAME, attributes.getPluginName());
    }

    tx.saveComponent(component);
  }

  return component;
}
 
Example 10
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 11
Source File: OrientPyPiHostedFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
protected Asset savePyPiWheelPayload(
    final String filename,
    final Map<String, String> attributes,
    final TempBlobPartPayload wheelPayload) throws IOException
{
  checkNotNull(filename);

  TempBlob tempBlob = wheelPayload.getTempBlob();

  final String name = checkNotNull(attributes.get(P_NAME));
  final String version = checkNotNull(attributes.get(P_VERSION));
  final String normalizedName = normalizeName(name);

  validateMd5Hash(attributes, tempBlob);

  PyPiIndexFacet indexFacet = facet(PyPiIndexFacet.class);
  // A package has been added or redeployed and therefore the cached index is no longer relevant
  indexFacet.deleteIndex(name);

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

  String packagePath = createPackagePath(name, version, filename);

  Component component = findOrCreateComponent(name, version, normalizedName, indexFacet, tx, bucket);

  component.formatAttributes().set(P_SUMMARY, attributes.get(P_SUMMARY)); // use the most recent summary received?
  tx.saveComponent(component);

  Asset asset = findOrCreateAsset(tx, bucket, packagePath, component, AssetKind.PACKAGE.name());

  copyAttributes(asset, attributes);
  saveAsset(tx, asset, tempBlob, wheelPayload);

  return asset;
}
 
Example 12
Source File: RawContentFacetImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreMetadata
public Asset getOrCreateAsset(final Repository repository, final String componentName, final String componentGroup,
                              final String assetName) {
  final StorageTx tx = UnitOfWork.currentTx();

  final Bucket bucket = tx.findBucket(getRepository());
  Component component = tx.findComponentWithProperty(P_NAME, componentName, bucket);
  Asset asset;
  if (component == null) {
    // CREATE
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(componentGroup)
        .name(componentName);

    tx.saveComponent(component);

    asset = tx.createAsset(bucket, component);
    asset.name(assetName);
  }
  else {
    // UPDATE
    asset = tx.firstAsset(component);
    asset = asset != null ? asset : tx.createAsset(bucket, component).name(assetName);
  }

  return asset;
}
 
Example 13
Source File: GolangDataAccess.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@TransactionalStoreBlob
public Content maybeCreateAndSaveComponent(final Repository repository,
                                           final GolangAttributes golangAttributes,
                                           final String assetPath,
                                           final TempBlob tempBlob,
                                           final Payload payload,
                                           final AssetKind assetKind) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(repository);

  Component component = findComponent(tx,
      repository,
      golangAttributes.getModule(),
      golangAttributes.getVersion());

  if (component == null) {
    component = tx.createComponent(bucket, repository.getFormat())
        .name(golangAttributes.getModule())
        .version(golangAttributes.getVersion());
    tx.saveComponent(component);
  }

  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());
  }
  return saveAsset(tx, asset, tempBlob, payload);
}
 
Example 14
Source File: HelmFacetImpl.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
private Component findOrCreateComponent(final StorageTx tx,
                                        final Bucket bucket,
                                        final String name,
                                        final String version)
{
  Optional<Component> componentOpt = findComponent(tx, name, version);
  if (!componentOpt.isPresent()) {
    Component component = tx.createComponent(bucket, getRepository().getFormat())
        .name(name)
        .version(version);
    tx.saveComponent(component);
    return component;
  }
  return componentOpt.get();
}
 
Example 15
Source File: OrientPyPiFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Asset putPackage(final String path, final AssetBlob assetBlob) throws IOException {
  final StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());

  Asset asset = findAsset(tx, bucket, path);

  if (asset == null) {
    final String filename = extractFilenameFromPath(path);

    Map<String, String> attributes;
    try (InputStream is = assetBlob.getBlob().getInputStream()) {
      attributes = extractMetadata(is);
    }

    if (!attributes.containsKey(P_NAME)) {
      log.debug("No name found in metadata for {}, extracting from filename.", filename);
      attributes.put(P_NAME, normalizeName(extractNameFromFilename(filename)));
    }
    if (!attributes.containsKey(P_VERSION)) {
      log.debug("No version found in metadata for {}, extracting from filename.", filename);
      attributes.put(P_VERSION, extractVersionFromFilename(filename));
    }

    final String name = attributes.get(P_NAME);
    final String version = attributes.get(P_VERSION);

    Component  component = findComponent(tx, getRepository(), name, version);
    if (component == null) {
      component = tx.createComponent(bucket, getRepository().getFormat());
      component.name(name);
      component.version(version);
      component.formatAttributes().set(P_SUMMARY, attributes.get(P_SUMMARY));
      tx.saveComponent(component);
    }
    asset = tx.createAsset(bucket, component);
    asset.name(path);
    copyAttributes(asset, attributes);
    saveAsset(tx, asset, assetBlob, AssetKind.PACKAGE);
  }

  return asset;
}
 
Example 16
Source File: OrientPyPiProxyFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@TransactionalStoreBlob
protected Content doPutPackage(final String path,
                               final TempBlob tempBlob,
                               final Payload payload) throws IOException
{
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());

  String filename = extractFilenameFromPath(path);
  Map<String, String> attributes;
  try (InputStream is = tempBlob.get()) {
    attributes = extractMetadata(is);
  }

  if (!attributes.containsKey(P_NAME)) {
    log.debug("No name found in metadata for {}, extracting from filename.", filename);
    attributes.put(P_NAME, extractNameFromFilename(filename));
  }
  if (!attributes.containsKey(P_VERSION)) {
    log.debug("No version found in metadata for {}, extracting from filename.", filename);
    attributes.put(P_VERSION, extractVersionFromFilename(filename));
  }

  String name = attributes.get(P_NAME);
  String version = attributes.get(P_VERSION);

  Component component = findComponent(tx, getRepository(), name, version);
  if (component == null) {
    component = tx.createComponent(bucket, getRepository().getFormat()).name(name).version(version);
  }
  if (component.isNew() || attributes.containsKey(P_SUMMARY)) {
    component.formatAttributes().set(P_SUMMARY, attributes.get(P_SUMMARY));
    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.PACKAGE.name());
  }

  copyAttributes(asset, attributes);
  return saveAsset(tx, asset, tempBlob, payload);
}
 
Example 17
Source File: MavenFacetImpl.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
private Asset putArtifact(final StorageTx tx,
                          final MavenPath path,
                          final AssetBlob assetBlob,
                          @Nullable final AttributesMap contentAttributes)
    throws IOException
{
  final Coordinates coordinates = checkNotNull(path.getCoordinates());
  final Bucket bucket = tx.findBucket(getRepository());
  Component component = findComponent(tx, getRepository(), path);
  boolean updatePomModel = false;
  if (component == null) {
    // Create and set top-level properties
    component = tx.createComponent(bucket, getRepository().getFormat())
        .group(coordinates.getGroupId())
        .name(coordinates.getArtifactId())
        .version(coordinates.getVersion());

    // Set format specific attributes
    final NestedAttributesMap componentAttributes = component.formatAttributes();
    componentAttributes.set(P_GROUP_ID, coordinates.getGroupId());
    componentAttributes.set(P_ARTIFACT_ID, coordinates.getArtifactId());
    componentAttributes.set(P_VERSION, coordinates.getVersion());
    componentAttributes.set(P_BASE_VERSION, coordinates.getBaseVersion());
    if (path.isPom()) {
      fillInFromModel(path, assetBlob, component.formatAttributes());
    }
    tx.saveComponent(component);
  }
  else if (path.isPom()) {
    // reduce copying by deferring update of pom.xml attributes (which involves reading the blob)
    // until after putAssetPayload, in case the blob is a duplicate and the model has not changed
    updatePomModel = true;
  }

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

    asset.name(path.getPath());

    final NestedAttributesMap assetAttributes = asset.formatAttributes();
    assetAttributes.set(P_GROUP_ID, coordinates.getGroupId());
    assetAttributes.set(P_ARTIFACT_ID, coordinates.getArtifactId());
    assetAttributes.set(P_VERSION, coordinates.getVersion());
    assetAttributes.set(P_BASE_VERSION, coordinates.getBaseVersion());
    assetAttributes.set(P_CLASSIFIER, coordinates.getClassifier());
    assetAttributes.set(P_EXTENSION, coordinates.getExtension());
    assetAttributes.set(
        P_ASSET_KIND,
        path.isSubordinate() ? AssetKind.ARTIFACT_SUBORDINATE.name() : AssetKind.ARTIFACT.name()
    );
  }

  putAssetPayload(tx, asset, assetBlob, contentAttributes);

  tx.saveAsset(asset);

  // avoid re-reading pom.xml if it's a duplicate of the old asset
  if (updatePomModel && !assetBlob.isDuplicate()) {
    fillInFromModel(path, assetBlob, component.formatAttributes());
    tx.saveComponent(component);
  }

  return asset;
}